-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce way to get property value from dictionary and throw i…
…f it does not exist
- Loading branch information
Showing
7 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import curry from 'lodash/fp/curry'; | ||
import get from 'lodash/fp/get'; | ||
|
||
export default curry((dictionary, propertyName) => | ||
get(propertyName, dictionary), | ||
); |
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 @@ | ||
import getSafeFrom from './getFrom'; | ||
|
||
describe('getSafeFrom', () => { | ||
it('when getting existing value, returns value', () => { | ||
const actual = getSafeFrom({ someProperty: 'some-value' })('someProperty'); | ||
|
||
expect(actual).toBe('some-value'); | ||
}); | ||
|
||
it('when getting non existing value, return undefined', () => { | ||
const actual = getSafeFrom({})('someNonExistingProperty'); | ||
|
||
expect(actual).toBeUndefined(); | ||
}); | ||
|
||
it('when getting existing nested value, returns value', () => { | ||
const actual = getSafeFrom({ someRoot: { someProperty: 'some-value' } })( | ||
'someRoot.someProperty', | ||
); | ||
|
||
expect(actual).toBe('some-value'); | ||
}); | ||
}); |
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,11 @@ | ||
import curry from 'lodash/fp/curry'; | ||
import get from 'lodash/fp/get'; | ||
import has from 'lodash/fp/has'; | ||
|
||
export default curry((dictionary, propertyName) => { | ||
if (!has(propertyName, dictionary)) { | ||
throw new Error(`Tried to get unknown property "${propertyName}"`); | ||
} | ||
|
||
return get(propertyName, dictionary); | ||
}); |
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 @@ | ||
import getSafeFrom from './getSafeFrom'; | ||
|
||
describe('getSafeFrom', () => { | ||
it('when getting existing value, returns value', () => { | ||
const actual = getSafeFrom({ someProperty: 'some-value' })('someProperty'); | ||
|
||
expect(actual).toBe('some-value'); | ||
}); | ||
|
||
it('when getting non existing value, throws', () => { | ||
expect(() => { | ||
getSafeFrom({})('someNonExistingProperty'); | ||
}).toThrow('Tried to get unknown property "someNonExistingProperty"'); | ||
}); | ||
|
||
it('when getting existing nested value, returns value', () => { | ||
const actual = getSafeFrom({ someRoot: { someProperty: 'some-value' } })( | ||
'someRoot.someProperty', | ||
); | ||
|
||
expect(actual).toBe('some-value'); | ||
}); | ||
}); |