inflection
Advanced tools
Comparing version 1.13.4 to 2.0.0
{ | ||
"editor.formatOnSave": false | ||
"editor.formatOnSave": true | ||
} |
# History | ||
## 1.13.3 and later | ||
Please see the [GitHub Releases](https://github.com/dreamerslab/node.inflection/releases) for release notes. | ||
## 1.13.2 / 2022-01-29 | ||
@@ -4,0 +7,0 @@ |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/*! | ||
@@ -9,19 +10,9 @@ * inflection | ||
*/ | ||
( function ( root, factory ){ | ||
if( typeof define === 'function' && define.amd ){ | ||
define([], factory ); | ||
}else if( typeof exports === 'object' ){ | ||
module.exports = factory(); | ||
}else{ | ||
root.inflection = factory(); | ||
} | ||
}( this, function (){ | ||
/** | ||
* @description This is a list of nouns that use the same form for both singular and plural. | ||
* This list should remain entirely in lower case to correctly match Strings. | ||
* @private | ||
*/ | ||
var uncountable_words = [ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transform = exports.ordinalize = exports.foreignKey = exports.classify = exports.tableize = exports.demodulize = exports.titleize = exports.dasherize = exports.capitalize = exports.humanize = exports.underscore = exports.camelize = exports.inflect = exports.singularize = exports.pluralize = exports.indexOf = void 0; | ||
/** | ||
* @description This is a list of nouns that use the same form for both singular and plural. | ||
* This list should remain entirely in lower case to correctly match Strings. | ||
*/ | ||
const uncountableWords = [ | ||
// 'access', | ||
@@ -44,3 +35,2 @@ 'accommodation', | ||
// 'attention', | ||
'bacon', | ||
@@ -61,3 +51,2 @@ 'baggage', | ||
'butter', | ||
'carbon', | ||
@@ -71,3 +60,2 @@ 'cardboard', | ||
'countryside', | ||
// 'damage', | ||
@@ -83,3 +71,2 @@ 'dancing', | ||
'dust', | ||
'economics', | ||
@@ -98,3 +85,2 @@ 'education', | ||
'evolution', | ||
// 'failure', | ||
@@ -114,3 +100,2 @@ // 'faith', | ||
'furniture', | ||
'gallows', | ||
@@ -131,3 +116,2 @@ 'garbage', | ||
'gymnastics', | ||
// 'hair', | ||
@@ -151,3 +135,2 @@ 'happiness', | ||
'hydrogen', | ||
'ice', | ||
@@ -162,3 +145,2 @@ 'importance', | ||
'irony', | ||
'jam', | ||
@@ -172,7 +154,5 @@ // 'jealousy', | ||
// 'justice', | ||
'karate', | ||
// 'kindness', | ||
'knowledge', | ||
// 'labour', | ||
@@ -197,3 +177,2 @@ 'lack', | ||
'luggage', | ||
'macaroni', | ||
@@ -219,3 +198,2 @@ 'machinery', | ||
'mumps', | ||
'nature', | ||
@@ -227,3 +205,2 @@ 'news', | ||
'nutrition', | ||
'obedience', | ||
@@ -233,3 +210,2 @@ 'obesity', | ||
'oxygen', | ||
// 'paper', | ||
@@ -252,7 +228,5 @@ // 'passion', | ||
'punctuation', | ||
// 'quality', | ||
// 'quantity', | ||
'quartz', | ||
'racism', | ||
@@ -269,3 +243,2 @@ // 'rain', | ||
'rum', | ||
'safety', | ||
@@ -307,3 +280,2 @@ // 'salad', | ||
'symmetry', | ||
// 'tea', | ||
@@ -322,3 +294,2 @@ 'tennis', | ||
'trust', | ||
// 'understanding', | ||
@@ -329,3 +300,2 @@ 'underwear', | ||
// 'usage', | ||
'validity', | ||
@@ -339,3 +309,2 @@ 'veal', | ||
'vitality', | ||
'warmth', | ||
@@ -356,672 +325,584 @@ // 'water', | ||
// 'work', | ||
// 'yeast', | ||
'yoga', | ||
'zinc', | ||
'zoology' | ||
]; | ||
/** | ||
* @description These rules translate from the singular form of a noun to its plural form. | ||
* @private | ||
*/ | ||
var regex = { | ||
plural : { | ||
men : new RegExp( '^(m|wom)en$' , 'gi' ), | ||
people : new RegExp( '(pe)ople$' , 'gi' ), | ||
children : new RegExp( '(child)ren$' , 'gi' ), | ||
tia : new RegExp( '([ti])a$' , 'gi' ), | ||
analyses : new RegExp( '((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$','gi' ), | ||
databases : new RegExp( '(database)s$' , 'gi' ), | ||
drives : new RegExp( '(drive)s$' , 'gi' ), | ||
hives : new RegExp( '(hi|ti)ves$' , 'gi' ), | ||
curves : new RegExp( '(curve)s$' , 'gi' ), | ||
lrves : new RegExp( '([lr])ves$' , 'gi' ), | ||
aves : new RegExp( '([a])ves$' , 'gi' ), | ||
foves : new RegExp( '([^fo])ves$' , 'gi' ), | ||
movies : new RegExp( '(m)ovies$' , 'gi' ), | ||
aeiouyies : new RegExp( '([^aeiouy]|qu)ies$' , 'gi' ), | ||
series : new RegExp( '(s)eries$' , 'gi' ), | ||
xes : new RegExp( '(x|ch|ss|sh)es$' , 'gi' ), | ||
mice : new RegExp( '([m|l])ice$' , 'gi' ), | ||
buses : new RegExp( '(bus)es$' , 'gi' ), | ||
oes : new RegExp( '(o)es$' , 'gi' ), | ||
shoes : new RegExp( '(shoe)s$' , 'gi' ), | ||
crises : new RegExp( '(cris|ax|test)es$' , 'gi' ), | ||
octopuses : new RegExp( '(octop|vir)uses$' , 'gi' ), | ||
aliases : new RegExp( '(alias|canvas|status|campus)es$', 'gi' ), | ||
summonses : new RegExp( '^(summons|bonus)es$' , 'gi' ), | ||
oxen : new RegExp( '^(ox)en' , 'gi' ), | ||
matrices : new RegExp( '(matr)ices$' , 'gi' ), | ||
vertices : new RegExp( '(vert|ind)ices$' , 'gi' ), | ||
feet : new RegExp( '^feet$' , 'gi' ), | ||
teeth : new RegExp( '^teeth$' , 'gi' ), | ||
geese : new RegExp( '^geese$' , 'gi' ), | ||
quizzes : new RegExp( '(quiz)zes$' , 'gi' ), | ||
whereases : new RegExp( '^(whereas)es$' , 'gi' ), | ||
criteria : new RegExp( '^(criteri)a$' , 'gi' ), | ||
genera : new RegExp( '^genera$' , 'gi' ), | ||
ss : new RegExp( 'ss$' , 'gi' ), | ||
s : new RegExp( 's$' , 'gi' ) | ||
'zoology', | ||
]; | ||
/** | ||
* @description These rules translate from the singular form of a noun to its plural form. | ||
*/ | ||
const regex = { | ||
plural: { | ||
men: new RegExp('^(m|wom)en$', 'gi'), | ||
people: new RegExp('(pe)ople$', 'gi'), | ||
children: new RegExp('(child)ren$', 'gi'), | ||
tia: new RegExp('([ti])a$', 'gi'), | ||
analyses: new RegExp('((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$', 'gi'), | ||
databases: new RegExp('(database)s$', 'gi'), | ||
drives: new RegExp('(drive)s$', 'gi'), | ||
hives: new RegExp('(hi|ti)ves$', 'gi'), | ||
curves: new RegExp('(curve)s$', 'gi'), | ||
lrves: new RegExp('([lr])ves$', 'gi'), | ||
aves: new RegExp('([a])ves$', 'gi'), | ||
foves: new RegExp('([^fo])ves$', 'gi'), | ||
movies: new RegExp('(m)ovies$', 'gi'), | ||
aeiouyies: new RegExp('([^aeiouy]|qu)ies$', 'gi'), | ||
series: new RegExp('(s)eries$', 'gi'), | ||
xes: new RegExp('(x|ch|ss|sh)es$', 'gi'), | ||
mice: new RegExp('([m|l])ice$', 'gi'), | ||
buses: new RegExp('(bus)es$', 'gi'), | ||
oes: new RegExp('(o)es$', 'gi'), | ||
shoes: new RegExp('(shoe)s$', 'gi'), | ||
crises: new RegExp('(cris|ax|test)es$', 'gi'), | ||
octopuses: new RegExp('(octop|vir)uses$', 'gi'), | ||
aliases: new RegExp('(alias|canvas|status|campus)es$', 'gi'), | ||
summonses: new RegExp('^(summons|bonus)es$', 'gi'), | ||
oxen: new RegExp('^(ox)en', 'gi'), | ||
matrices: new RegExp('(matr)ices$', 'gi'), | ||
vertices: new RegExp('(vert|ind)ices$', 'gi'), | ||
feet: new RegExp('^feet$', 'gi'), | ||
teeth: new RegExp('^teeth$', 'gi'), | ||
geese: new RegExp('^geese$', 'gi'), | ||
quizzes: new RegExp('(quiz)zes$', 'gi'), | ||
whereases: new RegExp('^(whereas)es$', 'gi'), | ||
criteria: new RegExp('^(criteri)a$', 'gi'), | ||
genera: new RegExp('^genera$', 'gi'), | ||
ss: new RegExp('ss$', 'gi'), | ||
s: new RegExp('s$', 'gi'), | ||
}, | ||
singular : { | ||
man : new RegExp( '^(m|wom)an$' , 'gi' ), | ||
person : new RegExp( '(pe)rson$' , 'gi' ), | ||
child : new RegExp( '(child)$' , 'gi' ), | ||
drive : new RegExp( '(drive)$' , 'gi' ), | ||
ox : new RegExp( '^(ox)$' , 'gi' ), | ||
axis : new RegExp( '(ax|test)is$' , 'gi' ), | ||
octopus : new RegExp( '(octop|vir)us$' , 'gi' ), | ||
alias : new RegExp( '(alias|status|canvas|campus)$', 'gi' ), | ||
summons : new RegExp( '^(summons|bonus)$' , 'gi' ), | ||
bus : new RegExp( '(bu)s$' , 'gi' ), | ||
buffalo : new RegExp( '(buffal|tomat|potat)o$' , 'gi' ), | ||
tium : new RegExp( '([ti])um$' , 'gi' ), | ||
sis : new RegExp( 'sis$' , 'gi' ), | ||
ffe : new RegExp( '(?:([^f])fe|([lr])f)$' , 'gi' ), | ||
hive : new RegExp( '(hi|ti)ve$' , 'gi' ), | ||
aeiouyy : new RegExp( '([^aeiouy]|qu)y$' , 'gi' ), | ||
x : new RegExp( '(x|ch|ss|sh)$' , 'gi' ), | ||
matrix : new RegExp( '(matr)ix$' , 'gi' ), | ||
vertex : new RegExp( '(vert|ind)ex$' , 'gi' ), | ||
mouse : new RegExp( '([m|l])ouse$' , 'gi' ), | ||
foot : new RegExp( '^foot$' , 'gi' ), | ||
tooth : new RegExp( '^tooth$' , 'gi' ), | ||
goose : new RegExp( '^goose$' , 'gi' ), | ||
quiz : new RegExp( '(quiz)$' , 'gi' ), | ||
whereas : new RegExp( '^(whereas)$' , 'gi' ), | ||
criterion : new RegExp( '^(criteri)on$' , 'gi' ), | ||
genus : new RegExp( '^genus$' , 'gi' ), | ||
s : new RegExp( 's$' , 'gi' ), | ||
common : new RegExp( '$' , 'gi' ) | ||
} | ||
}; | ||
var plural_rules = [ | ||
singular: { | ||
man: new RegExp('^(m|wom)an$', 'gi'), | ||
person: new RegExp('(pe)rson$', 'gi'), | ||
child: new RegExp('(child)$', 'gi'), | ||
drive: new RegExp('(drive)$', 'gi'), | ||
ox: new RegExp('^(ox)$', 'gi'), | ||
axis: new RegExp('(ax|test)is$', 'gi'), | ||
octopus: new RegExp('(octop|vir)us$', 'gi'), | ||
alias: new RegExp('(alias|status|canvas|campus)$', 'gi'), | ||
summons: new RegExp('^(summons|bonus)$', 'gi'), | ||
bus: new RegExp('(bu)s$', 'gi'), | ||
buffalo: new RegExp('(buffal|tomat|potat)o$', 'gi'), | ||
tium: new RegExp('([ti])um$', 'gi'), | ||
sis: new RegExp('sis$', 'gi'), | ||
ffe: new RegExp('(?:([^f])fe|([lr])f)$', 'gi'), | ||
hive: new RegExp('(hi|ti)ve$', 'gi'), | ||
aeiouyy: new RegExp('([^aeiouy]|qu)y$', 'gi'), | ||
x: new RegExp('(x|ch|ss|sh)$', 'gi'), | ||
matrix: new RegExp('(matr)ix$', 'gi'), | ||
vertex: new RegExp('(vert|ind)ex$', 'gi'), | ||
mouse: new RegExp('([m|l])ouse$', 'gi'), | ||
foot: new RegExp('^foot$', 'gi'), | ||
tooth: new RegExp('^tooth$', 'gi'), | ||
goose: new RegExp('^goose$', 'gi'), | ||
quiz: new RegExp('(quiz)$', 'gi'), | ||
whereas: new RegExp('^(whereas)$', 'gi'), | ||
criterion: new RegExp('^(criteri)on$', 'gi'), | ||
genus: new RegExp('^genus$', 'gi'), | ||
s: new RegExp('s$', 'gi'), | ||
common: new RegExp('$', 'gi'), | ||
}, | ||
}; | ||
const pluralRules = [ | ||
// do not replace if its already a plural word | ||
[ regex.plural.men ], | ||
[ regex.plural.people ], | ||
[ regex.plural.children ], | ||
[ regex.plural.tia ], | ||
[ regex.plural.analyses ], | ||
[ regex.plural.databases ], | ||
[ regex.plural.drives ], | ||
[ regex.plural.hives ], | ||
[ regex.plural.curves ], | ||
[ regex.plural.lrves ], | ||
[ regex.plural.foves ], | ||
[ regex.plural.aeiouyies ], | ||
[ regex.plural.series ], | ||
[ regex.plural.movies ], | ||
[ regex.plural.xes ], | ||
[ regex.plural.mice ], | ||
[ regex.plural.buses ], | ||
[ regex.plural.oes ], | ||
[ regex.plural.shoes ], | ||
[ regex.plural.crises ], | ||
[ regex.plural.octopuses ], | ||
[ regex.plural.aliases ], | ||
[ regex.plural.summonses ], | ||
[ regex.plural.oxen ], | ||
[ regex.plural.matrices ], | ||
[ regex.plural.feet ], | ||
[ regex.plural.teeth ], | ||
[ regex.plural.geese ], | ||
[ regex.plural.quizzes ], | ||
[ regex.plural.whereases ], | ||
[ regex.plural.criteria ], | ||
[ regex.plural.genera ], | ||
[regex.plural.men], | ||
[regex.plural.people], | ||
[regex.plural.children], | ||
[regex.plural.tia], | ||
[regex.plural.analyses], | ||
[regex.plural.databases], | ||
[regex.plural.drives], | ||
[regex.plural.hives], | ||
[regex.plural.curves], | ||
[regex.plural.lrves], | ||
[regex.plural.foves], | ||
[regex.plural.aeiouyies], | ||
[regex.plural.series], | ||
[regex.plural.movies], | ||
[regex.plural.xes], | ||
[regex.plural.mice], | ||
[regex.plural.buses], | ||
[regex.plural.oes], | ||
[regex.plural.shoes], | ||
[regex.plural.crises], | ||
[regex.plural.octopuses], | ||
[regex.plural.aliases], | ||
[regex.plural.summonses], | ||
[regex.plural.oxen], | ||
[regex.plural.matrices], | ||
[regex.plural.feet], | ||
[regex.plural.teeth], | ||
[regex.plural.geese], | ||
[regex.plural.quizzes], | ||
[regex.plural.whereases], | ||
[regex.plural.criteria], | ||
[regex.plural.genera], | ||
// original rule | ||
[ regex.singular.man , '$1en' ], | ||
[ regex.singular.person , '$1ople' ], | ||
[ regex.singular.child , '$1ren' ], | ||
[ regex.singular.drive , '$1s' ], | ||
[ regex.singular.ox , '$1en' ], | ||
[ regex.singular.axis , '$1es' ], | ||
[ regex.singular.octopus , '$1uses' ], | ||
[ regex.singular.alias , '$1es' ], | ||
[ regex.singular.summons , '$1es' ], | ||
[ regex.singular.bus , '$1ses' ], | ||
[ regex.singular.buffalo , '$1oes' ], | ||
[ regex.singular.tium , '$1a' ], | ||
[ regex.singular.sis , 'ses' ], | ||
[ regex.singular.ffe , '$1$2ves' ], | ||
[ regex.singular.hive , '$1ves' ], | ||
[ regex.singular.aeiouyy , '$1ies' ], | ||
[ regex.singular.matrix , '$1ices' ], | ||
[ regex.singular.vertex , '$1ices' ], | ||
[ regex.singular.x , '$1es' ], | ||
[ regex.singular.mouse , '$1ice' ], | ||
[ regex.singular.foot , 'feet' ], | ||
[ regex.singular.tooth , 'teeth' ], | ||
[ regex.singular.goose , 'geese' ], | ||
[ regex.singular.quiz , '$1zes' ], | ||
[ regex.singular.whereas , '$1es' ], | ||
[ regex.singular.criterion, '$1a' ], | ||
[ regex.singular.genus , 'genera' ], | ||
[ regex.singular.s , 's' ], | ||
[ regex.singular.common, 's' ] | ||
]; | ||
/** | ||
* @description These rules translate from the plural form of a noun to its singular form. | ||
* @private | ||
*/ | ||
var singular_rules = [ | ||
[regex.singular.man, '$1en'], | ||
[regex.singular.person, '$1ople'], | ||
[regex.singular.child, '$1ren'], | ||
[regex.singular.drive, '$1s'], | ||
[regex.singular.ox, '$1en'], | ||
[regex.singular.axis, '$1es'], | ||
[regex.singular.octopus, '$1uses'], | ||
[regex.singular.alias, '$1es'], | ||
[regex.singular.summons, '$1es'], | ||
[regex.singular.bus, '$1ses'], | ||
[regex.singular.buffalo, '$1oes'], | ||
[regex.singular.tium, '$1a'], | ||
[regex.singular.sis, 'ses'], | ||
[regex.singular.ffe, '$1$2ves'], | ||
[regex.singular.hive, '$1ves'], | ||
[regex.singular.aeiouyy, '$1ies'], | ||
[regex.singular.matrix, '$1ices'], | ||
[regex.singular.vertex, '$1ices'], | ||
[regex.singular.x, '$1es'], | ||
[regex.singular.mouse, '$1ice'], | ||
[regex.singular.foot, 'feet'], | ||
[regex.singular.tooth, 'teeth'], | ||
[regex.singular.goose, 'geese'], | ||
[regex.singular.quiz, '$1zes'], | ||
[regex.singular.whereas, '$1es'], | ||
[regex.singular.criterion, '$1a'], | ||
[regex.singular.genus, 'genera'], | ||
[regex.singular.s, 's'], | ||
[regex.singular.common, 's'], | ||
]; | ||
/** | ||
* @description These rules translate from the plural form of a noun to its singular form. | ||
*/ | ||
const singularRules = [ | ||
// do not replace if its already a singular word | ||
[ regex.singular.man ], | ||
[ regex.singular.person ], | ||
[ regex.singular.child ], | ||
[ regex.singular.drive ], | ||
[ regex.singular.ox ], | ||
[ regex.singular.axis ], | ||
[ regex.singular.octopus ], | ||
[ regex.singular.alias ], | ||
[ regex.singular.summons ], | ||
[ regex.singular.bus ], | ||
[ regex.singular.buffalo ], | ||
[ regex.singular.tium ], | ||
[ regex.singular.sis ], | ||
[ regex.singular.ffe ], | ||
[ regex.singular.hive ], | ||
[ regex.singular.aeiouyy ], | ||
[ regex.singular.x ], | ||
[ regex.singular.matrix ], | ||
[ regex.singular.mouse ], | ||
[ regex.singular.foot ], | ||
[ regex.singular.tooth ], | ||
[ regex.singular.goose ], | ||
[ regex.singular.quiz ], | ||
[ regex.singular.whereas ], | ||
[ regex.singular.criterion ], | ||
[ regex.singular.genus ], | ||
[regex.singular.man], | ||
[regex.singular.person], | ||
[regex.singular.child], | ||
[regex.singular.drive], | ||
[regex.singular.ox], | ||
[regex.singular.axis], | ||
[regex.singular.octopus], | ||
[regex.singular.alias], | ||
[regex.singular.summons], | ||
[regex.singular.bus], | ||
[regex.singular.buffalo], | ||
[regex.singular.tium], | ||
[regex.singular.sis], | ||
[regex.singular.ffe], | ||
[regex.singular.hive], | ||
[regex.singular.aeiouyy], | ||
[regex.singular.x], | ||
[regex.singular.matrix], | ||
[regex.singular.mouse], | ||
[regex.singular.foot], | ||
[regex.singular.tooth], | ||
[regex.singular.goose], | ||
[regex.singular.quiz], | ||
[regex.singular.whereas], | ||
[regex.singular.criterion], | ||
[regex.singular.genus], | ||
// original rule | ||
[ regex.plural.men , '$1an' ], | ||
[ regex.plural.people , '$1rson' ], | ||
[ regex.plural.children , '$1' ], | ||
[ regex.plural.databases, '$1'], | ||
[ regex.plural.drives , '$1'], | ||
[ regex.plural.genera , 'genus'], | ||
[ regex.plural.criteria , '$1on'], | ||
[ regex.plural.tia , '$1um' ], | ||
[ regex.plural.analyses , '$1$2sis' ], | ||
[ regex.plural.hives , '$1ve' ], | ||
[ regex.plural.curves , '$1' ], | ||
[ regex.plural.lrves , '$1f' ], | ||
[ regex.plural.aves , '$1ve' ], | ||
[ regex.plural.foves , '$1fe' ], | ||
[ regex.plural.movies , '$1ovie' ], | ||
[ regex.plural.aeiouyies, '$1y' ], | ||
[ regex.plural.series , '$1eries' ], | ||
[ regex.plural.xes , '$1' ], | ||
[ regex.plural.mice , '$1ouse' ], | ||
[ regex.plural.buses , '$1' ], | ||
[ regex.plural.oes , '$1' ], | ||
[ regex.plural.shoes , '$1' ], | ||
[ regex.plural.crises , '$1is' ], | ||
[ regex.plural.octopuses, '$1us' ], | ||
[ regex.plural.aliases , '$1' ], | ||
[ regex.plural.summonses, '$1' ], | ||
[ regex.plural.oxen , '$1' ], | ||
[ regex.plural.matrices , '$1ix' ], | ||
[ regex.plural.vertices , '$1ex' ], | ||
[ regex.plural.feet , 'foot' ], | ||
[ regex.plural.teeth , 'tooth' ], | ||
[ regex.plural.geese , 'goose' ], | ||
[ regex.plural.quizzes , '$1' ], | ||
[ regex.plural.whereases, '$1' ], | ||
[ regex.plural.ss, 'ss' ], | ||
[ regex.plural.s , '' ] | ||
]; | ||
/** | ||
* @description This is a list of words that should not be capitalized for title case. | ||
* @private | ||
*/ | ||
var non_titlecased_words = [ | ||
'and', 'or', 'nor', 'a', 'an', 'the', 'so', 'but', 'to', 'of', 'at','by', | ||
'from', 'into', 'on', 'onto', 'off', 'out', 'in', 'over', 'with', 'for' | ||
]; | ||
/** | ||
* @description These are regular expressions used for converting between String formats. | ||
* @private | ||
*/ | ||
var id_suffix = new RegExp( '(_ids|_id)$', 'g' ); | ||
var underbar = new RegExp( '_', 'g' ); | ||
var space_or_underbar = new RegExp( '[\ _]', 'g' ); | ||
var uppercase = new RegExp( '([A-Z])', 'g' ); | ||
var underbar_prefix = new RegExp( '^_' ); | ||
var inflector = { | ||
/** | ||
* A helper method that applies rules based replacement to a String. | ||
* @private | ||
* @function | ||
* @param {String} str String to modify and return based on the passed rules. | ||
* @param {Array: [RegExp, String]} rules Regexp to match paired with String to use for replacement | ||
* @param {Array: [String]} skip Strings to skip if they match | ||
* @param {String} override String to return as though this method succeeded (used to conform to APIs) | ||
* @returns {String} Return passed String modified by passed rules. | ||
* @example | ||
* | ||
* this._apply_rules( 'cows', singular_rules ); // === 'cow' | ||
*/ | ||
_apply_rules : function ( str, rules, skip, override ){ | ||
if( override ){ | ||
[regex.plural.men, '$1an'], | ||
[regex.plural.people, '$1rson'], | ||
[regex.plural.children, '$1'], | ||
[regex.plural.databases, '$1'], | ||
[regex.plural.drives, '$1'], | ||
[regex.plural.genera, 'genus'], | ||
[regex.plural.criteria, '$1on'], | ||
[regex.plural.tia, '$1um'], | ||
[regex.plural.analyses, '$1$2sis'], | ||
[regex.plural.hives, '$1ve'], | ||
[regex.plural.curves, '$1'], | ||
[regex.plural.lrves, '$1f'], | ||
[regex.plural.aves, '$1ve'], | ||
[regex.plural.foves, '$1fe'], | ||
[regex.plural.movies, '$1ovie'], | ||
[regex.plural.aeiouyies, '$1y'], | ||
[regex.plural.series, '$1eries'], | ||
[regex.plural.xes, '$1'], | ||
[regex.plural.mice, '$1ouse'], | ||
[regex.plural.buses, '$1'], | ||
[regex.plural.oes, '$1'], | ||
[regex.plural.shoes, '$1'], | ||
[regex.plural.crises, '$1is'], | ||
[regex.plural.octopuses, '$1us'], | ||
[regex.plural.aliases, '$1'], | ||
[regex.plural.summonses, '$1'], | ||
[regex.plural.oxen, '$1'], | ||
[regex.plural.matrices, '$1ix'], | ||
[regex.plural.vertices, '$1ex'], | ||
[regex.plural.feet, 'foot'], | ||
[regex.plural.teeth, 'tooth'], | ||
[regex.plural.geese, 'goose'], | ||
[regex.plural.quizzes, '$1'], | ||
[regex.plural.whereases, '$1'], | ||
[regex.plural.ss, 'ss'], | ||
[regex.plural.s, ''], | ||
]; | ||
/** | ||
* @description This is a list of words that should not be capitalized for title case. | ||
*/ | ||
const nonTitlecasedWords = [ | ||
'and', | ||
'or', | ||
'nor', | ||
'a', | ||
'an', | ||
'the', | ||
'so', | ||
'but', | ||
'to', | ||
'of', | ||
'at', | ||
'by', | ||
'from', | ||
'into', | ||
'on', | ||
'onto', | ||
'off', | ||
'out', | ||
'in', | ||
'over', | ||
'with', | ||
'for', | ||
]; | ||
/** | ||
* @description These are regular expressions used for converting between String formats. | ||
*/ | ||
const idSuffix = new RegExp('(_ids|_id)$', 'g'); | ||
const underbar = new RegExp('_', 'g'); | ||
const spaceOrUnderbar = new RegExp('[ _]', 'g'); | ||
const uppercase = new RegExp('([A-Z])', 'g'); | ||
const underbarPrefix = new RegExp('^_'); | ||
/** | ||
* A helper method that applies rules based replacement to a String. | ||
* @param str String to modify and return based on the passed rules. | ||
* @param rules Regexp to match paired with String to use for replacement | ||
* @param skip Strings to skip if they match | ||
* @param override String to return as though this method succeeded (used to conform to APIs) | ||
* @returns Return passed String modified by passed rules. | ||
* @example | ||
* | ||
* applyRules( 'cows', singular_rules ); // === 'cow' | ||
*/ | ||
function applyRules(str, rules, skip, override) { | ||
if (override) { | ||
str = override; | ||
}else{ | ||
var ignore = ( inflector.indexOf( skip, str.toLowerCase()) > -1 ); | ||
if( !ignore ){ | ||
var i = 0; | ||
var j = rules.length; | ||
for( ; i < j; i++ ){ | ||
if( str.match( rules[ i ][ 0 ])){ | ||
if( rules[ i ][ 1 ] !== undefined ){ | ||
str = str.replace( rules[ i ][ 0 ], rules[ i ][ 1 ]); | ||
} | ||
break; | ||
} | ||
else { | ||
const ignore = indexOf(skip, str.toLowerCase()) > -1; | ||
if (!ignore) { | ||
const j = rules.length; | ||
for (let i = 0; i < j; i++) { | ||
if (str.match(rules[i][0])) { | ||
const replaceRule = rules[i][1]; | ||
if (replaceRule !== undefined) { | ||
str = str.replace(rules[i][0], replaceRule); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return str; | ||
}, | ||
/** | ||
* This lets us detect if an Array contains a given element. | ||
* @public | ||
* @function | ||
* @param {Array} arr The subject array. | ||
* @param {Object} item Object to locate in the Array. | ||
* @param {Number} from_index Starts checking from this position in the Array.(optional) | ||
* @param {Function} compare_func Function used to compare Array item vs passed item.(optional) | ||
* @returns {Number} Return index position in the Array of the passed item. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1 | ||
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0 | ||
*/ | ||
indexOf : function ( arr, item, from_index, compare_func ){ | ||
if( !from_index ){ | ||
from_index = -1; | ||
} | ||
var index = -1; | ||
var i = from_index; | ||
var j = arr.length; | ||
for( ; i < j; i++ ){ | ||
if( arr[ i ] === item || compare_func && compare_func( arr[ i ], item )){ | ||
index = i; | ||
break; | ||
} | ||
return str; | ||
} | ||
/** | ||
* This lets us detect if an Array contains a given element. | ||
* @param arr The subject array. | ||
* @param item Object to locate in the Array. | ||
* @param fromIndex Starts checking from this position in the Array.(optional) | ||
* @param compareFunc Function used to compare Array item vs passed item.(optional) | ||
* @returns Return index position in the Array of the passed item. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1 | ||
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0 | ||
*/ | ||
function indexOf(arr, item, fromIndex, compareFunc) { | ||
if (!fromIndex) { | ||
fromIndex = -1; | ||
} | ||
let index = -1; | ||
for (let i = fromIndex; i < arr.length; i++) { | ||
if (arr[i] === item || (compareFunc && compareFunc(arr[i], item))) { | ||
index = i; | ||
break; | ||
} | ||
} | ||
return index; | ||
}, | ||
/** | ||
* This function adds pluralization support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {String} plural Overrides normal output with said String.(optional) | ||
* @returns {String} Singular English language nouns are returned in plural form. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.pluralize( 'person' ); // === 'people' | ||
* inflection.pluralize( 'octopus' ); // === 'octopuses' | ||
* inflection.pluralize( 'Hat' ); // === 'Hats' | ||
* inflection.pluralize( 'person', 'guys' ); // === 'guys' | ||
*/ | ||
pluralize : function ( str, plural ){ | ||
return inflector._apply_rules( str, plural_rules, uncountable_words, plural ); | ||
}, | ||
/** | ||
* This function adds singularization support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {String} singular Overrides normal output with said String.(optional) | ||
* @returns {String} Plural English language nouns are returned in singular form. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.singularize( 'people' ); // === 'person' | ||
* inflection.singularize( 'octopuses' ); // === 'octopus' | ||
* inflection.singularize( 'Hats' ); // === 'Hat' | ||
* inflection.singularize( 'guys', 'person' ); // === 'person' | ||
*/ | ||
singularize : function ( str, singular ){ | ||
return inflector._apply_rules( str, singular_rules, uncountable_words, singular ); | ||
}, | ||
/** | ||
* This function will pluralize or singularlize a String appropriately based on a number value | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {Number} count The number to base pluralization off of. | ||
* @param {String} singular Overrides normal output with said String.(optional) | ||
* @param {String} plural Overrides normal output with said String.(optional) | ||
* @returns {String} English language nouns are returned in the plural or singular form based on the count. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.inflect( 'people' 1 ); // === 'person' | ||
* inflection.inflect( 'octopuses' 1 ); // === 'octopus' | ||
* inflection.inflect( 'Hats' 1 ); // === 'Hat' | ||
* inflection.inflect( 'guys', 1 , 'person' ); // === 'person' | ||
* inflection.inflect( 'inches', 1.5 ); // === 'inches' | ||
* inflection.inflect( 'person', 2 ); // === 'people' | ||
* inflection.inflect( 'octopus', 2 ); // === 'octopuses' | ||
* inflection.inflect( 'Hat', 2 ); // === 'Hats' | ||
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys' | ||
*/ | ||
inflect : function ( str, count, singular, plural ){ | ||
count = parseFloat( count, 10 ); | ||
if( isNaN( count )) return str; | ||
if( count === 1 ){ | ||
return inflector._apply_rules( str, singular_rules, uncountable_words, singular ); | ||
}else{ | ||
return inflector._apply_rules( str, plural_rules, uncountable_words, plural ); | ||
} | ||
}, | ||
/** | ||
* This function adds camelization support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {Boolean} low_first_letter Default is to capitalize the first letter of the results.(optional) | ||
* Passing true will lowercase it. | ||
* @returns {String} Lower case underscored words will be returned in camel case. | ||
* additionally '/' is translated to '::' | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.camelize( 'message_properties' ); // === 'MessageProperties' | ||
* inflection.camelize( 'message_properties', true ); // === 'messageProperties' | ||
*/ | ||
camelize : function ( str, low_first_letter ){ | ||
var str_path = str.split( '/' ); | ||
var i = 0; | ||
var j = str_path.length; | ||
var str_arr, init_x, k, l, first; | ||
for( ; i < j; i++ ){ | ||
str_arr = str_path[ i ].split( '_' ); | ||
k = 0; | ||
l = str_arr.length; | ||
for( ; k < l; k++ ){ | ||
if( k !== 0 ){ | ||
str_arr[ k ] = str_arr[ k ].toLowerCase(); | ||
} | ||
first = str_arr[ k ].charAt( 0 ); | ||
first = low_first_letter && i === 0 && k === 0 | ||
? first.toLowerCase() : first.toUpperCase(); | ||
str_arr[ k ] = first + str_arr[ k ].substring( 1 ); | ||
} | ||
return index; | ||
} | ||
exports.indexOf = indexOf; | ||
/** | ||
* This function adds pluralization support to every String object. | ||
* @param str The subject string. | ||
* @param plural Overrides normal output with said String.(optional) | ||
* @returns Singular English language nouns are returned in plural form. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.pluralize( 'person' ); // === 'people' | ||
* inflection.pluralize( 'octopus' ); // === 'octopuses' | ||
* inflection.pluralize( 'Hat' ); // === 'Hats' | ||
* inflection.pluralize( 'person', 'guys' ); // === 'guys' | ||
*/ | ||
function pluralize(str, plural) { | ||
return applyRules(str, pluralRules, uncountableWords, plural); | ||
} | ||
exports.pluralize = pluralize; | ||
/** | ||
* This function adds singularization support to every String object. | ||
* @param str The subject string. | ||
* @param singular Overrides normal output with said String.(optional) | ||
* @returns Plural English language nouns are returned in singular form. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.singularize( 'people' ); // === 'person' | ||
* inflection.singularize( 'octopuses' ); // === 'octopus' | ||
* inflection.singularize( 'Hats' ); // === 'Hat' | ||
* inflection.singularize( 'guys', 'person' ); // === 'person' | ||
*/ | ||
function singularize(str, singular) { | ||
return applyRules(str, singularRules, uncountableWords, singular); | ||
} | ||
exports.singularize = singularize; | ||
/** | ||
* This function will pluralize or singularlize a String appropriately based on a number value | ||
* @param str The subject string. | ||
* @param count The number to base pluralization off of. | ||
* @param singular Overrides normal output with said String.(optional) | ||
* @param plural Overrides normal output with said String.(optional) | ||
* @returns English language nouns are returned in the plural or singular form based on the count. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.inflect( 'people' 1 ); // === 'person' | ||
* inflection.inflect( 'octopuses' 1 ); // === 'octopus' | ||
* inflection.inflect( 'Hats' 1 ); // === 'Hat' | ||
* inflection.inflect( 'guys', 1 , 'person' ); // === 'person' | ||
* inflection.inflect( 'inches', 1.5 ); // === 'inches' | ||
* inflection.inflect( 'person', 2 ); // === 'people' | ||
* inflection.inflect( 'octopus', 2 ); // === 'octopuses' | ||
* inflection.inflect( 'Hat', 2 ); // === 'Hats' | ||
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys' | ||
*/ | ||
function inflect(str, count, singular, plural) { | ||
if (isNaN(count)) | ||
return str; | ||
if (count === 1) { | ||
return applyRules(str, singularRules, uncountableWords, singular); | ||
} | ||
else { | ||
return applyRules(str, pluralRules, uncountableWords, plural); | ||
} | ||
} | ||
exports.inflect = inflect; | ||
/** | ||
* This function adds camelization support to every String object. | ||
* @param str The subject string. | ||
* @param lowFirstLetter Default is to capitalize the first letter of the results.(optional) | ||
* Passing true will lowercase it. | ||
* @returns Lower case underscored words will be returned in camel case. | ||
* additionally '/' is translated to '::' | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.camelize( 'message_properties' ); // === 'MessageProperties' | ||
* inflection.camelize( 'message_properties', true ); // === 'messageProperties' | ||
*/ | ||
function camelize(str, lowFirstLetter) { | ||
const strPath = str.split('/'); | ||
const j = strPath.length; | ||
let strArr, k, l, first; | ||
for (let i = 0; i < j; i++) { | ||
strArr = strPath[i].split('_'); | ||
k = 0; | ||
l = strArr.length; | ||
for (; k < l; k++) { | ||
if (k !== 0) { | ||
strArr[k] = strArr[k].toLowerCase(); | ||
} | ||
first = strArr[k].charAt(0); | ||
first = | ||
lowFirstLetter && i === 0 && k === 0 | ||
? first.toLowerCase() | ||
: first.toUpperCase(); | ||
strArr[k] = first + strArr[k].substring(1); | ||
} | ||
str_path[ i ] = str_arr.join( '' ); | ||
} | ||
return str_path.join( '::' ); | ||
}, | ||
/** | ||
* This function adds underscore support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {Boolean} all_upper_case Default is to lowercase and add underscore prefix.(optional) | ||
* Passing true will return as entered. | ||
* @returns {String} Camel cased words are returned as lower cased and underscored. | ||
* additionally '::' is translated to '/'. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.underscore( 'MessageProperties' ); // === 'message_properties' | ||
* inflection.underscore( 'messageProperties' ); // === 'message_properties' | ||
* inflection.underscore( 'MP', true ); // === 'MP' | ||
*/ | ||
underscore : function ( str, all_upper_case ){ | ||
if( all_upper_case && str === str.toUpperCase()) return str; | ||
var str_path = str.split( '::' ); | ||
var i = 0; | ||
var j = str_path.length; | ||
for( ; i < j; i++ ){ | ||
str_path[ i ] = str_path[ i ].replace( uppercase, '_$1' ); | ||
str_path[ i ] = str_path[ i ].replace( underbar_prefix, '' ); | ||
} | ||
return str_path.join( '/' ).toLowerCase(); | ||
}, | ||
/** | ||
* This function adds humanize support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {Boolean} low_first_letter Default is to capitalize the first letter of the results.(optional) | ||
* Passing true will lowercase it. | ||
* @returns {String} Lower case underscored words will be returned in humanized form. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.humanize( 'message_properties' ); // === 'Message properties' | ||
* inflection.humanize( 'message_properties', true ); // === 'message properties' | ||
*/ | ||
humanize : function ( str, low_first_letter ){ | ||
str = str.toLowerCase(); | ||
str = str.replace( id_suffix, '' ); | ||
str = str.replace( underbar, ' ' ); | ||
if( !low_first_letter ){ | ||
str = inflector.capitalize( str ); | ||
} | ||
return str; | ||
}, | ||
/** | ||
* This function adds capitalization support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} All characters will be lower case and the first will be upper. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.capitalize( 'message_properties' ); // === 'Message_properties' | ||
* inflection.capitalize( 'message properties', true ); // === 'Message properties' | ||
*/ | ||
capitalize : function ( str ){ | ||
str = str.toLowerCase(); | ||
return str.substring( 0, 1 ).toUpperCase() + str.substring( 1 ); | ||
}, | ||
/** | ||
* This function replaces underscores with dashes in the string. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} Replaces all spaces or underscores with dashes. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.dasherize( 'message_properties' ); // === 'message-properties' | ||
* inflection.dasherize( 'Message Properties' ); // === 'Message-Properties' | ||
*/ | ||
dasherize : function ( str ){ | ||
return str.replace( space_or_underbar, '-' ); | ||
}, | ||
/** | ||
* This function adds titleize support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} Capitalizes words as you would for a book title. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.titleize( 'message_properties' ); // === 'Message Properties' | ||
* inflection.titleize( 'message properties to keep' ); // === 'Message Properties to Keep' | ||
*/ | ||
titleize : function ( str ){ | ||
str = str.toLowerCase().replace( underbar, ' ' ); | ||
var str_arr = str.split( ' ' ); | ||
var i = 0; | ||
var j = str_arr.length; | ||
var d, k, l; | ||
for( ; i < j; i++ ){ | ||
d = str_arr[ i ].split( '-' ); | ||
k = 0; | ||
strPath[i] = strArr.join(''); | ||
} | ||
return strPath.join('::'); | ||
} | ||
exports.camelize = camelize; | ||
/** | ||
* This function adds underscore support to every String object. | ||
* @param str The subject string. | ||
* @param allUpperCase Default is to lowercase and add underscore prefix.(optional) | ||
* Passing true will return as entered. | ||
* @returns Camel cased words are returned as lower cased and underscored. | ||
* additionally '::' is translated to '/'. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.underscore( 'MessageProperties' ); // === 'message_properties' | ||
* inflection.underscore( 'messageProperties' ); // === 'message_properties' | ||
* inflection.underscore( 'MP', true ); // === 'MP' | ||
*/ | ||
function underscore(str, allUpperCase) { | ||
if (allUpperCase && str === str.toUpperCase()) | ||
return str; | ||
const strPath = str.split('::'); | ||
const j = strPath.length; | ||
for (let i = 0; i < j; i++) { | ||
strPath[i] = strPath[i].replace(uppercase, '_$1'); | ||
strPath[i] = strPath[i].replace(underbarPrefix, ''); | ||
} | ||
return strPath.join('/').toLowerCase(); | ||
} | ||
exports.underscore = underscore; | ||
/** | ||
* This function adds humanize support to every String object. | ||
* @param str The subject string. | ||
* @param lowFirstLetter Default is to capitalize the first letter of the results.(optional) | ||
* Passing true will lowercase it. | ||
* @returns Lower case underscored words will be returned in humanized form. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.humanize( 'message_properties' ); // === 'Message properties' | ||
* inflection.humanize( 'message_properties', true ); // === 'message properties' | ||
*/ | ||
function humanize(str, lowFirstLetter) { | ||
str = str.toLowerCase(); | ||
str = str.replace(idSuffix, ''); | ||
str = str.replace(underbar, ' '); | ||
if (!lowFirstLetter) { | ||
str = capitalize(str); | ||
} | ||
return str; | ||
} | ||
exports.humanize = humanize; | ||
/** | ||
* This function adds capitalization support to every String object. | ||
* @param str The subject string. | ||
* @returns All characters will be lower case and the first will be upper. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.capitalize( 'message_properties' ); // === 'Message_properties' | ||
* inflection.capitalize( 'message properties', true ); // === 'Message properties' | ||
*/ | ||
function capitalize(str) { | ||
str = str.toLowerCase(); | ||
return str.substring(0, 1).toUpperCase() + str.substring(1); | ||
} | ||
exports.capitalize = capitalize; | ||
/** | ||
* This function replaces underscores with dashes in the string. | ||
* @param str The subject string. | ||
* @returns Replaces all spaces or underscores with dashes. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.dasherize( 'message_properties' ); // === 'message-properties' | ||
* inflection.dasherize( 'Message Properties' ); // === 'Message-Properties' | ||
*/ | ||
function dasherize(str) { | ||
return str.replace(spaceOrUnderbar, '-'); | ||
} | ||
exports.dasherize = dasherize; | ||
/** | ||
* This function adds titleize support to every String object. | ||
* @param str The subject string. | ||
* @returns Capitalizes words as you would for a book title. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.titleize( 'message_properties' ); // === 'Message Properties' | ||
* inflection.titleize( 'message properties to keep' ); // === 'Message Properties to Keep' | ||
*/ | ||
function titleize(str) { | ||
str = str.toLowerCase().replace(underbar, ' '); | ||
const strArr = str.split(' '); | ||
const j = strArr.length; | ||
let d, l; | ||
for (let i = 0; i < j; i++) { | ||
d = strArr[i].split('-'); | ||
l = d.length; | ||
for( ; k < l; k++){ | ||
if( inflector.indexOf( non_titlecased_words, d[ k ].toLowerCase()) < 0 ){ | ||
d[ k ] = inflector.capitalize( d[ k ]); | ||
} | ||
for (let k = 0; k < l; k++) { | ||
if (indexOf(nonTitlecasedWords, d[k].toLowerCase()) < 0) { | ||
d[k] = capitalize(d[k]); | ||
} | ||
} | ||
str_arr[ i ] = d.join( '-' ); | ||
} | ||
str = str_arr.join( ' ' ); | ||
str = str.substring( 0, 1 ).toUpperCase() + str.substring( 1 ); | ||
return str; | ||
}, | ||
/** | ||
* This function adds demodulize support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} Removes module names leaving only class names.(Ruby style) | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.demodulize( 'Message::Bus::Properties' ); // === 'Properties' | ||
*/ | ||
demodulize : function ( str ){ | ||
var str_arr = str.split( '::' ); | ||
return str_arr[ str_arr.length - 1 ]; | ||
}, | ||
/** | ||
* This function adds tableize support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} Return camel cased words into their underscored plural form. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.tableize( 'MessageBusProperty' ); // === 'message_bus_properties' | ||
*/ | ||
tableize : function ( str ){ | ||
str = inflector.underscore( str ); | ||
str = inflector.pluralize( str ); | ||
return str; | ||
}, | ||
/** | ||
* This function adds classification support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} Underscored plural nouns become the camel cased singular form. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.classify( 'message_bus_properties' ); // === 'MessageBusProperty' | ||
*/ | ||
classify : function ( str ){ | ||
str = inflector.camelize( str ); | ||
str = inflector.singularize( str ); | ||
return str; | ||
}, | ||
/** | ||
strArr[i] = d.join('-'); | ||
} | ||
str = strArr.join(' '); | ||
str = str.substring(0, 1).toUpperCase() + str.substring(1); | ||
return str; | ||
} | ||
exports.titleize = titleize; | ||
/** | ||
* This function adds demodulize support to every String object. | ||
* @param str The subject string. | ||
* @returns Removes module names leaving only class names.(Ruby style) | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.demodulize( 'Message::Bus::Properties' ); // === 'Properties' | ||
*/ | ||
function demodulize(str) { | ||
const strArr = str.split('::'); | ||
return strArr[strArr.length - 1]; | ||
} | ||
exports.demodulize = demodulize; | ||
/** | ||
* This function adds tableize support to every String object. | ||
* @param str The subject string. | ||
* @returns Return camel cased words into their underscored plural form. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.tableize( 'MessageBusProperty' ); // === 'message_bus_properties' | ||
*/ | ||
function tableize(str) { | ||
str = underscore(str); | ||
str = pluralize(str); | ||
return str; | ||
} | ||
exports.tableize = tableize; | ||
/** | ||
* This function adds classification support to every String object. | ||
* @param str The subject string. | ||
* @returns Underscored plural nouns become the camel cased singular form. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.classify( 'message_bus_properties' ); // === 'MessageBusProperty' | ||
*/ | ||
function classify(str) { | ||
str = camelize(str); | ||
str = singularize(str); | ||
return str; | ||
} | ||
exports.classify = classify; | ||
/** | ||
* This function adds foreign key support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {Boolean} drop_id_ubar Default is to seperate id with an underbar at the end of the class name, | ||
* @param str The subject string. | ||
* @param dropIdUbar Default is to seperate id with an underbar at the end of the class name, | ||
you can pass true to skip it.(optional) | ||
* @returns {String} Underscored plural nouns become the camel cased singular form. | ||
* @returns Underscored plural nouns become the camel cased singular form. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* const inflection = require( 'inflection' ); | ||
* | ||
@@ -1031,88 +912,81 @@ * inflection.foreign_key( 'MessageBusProperty' ); // === 'message_bus_property_id' | ||
*/ | ||
foreign_key : function ( str, drop_id_ubar ){ | ||
str = inflector.demodulize( str ); | ||
str = inflector.underscore( str ) + (( drop_id_ubar ) ? ( '' ) : ( '_' )) + 'id'; | ||
return str; | ||
}, | ||
/** | ||
* This function adds ordinalize support to every String object. | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @returns {String} Return all found numbers their sequence like '22nd'. | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.ordinalize( 'the 1 pitch' ); // === 'the 1st pitch' | ||
*/ | ||
ordinalize : function ( str ){ | ||
var str_arr = str.split( ' ' ); | ||
var i = 0; | ||
var j = str_arr.length; | ||
for( ; i < j; i++ ){ | ||
var k = parseInt( str_arr[ i ], 10 ); | ||
if( !isNaN( k )){ | ||
var ltd = str_arr[ i ].substring( str_arr[ i ].length - 2 ); | ||
var ld = str_arr[ i ].substring( str_arr[ i ].length - 1 ); | ||
var suf = 'th'; | ||
if( ltd != '11' && ltd != '12' && ltd != '13' ){ | ||
if( ld === '1' ){ | ||
suf = 'st'; | ||
}else if( ld === '2' ){ | ||
suf = 'nd'; | ||
}else if( ld === '3' ){ | ||
suf = 'rd'; | ||
function foreignKey(str, dropIdUbar) { | ||
str = demodulize(str); | ||
str = underscore(str) + (dropIdUbar ? '' : '_') + 'id'; | ||
return str; | ||
} | ||
exports.foreignKey = foreignKey; | ||
/** | ||
* This function adds ordinalize support to every String object. | ||
* @param str The subject string. | ||
* @returns Return all found numbers their sequence like '22nd'. | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.ordinalize( 'the 1 pitch' ); // === 'the 1st pitch' | ||
*/ | ||
function ordinalize(str) { | ||
const strArr = str.split(' '); | ||
const j = strArr.length; | ||
for (let i = 0; i < j; i++) { | ||
const k = parseInt(strArr[i], 10); | ||
if (!isNaN(k)) { | ||
const ltd = strArr[i].substring(strArr[i].length - 2); | ||
const ld = strArr[i].substring(strArr[i].length - 1); | ||
let suf = 'th'; | ||
if (ltd != '11' && ltd != '12' && ltd != '13') { | ||
if (ld === '1') { | ||
suf = 'st'; | ||
} | ||
else if (ld === '2') { | ||
suf = 'nd'; | ||
} | ||
else if (ld === '3') { | ||
suf = 'rd'; | ||
} | ||
} | ||
} | ||
str_arr[ i ] += suf; | ||
strArr[i] += suf; | ||
} | ||
} | ||
return str_arr.join( ' ' ); | ||
}, | ||
/** | ||
* This function performs multiple inflection methods on a string | ||
* @public | ||
* @function | ||
* @param {String} str The subject string. | ||
* @param {Array} arr An array of inflection methods. | ||
* @returns {String} | ||
* @example | ||
* | ||
* var inflection = require( 'inflection' ); | ||
* | ||
* inflection.transform( 'all job', [ 'pluralize', 'capitalize', 'dasherize' ]); // === 'All-jobs' | ||
*/ | ||
transform : function ( str, arr ){ | ||
var i = 0; | ||
var j = arr.length; | ||
for( ;i < j; i++ ){ | ||
var method = arr[ i ]; | ||
if( inflector.hasOwnProperty( method )){ | ||
str = inflector[ method ]( str ); | ||
} | ||
} | ||
return str; | ||
} | ||
}; | ||
return strArr.join(' '); | ||
} | ||
exports.ordinalize = ordinalize; | ||
const transformFunctions = { | ||
pluralize, | ||
singularize, | ||
camelize, | ||
underscore, | ||
humanize, | ||
capitalize, | ||
dasherize, | ||
titleize, | ||
demodulize, | ||
tableize, | ||
classify, | ||
foreignKey, | ||
ordinalize, | ||
}; | ||
/** | ||
* @public | ||
* This function performs multiple inflection methods on a string | ||
* @param str The subject string. | ||
* @param arr An array of inflection methods. | ||
* @returns | ||
* @example | ||
* | ||
* const inflection = require( 'inflection' ); | ||
* | ||
* inflection.transform( 'all job', [ 'pluralize', 'capitalize', 'dasherize' ]); // === 'All-jobs' | ||
*/ | ||
inflector.version = '1.13.1'; | ||
return inflector; | ||
})); | ||
function transform(str, arr) { | ||
const j = arr.length; | ||
for (let i = 0; i < j; i++) { | ||
const method = arr[i]; | ||
const methodFn = transformFunctions[method]; | ||
if (methodFn) { | ||
str = methodFn(str); | ||
} | ||
} | ||
return str; | ||
} | ||
exports.transform = transform; |
{ | ||
"name": "inflection", | ||
"version": "1.13.4", | ||
"version": "2.0.0", | ||
"description": "A port of inflection-js to node.js module", | ||
@@ -95,6 +95,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"terser": "^5.15.0", | ||
"vitest": "^0.23.4" | ||
"typescript": "^4.8.3", | ||
"vitest": "^0.25.2" | ||
}, | ||
"main": "./lib/inflection.js", | ||
"types": "./lib/inflection.d.ts", | ||
"repository": { | ||
@@ -104,10 +105,11 @@ "type": "git", | ||
}, | ||
"engines": [ | ||
"node >= 0.4.0" | ||
], | ||
"engines": { | ||
"node": ">=14.0.0" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "vitest", | ||
"minify": "terser lib/inflection.js -o inflection.min.js -c -m" | ||
"prepublishOnly": "npm run test -- --run && npm run build" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93857
11
2280
1