@@ -31,14 +31,14 @@ export const drop = <T>(list: T[], n = 1): T[] => list.slice(n);
31
31
*
32
32
* @example
33
33
* ```ts
34
- * const newList = dropRight ([1,2,3])
34
+ * const newList = pop ([1,2,3])
35
35
* newList // => [1,2]
36
36
*
37
- * const newList = dropRight ([1,2,3], 2)
37
+ * const newList = pop ([1,2,3], 2)
38
38
* newList // => [1]
39
39
* ```
40
40
*/
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 ) ;
42
42
43
43
/**
44
44
* standalone `Array.prototype.filter()` that filters out passed item
@@ -62,7 +62,7 @@ export function filter<T>(list: readonly T[], predicate: Predicate<T>): T[] & {
62
62
* @returns changed array copy
63
63
*/
64
64
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 ) ;
66
66
67
67
/**
68
68
* standalone `Array.prototype.map()` function
@@ -84,14 +84,14 @@ export const splice = <T>(
84
84
start : number ,
85
85
deleteCount : number = 0 ,
86
86
...items : T [ ]
87
- ) : T [ ] => withArrayCopy ( list , list => list . splice ( start , deleteCount , ...items ) ) ;
87
+ ) : T [ ] => list . slice ( ) . splice ( start , deleteCount , ...items ) ;
88
88
89
89
/**
90
90
* non-mutating `Array.prototype.fill()` as a standalone function
91
91
* @returns changed array copy
92
92
*/
93
93
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 ) ;
95
95
96
96
/**
97
97
* 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[] => {
117
117
return splice ( list , index , 1 ) ;
118
118
} ;
119
119
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
+
120
135
/**
121
136
* Flattens a nested array into a one-level array
122
137
* @returns changed array copy
0 commit comments