Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
feat(adjust): Add module adjust
Browse files Browse the repository at this point in the history
Applies a function onto given index of an array
  • Loading branch information
adambrgmn committed Jan 16, 2018
1 parent da5af54 commit aac6fc4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/adjust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import adjust from '../adjust';

test('Core.adjust', () => {
const add = x => y => x + y;
const list = [0, 1, 2];

expect(adjust(add(10), 1, list)).toEqual([0, 11, 2]);
expect(adjust(add(10), 1, list)).not.toBe(list);

expect(adjust(add(10), -1, list)).toEqual([0, 1, 12]);
expect(adjust(add(10), 10, list)).toBe(list);
});
13 changes: 13 additions & 0 deletions src/adjust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import concat from './concat';
import length from './length';

export default (fn, i, arr) => {
const arrLength = length(arr);
if (i >= arrLength || i < -arrLength) return arr;

const idx = i < 0 ? arrLength + i : i;
const list = concat([], arr);
list[idx] = fn(list[idx]);

return list;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import adjust from './adjust';
import allPass from './allPass';
import anyPass from './anyPass';
import append from './append';
Expand Down Expand Up @@ -33,6 +34,7 @@ import reverse from './reverse';
import slice from './slice';

export {
adjust,
allPass,
anyPass,
append,
Expand Down

0 comments on commit aac6fc4

Please sign in to comment.