Skip to content

Commit

Permalink
feat(utils): create appendProps utility function
Browse files Browse the repository at this point in the history
conditionally append properties as the're defined
  • Loading branch information
aneurysmjs committed Sep 27, 2019
1 parent 5b76d2f commit 3243592
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/app/utils/appendProps/appendProps.js
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;
43 changes: 43 additions & 0 deletions src/app/utils/appendProps/appendProps.test.js
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();
});
});
2 changes: 2 additions & 0 deletions src/app/utils/appendProps/index.js
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';

0 comments on commit 3243592

Please sign in to comment.