es-toolkit 1.22.0-dev.694 → 1.22.0-dev.695
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.
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @param {T[]} arr - The array to iterate over.
|
|
6
6
|
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - The function invoked per iteration.
|
|
7
7
|
* @returns {T[]} - Returns a new array of elements that satisfy the given doesMatch function.
|
|
8
|
-
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
11
10
|
* filter([1, 2, 3], n => n % 2 === 0)
|
|
@@ -30,7 +29,7 @@ declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
|
|
|
30
29
|
* Filters elements in a arr that match the given key-value pair.
|
|
31
30
|
*
|
|
32
31
|
* @template T
|
|
33
|
-
* @param {
|
|
32
|
+
* @param {T[]} arr - The array to iterate over.
|
|
34
33
|
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
35
34
|
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
36
35
|
*
|
|
@@ -44,7 +43,7 @@ declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unkno
|
|
|
44
43
|
* Filters the arr, returning elements that contain the given property name.
|
|
45
44
|
*
|
|
46
45
|
* @template T
|
|
47
|
-
* @param {
|
|
46
|
+
* @param {T[]} arr - The array to iterate over.
|
|
48
47
|
* @param {string} propertyToCheck - The property name to check.
|
|
49
48
|
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
50
49
|
*
|
|
@@ -58,25 +57,25 @@ declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
|
|
|
58
57
|
* Filters items from a object and returns an array of elements that match the given predicate function.
|
|
59
58
|
*
|
|
60
59
|
* @template T
|
|
61
|
-
* @param {T
|
|
62
|
-
* @param {(
|
|
60
|
+
* @param {T} object - The object to iterate over.
|
|
61
|
+
* @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch - The function invoked per iteration.
|
|
63
62
|
* @returns {T[]} - Returns a new array of elements that satisfy the given predicate function.
|
|
64
63
|
*
|
|
65
64
|
* @example
|
|
66
65
|
* const obj = { item1: { a: 0 }, item2: { a: 1 }, item3: { a: 0 } }
|
|
67
|
-
* filter(obj,
|
|
66
|
+
* filter(obj, value => value.a)
|
|
68
67
|
* // => [{ a: 1 }]
|
|
69
68
|
*
|
|
70
69
|
* const obj = { a: 1, b: 2, c: 3 };
|
|
71
|
-
* filter(obj,
|
|
70
|
+
* filter(obj, value => value > 2)
|
|
72
71
|
* // => [3]
|
|
73
72
|
*/
|
|
74
|
-
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (
|
|
73
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): T[];
|
|
75
74
|
/**
|
|
76
75
|
* Filters elements in a object that match the properties of the given partial object.
|
|
77
76
|
*
|
|
78
77
|
* @template T
|
|
79
|
-
* @param {T
|
|
78
|
+
* @param {T} object - The object to iterate over.
|
|
80
79
|
* @param {Partial<T[keyof T]>} doesMatch - The partial object to match
|
|
81
80
|
* @returns {T[]} - Returns a new array of elements that match the given properties.pair.
|
|
82
81
|
*
|
|
@@ -90,7 +89,7 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatch:
|
|
|
90
89
|
* Filters elements in a arr that match the given key-value pair.
|
|
91
90
|
*
|
|
92
91
|
* @template T
|
|
93
|
-
* @param {T
|
|
92
|
+
* @param {T} object - The object to iterate over.
|
|
94
93
|
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
95
94
|
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
96
95
|
*
|
|
@@ -104,7 +103,7 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatchP
|
|
|
104
103
|
* Filters the object, returning elements that contain the given property name.
|
|
105
104
|
*
|
|
106
105
|
* @template T
|
|
107
|
-
* @param {T
|
|
106
|
+
* @param {T} object - The object to iterate over.
|
|
108
107
|
* @param {string} propertyToCheck - The property name to check.
|
|
109
108
|
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
110
109
|
*
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @param {T[]} arr - The array to iterate over.
|
|
6
6
|
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - The function invoked per iteration.
|
|
7
7
|
* @returns {T[]} - Returns a new array of elements that satisfy the given doesMatch function.
|
|
8
|
-
|
|
9
8
|
*
|
|
10
9
|
* @example
|
|
11
10
|
* filter([1, 2, 3], n => n % 2 === 0)
|
|
@@ -30,7 +29,7 @@ declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
|
|
|
30
29
|
* Filters elements in a arr that match the given key-value pair.
|
|
31
30
|
*
|
|
32
31
|
* @template T
|
|
33
|
-
* @param {
|
|
32
|
+
* @param {T[]} arr - The array to iterate over.
|
|
34
33
|
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
35
34
|
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
36
35
|
*
|
|
@@ -44,7 +43,7 @@ declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unkno
|
|
|
44
43
|
* Filters the arr, returning elements that contain the given property name.
|
|
45
44
|
*
|
|
46
45
|
* @template T
|
|
47
|
-
* @param {
|
|
46
|
+
* @param {T[]} arr - The array to iterate over.
|
|
48
47
|
* @param {string} propertyToCheck - The property name to check.
|
|
49
48
|
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
50
49
|
*
|
|
@@ -58,25 +57,25 @@ declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
|
|
|
58
57
|
* Filters items from a object and returns an array of elements that match the given predicate function.
|
|
59
58
|
*
|
|
60
59
|
* @template T
|
|
61
|
-
* @param {T
|
|
62
|
-
* @param {(
|
|
60
|
+
* @param {T} object - The object to iterate over.
|
|
61
|
+
* @param {(value: T[keyof T], key: keyof T, object: T) => unknown} doesMatch - The function invoked per iteration.
|
|
63
62
|
* @returns {T[]} - Returns a new array of elements that satisfy the given predicate function.
|
|
64
63
|
*
|
|
65
64
|
* @example
|
|
66
65
|
* const obj = { item1: { a: 0 }, item2: { a: 1 }, item3: { a: 0 } }
|
|
67
|
-
* filter(obj,
|
|
66
|
+
* filter(obj, value => value.a)
|
|
68
67
|
* // => [{ a: 1 }]
|
|
69
68
|
*
|
|
70
69
|
* const obj = { a: 1, b: 2, c: 3 };
|
|
71
|
-
* filter(obj,
|
|
70
|
+
* filter(obj, value => value > 2)
|
|
72
71
|
* // => [3]
|
|
73
72
|
*/
|
|
74
|
-
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (
|
|
73
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): T[];
|
|
75
74
|
/**
|
|
76
75
|
* Filters elements in a object that match the properties of the given partial object.
|
|
77
76
|
*
|
|
78
77
|
* @template T
|
|
79
|
-
* @param {T
|
|
78
|
+
* @param {T} object - The object to iterate over.
|
|
80
79
|
* @param {Partial<T[keyof T]>} doesMatch - The partial object to match
|
|
81
80
|
* @returns {T[]} - Returns a new array of elements that match the given properties.pair.
|
|
82
81
|
*
|
|
@@ -90,7 +89,7 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatch:
|
|
|
90
89
|
* Filters elements in a arr that match the given key-value pair.
|
|
91
90
|
*
|
|
92
91
|
* @template T
|
|
93
|
-
* @param {T
|
|
92
|
+
* @param {T} object - The object to iterate over.
|
|
94
93
|
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
95
94
|
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
96
95
|
*
|
|
@@ -104,7 +103,7 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatchP
|
|
|
104
103
|
* Filters the object, returning elements that contain the given property name.
|
|
105
104
|
*
|
|
106
105
|
* @template T
|
|
107
|
-
* @param {T
|
|
106
|
+
* @param {T} object - The object to iterate over.
|
|
108
107
|
* @param {string} propertyToCheck - The property name to check.
|
|
109
108
|
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
110
109
|
*
|
|
@@ -11,6 +11,19 @@ function filter(source, predicate) {
|
|
|
11
11
|
const collection = isArray(source) ? source : Object.values(source);
|
|
12
12
|
switch (typeof predicate) {
|
|
13
13
|
case 'function': {
|
|
14
|
+
if (!Array.isArray(source)) {
|
|
15
|
+
const result = [];
|
|
16
|
+
const entries = Object.entries(source);
|
|
17
|
+
for (let i = 0; i < entries.length; i++) {
|
|
18
|
+
const entry = entries[i];
|
|
19
|
+
const key = entry[0];
|
|
20
|
+
const value = entry[1];
|
|
21
|
+
if (predicate(value, key, source)) {
|
|
22
|
+
result.push(value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
14
27
|
return collection.filter(predicate);
|
|
15
28
|
}
|
|
16
29
|
case 'object': {
|
package/dist/compat/index.js
CHANGED
|
@@ -872,6 +872,19 @@ function filter(source, predicate) {
|
|
|
872
872
|
const collection = isArray(source) ? source : Object.values(source);
|
|
873
873
|
switch (typeof predicate) {
|
|
874
874
|
case 'function': {
|
|
875
|
+
if (!Array.isArray(source)) {
|
|
876
|
+
const result = [];
|
|
877
|
+
const entries = Object.entries(source);
|
|
878
|
+
for (let i = 0; i < entries.length; i++) {
|
|
879
|
+
const entry = entries[i];
|
|
880
|
+
const key = entry[0];
|
|
881
|
+
const value = entry[1];
|
|
882
|
+
if (predicate(value, key, source)) {
|
|
883
|
+
result.push(value);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
return result;
|
|
887
|
+
}
|
|
875
888
|
return collection.filter(predicate);
|
|
876
889
|
}
|
|
877
890
|
case 'object': {
|
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.22.0-dev.
|
|
4
|
+
"version": "1.22.0-dev.695+a093c732",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|