Skip to content

Commit f11f5f6

Browse files
committed
feat(immutable): add removeItems
1 parent ed2c49b commit f11f5f6

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

packages/immutable/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ setUser(p => update(p, "street", "number", 64));
5656

5757
- **`push`** - non-mutating `Array.prototype.push()`
5858
- **`drop`** - non-mutating function that drops n items from the array start
59-
- **`dropRight`** - non-mutating function that drops n items from the array end
59+
- **`pop`** - non-mutating function that drops n items from the array end
6060
- **`filterOut`** - standalone `Array.prototype.filter()` that filters out passed item
6161
- **`filter`** - standalone `Array.prototype.filter()`
6262
- **`sort`** - non-mutating `Array.prototype.sort()` as a standalone function
@@ -66,6 +66,7 @@ setUser(p => update(p, "street", "number", 64));
6666
- **`fill`** - non-mutating `Array.prototype.fill()` as a standalone function
6767
- **`concat`** - Creates a new array concatenating array with any additional arrays and/or values.
6868
- **`remove`** - Remove item from array
69+
- **`removeItems`** - Remove multiple items from an array
6970
- **`flatten`** - Flattens a nested array into a one-level array
7071
- **`sortBy`** - Sort an array by object key, or multiple keys
7172

packages/immutable/src/array.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ export const drop = <T>(list: T[], n = 1): T[] => list.slice(n);
3131
*
3232
* @example
3333
* ```ts
34-
* const newList = dropRight([1,2,3])
34+
* const newList = pop([1,2,3])
3535
* newList // => [1,2]
3636
*
37-
* const newList = dropRight([1,2,3], 2)
37+
* const newList = pop([1,2,3], 2)
3838
* newList // => [1]
3939
* ```
4040
*/
41-
export const dropRight = <T>(list: T[], n = 1): T[] => list.slice(0, list.length - n);
41+
export const pop = <T>(list: T[], n = 1): T[] => list.slice(0, list.length - n);
4242

4343
/**
4444
* standalone `Array.prototype.filter()` that filters out passed item
@@ -62,7 +62,7 @@ export function filter<T>(list: readonly T[], predicate: Predicate<T>): T[] & {
6262
* @returns changed array copy
6363
*/
6464
export const sort = <T>(list: T[], compareFn?: (a: T, b: T) => number): T[] =>
65-
withArrayCopy(list, list => list.sort(compareFn));
65+
list.slice().sort(compareFn);
6666

6767
/**
6868
* standalone `Array.prototype.map()` function
@@ -84,14 +84,14 @@ export const splice = <T>(
8484
start: number,
8585
deleteCount: number = 0,
8686
...items: T[]
87-
): T[] => withArrayCopy(list, list => list.splice(start, deleteCount, ...items));
87+
): T[] => list.slice().splice(start, deleteCount, ...items);
8888

8989
/**
9090
* non-mutating `Array.prototype.fill()` as a standalone function
9191
* @returns changed array copy
9292
*/
9393
export const fill = <T>(list: readonly T[], value: T, start?: number, end?: number): T[] =>
94-
withArrayCopy(list, list => list.fill(value, start, end));
94+
list.slice().fill(value, start, end);
9595

9696
/**
9797
* Creates a new array concatenating array with any additional arrays and/or values.
@@ -117,6 +117,21 @@ export const remove = <T>(list: readonly T[], item: T): T[] => {
117117
return splice(list, index, 1);
118118
};
119119

120+
/**
121+
* Remove multiple items from an array
122+
* @returns changed array copy
123+
*/
124+
export const removeItems = <T>(list: readonly T[], ...items: T[]): T[] => {
125+
const res = [];
126+
for (let i = 0; i < list.length; i++) {
127+
const item = list[i];
128+
const ii = items.indexOf(item);
129+
if (ii !== -1) items.splice(ii, 1);
130+
else res.push(item);
131+
}
132+
return res;
133+
};
134+
120135
/**
121136
* Flattens a nested array into a one-level array
122137
* @returns changed array copy

packages/immutable/test/index.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { update, concat, split, get, sortBy, merge } from "../src";
1+
import { update, concat, split, get, sortBy, merge, removeItems } from "../src";
22
import { suite } from "uvu";
33
import * as assert from "uvu/assert";
44
import { cloneDeep } from "lodash";
@@ -155,3 +155,12 @@ testMerge("merges", () => {
155155
});
156156

157157
testMerge.run();
158+
159+
const testRemoveItems = suite("removeItems");
160+
161+
testRemoveItems("", () => {
162+
const res = removeItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 6, 7, 11);
163+
assert.equal(res, [1, 3, 4, 5, 8, 9]);
164+
});
165+
166+
testRemoveItems.run();

0 commit comments

Comments
 (0)