Code Monkey home page Code Monkey logo

Comments (7)

vincenzo avatar vincenzo commented on July 18, 2024

I've just written this in my own inflector plugin:

builder.hook("inflection", inflection => {
    inflection.smartPluralize = singular => {
      const plural = inflection.pluralize(singular);
      if (plural == singular) {
        if (
          plural.endsWith("ch") ||
          plural.endsWith("s") ||
          plural.endsWith("sh") ||
          plural.endsWith("x") ||
          plural.endsWith("z")
        ) {
          return plural + "es";
        } else {
          if (plural.endsWith("y")) {
            return plural.slice(0, -1) + "ies";
          } else {
            return plural + "s";
          }
        }
      }
      return plural;
    };
    return {
      ...inflection,
      // Rest of the plugin where I use 'this.smartPluralize()' instead of 'this.pluralize()'.
    };
  });
};

It's very likely a better implementation could be written. That said, in principle, that function could be released in its own plugin? Not sure how that would look like though, at this stage.

from pg-simplify-inflector.

benjie avatar benjie commented on July 18, 2024

One caveat here is that you cannot assume that the argument to pluralize is singular. Or can you? I can't remember. I know singularise takes singular or plural and returns singular...

from pg-simplify-inflector.

vincenzo avatar vincenzo commented on July 18, 2024

That's a fair point, I was assuming it because in my particular use case what I pass to it is known to be singular (_singularizedTableName()).

To make it more general, I could probably singularise the input as first thing, then proceed with the rest.

from pg-simplify-inflector.

benjie avatar benjie commented on July 18, 2024

Also, I recommend this shape of inflector plugin:

builder.hook("inflection", (inflection, build) => {
  return build.extend(inflection, {
    smartPluralize(singular) {
      /* ... */
    }
  }, 'Adding smartPluralize inflector');
});

Or, if you prefer:

module.exports = require('graphile-utils').makeAddInflectorsPlugin({
  smartPluralize(singular) {
    /* ... */
  }
});

Both prevent you accidentally overwriting existing inflectors.

from pg-simplify-inflector.

vincenzo avatar vincenzo commented on July 18, 2024

I came up with this (currently in my local project):

const _hyper_pluralize = (singular, plural, hyper) => {
  if (!hyper || plural != singular) {
    return plural;
  }
  if (
    plural.endsWith("ch") ||
    plural.endsWith("s") ||
    plural.endsWith("sh") ||
    plural.endsWith("x") ||
    plural.endsWith("z")
  ) {
    return plural + "es";
  } else {
    if (plural.endsWith("y")) {
      return plural.slice(0, -1) + "ies";
    } else {
      return plural + "s";
    }
  }
};

module.exports.makeInflectionHyperPluralize = function makeInflectionHyperPluralize(
  inflection,
  mode = "override",
) {
  if (mode === "override") {
    inflection._pluralize = inflection.pluralize.bind();
    inflection.pluralize = (str, plural, hyper = true) => {
      const singular = inflection.singularize(str, plural);
      const _plural = inflection._pluralize(singular);
      return _hyper_pluralize(singular, _plural, hyper);
    };
  } else {
    if (mode === "extend") {
      inflection.hyperPluralize = (str, plural, hyper = true) => {
        const singular = inflection.singularize(str, plural);
        const _plural = inflection.pluralize(singular);
        return _hyper_pluralize(singular, _plural, hyper);
      };
    } else {
      throw new Error("`mode` accepts either `override` or `extend` only.");
    }
  }
  return inflection;
};

Thinking of releasing this as a simple npm package rather than a postgraphile plugin specifically.
This package could be used by anybody like this:

const { makeInflectionHyperPluralize } = require("inflection-hyper-pluralize");
const inflection = require("inflection");

inflection = makeInflectionHyperPluralize(inflection);

inflection.pluralize("person"); // this is now running the "hyper pluralize" version.

This can then, of course, be used in a PG plugin like this:

const { makeInflectionHyperPluralize } = require("inflection-hyper-pluralize");

module.exports.MyPlugin = function MyPlugin(builder) {
  builder.hook("inflection", inflection => {
    inflection = makeInflectionHyperPluralize(inflection);
    // Rest of the plugin ... 
  };
};

from pg-simplify-inflector.

benjie avatar benjie commented on July 18, 2024

Issues now solved via new distinctPluralize function.

from pg-simplify-inflector.

benjie avatar benjie commented on July 18, 2024

Released in the v4 alpha that just went out.

from pg-simplify-inflector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.