From a99ffcc41891d17eecd230c6915839b013e9b0ad Mon Sep 17 00:00:00 2001 From: Shahar Soel Date: Sun, 10 Jan 2016 00:43:19 +0200 Subject: [PATCH] Remove usage of lodash _.every _.isObject _.isRegExp _.isArray. Relates to #68. --- src/parse/gast_builder.ts | 2 +- src/parse/grammar/gast.ts | 2 +- src/parse/parser_public.ts | 4 ++-- src/scan/lexer.ts | 2 +- src/scan/tokens_public.ts | 2 +- src/utils/utils.ts | 23 ++++++++++++++++++++++- test/utils/utils_spec.ts | 8 ++++++++ 7 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/parse/gast_builder.ts b/src/parse/gast_builder.ts index fd5abbd78..240188332 100644 --- a/src/parse/gast_builder.ts +++ b/src/parse/gast_builder.ts @@ -203,7 +203,7 @@ namespace chevrotain.gastBuilder { export function getDirectlyContainedRanges(y:r.IRange, prodRanges:IProdRange[]):IProdRange[] { return utils.filter(prodRanges, (x:IProdRange) => { let isXDescendantOfY = y.strictlyContainsRange(x.range) - let xDoesNotHaveAnyAncestorWhichIsDecendantOfY = _.every(prodRanges, (maybeAnotherParent:IProdRange) => { + let xDoesNotHaveAnyAncestorWhichIsDecendantOfY = utils.every(prodRanges, (maybeAnotherParent:IProdRange) => { let isParentOfX = maybeAnotherParent.range.strictlyContainsRange(x.range) let isChildOfY = maybeAnotherParent.range.isStrictlyContainedInRange(y) return !(isParentOfX && isChildOfY) diff --git a/src/parse/grammar/gast.ts b/src/parse/grammar/gast.ts index c0ada6cd2..36d5c90cd 100644 --- a/src/parse/grammar/gast.ts +++ b/src/parse/grammar/gast.ts @@ -38,7 +38,7 @@ namespace chevrotain.gast { if (prod instanceof NonTerminal) { alreadyVisited.push(prod) } - return _.every((prod).definition, (subProd:IProduction) => { + return utils.every((prod).definition, (subProd:IProduction) => { return isOptionalProd(subProd, alreadyVisited) }) } diff --git a/src/parse/parser_public.ts b/src/parse/parser_public.ts index 84e025d63..a02e52f4a 100644 --- a/src/parse/parser_public.ts +++ b/src/parse/parser_public.ts @@ -227,13 +227,13 @@ namespace chevrotain { this.definitionErrors = cache.CLASS_TO_DEFINITION_ERRORS.get(this.className) } - if (_.isArray(tokensMapOrArr)) { + if (utils.isArray(tokensMapOrArr)) { this.tokensMap = utils.reduce(tokensMapOrArr, (acc, tokenClazz:Function) => { acc[tokenName(tokenClazz)] = tokenClazz return acc }, {}) } - else if (_.isObject(tokensMapOrArr)) { + else if (utils.isObject(tokensMapOrArr)) { this.tokensMap = utils.cloneObj(tokensMapOrArr) } else { diff --git a/src/scan/lexer.ts b/src/scan/lexer.ts index cd5728c3b..09582f688 100644 --- a/src/scan/lexer.ts +++ b/src/scan/lexer.ts @@ -121,7 +121,7 @@ namespace chevrotain { export function findInvalidPatterns(tokenClasses:TokenConstructor[]) { let tokenClassesWithInvalidPattern = utils.filter(tokenClasses, (currClass) => { let pattern = currClass[PATTERN] - return !_.isRegExp(pattern) + return !utils.isRegExp(pattern) }) let errors = utils.map(tokenClassesWithInvalidPattern, (currClass) => { diff --git a/src/scan/tokens_public.ts b/src/scan/tokens_public.ts index 572cee921..f1332fc85 100644 --- a/src/scan/tokens_public.ts +++ b/src/scan/tokens_public.ts @@ -28,7 +28,7 @@ namespace chevrotain { export function extendToken(tokenName:string, patternOrParent:any = undefined, parentConstructor:Function = Token) { let pattern - if (_.isRegExp(patternOrParent) || + if (utils.isRegExp(patternOrParent) || patternOrParent === chevrotain.Lexer.SKIPPED || patternOrParent === chevrotain.Lexer.NA) { pattern = patternOrParent diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 26b7e9e22..9327cde55 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -196,7 +196,28 @@ namespace chevrotain.utils { export function partial(func:Function, ...restArgs:any[]):Function { let firstArg = [null] - let allArgs = firstArg.concat(restArgs) + let allArgs = firstArg.concat(restArgs) return Function.bind.apply(func, allArgs) } + + export function isArray(obj:any):boolean { + return Array.isArray(obj) + } + + export function isRegExp(obj:any):boolean { + return obj instanceof RegExp + } + + export function isObject(obj:any):boolean { + return obj instanceof Object + } + + export function every(arr:T[], predicate:(item:T) => boolean):boolean { + for (let i = 0; i < arr.length; i++) { + if (!predicate(arr[i])) { + return false + } + } + return true + } } diff --git a/test/utils/utils_spec.ts b/test/utils/utils_spec.ts index bc69cfb92..3ad5e7675 100644 --- a/test/utils/utils_spec.ts +++ b/test/utils/utils_spec.ts @@ -133,6 +133,14 @@ namespace chevrotain.utils.spec { expect(partial(add, 2)(3)).to.equal(5) expect(partial(add, 2, 3)()).to.equal(5) }) + + it("exports an every utility", () => { + expect(every([], (item) => {return true})).to.be.true + // empty set always true... + expect(every([], (item) => {return false})).to.be.true + expect(every([1, 2, 3], (item) => {return item % 2 === 0})).to.be.false + expect(every([2, 4, 6], (item) => {return item % 2 === 0})).to.be.true + }) }) }