Skip to content

Commit

Permalink
Remove usage of lodash _.reduce
Browse files Browse the repository at this point in the history
Relates to #68.
  • Loading branch information
Shahar Soel committed Jan 8, 2016
1 parent 8cf4ec6 commit 6c5dd0d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/parse/grammar/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ namespace chevrotain.checks {
topLevelRule.accept(orCollector)
let ors = orCollector.alternations

let errors = _.reduce(ors, (errors, currOr) => {
let errors = utils.reduce(ors, (errors, currOr) => {
let exceptLast = utils.dropRight(currOr.definition)
let currErrors = utils.map(exceptLast, (currAlternative:IProduction, currAltIdx) => {
if (utils.isEmpty(first.first(currAlternative))) {
Expand Down
2 changes: 1 addition & 1 deletion src/parse/parser_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace chevrotain {
}

if (_.isArray(tokensMapOrArr)) {
this.tokensMap = <any>_.reduce(<any>tokensMapOrArr, (acc, tokenClazz:Function) => {
this.tokensMap = <any>utils.reduce(<any>tokensMapOrArr, (acc, tokenClazz:Function) => {
acc[tokenName(tokenClazz)] = tokenClazz
return acc
}, {})
Expand Down
4 changes: 2 additions & 2 deletions src/scan/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace chevrotain {
return /\\n|\\r|\\s/g.test(pattern.source)
})

let emptyGroups = _.reduce(onlyRelevantClasses, (acc, clazz:any) => {
let emptyGroups = utils.reduce(onlyRelevantClasses, (acc, clazz:any) => {
let groupName = clazz.GROUP
if (utils.isString(groupName)) {
acc[groupName] = []
Expand Down Expand Up @@ -178,7 +178,7 @@ namespace chevrotain {

let found = []
let identicalPatterns = utils.map(tokenClasses, (outerClass:any) => {
return _.reduce(tokenClasses, (result, innerClass:any) => {
return utils.reduce(tokenClasses, (result, innerClass:any) => {
if ((outerClass.PATTERN.source === innerClass.PATTERN.source) &&
!utils.contains(found, innerClass) &&
innerClass.PATTERN !== Lexer.NA) {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,14 @@ namespace chevrotain.utils {
}
return undefined
}

export function reduce<T, A>(arrOrObj:Array<T>|Object, iterator:(result:A, item) => A, initial:A):A {
let vals = Array.isArray(arrOrObj) ? arrOrObj : values(arrOrObj)

let accumulator = initial
for (let i = 0; i < vals.length; i++) {
accumulator = iterator.call(null, accumulator, vals[i])
}
return accumulator
}
}
10 changes: 10 additions & 0 deletions test/utils/utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ namespace chevrotain.utils.spec {
let b = {}
expect(find([a, b], (item) => item === b)).to.equal(b)
})

it("exports a reduce utility", () => {
expect(reduce([1, 2, 3], (result, item) => {
return result.concat([item * 2])
}, [])).to.deep.equal([2, 4, 6])

expect(reduce({one: 1, two: 2, three: 3}, (result, item) => {
return result.concat([item * 2])
}, [])).to.deep.equal([2, 4, 6])
})
})
}

0 comments on commit 6c5dd0d

Please sign in to comment.