Skip to content

Commit 166efde

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 ce7bb3f commit 166efde

2 files changed

Lines changed: 17 additions & 39 deletions

File tree

src/inflection.ts

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

619-
return str;
605+
for (const rule of rules) {
606+
if (str.match(rule[0])) {
607+
if (rule[1] !== undefined) {
608+
return str.replace(rule[0], rule[1]);
620609
}
610+
611+
return str;
621612
}
622613
}
623614

@@ -636,10 +627,9 @@ function applyRules(
636627
* inflection.pluralize( 'person' ); // === 'people'
637628
* inflection.pluralize( 'octopus' ); // === 'octopuses'
638629
* inflection.pluralize( 'Hat' ); // === 'Hats'
639-
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
640630
*/
641-
export function pluralize(str: string, plural?: string) {
642-
return applyRules(str, pluralRules, uncountableWords, plural);
631+
export function pluralize(str: string) {
632+
return applyRules(str, pluralRules, uncountableWords);
643633
}
644634

645635
/**
@@ -654,10 +644,9 @@ export function pluralize(str: string, plural?: string) {
654644
* inflection.singularize( 'people' ); // === 'person'
655645
* inflection.singularize( 'octopuses' ); // === 'octopus'
656646
* inflection.singularize( 'Hats' ); // === 'Hat'
657-
* inflection.singularize( 'guys', 'person' ); // === 'person'
658647
*/
659-
export function singularize(str: string, singular?: string) {
660-
return applyRules(str, singularRules, uncountableWords, singular);
648+
export function singularize(str: string) {
649+
return applyRules(str, singularRules, uncountableWords);
661650
}
662651

663652
/**
@@ -679,20 +668,14 @@ export function singularize(str: string, singular?: string) {
679668
* inflection.inflect( 'person', 2 ); // === 'people'
680669
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
681670
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
682-
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
683671
*/
684-
export function inflect(
685-
str: string,
686-
count: number,
687-
singular?: string,
688-
plural?: string
689-
) {
672+
export function inflect(str: string, count: number) {
690673
if (isNaN(count)) return str;
691674

692675
if (count === 1) {
693-
return applyRules(str, singularRules, uncountableWords, singular);
676+
return applyRules(str, singularRules, uncountableWords);
694677
} else {
695-
return applyRules(str, pluralRules, uncountableWords, plural);
678+
return applyRules(str, pluralRules, uncountableWords);
696679
}
697680
}
698681

test/inflection.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ describe("test .pluralize", function () {
3131
expect(inflection.pluralize("meta")).toEqual("meta");
3232
expect(inflection.pluralize("summons")).toEqual("summonses");
3333
expect(inflection.pluralize("whereas")).toEqual("whereases");
34-
expect(inflection.pluralize("person", "guys")).toEqual("guys");
3534
expect(inflection.pluralize("index")).toEqual("indices");
3635
expect(inflection.pluralize("matrix")).toEqual("matrices");
3736
expect(inflection.pluralize("vertex")).toEqual("vertices");
@@ -81,7 +80,6 @@ describe("test .singularize", function () {
8180
expect(inflection.singularize("data")).toEqual("datum");
8281
expect(inflection.singularize("meta")).toEqual("metum");
8382
expect(inflection.singularize("whereases")).toEqual("whereas");
84-
expect(inflection.singularize("guys", "person")).toEqual("person");
8583
expect(inflection.singularize("matrices")).toEqual("matrix");
8684
expect(inflection.singularize("vertices")).toEqual("vertex");
8785
expect(inflection.singularize("canvases")).toEqual("canvas");
@@ -109,7 +107,6 @@ describe("test .inflect", function () {
109107
expect(inflection.inflect("Hat", 0)).toEqual("Hats");
110108
expect(inflection.inflect("data", 0)).toEqual("data");
111109
expect(inflection.inflect("meta", 0)).toEqual("meta");
112-
expect(inflection.inflect("person", 0, "guy", "guys")).toEqual("guys");
113110
expect(inflection.inflect("drive", 0)).toEqual("drives");
114111
// greater than 1 should use plural state
115112
expect(inflection.inflect("people", 2)).toEqual("people");
@@ -119,7 +116,6 @@ describe("test .inflect", function () {
119116
expect(inflection.inflect("Hat", 2)).toEqual("Hats");
120117
expect(inflection.inflect("data", 2)).toEqual("data");
121118
expect(inflection.inflect("meta", 2)).toEqual("meta");
122-
expect(inflection.inflect("person", 2, "guy", "guys")).toEqual("guys");
123119
expect(inflection.inflect("drive", 2)).toEqual("drives");
124120
// 1 should use singular state
125121
expect(inflection.inflect("status", 1)).toEqual("status");
@@ -134,7 +130,6 @@ describe("test .inflect", function () {
134130
expect(inflection.inflect("Hats", 1)).toEqual("Hat");
135131
expect(inflection.inflect("data", 1)).toEqual("datum");
136132
expect(inflection.inflect("meta", 1)).toEqual("metum");
137-
expect(inflection.inflect("guys", 1, "person", "people")).toEqual("person");
138133
// not a number should return original value
139134
expect(inflection.inflect("drive", 1)).toEqual("drive");
140135
expect(inflection.inflect("drives", 1)).toEqual("drive");

0 commit comments

Comments
 (0)