diff --git a/src/utils/common.js b/src/utils/common.js index a4d014b9..1c9ab200 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -39,6 +39,6 @@ export function indexEquals(withNumber) { * @param byCondition * @param map */ -export function selectItem(byCondition, map){ +export function selectItem(byCondition, map = i => i) { return (result, item) => byCondition(item) ? map(item): map(result) } \ No newline at end of file diff --git a/test/utils/common.js b/test/utils/common.js index d6b1fcbf..d94bef14 100644 --- a/test/utils/common.js +++ b/test/utils/common.js @@ -4,14 +4,14 @@ import {selectItem} from '../../src/utils/common'; describe('utils', () => { describe('common.js', () => { describe('selectItem', () => { - let sample = [[1235, 'hello'], [2352, 'banana']]; - it('should return a function', () => { let fn = selectItem(); fn.should.be.type('function'); }); it('should accept a function as a first parameter and use it as byCondition', () => { + let sample = [[1235, 'hello'], [2352, 'banana']]; + let fn = selectItem( i => i[0] === 2352 && i[1] === 'banana' && i.length === 2, i => i[1] @@ -22,6 +22,8 @@ describe('utils', () => { }); it('should accept a function as a second parameter and use it as mapping function', () => { + let sample = [[1235, 'hello'], [2352, 'banana']]; + let fn = selectItem( i => i[0] === 2352, i => i[1] @@ -30,6 +32,20 @@ describe('utils', () => { let result = sample.reduce(fn); result.should.be.equal('banana'); }); + + it('should pass with one parameter', () => { + let sample = ['hello', 'lol', 'banana']; + + let result = sample.reduce(selectItem(i => i === 'banana')); + result.should.be.equal('banana'); + }); + + it('should get a null in reduce result if reduce calls with second parameter that is null', () => { + let sample = ['hello', 'lol', 'banana']; + + let result = sample.reduce(selectItem(i => i === 'apple'), null); + should.not.exist(result); + }); }); }); }); \ No newline at end of file