How to use the singularize function from inflection
Find comprehensive JavaScript inflection.singularize code examples handpicked from public code repositorys.
inflection.singularize is a function that converts a plural noun to its singular form.
46 47 48 49 50 51 52 53 54 55
// normalize aggregatable const $aggregatable = common.mergeObjects({}, aggregatable); // normalize unwind const unwindPathName = isArray ? inflection.singularize(pathName) : pathName; const unwindDefaults = { path: `$${unwindPathName}`, preserveNullAndEmptyArrays: true, };
+ 2 other calls in file
100 101 102 103 104 105 106 107 108 109 110 111
return inflection.underscore(str); } exports.underscore = underscore; function singularize(str) { return inflection.singularize(str); } exports.singularize = singularize; function pluralize(str) {
How does inflection.singularize work?
inflection.singularize
works by converting a given plural noun to its corresponding singular form. It uses a set of predefined rules to make the conversion, such as:
- Removing common English plural suffixes like "-s", "-es", "-ies", and "-ves".
- Replacing certain irregular plural forms with their singular counterparts, such as "children" to "child" or "geese" to "goose".
- Leaving certain words unchanged, such as uncountable nouns like "sheep" or "fish".
The function is typically used in the context of string manipulation and text processing, and is useful for generating singular labels or names based on a given set of plural nouns.
Note that inflection.singularize
is part of the inflection
library, which provides a range of string manipulation and inflection-related functions for JavaScript.
31 32 33 34 35 36 37 38 39 40
if (typeof anotherClass === 'string') { params.as = anotherClass; if (params.model) { anotherClass = params.model; } else { const anotherClassName = i8n.singularize(anotherClass).toLowerCase(); Object.keys(this.schema.models).forEach(name => { if (name.toLowerCase() === anotherClassName) { anotherClass = this.schema.models[name]; }
1 2 3 4 5 6 7 8 9 10 11 12 13
const inflection = require('inflection') const SCALOR_TYPES = ['Float', 'String', 'Int', 'Boolean', 'DateTime', 'Bytes', 'Decimal'] const singularName = (name) => { return inflection.singularize(name) } const pluralName = (name) => { return inflection.pluralize(name)
Ai Example
1 2 3 4 5 6 7
const inflection = require("inflection"); const pluralNoun = "apples"; const singularNoun = inflection.singularize(pluralNoun); console.log(`One ${singularNoun} is better than many ${pluralNoun}.`); // Output: One apple is better than many apples.
In this example, we first import the inflection library using require(). We then define a plural noun, "apples", and use inflection.singularize() to convert it to its singular form, "apple". We store the result in a variable called singularNoun. Finally, we log a message to the console using both the plural and singular forms of the noun. The message illustrates the difference between singular and plural forms by stating that one apple is better than many apples.
565 566 567 568 569 570 571 572 573 574
modelTo = params.model || modelToRef; // modelToRef might be modelTo name if (typeof modelTo === 'string') { // lookup modelTo by name modelToName = modelTo; modelToName = (singularize ? i8n.singularize(modelToName) : modelToName).toLowerCase(); modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName); } if (!modelTo) {
GitHub: pb-it/wing-cms-api
373 374 375 376 377 378 379 380 381
var model = this._shelf.getModel(attribute['model']); if (model) { var modelTable = model.getTableName(); var relTable = this.getJunctionTableName(attribute); if (!await knex.schema.hasTable(relTable)) { var id = inflection.singularize(this._tableName) + "_id"; var fid = inflection.singularize(modelTable) + "_id"; await knex.schema.createTable(relTable, function (table) { table.increments('id').primary();
+ 45 other calls in file
GitHub: secretsudev/bookshelf
641 642 643 644 645 646 647 648 649 650
// Simple memoization of the singularize call. const singularMemo = (function() { const cache = Object.create(null); return function(arg) { if (!(arg in cache)) { cache[arg] = inflection.singularize(arg); } return cache[arg]; }; })();
GitHub: TravColbert/flash
66 67 68 69 70 71 72 73 74 75
} }) const iterateModelDefinitions = async function (db, modelDefinitions, func) { for (const modelDefinition of modelDefinitions) { const modelName = inflection.singularize(modelDefinition.name) await func(db, modelName, modelDefinition) } return db }
538 539 540 541 542 543 544 545 546 547
if ("string" === typeof modelTo) { var modelToName; params.as = params.as || modelTo; modelTo = params.model || modelTo; if (typeof modelTo === "string") { modelToName = (singularize ? i8n.singularize(modelTo) : modelTo).toLowerCase(); modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName) || modelTo; } if (typeof modelTo === "string") { modelToName = (singularize ? i8n.singularize(params.as) : params.as).toLowerCase();
inflection.titleize is the most popular function in inflection (146 examples)