Skip to content

Commit 4bfb1de

Browse files
committed
refactor!: simplify code by removing override parameters
they just return the giving parameter and therefore make no sense at all
1 parent 36a3ff4 commit 4bfb1de

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

lib/inflection.d.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
* inflection.pluralize( 'person' ); // === 'people'
1919
* inflection.pluralize( 'octopus' ); // === 'octopuses'
2020
* inflection.pluralize( 'Hat' ); // === 'Hats'
21-
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
2221
*/
23-
export declare function pluralize(str: string, plural?: string): string;
22+
export declare function pluralize(str: string): string;
2423
/**
2524
* This function adds singularization support to every String object.
2625
* @param str The subject string.
@@ -33,9 +32,8 @@ export declare function pluralize(str: string, plural?: string): string;
3332
* inflection.singularize( 'people' ); // === 'person'
3433
* inflection.singularize( 'octopuses' ); // === 'octopus'
3534
* inflection.singularize( 'Hats' ); // === 'Hat'
36-
* inflection.singularize( 'guys', 'person' ); // === 'person'
3735
*/
38-
export declare function singularize(str: string, singular?: string): string;
36+
export declare function singularize(str: string): string;
3937
/**
4038
* This function will pluralize or singularlize a String appropriately based on a number value
4139
* @param str The subject string.
@@ -55,9 +53,8 @@ export declare function singularize(str: string, singular?: string): string;
5553
* inflection.inflect( 'person', 2 ); // === 'people'
5654
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
5755
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
58-
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
5956
*/
60-
export declare function inflect(str: string, count: number, singular?: string, plural?: string): string;
57+
export declare function inflect(str: string, count: number): string;
6158
/**
6259
* This function adds camelization support to every String object.
6360
* @param str The subject string.

lib/inflection.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -577,21 +577,16 @@ const underbarPrefix = new RegExp('^_');
577577
*
578578
* applyRules( 'cows', singular_rules ); // === 'cow'
579579
*/
580-
function applyRules(str, rules, skip, override) {
581-
if (override) {
582-
return override;
580+
function applyRules(str, rules, skip) {
581+
if (skip.includes(str.toLocaleLowerCase())) {
582+
return str;
583583
}
584-
else {
585-
if (skip.includes(str.toLocaleLowerCase())) {
586-
return str;
587-
}
588-
for (const rule of rules) {
589-
if (str.match(rule[0])) {
590-
if (rule[1] !== undefined) {
591-
return str.replace(rule[0], rule[1]);
592-
}
593-
return str;
584+
for (const rule of rules) {
585+
if (str.match(rule[0])) {
586+
if (rule[1] !== undefined) {
587+
return str.replace(rule[0], rule[1]);
594588
}
589+
return str;
595590
}
596591
}
597592
return str;
@@ -608,10 +603,9 @@ function applyRules(str, rules, skip, override) {
608603
* inflection.pluralize( 'person' ); // === 'people'
609604
* inflection.pluralize( 'octopus' ); // === 'octopuses'
610605
* inflection.pluralize( 'Hat' ); // === 'Hats'
611-
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
612606
*/
613-
function pluralize(str, plural) {
614-
return applyRules(str, pluralRules, uncountableWords, plural);
607+
function pluralize(str) {
608+
return applyRules(str, pluralRules, uncountableWords);
615609
}
616610
/**
617611
* This function adds singularization support to every String object.
@@ -625,10 +619,9 @@ function pluralize(str, plural) {
625619
* inflection.singularize( 'people' ); // === 'person'
626620
* inflection.singularize( 'octopuses' ); // === 'octopus'
627621
* inflection.singularize( 'Hats' ); // === 'Hat'
628-
* inflection.singularize( 'guys', 'person' ); // === 'person'
629622
*/
630-
function singularize(str, singular) {
631-
return applyRules(str, singularRules, uncountableWords, singular);
623+
function singularize(str) {
624+
return applyRules(str, singularRules, uncountableWords);
632625
}
633626
/**
634627
* This function will pluralize or singularlize a String appropriately based on a number value
@@ -649,16 +642,15 @@ function singularize(str, singular) {
649642
* inflection.inflect( 'person', 2 ); // === 'people'
650643
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
651644
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
652-
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
653645
*/
654-
function inflect(str, count, singular, plural) {
646+
function inflect(str, count) {
655647
if (isNaN(count))
656648
return str;
657649
if (count === 1) {
658-
return applyRules(str, singularRules, uncountableWords, singular);
650+
return applyRules(str, singularRules, uncountableWords);
659651
}
660652
else {
661-
return applyRules(str, pluralRules, uncountableWords, plural);
653+
return applyRules(str, pluralRules, uncountableWords);
662654
}
663655
}
664656
/**

src/inflection.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -600,27 +600,18 @@ const underbarPrefix = new RegExp('^_');
600600
*
601601
* applyRules( 'cows', singular_rules ); // === 'cow'
602602
*/
603-
function applyRules(
604-
str: string,
605-
rules: [RegExp, string?][],
606-
skip: string[],
607-
override?: string
608-
) {
609-
if (override) {
610-
return override;
611-
} else {
612-
if (skip.includes(str.toLocaleLowerCase())) {
613-
return str;
614-
}
615-
616-
for (const rule of rules) {
617-
if (str.match(rule[0])) {
618-
if (rule[1] !== undefined) {
619-
return str.replace(rule[0], rule[1]);
620-
}
603+
function applyRules(str: string, rules: [RegExp, string?][], skip: string[]) {
604+
if (skip.includes(str.toLocaleLowerCase())) {
605+
return str;
606+
}
621607

622-
return str;
608+
for (const rule of rules) {
609+
if (str.match(rule[0])) {
610+
if (rule[1] !== undefined) {
611+
return str.replace(rule[0], rule[1]);
623612
}
613+
614+
return str;
624615
}
625616
}
626617

@@ -639,10 +630,9 @@ function applyRules(
639630
* inflection.pluralize( 'person' ); // === 'people'
640631
* inflection.pluralize( 'octopus' ); // === 'octopuses'
641632
* inflection.pluralize( 'Hat' ); // === 'Hats'
642-
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
643633
*/
644-
export function pluralize(str: string, plural?: string) {
645-
return applyRules(str, pluralRules, uncountableWords, plural);
634+
export function pluralize(str: string) {
635+
return applyRules(str, pluralRules, uncountableWords);
646636
}
647637

648638
/**
@@ -657,10 +647,9 @@ export function pluralize(str: string, plural?: string) {
657647
* inflection.singularize( 'people' ); // === 'person'
658648
* inflection.singularize( 'octopuses' ); // === 'octopus'
659649
* inflection.singularize( 'Hats' ); // === 'Hat'
660-
* inflection.singularize( 'guys', 'person' ); // === 'person'
661650
*/
662-
export function singularize(str: string, singular?: string) {
663-
return applyRules(str, singularRules, uncountableWords, singular);
651+
export function singularize(str: string) {
652+
return applyRules(str, singularRules, uncountableWords);
664653
}
665654

666655
/**
@@ -682,20 +671,14 @@ export function singularize(str: string, singular?: string) {
682671
* inflection.inflect( 'person', 2 ); // === 'people'
683672
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
684673
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
685-
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
686674
*/
687-
export function inflect(
688-
str: string,
689-
count: number,
690-
singular?: string,
691-
plural?: string
692-
) {
675+
export function inflect(str: string, count: number) {
693676
if (isNaN(count)) return str;
694677

695678
if (count === 1) {
696-
return applyRules(str, singularRules, uncountableWords, singular);
679+
return applyRules(str, singularRules, uncountableWords);
697680
} else {
698-
return applyRules(str, pluralRules, uncountableWords, plural);
681+
return applyRules(str, pluralRules, uncountableWords);
699682
}
700683
}
701684

0 commit comments

Comments
 (0)