common util selectItem signature update; common util test update

This commit is contained in:
tmshv committed 2016-02-22 04:35:16 +03:00
1 parent 62d710ea31
commit 78cbb576e3
2 files changed
+19 -3

No files matched your search

+1 -1
View File
@@ -39,6 +39,6 @@ export function indexEquals(withNumber) {
* @param byCondition * @param byCondition
* @param map * @param map
*/ */
export function selectItem(byCondition, map){ export function selectItem(byCondition, map = i => i) {
return (result, item) => byCondition(item) ? map(item): map(result) return (result, item) => byCondition(item) ? map(item): map(result)
} }
+18 -2
View File
@@ -4,14 +4,14 @@ import {selectItem} from '../../src/utils/common';
describe('utils', () => { describe('utils', () => {
describe('common.js', () => { describe('common.js', () => {
describe('selectItem', () => { describe('selectItem', () => {
let sample = [[1235, 'hello'], [2352, 'banana']];
it('should return a function', () => { it('should return a function', () => {
let fn = selectItem(); let fn = selectItem();
fn.should.be.type('function'); fn.should.be.type('function');
}); });
it('should accept a function as a first parameter and use it as byCondition', () => { it('should accept a function as a first parameter and use it as byCondition', () => {
let sample = [[1235, 'hello'], [2352, 'banana']];
let fn = selectItem( let fn = selectItem(
i => i[0] === 2352 && i[1] === 'banana' && i.length === 2, i => i[0] === 2352 && i[1] === 'banana' && i.length === 2,
i => i[1] i => i[1]
@@ -22,6 +22,8 @@ describe('utils', () => {
}); });
it('should accept a function as a second parameter and use it as mapping function', () => { it('should accept a function as a second parameter and use it as mapping function', () => {
let sample = [[1235, 'hello'], [2352, 'banana']];
let fn = selectItem( let fn = selectItem(
i => i[0] === 2352, i => i[0] === 2352,
i => i[1] i => i[1]
@@ -30,6 +32,20 @@ describe('utils', () => {
let result = sample.reduce(fn); let result = sample.reduce(fn);
result.should.be.equal('banana'); 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);
});
}); });
}); });
}); });