Comments (7)
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.
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.
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.
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.
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.
Issues now solved via new distinctPluralize
function.
from pg-simplify-inflector.
Released in the v4 alpha that just went out.
from pg-simplify-inflector.
Related Issues (20)
- Adding list suffix results in incorrect name HOT 4
- handling field conflict HOT 7
- Incompatible with --simple-collections (both|only) HOT 4
- Supporting "both" / "only" option for graceful migration HOT 2
- Consider making primary key fetchers/updaters shorter HOT 4
- Typescript Support HOT 2
- Using pgSimpleCollections: 'only' without pgOmitListSuffix: true will throw error HOT 4
- Using alongside postgraphile-plugin-connection-filter HOT 3
- Replace pluralize with a version that only pluralizes the end word HOT 1
- Error: Cannot find module '@graphile-contrib/pg-simplify-inflector' HOT 1
- Change log very out of date HOT 2
- npm install -g for CLI use HOT 5
- Lacking Typescript declarations HOT 5
- Document `foreignSimpleFieldName` HOT 3
- Field name simplification broken after renaming with Smart Tag HOT 7
- Primary keys that are also foreign keys not simplified HOT 3
- Incompatibility/warning messages when installing at a Docker container HOT 1
- Import problems from ESM to this module HOT 10
- mutationFunctionResultFieldName: () => 'result' HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pg-simplify-inflector.