Skip to content

Commit

Permalink
remove more lodash functions.
Browse files Browse the repository at this point in the history
relates to #68
  • Loading branch information
Shahar Soel committed Jan 5, 2016
1 parent 2124402 commit 274a739
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/parse/grammar/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace chevrotain.checks {
let isFirstOptional = gast.isOptionalProd(firstProd)
let hasMore = definition.length > 1
if (isFirstOptional && hasMore) {
let rest = _.drop(definition)
let rest = utils.drop(definition)
return result.concat(getFirstNoneTerminal(rest))
}
else {
Expand All @@ -244,7 +244,7 @@ namespace chevrotain.checks {
let ors = orCollector.alternations

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

walk(prod:g.AbstractProduction, prevRest:any[] = []):void {
utils.forEach(prod.definition, (subProd:gast.IProduction, index) => {
let currRest = _.drop(prod.definition, index + 1)
let currRest = utils.drop(prod.definition, index + 1)

if (subProd instanceof g.NonTerminal) {
this.walkProdRef(subProd, currRest, prevRest)
Expand Down
4 changes: 2 additions & 2 deletions src/scan/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace chevrotain {
else if (utils.isString(groupName)) {
return groupName
}
else if (_.isUndefined(groupName)) {
else if (utils.isUndefined(groupName)) {
return "default"
}
else {
Expand Down Expand Up @@ -193,7 +193,7 @@ namespace chevrotain {
identicalPatterns = _.compact(identicalPatterns)

let duplicatePatterns = _.filter(identicalPatterns, (currIdenticalSet) => {
return _.size(currIdenticalSet) > 1
return currIdenticalSet.length > 1
})

let errors = utils.map(duplicatePatterns, (setOfIdentical:any) => {
Expand Down
4 changes: 2 additions & 2 deletions src/scan/tokens_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace chevrotain {
patternOrParent === chevrotain.Lexer.NA) {
pattern = patternOrParent
}
else if (_.isFunction(patternOrParent)) {
else if (utils.isFunction(patternOrParent)) {
parentConstructor = patternOrParent
pattern = undefined
}
Expand All @@ -51,7 +51,7 @@ namespace chevrotain {
derivedCostructor.tokenName = tokenName
derivedCostructor.prototype = Object.create(parentConstructor.prototype)
derivedCostructor.prototype.constructor = derivedCostructor
if (!_.isUndefined(pattern)) {
if (!utils.isUndefined(pattern)) {
derivedCostructor.PATTERN = pattern
}

Expand Down
16 changes: 16 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,20 @@ namespace chevrotain.utils {
export function isString(item:any):boolean {
return typeof item === "string"
}

export function isUndefined(item:any):boolean {
return item === undefined
}

export function isFunction(item:any):boolean {
return item instanceof Function
}

export function drop<T>(arr:T[], howMuch:number = 1):T[] {
return arr.slice(howMuch, arr.length)
}

export function dropRight<T>(arr:T[], howMuch:number = 1):T[] {
return arr.slice(0, arr.length - howMuch)
}
}
14 changes: 14 additions & 0 deletions test/utils/utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,19 @@ namespace chevrotain.utils.spec {
expect(isString(null)).to.be.false
})

it("exports a drop utility", () => {
expect(drop([])).to.deep.equal([])
expect(drop([1, 2, 3])).to.deep.equal([2, 3])
expect(drop([1, 2, 3], 2)).to.deep.equal([3])
expect(drop([1, 2, 3], 3)).to.deep.equal([])
})

it("exports a dropRight utility", () => {
expect(dropRight([])).to.deep.equal([])
expect(dropRight([1, 2, 3])).to.deep.equal([1, 2])
expect(dropRight([1, 2, 3], 2)).to.deep.equal([1])
expect(dropRight([1, 2, 3], 3)).to.deep.equal([])
})

})
}

0 comments on commit 274a739

Please sign in to comment.