Skip to content

Commit 740b747

Browse files
committed
refactor: use more modern function to implement applyRules method
1 parent e8f60d0 commit 740b747

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

lib/inflection.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -562,20 +562,18 @@ const underbarPrefix = new RegExp('^_');
562562
*/
563563
function applyRules(str, rules, skip, override) {
564564
if (override) {
565-
str = override;
565+
return override;
566566
}
567567
else {
568-
const ignore = skip.indexOf(str.toLocaleLowerCase()) > -1;
569-
if (!ignore) {
570-
const j = rules.length;
571-
for (let i = 0; i < j; i++) {
572-
if (str.match(rules[i][0])) {
573-
const replaceRule = rules[i][1];
574-
if (replaceRule !== undefined) {
575-
str = str.replace(rules[i][0], replaceRule);
576-
}
577-
break;
568+
if (skip.includes(str.toLocaleLowerCase())) {
569+
return str;
570+
}
571+
for (const rule of rules) {
572+
if (str.match(rule[0])) {
573+
if (rule[1] !== undefined) {
574+
return str.replace(rule[0], rule[1]);
578575
}
576+
return str;
579577
}
580578
}
581579
}

src/inflection.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -604,21 +604,19 @@ function applyRules(
604604
override?: string
605605
) {
606606
if (override) {
607-
str = override;
607+
return override;
608608
} else {
609-
const ignore = skip.indexOf(str.toLocaleLowerCase()) > -1;
610-
611-
if (!ignore) {
612-
const j = rules.length;
613-
614-
for (let i = 0; i < j; i++) {
615-
if (str.match(rules[i][0])) {
616-
const replaceRule = rules[i][1];
617-
if (replaceRule !== undefined) {
618-
str = str.replace(rules[i][0], replaceRule);
619-
}
620-
break;
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]);
621617
}
618+
619+
return str;
622620
}
623621
}
624622
}

0 commit comments

Comments
 (0)