Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace lodash.get with local function; support array paths. (#177)
- Loading branch information
1 parent
71da326
commit bf8e534
Showing
6 changed files
with
151 additions
and
5 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
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,23 @@ | ||
// @flow | ||
'use strict'; | ||
|
||
// Resolves property names or property paths defined with period-delimited | ||
// strings or arrays of strings. Property names that are found on the source | ||
// object are used directly (even if they include a period). | ||
// Nested property names that include periods, within a path, are only | ||
// understood in array paths. | ||
function getPropertyByPath(source: Object, path: string | Array<string>): any { | ||
if (typeof path === 'string' && source.hasOwnProperty(path)) { | ||
return source[path]; | ||
} | ||
|
||
const parsedPath = typeof path === 'string' ? path.split('.') : path; | ||
return parsedPath.reduce((previous, key) => { | ||
if (previous === undefined) { | ||
return previous; | ||
} | ||
return previous[key]; | ||
}, source); | ||
} | ||
|
||
module.exports = getPropertyByPath; |
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,91 @@ | ||
'use strict'; | ||
|
||
const getPropertyPath = require('../src/getPropertyByPath'); | ||
|
||
const source = { | ||
ant: { | ||
beetle: { | ||
cootie: { | ||
flea: 'foo', | ||
}, | ||
louse: { | ||
vermin: 'bar', | ||
}, | ||
}, | ||
'fancy.name': { | ||
'another.fancy.name': 9, | ||
}, | ||
}, | ||
'ant.beetle.cootie': 333, | ||
}; | ||
|
||
describe('with a property name that includes a period', () => { | ||
test('does not treat it as a period-delimited path', () => { | ||
expect(getPropertyPath(source, 'ant.beetle.cootie')).toBe(333); | ||
}); | ||
}); | ||
|
||
describe('with period-delimited string path', () => { | ||
test('returns a defined value', () => { | ||
expect(getPropertyPath(source, 'ant')).toBe(source.ant); | ||
|
||
expect(getPropertyPath(source, 'ant.beetle.cootie.flea')).toBe('foo'); | ||
|
||
expect(getPropertyPath(source, 'ant.beetle.louse')).toBe( | ||
source.ant.beetle.louse | ||
); | ||
}); | ||
|
||
test('returns undefined', () => { | ||
expect(getPropertyPath(source, 'beetle')).toBeUndefined(); | ||
|
||
expect(getPropertyPath(source, 'ant.beetle.cootie.fleeee')).toBeUndefined(); | ||
|
||
expect(getPropertyPath(source, 'ant.beetle.vermin')).toBeUndefined(); | ||
|
||
expect(getPropertyPath(source, 'ant.fancy.name')).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('with array path', () => { | ||
test('returns a defined value', () => { | ||
expect(getPropertyPath(source, ['ant'])).toBe(source.ant); | ||
|
||
expect(getPropertyPath(source, ['ant', 'beetle', 'cootie', 'flea'])).toBe( | ||
'foo' | ||
); | ||
|
||
expect(getPropertyPath(source, ['ant', 'beetle', 'louse'])).toBe( | ||
source.ant.beetle.louse | ||
); | ||
}); | ||
|
||
test('returns undefined', () => { | ||
expect(getPropertyPath(source, ['beetle'])).toBeUndefined(); | ||
|
||
expect( | ||
getPropertyPath(source, ['ant', 'beetle', 'cootie', 'fleeee']) | ||
).toBeUndefined(); | ||
|
||
expect( | ||
getPropertyPath(source, ['ant', 'beetle', 'vermin']) | ||
).toBeUndefined(); | ||
}); | ||
|
||
test('handles property names with periods', () => { | ||
expect( | ||
getPropertyPath(source, ['ant', 'fancy.name', 'another.fancy.name']) | ||
).toBe(9); | ||
|
||
expect( | ||
getPropertyPath(source, [ | ||
'ant', | ||
'fancy.name', | ||
'another.fancy.name', | ||
'foo', | ||
]) | ||
).toBeUndefined; | ||
|
||
expect(getPropertyPath(source, ['ant', 'fancy.namez'])).toBeUndefined; | ||
}); | ||
}); |