es-toolkit 1.25.2-dev.803 → 1.25.2-dev.805

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.
@@ -57,7 +57,7 @@ declare function filter<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck
57
57
  * Filters items from a object and returns an array of elements that match the given predicate function.
58
58
  *
59
59
  * @template T
60
- * @param {T} object - The object to iterate over.
60
+ * @param {T | null | undefined} object - The object to iterate over.
61
61
  * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch - The function invoked per iteration.
62
62
  * @returns {T[]} - Returns a new array of elements that satisfy the given predicate function.
63
63
  *
@@ -57,7 +57,7 @@ declare function filter<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck
57
57
  * Filters items from a object and returns an array of elements that match the given predicate function.
58
58
  *
59
59
  * @template T
60
- * @param {T} object - The object to iterate over.
60
+ * @param {T | null | undefined} object - The object to iterate over.
61
61
  * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch - The function invoked per iteration.
62
62
  * @returns {T[]} - Returns a new array of elements that satisfy the given predicate function.
63
63
  *
@@ -1,30 +1,33 @@
1
1
  /**
2
2
  * Checks if there is an element in an array that is truthy.
3
3
  *
4
- * @param {T[]} arr The array to iterate over.
4
+ * @template T
5
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
5
6
  * @returns {boolean} Returns `true` if any element is truthy, else `false`.
6
7
  *
7
8
  * @example
8
9
  * some([1, 2, 3, 4]);
9
10
  * // => true
10
11
  */
11
- declare function some<T>(arr: readonly T[]): boolean;
12
+ declare function some<T>(arr: ArrayLike<T> | null | undefined): boolean;
12
13
  /**
13
14
  * Checks if there is an element in an array that matches the given predicate function.
14
15
  *
15
- * @param {T[]} arr The array to iterate over.
16
- * @param {(item: T, index: number, arr: any) => unknown} predicate The function invoked per iteration.
16
+ * @template T
17
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
18
+ * @param {(item: T, index: number, arr: readonly T[]) => unknown} predicate The function invoked per iteration.
17
19
  * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
18
20
  *
19
21
  * @example
20
22
  * some([1, 2, 3, 4], n => n % 2 === 0);
21
23
  * // => true
22
24
  */
23
- declare function some<T>(arr: readonly T[], predicate: (item: T, index: number, arr: any) => unknown): boolean;
25
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, predicate: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
24
26
  /**
25
27
  * Checks if there is an element in an array that matches the given key-value pair.
26
28
  *
27
- * @param {T[]} arr The array to iterate over.
29
+ * @template T
30
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
28
31
  * @param {[keyof T, unknown]} predicate The key-value pair to match.
29
32
  * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
30
33
  *
@@ -32,11 +35,12 @@ declare function some<T>(arr: readonly T[], predicate: (item: T, index: number,
32
35
  * some([{ a: 1 }, { a: 2 }, { a: 3 }], ['a', 2]);
33
36
  * // => true
34
37
  */
35
- declare function some<T>(arr: readonly T[], predicate: [keyof T, unknown]): boolean;
38
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, predicate: [keyof T, unknown]): boolean;
36
39
  /**
37
40
  * Checks if there is an element in an array that has a truthy value for the given property name.
38
41
  *
39
- * @param {T[]} arr The array to iterate over.
42
+ * @template T
43
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
40
44
  * @param {string} propertyToCheck The property name to check.
41
45
  * @returns {boolean} Returns `true` if any element has a truthy value for the property, else `false`.
42
46
  *
@@ -44,11 +48,12 @@ declare function some<T>(arr: readonly T[], predicate: [keyof T, unknown]): bool
44
48
  * some([{ a: 1 }, { a: 2 }, { a: 3 }], 'a');
45
49
  * // => true
46
50
  */
47
- declare function some<T>(arr: readonly T[], propertyToCheck: string): boolean;
51
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): boolean;
48
52
  /**
49
53
  * Checks if there is an element in an array that matches the given partial object.
50
54
  *
51
- * @param {T[]} arr The array to iterate over.
55
+ * @template T
56
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
52
57
  * @param {Partial<T>} doesMatch The partial object to match.
53
58
  * @returns {boolean} Returns `true` if any element matches the partial object, else `false`.
54
59
  *
@@ -56,30 +61,77 @@ declare function some<T>(arr: readonly T[], propertyToCheck: string): boolean;
56
61
  * some([{ a: 1 }, { a: 2 }, { a: 3 }], { a: 2 });
57
62
  * // => true
58
63
  */
59
- declare function some<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
64
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): boolean;
60
65
  /**
61
- * Checks if there is an element in an array that matches the given predicate.
62
66
  *
63
- * Iteration is stopped once there is an element that matches `predicate`.
67
+ * Checks if there is an element in an object that matches the given predicate function.
64
68
  *
65
- * @param {T[]} arr The array to iterate over.
66
- * @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | string} [predicate=identity] The function invoked per iteration.
67
- * If a property name or an object is provided it will be used to create a predicate function.
69
+ * @template T
70
+ * @param {T | null | undefined} object The object to iterate over.
71
+ * @returns {boolean} Returns `true` if any element is truthy, else `false`.
72
+ *
73
+ * @example
74
+ * some({ a: 1, b: 2, c: 3 });
75
+ * // => true
76
+ */
77
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined): boolean;
78
+ /**
79
+ *
80
+ * Checks if there is an element in an object that matches the given predicate function.
81
+ *
82
+ * @template T
83
+ * @param {T | null | undefined} object The object to iterate over.
84
+ * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch A function that takes an value, its key, and the object, and returns a truthy value if the item matches the criteria.
68
85
  * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
69
86
  *
70
87
  * @example
71
- * some([1, 2, 3, 4], n => n % 2 === 0);
88
+ * some({ a: 1, b: 2, c: 3 }, n => n % 2 === 0);
72
89
  * // => true
90
+ */
91
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): boolean;
92
+ /**
73
93
  *
74
- * some([{ a: 1 }, { a: 2 }, { a: 3 }], { a: 2 });
94
+ * Checks if there is an element in an object that matches the given partial value.
95
+ *
96
+ * @template T
97
+ * @param {T | null | undefined} object The object to iterate over.
98
+ * @param {Partial<T[keyof T]>} doesMatch A partial value to match against the values of the object.
99
+ * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
100
+ *
101
+ * @example
102
+ * some({ a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } }, { name: 'Bob' });
75
103
  * // => true
104
+ */
105
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): boolean;
106
+ /**
76
107
  *
77
- * some([{ a: 1 }, { a: 2 }, { a: 3 }], ['a', 2]);
108
+ * Checks if there is an element in an object that matches a property with a specific value.
109
+ *
110
+ * @template T
111
+ * @param {T | null | undefined} object The object to iterate over.
112
+ * @param {[keyof T, unknown]} doesMatchProperty An array where the first element is the property key and the second element is the value to match.
113
+ * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
114
+ *
115
+ * @example
116
+ * const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
117
+ * const result = some(obj, ['name', 'Alice']);
78
118
  * // => true
119
+ */
120
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown]): boolean;
121
+ /**
79
122
  *
80
- * some([{ a: 1 }, { a: 2 }, { a: 3 }], 'a');
123
+ * Checks if there is an element in an object that has a specific property, where the property name is provided as a string.
124
+ *
125
+ * @template T
126
+ * @param {T | null | undefined} object The object to iterate over.
127
+ * @param {string} propertyToCheck The property name to check.
128
+ * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
129
+ *
130
+ * @example
131
+ * const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
132
+ * const result = some(obj, 'name');
81
133
  * // => true
82
134
  */
83
- declare function some<T>(arr: readonly T[] | null | undefined, predicate?: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | string, guard?: unknown): boolean;
135
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): boolean;
84
136
 
85
137
  export { some };
@@ -1,30 +1,33 @@
1
1
  /**
2
2
  * Checks if there is an element in an array that is truthy.
3
3
  *
4
- * @param {T[]} arr The array to iterate over.
4
+ * @template T
5
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
5
6
  * @returns {boolean} Returns `true` if any element is truthy, else `false`.
6
7
  *
7
8
  * @example
8
9
  * some([1, 2, 3, 4]);
9
10
  * // => true
10
11
  */
11
- declare function some<T>(arr: readonly T[]): boolean;
12
+ declare function some<T>(arr: ArrayLike<T> | null | undefined): boolean;
12
13
  /**
13
14
  * Checks if there is an element in an array that matches the given predicate function.
14
15
  *
15
- * @param {T[]} arr The array to iterate over.
16
- * @param {(item: T, index: number, arr: any) => unknown} predicate The function invoked per iteration.
16
+ * @template T
17
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
18
+ * @param {(item: T, index: number, arr: readonly T[]) => unknown} predicate The function invoked per iteration.
17
19
  * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
18
20
  *
19
21
  * @example
20
22
  * some([1, 2, 3, 4], n => n % 2 === 0);
21
23
  * // => true
22
24
  */
23
- declare function some<T>(arr: readonly T[], predicate: (item: T, index: number, arr: any) => unknown): boolean;
25
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, predicate: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
24
26
  /**
25
27
  * Checks if there is an element in an array that matches the given key-value pair.
26
28
  *
27
- * @param {T[]} arr The array to iterate over.
29
+ * @template T
30
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
28
31
  * @param {[keyof T, unknown]} predicate The key-value pair to match.
29
32
  * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
30
33
  *
@@ -32,11 +35,12 @@ declare function some<T>(arr: readonly T[], predicate: (item: T, index: number,
32
35
  * some([{ a: 1 }, { a: 2 }, { a: 3 }], ['a', 2]);
33
36
  * // => true
34
37
  */
35
- declare function some<T>(arr: readonly T[], predicate: [keyof T, unknown]): boolean;
38
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, predicate: [keyof T, unknown]): boolean;
36
39
  /**
37
40
  * Checks if there is an element in an array that has a truthy value for the given property name.
38
41
  *
39
- * @param {T[]} arr The array to iterate over.
42
+ * @template T
43
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
40
44
  * @param {string} propertyToCheck The property name to check.
41
45
  * @returns {boolean} Returns `true` if any element has a truthy value for the property, else `false`.
42
46
  *
@@ -44,11 +48,12 @@ declare function some<T>(arr: readonly T[], predicate: [keyof T, unknown]): bool
44
48
  * some([{ a: 1 }, { a: 2 }, { a: 3 }], 'a');
45
49
  * // => true
46
50
  */
47
- declare function some<T>(arr: readonly T[], propertyToCheck: string): boolean;
51
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): boolean;
48
52
  /**
49
53
  * Checks if there is an element in an array that matches the given partial object.
50
54
  *
51
- * @param {T[]} arr The array to iterate over.
55
+ * @template T
56
+ * @param {ArrayLike<T> | null | undefined} arr The array to iterate over.
52
57
  * @param {Partial<T>} doesMatch The partial object to match.
53
58
  * @returns {boolean} Returns `true` if any element matches the partial object, else `false`.
54
59
  *
@@ -56,30 +61,77 @@ declare function some<T>(arr: readonly T[], propertyToCheck: string): boolean;
56
61
  * some([{ a: 1 }, { a: 2 }, { a: 3 }], { a: 2 });
57
62
  * // => true
58
63
  */
59
- declare function some<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
64
+ declare function some<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): boolean;
60
65
  /**
61
- * Checks if there is an element in an array that matches the given predicate.
62
66
  *
63
- * Iteration is stopped once there is an element that matches `predicate`.
67
+ * Checks if there is an element in an object that matches the given predicate function.
64
68
  *
65
- * @param {T[]} arr The array to iterate over.
66
- * @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | string} [predicate=identity] The function invoked per iteration.
67
- * If a property name or an object is provided it will be used to create a predicate function.
69
+ * @template T
70
+ * @param {T | null | undefined} object The object to iterate over.
71
+ * @returns {boolean} Returns `true` if any element is truthy, else `false`.
72
+ *
73
+ * @example
74
+ * some({ a: 1, b: 2, c: 3 });
75
+ * // => true
76
+ */
77
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined): boolean;
78
+ /**
79
+ *
80
+ * Checks if there is an element in an object that matches the given predicate function.
81
+ *
82
+ * @template T
83
+ * @param {T | null | undefined} object The object to iterate over.
84
+ * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch A function that takes an value, its key, and the object, and returns a truthy value if the item matches the criteria.
68
85
  * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
69
86
  *
70
87
  * @example
71
- * some([1, 2, 3, 4], n => n % 2 === 0);
88
+ * some({ a: 1, b: 2, c: 3 }, n => n % 2 === 0);
72
89
  * // => true
90
+ */
91
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): boolean;
92
+ /**
73
93
  *
74
- * some([{ a: 1 }, { a: 2 }, { a: 3 }], { a: 2 });
94
+ * Checks if there is an element in an object that matches the given partial value.
95
+ *
96
+ * @template T
97
+ * @param {T | null | undefined} object The object to iterate over.
98
+ * @param {Partial<T[keyof T]>} doesMatch A partial value to match against the values of the object.
99
+ * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
100
+ *
101
+ * @example
102
+ * some({ a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } }, { name: 'Bob' });
75
103
  * // => true
104
+ */
105
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): boolean;
106
+ /**
76
107
  *
77
- * some([{ a: 1 }, { a: 2 }, { a: 3 }], ['a', 2]);
108
+ * Checks if there is an element in an object that matches a property with a specific value.
109
+ *
110
+ * @template T
111
+ * @param {T | null | undefined} object The object to iterate over.
112
+ * @param {[keyof T, unknown]} doesMatchProperty An array where the first element is the property key and the second element is the value to match.
113
+ * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
114
+ *
115
+ * @example
116
+ * const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
117
+ * const result = some(obj, ['name', 'Alice']);
78
118
  * // => true
119
+ */
120
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown]): boolean;
121
+ /**
79
122
  *
80
- * some([{ a: 1 }, { a: 2 }, { a: 3 }], 'a');
123
+ * Checks if there is an element in an object that has a specific property, where the property name is provided as a string.
124
+ *
125
+ * @template T
126
+ * @param {T | null | undefined} object The object to iterate over.
127
+ * @param {string} propertyToCheck The property name to check.
128
+ * @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
129
+ *
130
+ * @example
131
+ * const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
132
+ * const result = some(obj, 'name');
81
133
  * // => true
82
134
  */
83
- declare function some<T>(arr: readonly T[] | null | undefined, predicate?: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | string, guard?: unknown): boolean;
135
+ declare function some<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): boolean;
84
136
 
85
137
  export { some };
@@ -3,32 +3,44 @@ import { property } from '../object/property.mjs';
3
3
  import { matches } from '../predicate/matches.mjs';
4
4
  import { matchesProperty } from '../predicate/matchesProperty.mjs';
5
5
 
6
- function some(arr, predicate, guard) {
6
+ function some(source, predicate, guard) {
7
+ if (!source) {
8
+ return false;
9
+ }
7
10
  if (guard != null) {
8
11
  predicate = undefined;
9
12
  }
10
13
  if (!predicate) {
11
14
  predicate = identity;
12
15
  }
13
- if (!Array.isArray(arr)) {
14
- return false;
15
- }
16
+ const values = Array.isArray(source) ? source : Object.values(source);
16
17
  switch (typeof predicate) {
17
18
  case 'function': {
18
- return arr.some(predicate);
19
+ if (!Array.isArray(source)) {
20
+ const keys = Object.keys(source);
21
+ for (let i = 0; i < keys.length; i++) {
22
+ const key = keys[i];
23
+ const value = source[key];
24
+ if (predicate(value, key, source)) {
25
+ return true;
26
+ }
27
+ }
28
+ return false;
29
+ }
30
+ return values.some(predicate);
19
31
  }
20
32
  case 'object': {
21
33
  if (Array.isArray(predicate) && predicate.length === 2) {
22
34
  const key = predicate[0];
23
35
  const value = predicate[1];
24
- return arr.some(matchesProperty(key, value));
36
+ return values.some(matchesProperty(key, value));
25
37
  }
26
38
  else {
27
- return arr.some(matches(predicate));
39
+ return values.some(matches(predicate));
28
40
  }
29
41
  }
30
42
  case 'string': {
31
- return arr.some(property(predicate));
43
+ return values.some(property(predicate));
32
44
  }
33
45
  }
34
46
  }
@@ -1009,32 +1009,44 @@ function slice(array, start, end) {
1009
1009
  return result;
1010
1010
  }
1011
1011
 
1012
- function some(arr, predicate, guard) {
1012
+ function some(source, predicate, guard) {
1013
+ if (!source) {
1014
+ return false;
1015
+ }
1013
1016
  if (guard != null) {
1014
1017
  predicate = undefined;
1015
1018
  }
1016
1019
  if (!predicate) {
1017
1020
  predicate = identity;
1018
1021
  }
1019
- if (!Array.isArray(arr)) {
1020
- return false;
1021
- }
1022
+ const values = Array.isArray(source) ? source : Object.values(source);
1022
1023
  switch (typeof predicate) {
1023
1024
  case 'function': {
1024
- return arr.some(predicate);
1025
+ if (!Array.isArray(source)) {
1026
+ const keys = Object.keys(source);
1027
+ for (let i = 0; i < keys.length; i++) {
1028
+ const key = keys[i];
1029
+ const value = source[key];
1030
+ if (predicate(value, key, source)) {
1031
+ return true;
1032
+ }
1033
+ }
1034
+ return false;
1035
+ }
1036
+ return values.some(predicate);
1025
1037
  }
1026
1038
  case 'object': {
1027
1039
  if (Array.isArray(predicate) && predicate.length === 2) {
1028
1040
  const key = predicate[0];
1029
1041
  const value = predicate[1];
1030
- return arr.some(matchesProperty(key, value));
1042
+ return values.some(matchesProperty(key, value));
1031
1043
  }
1032
1044
  else {
1033
- return arr.some(matches(predicate));
1045
+ return values.some(matches(predicate));
1034
1046
  }
1035
1047
  }
1036
1048
  case 'string': {
1037
- return arr.some(property(predicate));
1049
+ return values.some(property(predicate));
1038
1050
  }
1039
1051
  }
1040
1052
  }
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.25.2-dev.803+aaf5dd1c",
4
+ "version": "1.25.2-dev.805+e4fd912b",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {