Add spliceIn utility function

This commit is contained in:
Jordan Eldredge 2018-01-01 13:01:03 -08:00
parent 9c993a2646
commit c2588292ce
2 changed files with 21 additions and 1 deletions

View file

@ -187,3 +187,9 @@ export const filterObject = (obj, predicate) =>
}
return newObj;
}, {});
export const spliceIn = (original, start, newValues) => {
const newArr = [...original];
newArr.splice(start, 0, ...newValues);
return newArr;
};

View file

@ -9,7 +9,8 @@ import {
normalize,
denormalize,
segment,
moveSelected
moveSelected,
spliceIn
} from "./utils";
const fixture = filename =>
@ -215,3 +216,16 @@ describe("moveSelected", () => {
expect(moveSelected(arr, i => arr[i], -1)).toEqual([false, true, false]);
});
});
describe("spliceIn", () => {
it("is immutable", () => {
const original = [1, 2, 3];
const spliced = spliceIn(original, 1, [200]);
expect(spliced).not.toBe(original);
expect(original).toEqual([1, 2, 3]);
});
it("adds values at the given index", () => {
const spliced = spliceIn([1, 2, 3], 1, [200]);
expect(spliced).toEqual([1, 200, 2, 3]);
});
});