Skip to content

Commit 36a3ff4

Browse files
committed
Revert "refactor!: simplify code by removing override parameters"
This reverts commit 166efde. fixes #106
1 parent 04f960e commit 36a3ff4

File tree

3 files changed

+66
-35
lines changed

3 files changed

+66
-35
lines changed

lib/inflection.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
* inflection.pluralize( 'person' ); // === 'people'
1919
* inflection.pluralize( 'octopus' ); // === 'octopuses'
2020
* inflection.pluralize( 'Hat' ); // === 'Hats'
21+
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
2122
*/
22-
export declare function pluralize(str: string): string;
23+
export declare function pluralize(str: string, plural?: string): string;
2324
/**
2425
* This function adds singularization support to every String object.
2526
* @param str The subject string.
@@ -32,8 +33,9 @@ export declare function pluralize(str: string): string;
3233
* inflection.singularize( 'people' ); // === 'person'
3334
* inflection.singularize( 'octopuses' ); // === 'octopus'
3435
* inflection.singularize( 'Hats' ); // === 'Hat'
36+
* inflection.singularize( 'guys', 'person' ); // === 'person'
3537
*/
36-
export declare function singularize(str: string): string;
38+
export declare function singularize(str: string, singular?: string): string;
3739
/**
3840
* This function will pluralize or singularlize a String appropriately based on a number value
3941
* @param str The subject string.
@@ -53,8 +55,9 @@ export declare function singularize(str: string): string;
5355
* inflection.inflect( 'person', 2 ); // === 'people'
5456
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
5557
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
58+
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
5659
*/
57-
export declare function inflect(str: string, count: number): string;
60+
export declare function inflect(str: string, count: number, singular?: string, plural?: string): string;
5861
/**
5962
* This function adds camelization support to every String object.
6063
* @param str The subject string.

lib/inflection.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ const regex = {
377377
tium: new RegExp('([ti])um$', 'gi'),
378378
sis: new RegExp('sis$', 'gi'),
379379
ffe: new RegExp('(?:([^f])fe|([lr])f)$', 'gi'),
380+
focus: new RegExp('^(focus)$', 'gi'),
380381
hive: new RegExp('(hi|ti)ve$', 'gi'),
381382
aeiouyy: new RegExp('([^aeiouy]|qu)y$', 'gi'),
382383
x: new RegExp('(x|ch|ss|sh)$', 'gi'),
@@ -443,6 +444,7 @@ const pluralRules = [
443444
[regex.singular.tium, '$1a'],
444445
[regex.singular.sis, 'ses'],
445446
[regex.singular.ffe, '$1$2ves'],
447+
[regex.singular.focus, '$1es'],
446448
[regex.singular.hive, '$1ves'],
447449
[regex.singular.aeiouyy, '$1ies'],
448450
[regex.singular.matrix, '$1ices'],
@@ -478,6 +480,7 @@ const singularRules = [
478480
[regex.singular.tium],
479481
[regex.singular.sis],
480482
[regex.singular.ffe],
483+
[regex.singular.focus],
481484
[regex.singular.hive],
482485
[regex.singular.aeiouyy],
483486
[regex.singular.x],
@@ -574,17 +577,22 @@ const underbarPrefix = new RegExp('^_');
574577
*
575578
* applyRules( 'cows', singular_rules ); // === 'cow'
576579
*/
577-
function applyRules(str, rules, skip) {
578-
if (skip.includes(str.toLocaleLowerCase())) {
579-
return str;
580+
function applyRules(str, rules, skip, override) {
581+
if (override) {
582+
return override;
580583
}
581-
for (const rule of rules) {
582-
if (str.match(rule[0])) {
583-
if (rule[1] !== undefined) {
584-
return str.replace(rule[0], rule[1]);
585-
}
584+
else {
585+
if (skip.includes(str.toLocaleLowerCase())) {
586586
return str;
587587
}
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;
594+
}
595+
}
588596
}
589597
return str;
590598
}
@@ -600,9 +608,10 @@ function applyRules(str, rules, skip) {
600608
* inflection.pluralize( 'person' ); // === 'people'
601609
* inflection.pluralize( 'octopus' ); // === 'octopuses'
602610
* inflection.pluralize( 'Hat' ); // === 'Hats'
611+
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
603612
*/
604-
function pluralize(str) {
605-
return applyRules(str, pluralRules, uncountableWords);
613+
function pluralize(str, plural) {
614+
return applyRules(str, pluralRules, uncountableWords, plural);
606615
}
607616
/**
608617
* This function adds singularization support to every String object.
@@ -616,9 +625,10 @@ function pluralize(str) {
616625
* inflection.singularize( 'people' ); // === 'person'
617626
* inflection.singularize( 'octopuses' ); // === 'octopus'
618627
* inflection.singularize( 'Hats' ); // === 'Hat'
628+
* inflection.singularize( 'guys', 'person' ); // === 'person'
619629
*/
620-
function singularize(str) {
621-
return applyRules(str, singularRules, uncountableWords);
630+
function singularize(str, singular) {
631+
return applyRules(str, singularRules, uncountableWords, singular);
622632
}
623633
/**
624634
* This function will pluralize or singularlize a String appropriately based on a number value
@@ -639,15 +649,16 @@ function singularize(str) {
639649
* inflection.inflect( 'person', 2 ); // === 'people'
640650
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
641651
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
652+
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
642653
*/
643-
function inflect(str, count) {
654+
function inflect(str, count, singular, plural) {
644655
if (isNaN(count))
645656
return str;
646657
if (count === 1) {
647-
return applyRules(str, singularRules, uncountableWords);
658+
return applyRules(str, singularRules, uncountableWords, singular);
648659
}
649660
else {
650-
return applyRules(str, pluralRules, uncountableWords);
661+
return applyRules(str, pluralRules, uncountableWords, plural);
651662
}
652663
}
653664
/**

src/inflection.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -600,18 +600,27 @@ const underbarPrefix = new RegExp('^_');
600600
*
601601
* applyRules( 'cows', singular_rules ); // === 'cow'
602602
*/
603-
function applyRules(str: string, rules: [RegExp, string?][], skip: string[]) {
604-
if (skip.includes(str.toLocaleLowerCase())) {
605-
return str;
606-
}
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+
}
607615

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]);
612-
}
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+
}
613621

614-
return str;
622+
return str;
623+
}
615624
}
616625
}
617626

@@ -630,9 +639,10 @@ function applyRules(str: string, rules: [RegExp, string?][], skip: string[]) {
630639
* inflection.pluralize( 'person' ); // === 'people'
631640
* inflection.pluralize( 'octopus' ); // === 'octopuses'
632641
* inflection.pluralize( 'Hat' ); // === 'Hats'
642+
* inflection.pluralize( 'person', 'guys' ); // === 'guys'
633643
*/
634-
export function pluralize(str: string) {
635-
return applyRules(str, pluralRules, uncountableWords);
644+
export function pluralize(str: string, plural?: string) {
645+
return applyRules(str, pluralRules, uncountableWords, plural);
636646
}
637647

638648
/**
@@ -647,9 +657,10 @@ export function pluralize(str: string) {
647657
* inflection.singularize( 'people' ); // === 'person'
648658
* inflection.singularize( 'octopuses' ); // === 'octopus'
649659
* inflection.singularize( 'Hats' ); // === 'Hat'
660+
* inflection.singularize( 'guys', 'person' ); // === 'person'
650661
*/
651-
export function singularize(str: string) {
652-
return applyRules(str, singularRules, uncountableWords);
662+
export function singularize(str: string, singular?: string) {
663+
return applyRules(str, singularRules, uncountableWords, singular);
653664
}
654665

655666
/**
@@ -671,14 +682,20 @@ export function singularize(str: string) {
671682
* inflection.inflect( 'person', 2 ); // === 'people'
672683
* inflection.inflect( 'octopus', 2 ); // === 'octopuses'
673684
* inflection.inflect( 'Hat', 2 ); // === 'Hats'
685+
* inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
674686
*/
675-
export function inflect(str: string, count: number) {
687+
export function inflect(
688+
str: string,
689+
count: number,
690+
singular?: string,
691+
plural?: string
692+
) {
676693
if (isNaN(count)) return str;
677694

678695
if (count === 1) {
679-
return applyRules(str, singularRules, uncountableWords);
696+
return applyRules(str, singularRules, uncountableWords, singular);
680697
} else {
681-
return applyRules(str, pluralRules, uncountableWords);
698+
return applyRules(str, pluralRules, uncountableWords, plural);
682699
}
683700
}
684701

0 commit comments

Comments
 (0)