Skip to content

Commit

Permalink
Merge bb2d6c4 into f7d0042
Browse files Browse the repository at this point in the history
  • Loading branch information
osorokotyaga committed Aug 19, 2018
2 parents f7d0042 + bb2d6c4 commit e74dc2c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/array/__tests__/find.js
Expand Up @@ -6,4 +6,15 @@ describe('utils/array/find', () => {
expect(find(a => a % 2 === 0)([1, 2, 3, 4])).toBe(2);
expect(find(a => a > 100, [1, 2, 3])).toBeUndefined();
});

it('test callback parameters', () => {
const fn = jest.fn();
const arr = [1, 2, 3];

find(fn, arr);

expect(fn).toHaveBeenCalledWith(1, 0, arr);
expect(fn).toHaveBeenCalledWith(2, 1, arr);
expect(fn).toHaveBeenCalledWith(3, 2, arr);
});
});
11 changes: 11 additions & 0 deletions src/array/__tests__/findIndex.js
Expand Up @@ -6,4 +6,15 @@ describe('utils/array/findIndex', () => {
expect(findIndex(a => a % 2 === 0)([1, 2, 3, 4])).toBe(1);
expect(findIndex(a => a > 100, [1, 2, 3])).toBe(-1);
});

it('test callback parameters', () => {
const fn = jest.fn();
const arr = [1, 2, 3];

findIndex(fn, arr);

expect(fn).toHaveBeenCalledWith(1, 0, arr);
expect(fn).toHaveBeenCalledWith(2, 1, arr);
expect(fn).toHaveBeenCalledWith(3, 2, arr);
});
});
11 changes: 11 additions & 0 deletions src/array/__tests__/findLast.js
Expand Up @@ -6,4 +6,15 @@ describe('utils/array/findLast', () => {
expect(findLast(a => a === 4, [1, 2, 3])).toBeUndefined();
expect(findLast(a => a === 4, [])).toBeUndefined();
});

it('test callback parameters', () => {
const fn = jest.fn();
const arr = [1, 2, 3];

findLast(fn, arr);

expect(fn).toHaveBeenCalledWith(3, 2, arr);
expect(fn).toHaveBeenCalledWith(2, 1, arr);
expect(fn).toHaveBeenCalledWith(1, 0, arr);
});
});
2 changes: 1 addition & 1 deletion src/array/find.js
Expand Up @@ -16,7 +16,7 @@ import curryN from '../function/curryN';
*/
export default curryN(2, (fn, arr = []) => {
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i])) {
if (fn(arr[i], i, arr)) {
return arr[i];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/array/findIndex.js
Expand Up @@ -16,7 +16,7 @@ import curryN from '../function/curryN';
*/
export default curryN(2, (fn, arr = []) => {
for (let i = 0; i < arr.length; i++) {
if (fn(arr[i])) {
if (fn(arr[i], i, arr)) {
return i;
}
}
Expand Down
12 changes: 3 additions & 9 deletions src/array/findLast.js
Expand Up @@ -16,15 +16,9 @@ import curry from '../function/curry';
* findLast(propEq('a', 4))(xs); //=> undefined
*/
export default curry((fn, list) => {
let idx = list.length - 1;

while (idx >= 0) {
if (fn(list[idx])) {
return list[idx];
for (let i = list.length - 1; i >= 0; i--) {
if (fn(list[i], i, list)) {
return list[i];
}

idx -= 1;
}

return undefined;
});
11 changes: 11 additions & 0 deletions src/object/__tests__/findKey.js
Expand Up @@ -8,4 +8,15 @@ describe('utils/object/findKey', () => {
expect(findKey(x => x > 3, { a: 4, b: 5 })).toBe('a');
expect(findKey(x => x > 3, { a: 3, b: 5 })).toBe('b');
});

it('test callback parameters', () => {
const fn = jest.fn();
const obj = { a: 1, b: 2, c: 3 };

findKey(fn, obj);

expect(fn).toHaveBeenCalledWith(1, 'a', obj);
expect(fn).toHaveBeenCalledWith(2, 'b', obj);
expect(fn).toHaveBeenCalledWith(3, 'c', obj);
});
});

0 comments on commit e74dc2c

Please sign in to comment.