diff --git a/src/scan/lexer.ts b/src/scan/lexer.ts index 7b2adb70a..5a5e68cc6 100644 --- a/src/scan/lexer.ts +++ b/src/scan/lexer.ts @@ -51,7 +51,7 @@ namespace chevrotain { let longerAltClass = clazz.LONGER_ALT if (longerAltClass) { - let longerAltIdx = _.indexOf(onlyRelevantClasses, longerAltClass) + let longerAltIdx = utils.indexOf(onlyRelevantClasses, longerAltClass) return longerAltIdx } }) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 493ed81fc..acd2c6dd6 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -233,4 +233,13 @@ namespace chevrotain.utils { } return false } + + export function indexOf(arr:T[], value:T):number { + for (let i = 0; i < arr.length; i++) { + if (arr[i] === value) { + return i + } + } + return -1 + } } diff --git a/test/utils/utils_spec.ts b/test/utils/utils_spec.ts index 2ab1fa198..2fdff7d37 100644 --- a/test/utils/utils_spec.ts +++ b/test/utils/utils_spec.ts @@ -155,6 +155,12 @@ namespace chevrotain.utils.spec { expect(some([1, 2, 3], (item) => {return item % 2 === 0})).to.be.true expect(some([1, 3, 5], (item) => {return item % 2 === 0})).to.be.false }) + + it("exports an indexOf utility", () => { + expect(indexOf([1, 2, 3], 2)).to.equal(1) + expect(indexOf([1, 2, 3], 3)).to.equal(2) + expect(indexOf([1, 2, 3], 0)).to.equal(-1) + expect(indexOf([], -2)).to.equal(-1) + }) }) } -