Skip to content

Commit

Permalink
Remove usage of lodash _.assing and _.forOwn.
Browse files Browse the repository at this point in the history
Relates to #68.
  • Loading branch information
Shahar Soel committed Jan 10, 2016
1 parent 4e05a8c commit bacefeb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lang/lang_extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace chevrotain.lang {
}

putAll(other:HashTable<V>):void {
this._state = _.assign(this._state, other._state)
this._state = utils.assign(this._state, other._state)
}

get(key:string):V {
Expand Down
4 changes: 1 addition & 3 deletions src/scan/tokens_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ namespace chevrotain {
}

// static properties mixing
_.forOwn(parentConstructor, (v, k) => {
derivedCostructor[k] = v
})
derivedCostructor = utils.assign(derivedCostructor, parentConstructor)

// the tokenName property will be used by the Parser for Error Messages if the Token's constructor is anonymous
derivedCostructor.tokenName = tokenName
Expand Down
15 changes: 15 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,19 @@ namespace chevrotain.utils {
}
return result
}

/**
* mutates! (and returns) target
*/
export function assign(target:Object, ...sources:Object[]):Object {
for (let i = 0; i < sources.length; i++) {
let curSource = sources[i]
let currSourceKeys = keys(curSource)
for (let j = 0; j < currSourceKeys.length; j++) {
let currKey = currSourceKeys[j]
target[currKey] = curSource[currKey]
}
}
return target
}
}
6 changes: 6 additions & 0 deletions test/utils/utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,11 @@ namespace chevrotain.utils.spec {
expect(() => zipObject(["ima", "aba"], [1, 2, 3])).to.throw("can't zipObject")
expect(zipObject([], [])).to.deep.equal({})
})

it("exports an assign utility", () => {
expect(assign(["ima", "aba", "bamba"], [1, 2, 3])).to.deep.equal([1, 2, 3])
expect(assign({}, {"ima": 666}, {"aba": 333})).to.deep.equal({"ima": 666, "aba": 333})
expect(assign({}, {"ima": 666}, {"aba": 333}, {"ima": 999})).to.deep.equal({"ima": 999, "aba": 333})
})
})
}

0 comments on commit bacefeb

Please sign in to comment.