Skip to content

Commit 9ef38a3

Browse files
committed
refactor!: remove indexOf method
Please use native Array.indexOf method instead
1 parent cab569c commit 9ef38a3

4 files changed

Lines changed: 5 additions & 91 deletions

File tree

lib/inflection.d.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66
* @fileoverview
77
* A port of inflection-js to node.js module.
88
*/
9-
/**
10-
* This lets us detect if an Array contains a given element.
11-
* @param arr The subject array.
12-
* @param item Object to locate in the Array.
13-
* @param fromIndex Starts checking from this position in the Array.(optional)
14-
* @param compareFunc Function used to compare Array item vs passed item.(optional)
15-
* @returns Return index position in the Array of the passed item.
16-
* @example
17-
*
18-
* const inflection = require( 'inflection' );
19-
*
20-
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
21-
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
22-
*/
23-
export declare function indexOf<T>(arr: T[], item: T, fromIndex?: number, compareFunc?: (el: T, arg1: T) => boolean): number;
249
/**
2510
* This function adds pluralization support to every String object.
2611
* @param str The subject string.

lib/inflection.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* A port of inflection-js to node.js module.
99
*/
1010
Object.defineProperty(exports, "__esModule", { value: true });
11-
exports.transform = exports.ordinalize = exports.foreignKey = exports.classify = exports.tableize = exports.demodulize = exports.titleize = exports.dasherize = exports.capitalize = exports.humanize = exports.underscore = exports.camelize = exports.inflect = exports.singularize = exports.pluralize = exports.indexOf = void 0;
11+
exports.transform = exports.ordinalize = exports.foreignKey = exports.classify = exports.tableize = exports.demodulize = exports.titleize = exports.dasherize = exports.capitalize = exports.humanize = exports.underscore = exports.camelize = exports.inflect = exports.singularize = exports.pluralize = void 0;
1212
/**
1313
* @description This is a list of nouns that use the same form for both singular and plural.
1414
* This list should remain entirely in lower case to correctly match Strings.
@@ -565,7 +565,7 @@ function applyRules(str, rules, skip, override) {
565565
str = override;
566566
}
567567
else {
568-
const ignore = indexOf(skip, str.toLowerCase()) > -1;
568+
const ignore = skip.indexOf(str.toLocaleLowerCase()) > -1;
569569
if (!ignore) {
570570
const j = rules.length;
571571
for (let i = 0; i < j; i++) {
@@ -581,34 +581,6 @@ function applyRules(str, rules, skip, override) {
581581
}
582582
return str;
583583
}
584-
/**
585-
* This lets us detect if an Array contains a given element.
586-
* @param arr The subject array.
587-
* @param item Object to locate in the Array.
588-
* @param fromIndex Starts checking from this position in the Array.(optional)
589-
* @param compareFunc Function used to compare Array item vs passed item.(optional)
590-
* @returns Return index position in the Array of the passed item.
591-
* @example
592-
*
593-
* const inflection = require( 'inflection' );
594-
*
595-
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
596-
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
597-
*/
598-
function indexOf(arr, item, fromIndex, compareFunc) {
599-
if (!fromIndex) {
600-
fromIndex = -1;
601-
}
602-
let index = -1;
603-
for (let i = fromIndex; i < arr.length; i++) {
604-
if (arr[i] === item || (compareFunc && compareFunc(arr[i], item))) {
605-
index = i;
606-
break;
607-
}
608-
}
609-
return index;
610-
}
611-
exports.indexOf = indexOf;
612584
/**
613585
* This function adds pluralization support to every String object.
614586
* @param str The subject string.
@@ -816,7 +788,7 @@ function titleize(str) {
816788
d = strArr[i].split('-');
817789
l = d.length;
818790
for (let k = 0; k < l; k++) {
819-
if (indexOf(nonTitlecasedWords, d[k].toLowerCase()) < 0) {
791+
if (nonTitlecasedWords.indexOf(d[k].toLowerCase()) < 0) {
820792
d[k] = capitalize(d[k]);
821793
}
822794
}

src/inflection.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ function applyRules(
606606
if (override) {
607607
str = override;
608608
} else {
609-
const ignore = indexOf(skip, str.toLowerCase()) > -1;
609+
const ignore = skip.indexOf(str.toLocaleLowerCase()) > -1;
610610

611611
if (!ignore) {
612612
const j = rules.length;
@@ -626,42 +626,6 @@ function applyRules(
626626
return str;
627627
}
628628

629-
/**
630-
* This lets us detect if an Array contains a given element.
631-
* @param arr The subject array.
632-
* @param item Object to locate in the Array.
633-
* @param fromIndex Starts checking from this position in the Array.(optional)
634-
* @param compareFunc Function used to compare Array item vs passed item.(optional)
635-
* @returns Return index position in the Array of the passed item.
636-
* @example
637-
*
638-
* const inflection = require( 'inflection' );
639-
*
640-
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
641-
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
642-
*/
643-
export function indexOf<T>(
644-
arr: T[],
645-
item: T,
646-
fromIndex?: number,
647-
compareFunc?: (el: T, arg1: T) => boolean
648-
) {
649-
if (!fromIndex) {
650-
fromIndex = -1;
651-
}
652-
653-
let index = -1;
654-
655-
for (let i = fromIndex; i < arr.length; i++) {
656-
if (arr[i] === item || (compareFunc && compareFunc(arr[i], item))) {
657-
index = i;
658-
break;
659-
}
660-
}
661-
662-
return index;
663-
}
664-
665629
/**
666630
* This function adds pluralization support to every String object.
667631
* @param str The subject string.
@@ -885,7 +849,7 @@ export function titleize(str: string) {
885849
l = d.length;
886850

887851
for (let k = 0; k < l; k++) {
888-
if (indexOf(nonTitlecasedWords, d[k].toLowerCase()) < 0) {
852+
if (nonTitlecasedWords.indexOf(d[k].toLowerCase()) < 0) {
889853
d[k] = capitalize(d[k]);
890854
}
891855
}

test/inflection.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import * as inflection from "../src/inflection";
22
import { describe, it, expect } from "vitest";
33

4-
describe("test .indexOf", function () {
5-
it("should return proper index key", function () {
6-
expect(inflection.indexOf(["hi", "there"], "guys")).toEqual(-1);
7-
expect(inflection.indexOf(["hi", "there"], "hi")).toEqual(0);
8-
});
9-
});
10-
114
describe("test .pluralize", function () {
125
it("should pluralize the given word", function () {
136
expect(inflection.pluralize("people")).toEqual("people");

0 commit comments

Comments
 (0)