Skip to content

Commit

Permalink
Remove usage of lodash _.uniq.
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 4fe9478 commit c10f1b3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/parse/gast_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ namespace chevrotain.gastBuilder {
orPartRanges = orPartRanges.concat(currOrParts)
})

let uniqueOrPartRanges = _.uniq(orPartRanges, (prodRange:IProdRange) => {
let uniqueOrPartRanges = utils.uniq(orPartRanges, (prodRange:IProdRange) => {
// using "~" as a separator for the identify function as its not a valid char in javascript
return prodRange.type + "~" + prodRange.range.start + "~" + prodRange.range.end + "~" + prodRange.text
})
Expand Down
4 changes: 2 additions & 2 deletions src/parse/grammar/first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ namespace chevrotain.first {
hasInnerProdsRemaining = seq.length > nextSubProdIdx
}

return _.uniq(firstSet)
return utils.uniq(firstSet)
}

export function firstForBranching(prod:gast.AbstractProduction):Function[] {
let allAlternativesFirsts:Function[][] = utils.map(prod.definition, (innerProd) => {
return first(innerProd)
})
return _.uniq(utils.flatten<Function>(allAlternativesFirsts))
return utils.uniq(utils.flatten<Function>(allAlternativesFirsts))
}

export function firstForTerminal(terminal:gast.Terminal):Function[] {
Expand Down
2 changes: 1 addition & 1 deletion src/parse/grammar/lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace chevrotain.lookahead {
export function checkAlternativesAmbiguities(alternativesTokens:Function[][]):IAmbiguityDescriptor[] {

let allTokensFlat = utils.flatten(alternativesTokens)
let uniqueTokensFlat = _.uniq(allTokensFlat)
let uniqueTokensFlat = utils.uniq(allTokensFlat)

let tokensToAltsIndicesItAppearsIn = utils.map(uniqueTokensFlat, (seekToken) => {
let altsCurrTokenAppearsIn = _.pick(alternativesTokens, (altToLookIn) => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,18 @@ namespace chevrotain.utils {
export function compact<T>(arr:T[]):T[] {
return reject(arr, (item) => item === null || item === undefined)
}

export function uniq<T>(arr:T[], identity:(item:T) => any = (item) => item):T[] {
let identities = []
return reduce(arr, (result, currItem) => {
let currIdentity = identity(currItem)
if (contains(identities, currIdentity)) {
return result
}
else {
identities.push(currIdentity)
return result.concat(currItem)
}
}, [])
}
}
6 changes: 6 additions & 0 deletions test/utils/utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ namespace chevrotain.utils.spec {
expect(compact([])).to.deep.equal([])
expect(compact([1, 2, 3])).to.deep.equal([1, 2, 3])
})

it("exports a uniq utility", () => {
expect(uniq([1, 2, 3, 2])).to.contain.members([1, 2, 3])
expect(uniq([2, 2, 4, 2], (item) => { return 666 })).to.have.length(1)
expect(uniq([])).to.deep.equal([])
})
})
}

0 comments on commit c10f1b3

Please sign in to comment.