-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1996387
commit f02c24d
Showing
3 changed files
with
123 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import 'mocha'; | ||
import { expect } from 'chai'; | ||
|
||
import { getPath, getPaths, setPath } from '../../utils'; | ||
|
||
describe('getPath', () => { | ||
it('works with an absent class member', () => { | ||
class Foo {} | ||
|
||
let foo = new Foo(); | ||
|
||
expect(getPath(foo, 'bar')).to.equal(undefined); | ||
}); | ||
|
||
it('works with a plain class member', () => { | ||
class Foo { | ||
foo = 123; | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
expect(getPath(foo, 'foo')).to.equal(123); | ||
}); | ||
|
||
it('works with an object as a class member', () => { | ||
class Foo { | ||
bar = { | ||
bao: 'bab', | ||
}; | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
expect(getPath(foo, 'bar')).to.equal(foo.bar); | ||
expect(getPath(foo, 'bar.bao')).to.equal(foo.bar.bao); | ||
}); | ||
}); | ||
|
||
describe('getPaths', () => { | ||
it('works', () => { | ||
class Foo { | ||
bar = 1; | ||
bab = '2'; | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
expect(getPaths(foo, ['bar', 'bab', 'baz'])).to.deep.equal([1, '2', undefined]); | ||
}); | ||
}); | ||
|
||
describe('setPath', () => { | ||
it('works with a plain object', () => { | ||
class Foo { | ||
bar: any; | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
setPath(foo, 'bar', 'baz'); | ||
|
||
expect(foo.bar).to.equal('baz'); | ||
}); | ||
|
||
it('works with an object with setter', () => { | ||
class Foo { | ||
set = function() { | ||
expect(true); | ||
}; | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
setPath(foo, 'bar', 'baz'); | ||
}); | ||
|
||
it('works with nested paths', () => { | ||
class Foo { | ||
bar = { | ||
baz: '', | ||
}; | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
setPath(foo, 'bar.baz', 'bao'); | ||
|
||
expect(foo.bar.baz).to.equal('bao'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export function getPath(obj: any, path: string) { | ||
let segments = path.split('.'); | ||
let current = obj; | ||
|
||
for (let segment of segments) { | ||
if (current === undefined || current === null) { | ||
break; | ||
} | ||
|
||
current = typeof current.get === 'function' ? current.get(segment) : current[segment]; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
export function getPaths(obj: any, paths: string[]) { | ||
return paths.map(p => getPath(obj, p)); | ||
} | ||
|
||
export function setPath(obj: any, path: string, value: any) { | ||
let objPath = path.substr(0, path.lastIndexOf('.')); | ||
let key = path.substr(path.lastIndexOf('.') + 1); | ||
|
||
let resolvedObj = objPath ? getPath(obj, objPath) : obj; | ||
|
||
if (typeof resolvedObj.set === 'function') { | ||
resolvedObj.set(key, value); | ||
} else { | ||
resolvedObj[key] = value; | ||
} | ||
} |