-
Notifications
You must be signed in to change notification settings - Fork 1
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
be917a9
commit 6a4242f
Showing
9 changed files
with
89 additions
and
37 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,66 @@ | ||
import {get, isFunction} from './util'; | ||
|
||
describe('Util', () => { | ||
|
||
describe('#isFunction', () => { | ||
|
||
it('should return true if is function', () => { | ||
expect(isFunction(function () {})).toBe(true); | ||
}); | ||
|
||
it('should return true if is anonymous function', () => { | ||
expect(isFunction(() => {})).toBe(true); | ||
}); | ||
|
||
it('should return true if is class', () => { | ||
expect(isFunction(class NotAFunction {})).toBe(true); | ||
}); | ||
|
||
it('should return true if is regex', () => { | ||
expect(isFunction(/abc/)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('#get', () => { | ||
|
||
const simpleObject = { a: { b: 2 } } | ||
const complexObject = { a: [{ bar: { c: 3 } }] } | ||
const falsyObject = { a: null, b: undefined, c: 0 } | ||
|
||
it('a.b', () => { | ||
expect(get(simpleObject, 'a.b')).toEqual(2); | ||
}); | ||
|
||
it('a[0].bar.c', () => { | ||
expect(get(complexObject, 'a[0].bar.c')).toEqual(3); | ||
}); | ||
|
||
it('[\'a\', \'0\', \'bar\', \'c\']', () => { | ||
expect(get(complexObject, ['a', '0', 'bar', 'c'])).toEqual(3); | ||
}); | ||
|
||
it('a.bar.c with default', () => { | ||
expect(get(simpleObject, 'a.bar.c', 'default')).toEqual('default'); | ||
}); | ||
|
||
it('a.bar.c with default', () => { | ||
expect(get(complexObject, 'a.bar.c', 'default')).toEqual('default'); | ||
}); | ||
|
||
it('null', () => { | ||
expect(get(complexObject, null)).toEqual(undefined); | ||
}); | ||
|
||
it('a with default', () => { | ||
expect(get(falsyObject, 'a', 'default')).toEqual(null); | ||
}); | ||
|
||
it('b with default', () => { | ||
expect(get(falsyObject, 'b', 'default')).toEqual('default'); | ||
}); | ||
|
||
it('c with default', () => { | ||
expect(get(falsyObject, 'c', 'default')).toEqual(0); | ||
}); | ||
}); | ||
}) |
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,17 @@ | ||
export function isFunction(value: any): boolean { | ||
return typeof value === 'function'; | ||
} | ||
|
||
export function get(obj: any, path: string|string[], defValue?: any): any { | ||
if (!path) { | ||
return undefined; | ||
} | ||
|
||
const pathArray: string[] = Array.isArray(path) ? path : path.match(/([^[.\]])+/g); | ||
const result: any = pathArray.reduce( | ||
(prevObj: string, key: string) => prevObj && prevObj[key], | ||
obj | ||
); | ||
|
||
return result === undefined ? defValue : result; | ||
} |
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