es-toolkit 1.35.0-dev.1174 → 1.35.0-dev.1176

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.
@@ -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
- * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
8
- * @param {(item: T, index: number, arr: readonly T[]) => unknown} doesMatch - The function invoked per iteration.
9
- * @returns {[T[], T[]]} - Returns the array of grouped elements.
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
- * @template T
20
- * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
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
- * @example
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>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): [T[], 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
- * that have the given property name, the second of which contains elements that don't.
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} object - The object to iterate over.
64
- * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch - The function invoked per iteration.
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
- * const obj = { item1: { a: 0 }, item2: { a: 1 }, item3: { a: 0 } }
69
- * partition(obj, value => value.a)
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
- * @template T
81
- * @param {T | null | undefined} object - The object to iterate over.
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
- * @example
86
- * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
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 extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): [T[], 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 based on the given key-value pair.
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]} doesMatchProperty - The key-value pair to match.
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
- * const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
101
- * partition(obj, ['name', 'Alice']);
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
- * @template T
110
- * @param {T | null | undefined} object - The object to iterate over.
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
- * @example
115
- * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, age: 28 } };
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 Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey): [T[], T[]];
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
- * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
8
- * @param {(item: T, index: number, arr: readonly T[]) => unknown} doesMatch - The function invoked per iteration.
9
- * @returns {[T[], T[]]} - Returns the array of grouped elements.
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
- * @template T
20
- * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
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
- * @example
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>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): [T[], 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
- * that have the given property name, the second of which contains elements that don't.
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} object - The object to iterate over.
64
- * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch - The function invoked per iteration.
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
- * const obj = { item1: { a: 0 }, item2: { a: 1 }, item3: { a: 0 } }
69
- * partition(obj, value => value.a)
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
- * @template T
81
- * @param {T | null | undefined} object - The object to iterate over.
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
- * @example
86
- * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
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 extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): [T[], 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 based on the given key-value pair.
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]} doesMatchProperty - The key-value pair to match.
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
- * const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
101
- * partition(obj, ['name', 'Alice']);
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
- * @template T
110
- * @param {T | null | undefined} object - The object to iterate over.
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
- * @example
115
- * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, age: 28 } };
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 Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey): [T[], T[]];
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 = isArray(source) ? source : Object.values(source);
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, i, source)) {
14
+ if (predicate(value)) {
31
15
  matched.push(value);
32
16
  }
33
17
  else {
@@ -1220,28 +1220,13 @@ function partition(source, predicate) {
1220
1220
  if (!source) {
1221
1221
  return [[], []];
1222
1222
  }
1223
- const collection = toSnakeCaseKeys.isArray(source) ? source : Object.values(source);
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, i, source)) {
1229
+ if (predicate(value)) {
1245
1230
  matched.push(value);
1246
1231
  }
1247
1232
  else {
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.1174+b82b79e5",
4
+ "version": "1.35.0-dev.1176+c74c4b39",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {