es-toolkit 1.35.0-dev.1174 → 1.35.0-dev.1178
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/partition.d.mts +34 -90
- package/dist/compat/array/partition.d.ts +34 -90
- package/dist/compat/array/partition.mjs +2 -18
- package/dist/compat/array/pullAllWith.d.mts +26 -0
- package/dist/compat/array/pullAllWith.d.ts +26 -0
- package/dist/compat/array/pullAllWith.mjs +19 -0
- package/dist/compat/index.d.mts +1 -0
- package/dist/compat/index.d.ts +1 -0
- package/dist/compat/index.js +21 -17
- package/dist/compat/index.mjs +1 -0
- package/package.json +1 -1
|
@@ -1,121 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates an array of elements split into two groups, the first of which contains elements
|
|
3
3
|
* `predicate` returns truthy for, the second of which contains elements
|
|
4
|
-
* `predicate` returns falsy for.
|
|
4
|
+
* `predicate` returns falsy for. The predicate is invoked with one argument: (value).
|
|
5
5
|
*
|
|
6
6
|
* @template T
|
|
7
|
-
* @
|
|
8
|
-
* @param {
|
|
9
|
-
* @
|
|
7
|
+
* @template U
|
|
8
|
+
* @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
|
|
9
|
+
* @param {(value: T) => value is U} predicate - The function invoked per iteration.
|
|
10
|
+
* @returns {[U[], Array<Exclude<T, U>>]} - Returns the array of grouped elements.
|
|
10
11
|
*
|
|
11
12
|
* @example
|
|
12
|
-
* partition([1, 2, 3], n => n % 2 === 0)
|
|
13
|
-
* // => [[2], [1, 3]]
|
|
14
|
-
*/
|
|
15
|
-
declare function partition<T>(arr: ArrayLike<T> | null | undefined, doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): [T[], T[]];
|
|
16
|
-
/**
|
|
17
|
-
* Creates an array of elements split into two groups based on the properties of the given partial object.
|
|
13
|
+
* partition([1, 2, 3, 4], n => n % 2 === 0);
|
|
14
|
+
* // => [[2, 4], [1, 3]]
|
|
18
15
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
22
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
16
|
+
* partition([1, 2, 3, 4], 'a');
|
|
17
|
+
* // => [[2, 4], [1, 3]]
|
|
23
18
|
*
|
|
24
|
-
*
|
|
25
|
-
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
26
|
-
* partition(arr, { name: 'Bob' });
|
|
27
|
-
* // => [[{ id: 2, name: 'Bob' }], [{ id: 1, name: 'Alice' }]]
|
|
19
|
+
* partition([1, 2, 3, 4], ['a', 2]);
|
|
28
20
|
*/
|
|
29
|
-
declare function partition<T>(
|
|
30
|
-
/**
|
|
31
|
-
* Creates an array of elements split into two groups based on the given key-value pair.
|
|
32
|
-
*
|
|
33
|
-
* @template T
|
|
34
|
-
* @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
|
|
35
|
-
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
36
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
40
|
-
* partition(arr, ['name', 'Alice']);
|
|
41
|
-
* // => [[{ id: 1, name: 'Alice' }], [{ id: 2, name: 'Bob' }]]
|
|
42
|
-
*/
|
|
43
|
-
declare function partition<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): [T[], T[]];
|
|
21
|
+
declare function partition<T, U extends T>(source: ArrayLike<T> | null | undefined, predicate: (value: T) => value is U): [U[], Array<Exclude<T, U>>];
|
|
44
22
|
/**
|
|
45
23
|
* Creates an array of elements split into two groups, the first of which contains elements
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @template T
|
|
49
|
-
* @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
|
|
50
|
-
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
51
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, age: 28 }];
|
|
55
|
-
* partition(arr, 'name');
|
|
56
|
-
* // => [[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }], [{ id: 3, age: 28 }]]
|
|
57
|
-
*/
|
|
58
|
-
declare function partition<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey): [T[], T[]];
|
|
59
|
-
/**
|
|
60
|
-
* Creates an array of elements split into two groups based on the predicate function.
|
|
24
|
+
* `predicate` returns truthy for, the second of which contains elements
|
|
25
|
+
* `predicate` returns falsy for. The predicate is invoked with one argument: (value).
|
|
61
26
|
*
|
|
62
27
|
* @template T
|
|
63
|
-
* @param {T | null | undefined}
|
|
64
|
-
* @param {(
|
|
28
|
+
* @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
|
|
29
|
+
* @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
|
|
65
30
|
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
66
31
|
*
|
|
67
32
|
* @example
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* // => [[{ a: 1 }], [{ a: 0 }, { a: 0 }]]
|
|
71
|
-
*
|
|
72
|
-
* const obj = { a: 1, b: 2, c: 3 };
|
|
73
|
-
* partition(obj, value => value > 2)
|
|
74
|
-
* // => [[3], [1, 2]]
|
|
75
|
-
*/
|
|
76
|
-
declare function partition<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): [T[], T[]];
|
|
77
|
-
/**
|
|
78
|
-
* Creates an array of elements split into two groups based on the properties of the given partial object.
|
|
33
|
+
* partition([1, 2, 3, 4], n => n % 2 === 0);
|
|
34
|
+
* // => [[2, 4], [1, 3]]
|
|
79
35
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* @param {Partial<T[keyof T]>} doesMatch - The partial object to match
|
|
83
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
36
|
+
* partition([1, 2, 3, 4], 'a');
|
|
37
|
+
* // => [[2, 4], [1, 3]]
|
|
84
38
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* partition(obj, { name: 'Bob' });
|
|
88
|
-
* // => [[{ id: 2, name: 'Bob' }], [{ id: 1, name: 'Alice' }]]
|
|
39
|
+
* partition([1, 2, 3, 4], ['a', 2]);
|
|
40
|
+
* // => [[2], [1, 3, 4]]
|
|
89
41
|
*/
|
|
90
|
-
declare function partition<T
|
|
42
|
+
declare function partition<T>(source: ArrayLike<T> | null | undefined, predicate?: ((value: T) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T>): [T[], T[]];
|
|
91
43
|
/**
|
|
92
|
-
* Creates an array of elements split into two groups
|
|
44
|
+
* Creates an array of elements split into two groups, the first of which contains elements
|
|
45
|
+
* `predicate` returns truthy for, the second of which contains elements
|
|
46
|
+
* `predicate` returns falsy for. The predicate is invoked with one argument: (value).
|
|
93
47
|
*
|
|
94
48
|
* @template T
|
|
95
49
|
* @param {T | null | undefined} object - The object to iterate over.
|
|
96
|
-
* @param {[keyof T[keyof T], unknown]}
|
|
97
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
50
|
+
* @param {((item: T[keyof T], key: keyof T) => unknown) | Partial<T[keyof T]> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
|
|
51
|
+
* @returns {[T[keyof T][], T[keyof T][]]} - Returns the array of grouped elements.
|
|
98
52
|
*
|
|
99
53
|
* @example
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* // => [[{ id: 1, name: 'Alice' }], [{ id: 2, name: 'Bob' }]]
|
|
103
|
-
*/
|
|
104
|
-
declare function partition<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown]): [T[], T[]];
|
|
105
|
-
/**
|
|
106
|
-
* Creates an array of elements split into two groups, the first of which contains elements
|
|
107
|
-
* that have the given property name, the second of which contains elements that don't.
|
|
54
|
+
* partition({ a: 1, b: 2, c: 3 }, n => n % 2 === 0);
|
|
55
|
+
* // => [[2], [1, 3]]
|
|
108
56
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
112
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
57
|
+
* partition({ a: 1, b: 2, c: 3 }, 'a');
|
|
58
|
+
* // => [[1], [2, 3]]
|
|
113
59
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* partition(obj, 'name');
|
|
117
|
-
* // => [[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }], [{ id: 3, age: 28 }]]
|
|
60
|
+
* partition({ a: 1, b: 2, c: 3 }, ['a', 1]);
|
|
61
|
+
* // => [[1], [2, 3]]
|
|
118
62
|
*/
|
|
119
|
-
declare function partition<T extends
|
|
63
|
+
declare function partition<T extends object>(object: T | null | undefined, predicate?: ((value: T[keyof T]) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T[keyof T]>): [Array<T[keyof T]>, Array<T[keyof T]>];
|
|
120
64
|
|
|
121
65
|
export { partition };
|
|
@@ -1,121 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates an array of elements split into two groups, the first of which contains elements
|
|
3
3
|
* `predicate` returns truthy for, the second of which contains elements
|
|
4
|
-
* `predicate` returns falsy for.
|
|
4
|
+
* `predicate` returns falsy for. The predicate is invoked with one argument: (value).
|
|
5
5
|
*
|
|
6
6
|
* @template T
|
|
7
|
-
* @
|
|
8
|
-
* @param {
|
|
9
|
-
* @
|
|
7
|
+
* @template U
|
|
8
|
+
* @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
|
|
9
|
+
* @param {(value: T) => value is U} predicate - The function invoked per iteration.
|
|
10
|
+
* @returns {[U[], Array<Exclude<T, U>>]} - Returns the array of grouped elements.
|
|
10
11
|
*
|
|
11
12
|
* @example
|
|
12
|
-
* partition([1, 2, 3], n => n % 2 === 0)
|
|
13
|
-
* // => [[2], [1, 3]]
|
|
14
|
-
*/
|
|
15
|
-
declare function partition<T>(arr: ArrayLike<T> | null | undefined, doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): [T[], T[]];
|
|
16
|
-
/**
|
|
17
|
-
* Creates an array of elements split into two groups based on the properties of the given partial object.
|
|
13
|
+
* partition([1, 2, 3, 4], n => n % 2 === 0);
|
|
14
|
+
* // => [[2, 4], [1, 3]]
|
|
18
15
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
22
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
16
|
+
* partition([1, 2, 3, 4], 'a');
|
|
17
|
+
* // => [[2, 4], [1, 3]]
|
|
23
18
|
*
|
|
24
|
-
*
|
|
25
|
-
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
26
|
-
* partition(arr, { name: 'Bob' });
|
|
27
|
-
* // => [[{ id: 2, name: 'Bob' }], [{ id: 1, name: 'Alice' }]]
|
|
19
|
+
* partition([1, 2, 3, 4], ['a', 2]);
|
|
28
20
|
*/
|
|
29
|
-
declare function partition<T>(
|
|
30
|
-
/**
|
|
31
|
-
* Creates an array of elements split into two groups based on the given key-value pair.
|
|
32
|
-
*
|
|
33
|
-
* @template T
|
|
34
|
-
* @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
|
|
35
|
-
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
36
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
40
|
-
* partition(arr, ['name', 'Alice']);
|
|
41
|
-
* // => [[{ id: 1, name: 'Alice' }], [{ id: 2, name: 'Bob' }]]
|
|
42
|
-
*/
|
|
43
|
-
declare function partition<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): [T[], T[]];
|
|
21
|
+
declare function partition<T, U extends T>(source: ArrayLike<T> | null | undefined, predicate: (value: T) => value is U): [U[], Array<Exclude<T, U>>];
|
|
44
22
|
/**
|
|
45
23
|
* Creates an array of elements split into two groups, the first of which contains elements
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @template T
|
|
49
|
-
* @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
|
|
50
|
-
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
51
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, age: 28 }];
|
|
55
|
-
* partition(arr, 'name');
|
|
56
|
-
* // => [[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }], [{ id: 3, age: 28 }]]
|
|
57
|
-
*/
|
|
58
|
-
declare function partition<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey): [T[], T[]];
|
|
59
|
-
/**
|
|
60
|
-
* Creates an array of elements split into two groups based on the predicate function.
|
|
24
|
+
* `predicate` returns truthy for, the second of which contains elements
|
|
25
|
+
* `predicate` returns falsy for. The predicate is invoked with one argument: (value).
|
|
61
26
|
*
|
|
62
27
|
* @template T
|
|
63
|
-
* @param {T | null | undefined}
|
|
64
|
-
* @param {(
|
|
28
|
+
* @param {ArrayLike<T> | T | null | undefined} source - The array or object to iterate over.
|
|
29
|
+
* @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
|
|
65
30
|
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
66
31
|
*
|
|
67
32
|
* @example
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* // => [[{ a: 1 }], [{ a: 0 }, { a: 0 }]]
|
|
71
|
-
*
|
|
72
|
-
* const obj = { a: 1, b: 2, c: 3 };
|
|
73
|
-
* partition(obj, value => value > 2)
|
|
74
|
-
* // => [[3], [1, 2]]
|
|
75
|
-
*/
|
|
76
|
-
declare function partition<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): [T[], T[]];
|
|
77
|
-
/**
|
|
78
|
-
* Creates an array of elements split into two groups based on the properties of the given partial object.
|
|
33
|
+
* partition([1, 2, 3, 4], n => n % 2 === 0);
|
|
34
|
+
* // => [[2, 4], [1, 3]]
|
|
79
35
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* @param {Partial<T[keyof T]>} doesMatch - The partial object to match
|
|
83
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
36
|
+
* partition([1, 2, 3, 4], 'a');
|
|
37
|
+
* // => [[2, 4], [1, 3]]
|
|
84
38
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* partition(obj, { name: 'Bob' });
|
|
88
|
-
* // => [[{ id: 2, name: 'Bob' }], [{ id: 1, name: 'Alice' }]]
|
|
39
|
+
* partition([1, 2, 3, 4], ['a', 2]);
|
|
40
|
+
* // => [[2], [1, 3, 4]]
|
|
89
41
|
*/
|
|
90
|
-
declare function partition<T
|
|
42
|
+
declare function partition<T>(source: ArrayLike<T> | null | undefined, predicate?: ((value: T) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T>): [T[], T[]];
|
|
91
43
|
/**
|
|
92
|
-
* Creates an array of elements split into two groups
|
|
44
|
+
* Creates an array of elements split into two groups, the first of which contains elements
|
|
45
|
+
* `predicate` returns truthy for, the second of which contains elements
|
|
46
|
+
* `predicate` returns falsy for. The predicate is invoked with one argument: (value).
|
|
93
47
|
*
|
|
94
48
|
* @template T
|
|
95
49
|
* @param {T | null | undefined} object - The object to iterate over.
|
|
96
|
-
* @param {[keyof T[keyof T], unknown]}
|
|
97
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
50
|
+
* @param {((item: T[keyof T], key: keyof T) => unknown) | Partial<T[keyof T]> | [keyof T, unknown] | PropertyKey} [predicate=identity] - The function invoked per iteration.
|
|
51
|
+
* @returns {[T[keyof T][], T[keyof T][]]} - Returns the array of grouped elements.
|
|
98
52
|
*
|
|
99
53
|
* @example
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* // => [[{ id: 1, name: 'Alice' }], [{ id: 2, name: 'Bob' }]]
|
|
103
|
-
*/
|
|
104
|
-
declare function partition<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown]): [T[], T[]];
|
|
105
|
-
/**
|
|
106
|
-
* Creates an array of elements split into two groups, the first of which contains elements
|
|
107
|
-
* that have the given property name, the second of which contains elements that don't.
|
|
54
|
+
* partition({ a: 1, b: 2, c: 3 }, n => n % 2 === 0);
|
|
55
|
+
* // => [[2], [1, 3]]
|
|
108
56
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
112
|
-
* @returns {[T[], T[]]} - Returns the array of grouped elements.
|
|
57
|
+
* partition({ a: 1, b: 2, c: 3 }, 'a');
|
|
58
|
+
* // => [[1], [2, 3]]
|
|
113
59
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* partition(obj, 'name');
|
|
117
|
-
* // => [[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }], [{ id: 3, age: 28 }]]
|
|
60
|
+
* partition({ a: 1, b: 2, c: 3 }, ['a', 1]);
|
|
61
|
+
* // => [[1], [2, 3]]
|
|
118
62
|
*/
|
|
119
|
-
declare function partition<T extends
|
|
63
|
+
declare function partition<T extends object>(object: T | null | undefined, predicate?: ((value: T[keyof T]) => unknown) | PropertyKey | [PropertyKey, any] | Partial<T[keyof T]>): [Array<T[keyof T]>, Array<T[keyof T]>];
|
|
120
64
|
|
|
121
65
|
export { partition };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { isArray } from '../predicate/isArray.mjs';
|
|
2
1
|
import { isArrayLike } from '../predicate/isArrayLike.mjs';
|
|
3
2
|
import { iteratee } from '../util/iteratee.mjs';
|
|
4
3
|
|
|
@@ -6,28 +5,13 @@ function partition(source, predicate) {
|
|
|
6
5
|
if (!source) {
|
|
7
6
|
return [[], []];
|
|
8
7
|
}
|
|
9
|
-
const collection =
|
|
8
|
+
const collection = isArrayLike(source) ? source : Object.values(source);
|
|
10
9
|
predicate = iteratee(predicate);
|
|
11
10
|
const matched = [];
|
|
12
11
|
const unmatched = [];
|
|
13
|
-
if (!isArray(source)) {
|
|
14
|
-
const keys = Object.keys(source);
|
|
15
|
-
const length = isArrayLike(source) ? source.length : keys.length;
|
|
16
|
-
for (let i = 0; i < length; i++) {
|
|
17
|
-
const key = keys[i];
|
|
18
|
-
const value = source[key];
|
|
19
|
-
if (predicate(value, key, source)) {
|
|
20
|
-
matched.push(value);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
unmatched.push(value);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return [matched, unmatched];
|
|
27
|
-
}
|
|
28
12
|
for (let i = 0; i < collection.length; i++) {
|
|
29
13
|
const value = collection[i];
|
|
30
|
-
if (predicate(value
|
|
14
|
+
if (predicate(value)) {
|
|
31
15
|
matched.push(value);
|
|
32
16
|
}
|
|
33
17
|
else {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes elements from an array that match the given comparator function and returns the modified array.
|
|
3
|
+
*
|
|
4
|
+
* This function mutates the original array by removing elements that match the criteria defined by `comparator`.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of elements in the array.
|
|
7
|
+
* @param {T[]} arr - The array from which elements will be removed.
|
|
8
|
+
* @param {T[]} values - The values to compare against, which determine which elements should be removed.
|
|
9
|
+
* @param {(a: T, b: T) => boolean} comparator - A function that compares elements in `array` with `values`. If it returns `true`, the element is removed.
|
|
10
|
+
* @returns {T[]} - The modified array after removing matching elements.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import pullAllWith from './pullAllWith';
|
|
14
|
+
* import isEqual from '../predicate';
|
|
15
|
+
*
|
|
16
|
+
* const array = [{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }];
|
|
17
|
+
* const valuesToRemove = [{ x: 3, y: 4 }];
|
|
18
|
+
*
|
|
19
|
+
* const result = pullAllWith(array, valuesToRemove, isEqual);
|
|
20
|
+
*
|
|
21
|
+
* console.log(result); // [{ x: 1, y: 2 }, { x: 5, y: 6 }]
|
|
22
|
+
* console.log(array); // [{ x: 1, y: 2 }, { x: 5, y: 6 }]
|
|
23
|
+
*/
|
|
24
|
+
declare function pullAllWith<T>(arr: T[], values: T[], comparator: (a: T, b: T) => boolean): T[];
|
|
25
|
+
|
|
26
|
+
export { pullAllWith };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes elements from an array that match the given comparator function and returns the modified array.
|
|
3
|
+
*
|
|
4
|
+
* This function mutates the original array by removing elements that match the criteria defined by `comparator`.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of elements in the array.
|
|
7
|
+
* @param {T[]} arr - The array from which elements will be removed.
|
|
8
|
+
* @param {T[]} values - The values to compare against, which determine which elements should be removed.
|
|
9
|
+
* @param {(a: T, b: T) => boolean} comparator - A function that compares elements in `array` with `values`. If it returns `true`, the element is removed.
|
|
10
|
+
* @returns {T[]} - The modified array after removing matching elements.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* import pullAllWith from './pullAllWith';
|
|
14
|
+
* import isEqual from '../predicate';
|
|
15
|
+
*
|
|
16
|
+
* const array = [{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }];
|
|
17
|
+
* const valuesToRemove = [{ x: 3, y: 4 }];
|
|
18
|
+
*
|
|
19
|
+
* const result = pullAllWith(array, valuesToRemove, isEqual);
|
|
20
|
+
*
|
|
21
|
+
* console.log(result); // [{ x: 1, y: 2 }, { x: 5, y: 6 }]
|
|
22
|
+
* console.log(array); // [{ x: 1, y: 2 }, { x: 5, y: 6 }]
|
|
23
|
+
*/
|
|
24
|
+
declare function pullAllWith<T>(arr: T[], values: T[], comparator: (a: T, b: T) => boolean): T[];
|
|
25
|
+
|
|
26
|
+
export { pullAllWith };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function pullAllWith(arr, values, comparator) {
|
|
2
|
+
let resultIndex = 0;
|
|
3
|
+
for (let i = 0; i < arr.length; i++) {
|
|
4
|
+
if (!(i in arr)) {
|
|
5
|
+
if (!values.includes(undefined)) {
|
|
6
|
+
delete arr[resultIndex++];
|
|
7
|
+
}
|
|
8
|
+
continue;
|
|
9
|
+
}
|
|
10
|
+
const shouldRemove = values.some(value => comparator(arr[i], value));
|
|
11
|
+
if (!shouldRemove) {
|
|
12
|
+
arr[resultIndex++] = arr[i];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
arr.length = resultIndex;
|
|
16
|
+
return arr;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { pullAllWith };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -100,6 +100,7 @@ export { partition } from './array/partition.mjs';
|
|
|
100
100
|
export { pull } from './array/pull.mjs';
|
|
101
101
|
export { pullAll } from './array/pullAll.mjs';
|
|
102
102
|
export { pullAllBy } from './array/pullAllBy.mjs';
|
|
103
|
+
export { pullAllWith } from './array/pullAllWith.mjs';
|
|
103
104
|
export { reduce } from './array/reduce.mjs';
|
|
104
105
|
export { reduceRight } from './array/reduceRight.mjs';
|
|
105
106
|
export { reject } from './array/reject.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -100,6 +100,7 @@ export { partition } from './array/partition.js';
|
|
|
100
100
|
export { pull } from './array/pull.js';
|
|
101
101
|
export { pullAll } from './array/pullAll.js';
|
|
102
102
|
export { pullAllBy } from './array/pullAllBy.js';
|
|
103
|
+
export { pullAllWith } from './array/pullAllWith.js';
|
|
103
104
|
export { reduce } from './array/reduce.js';
|
|
104
105
|
export { reduceRight } from './array/reduceRight.js';
|
|
105
106
|
export { reject } from './array/reject.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -1220,28 +1220,13 @@ function partition(source, predicate) {
|
|
|
1220
1220
|
if (!source) {
|
|
1221
1221
|
return [[], []];
|
|
1222
1222
|
}
|
|
1223
|
-
const collection =
|
|
1223
|
+
const collection = isArrayLike(source) ? source : Object.values(source);
|
|
1224
1224
|
predicate = iteratee(predicate);
|
|
1225
1225
|
const matched = [];
|
|
1226
1226
|
const unmatched = [];
|
|
1227
|
-
if (!toSnakeCaseKeys.isArray(source)) {
|
|
1228
|
-
const keys = Object.keys(source);
|
|
1229
|
-
const length = isArrayLike(source) ? source.length : keys.length;
|
|
1230
|
-
for (let i = 0; i < length; i++) {
|
|
1231
|
-
const key = keys[i];
|
|
1232
|
-
const value = source[key];
|
|
1233
|
-
if (predicate(value, key, source)) {
|
|
1234
|
-
matched.push(value);
|
|
1235
|
-
}
|
|
1236
|
-
else {
|
|
1237
|
-
unmatched.push(value);
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
return [matched, unmatched];
|
|
1241
|
-
}
|
|
1242
1227
|
for (let i = 0; i < collection.length; i++) {
|
|
1243
1228
|
const value = collection[i];
|
|
1244
|
-
if (predicate(value
|
|
1229
|
+
if (predicate(value)) {
|
|
1245
1230
|
matched.push(value);
|
|
1246
1231
|
}
|
|
1247
1232
|
else {
|
|
@@ -1278,6 +1263,24 @@ function pullAllBy(arr, valuesToRemove, _getValue) {
|
|
|
1278
1263
|
return arr;
|
|
1279
1264
|
}
|
|
1280
1265
|
|
|
1266
|
+
function pullAllWith(arr, values, comparator) {
|
|
1267
|
+
let resultIndex = 0;
|
|
1268
|
+
for (let i = 0; i < arr.length; i++) {
|
|
1269
|
+
if (!(i in arr)) {
|
|
1270
|
+
if (!values.includes(undefined)) {
|
|
1271
|
+
delete arr[resultIndex++];
|
|
1272
|
+
}
|
|
1273
|
+
continue;
|
|
1274
|
+
}
|
|
1275
|
+
const shouldRemove = values.some(value => comparator(arr[i], value));
|
|
1276
|
+
if (!shouldRemove) {
|
|
1277
|
+
arr[resultIndex++] = arr[i];
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
arr.length = resultIndex;
|
|
1281
|
+
return arr;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1281
1284
|
function reduce(collection, iteratee = unary.identity, accumulator) {
|
|
1282
1285
|
if (!collection) {
|
|
1283
1286
|
return accumulator;
|
|
@@ -3821,6 +3824,7 @@ exports.propertyOf = propertyOf;
|
|
|
3821
3824
|
exports.pull = pull;
|
|
3822
3825
|
exports.pullAll = pullAll;
|
|
3823
3826
|
exports.pullAllBy = pullAllBy;
|
|
3827
|
+
exports.pullAllWith = pullAllWith;
|
|
3824
3828
|
exports.random = random;
|
|
3825
3829
|
exports.range = range;
|
|
3826
3830
|
exports.rangeRight = rangeRight;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -102,6 +102,7 @@ export { partition } from './array/partition.mjs';
|
|
|
102
102
|
export { pull } from './array/pull.mjs';
|
|
103
103
|
export { pullAll } from './array/pullAll.mjs';
|
|
104
104
|
export { pullAllBy } from './array/pullAllBy.mjs';
|
|
105
|
+
export { pullAllWith } from './array/pullAllWith.mjs';
|
|
105
106
|
export { reduce } from './array/reduce.mjs';
|
|
106
107
|
export { reduceRight } from './array/reduceRight.mjs';
|
|
107
108
|
export { reject } from './array/reject.mjs';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
3
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
4
|
-
"version": "1.35.0-dev.
|
|
4
|
+
"version": "1.35.0-dev.1178+028c36e4",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|