-
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.
feat(utils): create appendProps utility function
conditionally append properties as the're defined
- Loading branch information
1 parent
5b76d2f
commit 3243592
Showing
3 changed files
with
56 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @desc: conditionally append properties as they're defined | ||
*/ | ||
const appendProps = (...args) => (action) => ( | ||
args.reduce((acc, arg) => ({ | ||
...acc, | ||
...action[arg] && { [arg]: action[arg] }, | ||
}), {}) | ||
); | ||
|
||
export default appendProps; |
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,43 @@ | ||
import appendProps from './appendProps'; | ||
|
||
describe('footer action', () => { | ||
it('should return a function', () => { | ||
const fn = appendProps('yeah'); | ||
expect(typeof fn).toEqual('function'); | ||
}); | ||
|
||
it('should return an object with property "a" and ignore the rest', () => { | ||
const fn = appendProps('a'); | ||
const obj = fn({ a: 1, b: 2 }); | ||
expect(obj).toEqual({ a: 1 }); | ||
}); | ||
|
||
it('should return an object with properties "a" and "b"', () => { | ||
const fn = appendProps('a', 'b'); | ||
const obj = fn({ a: 1, b: 2 }); | ||
expect(obj).toEqual({ a: 1, b: 2 }); | ||
}); | ||
|
||
it('should return an object with property "b"', () => { | ||
const fn = appendProps('b'); | ||
const obj = fn({ a: 1, b: 2 }); | ||
expect(obj).toEqual({ b: 2 }); | ||
}); | ||
|
||
it('should return empty object if the given object doesn\'t match with properties to reflect', () => { | ||
const fn = appendProps('a', 'b'); | ||
const obj = fn({ c: 3, d: 4 }); | ||
expect(obj).toEqual({}); | ||
}); | ||
|
||
it('should return empty object if there\'s not properties to append', () => { | ||
const fn = appendProps(); | ||
const obj = fn({ a: 1, b: 2 }); | ||
expect(obj).toEqual({}); | ||
}); | ||
|
||
it('should throw if there\'s not object to reflect', () => { | ||
const fn = appendProps('a', 'b'); | ||
expect(() => { fn(); }).toThrow(); | ||
}); | ||
}); |
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,2 @@ | ||
// eslint-disable-next-line import/prefer-default-export | ||
export { default as appendProps } from './appendProps'; |