Skip to content

Commit

Permalink
refactor!: remove indexOf method
Browse files Browse the repository at this point in the history
Please use native Array.indexOf method instead
  • Loading branch information
p-kuen committed Nov 18, 2022
1 parent cab569c commit 9ef38a3
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 91 deletions.
15 changes: 0 additions & 15 deletions lib/inflection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
* @fileoverview
* A port of inflection-js to node.js module.
*/
/**
* This lets us detect if an Array contains a given element.
* @param arr The subject array.
* @param item Object to locate in the Array.
* @param fromIndex Starts checking from this position in the Array.(optional)
* @param compareFunc Function used to compare Array item vs passed item.(optional)
* @returns Return index position in the Array of the passed item.
* @example
*
* const inflection = require( 'inflection' );
*
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
*/
export declare function indexOf<T>(arr: T[], item: T, fromIndex?: number, compareFunc?: (el: T, arg1: T) => boolean): number;
/**
* This function adds pluralization support to every String object.
* @param str The subject string.
Expand Down
34 changes: 3 additions & 31 deletions lib/inflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* A port of inflection-js to node.js module.
*/
Object.defineProperty(exports, "__esModule", { value: true });
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;
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;
/**
* @description This is a list of nouns that use the same form for both singular and plural.
* This list should remain entirely in lower case to correctly match Strings.
Expand Down Expand Up @@ -565,7 +565,7 @@ function applyRules(str, rules, skip, override) {
str = override;
}
else {
const ignore = indexOf(skip, str.toLowerCase()) > -1;
const ignore = skip.indexOf(str.toLocaleLowerCase()) > -1;
if (!ignore) {
const j = rules.length;
for (let i = 0; i < j; i++) {
Expand All @@ -581,34 +581,6 @@ function applyRules(str, rules, skip, override) {
}
return str;
}
/**
* This lets us detect if an Array contains a given element.
* @param arr The subject array.
* @param item Object to locate in the Array.
* @param fromIndex Starts checking from this position in the Array.(optional)
* @param compareFunc Function used to compare Array item vs passed item.(optional)
* @returns Return index position in the Array of the passed item.
* @example
*
* const inflection = require( 'inflection' );
*
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
*/
function indexOf(arr, item, fromIndex, compareFunc) {
if (!fromIndex) {
fromIndex = -1;
}
let index = -1;
for (let i = fromIndex; i < arr.length; i++) {
if (arr[i] === item || (compareFunc && compareFunc(arr[i], item))) {
index = i;
break;
}
}
return index;
}
exports.indexOf = indexOf;
/**
* This function adds pluralization support to every String object.
* @param str The subject string.
Expand Down Expand Up @@ -816,7 +788,7 @@ function titleize(str) {
d = strArr[i].split('-');
l = d.length;
for (let k = 0; k < l; k++) {
if (indexOf(nonTitlecasedWords, d[k].toLowerCase()) < 0) {
if (nonTitlecasedWords.indexOf(d[k].toLowerCase()) < 0) {
d[k] = capitalize(d[k]);
}
}
Expand Down
40 changes: 2 additions & 38 deletions src/inflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ function applyRules(
if (override) {
str = override;
} else {
const ignore = indexOf(skip, str.toLowerCase()) > -1;
const ignore = skip.indexOf(str.toLocaleLowerCase()) > -1;

if (!ignore) {
const j = rules.length;
Expand All @@ -626,42 +626,6 @@ function applyRules(
return str;
}

/**
* This lets us detect if an Array contains a given element.
* @param arr The subject array.
* @param item Object to locate in the Array.
* @param fromIndex Starts checking from this position in the Array.(optional)
* @param compareFunc Function used to compare Array item vs passed item.(optional)
* @returns Return index position in the Array of the passed item.
* @example
*
* const inflection = require( 'inflection' );
*
* inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
* inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
*/
export function indexOf<T>(
arr: T[],
item: T,
fromIndex?: number,
compareFunc?: (el: T, arg1: T) => boolean
) {
if (!fromIndex) {
fromIndex = -1;
}

let index = -1;

for (let i = fromIndex; i < arr.length; i++) {
if (arr[i] === item || (compareFunc && compareFunc(arr[i], item))) {
index = i;
break;
}
}

return index;
}

/**
* This function adds pluralization support to every String object.
* @param str The subject string.
Expand Down Expand Up @@ -885,7 +849,7 @@ export function titleize(str: string) {
l = d.length;

for (let k = 0; k < l; k++) {
if (indexOf(nonTitlecasedWords, d[k].toLowerCase()) < 0) {
if (nonTitlecasedWords.indexOf(d[k].toLowerCase()) < 0) {
d[k] = capitalize(d[k]);
}
}
Expand Down
7 changes: 0 additions & 7 deletions test/inflection.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import * as inflection from "../src/inflection";
import { describe, it, expect } from "vitest";

describe("test .indexOf", function () {
it("should return proper index key", function () {
expect(inflection.indexOf(["hi", "there"], "guys")).toEqual(-1);
expect(inflection.indexOf(["hi", "there"], "hi")).toEqual(0);
});
});

describe("test .pluralize", function () {
it("should pluralize the given word", function () {
expect(inflection.pluralize("people")).toEqual("people");
Expand Down

0 comments on commit 9ef38a3

Please sign in to comment.