Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/inflection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
* inflection.pluralize( 'person' ); // === 'people'
* inflection.pluralize( 'octopus' ); // === 'octopuses'
* inflection.pluralize( 'Hat' ); // === 'Hats'
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
*/
export declare function pluralize(str: string, plural?: string): string;
export declare function pluralize(str: string): string;
/**
* This function adds singularization support to every String object.
* @param str The subject string.
Expand All @@ -33,9 +32,8 @@ export declare function pluralize(str: string, plural?: string): string;
* inflection.singularize( 'people' ); // === 'person'
* inflection.singularize( 'octopuses' ); // === 'octopus'
* inflection.singularize( 'Hats' ); // === 'Hat'
* inflection.singularize( 'guys', 'person' ); // === 'person'
*/
export declare function singularize(str: string, singular?: string): string;
export declare function singularize(str: string): string;
/**
* This function will pluralize or singularlize a String appropriately based on a number value
* @param str The subject string.
Expand All @@ -55,9 +53,8 @@ export declare function singularize(str: string, singular?: string): string;
* inflection.inflect( 'person', 2 ); // === 'people'
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
*/
export declare function inflect(str: string, count: number, singular?: string, plural?: string): string;
export declare function inflect(str: string, count: number): string;
/**
* This function adds camelization support to every String object.
* @param str The subject string.
Expand Down
38 changes: 15 additions & 23 deletions lib/inflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,21 +577,16 @@ const underbarPrefix = new RegExp('^_');
*
* applyRules( 'cows', singular_rules ); // === 'cow'
*/
function applyRules(str, rules, skip, override) {
if (override) {
return override;
function applyRules(str, rules, skip) {
if (skip.includes(str.toLocaleLowerCase())) {
return str;
}
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;
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;
Expand All @@ -608,10 +603,9 @@ function applyRules(str, rules, skip, override) {
* 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);
function pluralize(str) {
return applyRules(str, pluralRules, uncountableWords);
}
/**
* This function adds singularization support to every String object.
Expand All @@ -625,10 +619,9 @@ function pluralize(str, plural) {
* 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);
function singularize(str) {
return applyRules(str, singularRules, uncountableWords);
}
/**
* This function will pluralize or singularlize a String appropriately based on a number value
Expand All @@ -649,16 +642,15 @@ function singularize(str, singular) {
* 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) {
function inflect(str, count) {
if (isNaN(count))
return str;
if (count === 1) {
return applyRules(str, singularRules, uncountableWords, singular);
return applyRules(str, singularRules, uncountableWords);
}
else {
return applyRules(str, pluralRules, uncountableWords, plural);
return applyRules(str, pluralRules, uncountableWords);
}
}
/**
Expand Down
51 changes: 17 additions & 34 deletions src/inflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,27 +600,18 @@ const underbarPrefix = new RegExp('^_');
*
* applyRules( 'cows', singular_rules ); // === 'cow'
*/
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]);
}
function applyRules(str: string, rules: [RegExp, string?][], skip: string[]) {
if (skip.includes(str.toLocaleLowerCase())) {
return str;
}

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;
}
}

Expand All @@ -639,10 +630,9 @@ function applyRules(
* inflection.pluralize( 'person' ); // === 'people'
* inflection.pluralize( 'octopus' ); // === 'octopuses'
* inflection.pluralize( 'Hat' ); // === 'Hats'
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
*/
export function pluralize(str: string, plural?: string) {
return applyRules(str, pluralRules, uncountableWords, plural);
export function pluralize(str: string) {
return applyRules(str, pluralRules, uncountableWords);
}

/**
Expand All @@ -657,10 +647,9 @@ export function pluralize(str: string, plural?: string) {
* inflection.singularize( 'people' ); // === 'person'
* inflection.singularize( 'octopuses' ); // === 'octopus'
* inflection.singularize( 'Hats' ); // === 'Hat'
* inflection.singularize( 'guys', 'person' ); // === 'person'
*/
export function singularize(str: string, singular?: string) {
return applyRules(str, singularRules, uncountableWords, singular);
export function singularize(str: string) {
return applyRules(str, singularRules, uncountableWords);
}

/**
Expand All @@ -682,20 +671,14 @@ export function singularize(str: string, singular?: string) {
* inflection.inflect( 'person', 2 ); // === 'people'
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
*/
export function inflect(
str: string,
count: number,
singular?: string,
plural?: string
) {
export function inflect(str: string, count: number) {
if (isNaN(count)) return str;

if (count === 1) {
return applyRules(str, singularRules, uncountableWords, singular);
return applyRules(str, singularRules, uncountableWords);
} else {
return applyRules(str, pluralRules, uncountableWords, plural);
return applyRules(str, pluralRules, uncountableWords);
}
}

Expand Down
Loading