Skip to content

Commit

Permalink
Remove usage of lodash _.every _.isObject _.isRegExp _.isArray.
Browse files Browse the repository at this point in the history
Relates to #68.
  • Loading branch information
Shahar Soel committed Jan 9, 2016
1 parent ee5e52e commit a99ffcc
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/parse/gast_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/parse/grammar/gast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace chevrotain.gast {
if (prod instanceof NonTerminal) {
alreadyVisited.push(prod)
}
return _.every((<AbstractProduction>prod).definition, (subProd:IProduction) => {
return utils.every((<AbstractProduction>prod).definition, (subProd:IProduction) => {
return isOptionalProd(subProd, alreadyVisited)
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/parse/parser_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <any>utils.reduce(<any>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 {
Expand Down
2 changes: 1 addition & 1 deletion src/scan/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/scan/tokens_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(arr:T[], predicate:(item:T) => boolean):boolean {
for (let i = 0; i < arr.length; i++) {
if (!predicate(arr[i])) {
return false
}
}
return true
}
}
8 changes: 8 additions & 0 deletions test/utils/utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
}

0 comments on commit a99ffcc

Please sign in to comment.