inflection
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -21,4 +21,5 @@ /*! | ||
* inflection.pluralize( 'Hat' ); // === 'Hats' | ||
* inflection.pluralize( 'person', 'guys' ); // === 'guys' | ||
*/ | ||
export declare function pluralize(str: string): string; | ||
export declare function pluralize(str: string, plural?: string): string; | ||
/** | ||
@@ -36,4 +37,5 @@ * This function adds singularization support to every String object. | ||
* inflection.singularize( 'Hats' ); // === 'Hat' | ||
* inflection.singularize( 'guys', 'person' ); // === 'person' | ||
*/ | ||
export declare function singularize(str: string): string; | ||
export declare function singularize(str: string, singular?: string): string; | ||
/** | ||
@@ -58,4 +60,5 @@ * This function will pluralize or singularlize a String appropriately based on a number value | ||
* inflection.inflect( 'Hat', 2 ); // === 'Hats' | ||
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys' | ||
*/ | ||
export declare function inflect(str: string, count: number): string; | ||
export declare function inflect(str: string, count: number, singular?: string, plural?: string): string; | ||
/** | ||
@@ -62,0 +65,0 @@ * This function adds camelization support to every String object. |
@@ -380,2 +380,3 @@ "use strict"; | ||
ffe: new RegExp('(?:([^f])fe|([lr])f)$', 'gi'), | ||
focus: new RegExp('^(focus)$', 'gi'), | ||
hive: new RegExp('(hi|ti)ve$', 'gi'), | ||
@@ -447,2 +448,3 @@ aeiouyy: new RegExp('([^aeiouy]|qu)y$', 'gi'), | ||
[regex.singular.ffe, '$1$2ves'], | ||
[regex.singular.focus, '$1es'], | ||
[regex.singular.hive, '$1ves'], | ||
@@ -483,2 +485,3 @@ [regex.singular.aeiouyy, '$1ies'], | ||
[regex.singular.ffe], | ||
[regex.singular.focus], | ||
[regex.singular.hive], | ||
@@ -580,13 +583,18 @@ [regex.singular.aeiouyy], | ||
*/ | ||
function applyRules(str, rules, skip) { | ||
if (skip.includes(str.toLocaleLowerCase())) { | ||
return str; | ||
function applyRules(str, rules, skip, override) { | ||
if (override) { | ||
return override; | ||
} | ||
for (const rule of rules) { | ||
if (str.match(rule[0])) { | ||
if (rule[1] !== undefined) { | ||
return str.replace(rule[0], rule[1]); | ||
} | ||
else { | ||
if (skip.includes(str.toLocaleLowerCase())) { | ||
return str; | ||
} | ||
for (const rule of rules) { | ||
if (str.match(rule[0])) { | ||
if (rule[1] !== undefined) { | ||
return str.replace(rule[0], rule[1]); | ||
} | ||
return str; | ||
} | ||
} | ||
} | ||
@@ -607,5 +615,6 @@ return str; | ||
* inflection.pluralize( 'Hat' ); // === 'Hats' | ||
* inflection.pluralize( 'person', 'guys' ); // === 'guys' | ||
*/ | ||
function pluralize(str) { | ||
return applyRules(str, pluralRules, uncountableWords); | ||
function pluralize(str, plural) { | ||
return applyRules(str, pluralRules, uncountableWords, plural); | ||
} | ||
@@ -624,5 +633,6 @@ /** | ||
* inflection.singularize( 'Hats' ); // === 'Hat' | ||
* inflection.singularize( 'guys', 'person' ); // === 'person' | ||
*/ | ||
function singularize(str) { | ||
return applyRules(str, singularRules, uncountableWords); | ||
function singularize(str, singular) { | ||
return applyRules(str, singularRules, uncountableWords, singular); | ||
} | ||
@@ -648,11 +658,12 @@ /** | ||
* inflection.inflect( 'Hat', 2 ); // === 'Hats' | ||
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys' | ||
*/ | ||
function inflect(str, count) { | ||
function inflect(str, count, singular, plural) { | ||
if (isNaN(count)) | ||
return str; | ||
if (count === 1) { | ||
return applyRules(str, singularRules, uncountableWords); | ||
return applyRules(str, singularRules, uncountableWords, singular); | ||
} | ||
else { | ||
return applyRules(str, pluralRules, uncountableWords); | ||
return applyRules(str, pluralRules, uncountableWords, plural); | ||
} | ||
@@ -659,0 +670,0 @@ } |
{ | ||
"name": "inflection", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "A port of inflection-js to node.js module", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -394,2 +394,3 @@ /*! | ||
ffe: new RegExp('(?:([^f])fe|([lr])f)$', 'gi'), | ||
focus: new RegExp('^(focus)$', 'gi'), | ||
hive: new RegExp('(hi|ti)ve$', 'gi'), | ||
@@ -463,2 +464,3 @@ aeiouyy: new RegExp('([^aeiouy]|qu)y$', 'gi'), | ||
[regex.singular.ffe, '$1$2ves'], | ||
[regex.singular.focus, '$1es'], | ||
[regex.singular.hive, '$1ves'], | ||
@@ -501,2 +503,3 @@ [regex.singular.aeiouyy, '$1ies'], | ||
[regex.singular.ffe], | ||
[regex.singular.focus], | ||
[regex.singular.hive], | ||
@@ -603,14 +606,23 @@ [regex.singular.aeiouyy], | ||
*/ | ||
function applyRules(str: string, rules: [RegExp, string?][], skip: string[]) { | ||
if (skip.includes(str.toLocaleLowerCase())) { | ||
return str; | ||
} | ||
function applyRules( | ||
str: string, | ||
rules: [RegExp, string?][], | ||
skip: string[], | ||
override?: string | ||
) { | ||
if (override) { | ||
return override; | ||
} else { | ||
if (skip.includes(str.toLocaleLowerCase())) { | ||
return str; | ||
} | ||
for (const rule of rules) { | ||
if (str.match(rule[0])) { | ||
if (rule[1] !== undefined) { | ||
return str.replace(rule[0], rule[1]); | ||
for (const rule of rules) { | ||
if (str.match(rule[0])) { | ||
if (rule[1] !== undefined) { | ||
return str.replace(rule[0], rule[1]); | ||
} | ||
return str; | ||
} | ||
return str; | ||
} | ||
@@ -634,5 +646,6 @@ } | ||
* inflection.pluralize( 'Hat' ); // === 'Hats' | ||
* inflection.pluralize( 'person', 'guys' ); // === 'guys' | ||
*/ | ||
export function pluralize(str: string) { | ||
return applyRules(str, pluralRules, uncountableWords); | ||
export function pluralize(str: string, plural?: string) { | ||
return applyRules(str, pluralRules, uncountableWords, plural); | ||
} | ||
@@ -652,5 +665,6 @@ | ||
* inflection.singularize( 'Hats' ); // === 'Hat' | ||
* inflection.singularize( 'guys', 'person' ); // === 'person' | ||
*/ | ||
export function singularize(str: string) { | ||
return applyRules(str, singularRules, uncountableWords); | ||
export function singularize(str: string, singular?: string) { | ||
return applyRules(str, singularRules, uncountableWords, singular); | ||
} | ||
@@ -677,10 +691,16 @@ | ||
* inflection.inflect( 'Hat', 2 ); // === 'Hats' | ||
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys' | ||
*/ | ||
export function inflect(str: string, count: number) { | ||
export function inflect( | ||
str: string, | ||
count: number, | ||
singular?: string, | ||
plural?: string | ||
) { | ||
if (isNaN(count)) return str; | ||
if (count === 1) { | ||
return applyRules(str, singularRules, uncountableWords); | ||
return applyRules(str, singularRules, uncountableWords, singular); | ||
} else { | ||
return applyRules(str, pluralRules, uncountableWords); | ||
return applyRules(str, pluralRules, uncountableWords, plural); | ||
} | ||
@@ -687,0 +707,0 @@ } |
90335
2205