es-toolkit 1.21.0-dev.683 → 1.21.0-dev.684
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/filter.d.mts +118 -0
- package/dist/compat/array/filter.d.ts +118 -0
- package/dist/compat/array/filter.mjs +27 -0
- package/dist/compat/index.d.mts +1 -0
- package/dist/compat/index.d.ts +1 -0
- package/dist/compat/index.js +25 -4
- package/dist/compat/index.mjs +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters items from a array and returns an array of elements.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T[]} arr - The array to iterate over.
|
|
6
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - The function invoked per iteration.
|
|
7
|
+
* @returns {T[]} - Returns a new array of elements that satisfy the given doesMatch function.
|
|
8
|
+
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* filter([1, 2, 3], n => n % 2 === 0)
|
|
12
|
+
* // => [2]
|
|
13
|
+
*/
|
|
14
|
+
declare function filter<T>(arr: readonly T[], doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): T[];
|
|
15
|
+
/**
|
|
16
|
+
* Filters elements in a arr that match the properties of the given partial object.
|
|
17
|
+
*
|
|
18
|
+
* @template T
|
|
19
|
+
* @param {T[]} arr - The array to iterate over.
|
|
20
|
+
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
21
|
+
* @returns {T[]} - Returns a new array of elements that match the given properties.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
25
|
+
* filter(arr, { name: 'Bob' });
|
|
26
|
+
* // => [{ id: 2, name: 'Bob' }]
|
|
27
|
+
*/
|
|
28
|
+
declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
|
|
29
|
+
/**
|
|
30
|
+
* Filters elements in a arr that match the given key-value pair.
|
|
31
|
+
*
|
|
32
|
+
* @template T
|
|
33
|
+
* @param {readonly T[]} arr - The array to iterate over.
|
|
34
|
+
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
35
|
+
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
39
|
+
* filter(arr, ['name', 'Alice']);
|
|
40
|
+
* // => [{ id: 1, name: 'Alice' }]
|
|
41
|
+
*/
|
|
42
|
+
declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T[];
|
|
43
|
+
/**
|
|
44
|
+
* Filters the arr, returning elements that contain the given property name.
|
|
45
|
+
*
|
|
46
|
+
* @template T
|
|
47
|
+
* @param {readonly T[]} arr - The array to iterate over.
|
|
48
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
49
|
+
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, age: 28 }];
|
|
53
|
+
* filter(arr, 'name');
|
|
54
|
+
* // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
55
|
+
*/
|
|
56
|
+
declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
|
|
57
|
+
/**
|
|
58
|
+
* Filters items from a object and returns an array of elements that match the given predicate function.
|
|
59
|
+
*
|
|
60
|
+
* @template T
|
|
61
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
62
|
+
* @param {(item: T[keyof T], index: number, arr: T) => unknown} doesMatch - The function invoked per iteration.
|
|
63
|
+
* @returns {T[]} - Returns a new array of elements that satisfy the given predicate function.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const obj = { item1: { a: 0 }, item2: { a: 1 }, item3: { a: 0 } }
|
|
67
|
+
* filter(obj, items => items.a)
|
|
68
|
+
* // => [{ a: 1 }]
|
|
69
|
+
*
|
|
70
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
71
|
+
* filter(obj, item => item > 2)
|
|
72
|
+
* // => [3]
|
|
73
|
+
*/
|
|
74
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: number, object: T) => unknown): T[];
|
|
75
|
+
/**
|
|
76
|
+
* Filters elements in a object that match the properties of the given partial object.
|
|
77
|
+
*
|
|
78
|
+
* @template T
|
|
79
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
80
|
+
* @param {Partial<T[keyof T]>} doesMatch - The partial object to match
|
|
81
|
+
* @returns {T[]} - Returns a new array of elements that match the given properties.pair.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
|
|
85
|
+
* filter(obj, { name: 'Bob' });
|
|
86
|
+
* // => [{ id: 2, name: 'Bob' }]
|
|
87
|
+
*/
|
|
88
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T[];
|
|
89
|
+
/**
|
|
90
|
+
* Filters elements in a arr that match the given key-value pair.
|
|
91
|
+
*
|
|
92
|
+
* @template T
|
|
93
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
94
|
+
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
95
|
+
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
|
|
99
|
+
* filter(obj, ['name', 'Alice']);
|
|
100
|
+
* // => [{ id: 1, name: 'Alice' }]
|
|
101
|
+
*/
|
|
102
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T[];
|
|
103
|
+
/**
|
|
104
|
+
* Filters the object, returning elements that contain the given property name.
|
|
105
|
+
*
|
|
106
|
+
* @template T
|
|
107
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
108
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
109
|
+
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, age: 28 } };
|
|
113
|
+
* filter(obj, 'name');
|
|
114
|
+
* // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
115
|
+
*/
|
|
116
|
+
declare function filter<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T[];
|
|
117
|
+
|
|
118
|
+
export { filter };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters items from a array and returns an array of elements.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T[]} arr - The array to iterate over.
|
|
6
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - The function invoked per iteration.
|
|
7
|
+
* @returns {T[]} - Returns a new array of elements that satisfy the given doesMatch function.
|
|
8
|
+
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* filter([1, 2, 3], n => n % 2 === 0)
|
|
12
|
+
* // => [2]
|
|
13
|
+
*/
|
|
14
|
+
declare function filter<T>(arr: readonly T[], doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): T[];
|
|
15
|
+
/**
|
|
16
|
+
* Filters elements in a arr that match the properties of the given partial object.
|
|
17
|
+
*
|
|
18
|
+
* @template T
|
|
19
|
+
* @param {T[]} arr - The array to iterate over.
|
|
20
|
+
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
21
|
+
* @returns {T[]} - Returns a new array of elements that match the given properties.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
25
|
+
* filter(arr, { name: 'Bob' });
|
|
26
|
+
* // => [{ id: 2, name: 'Bob' }]
|
|
27
|
+
*/
|
|
28
|
+
declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
|
|
29
|
+
/**
|
|
30
|
+
* Filters elements in a arr that match the given key-value pair.
|
|
31
|
+
*
|
|
32
|
+
* @template T
|
|
33
|
+
* @param {readonly T[]} arr - The array to iterate over.
|
|
34
|
+
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
35
|
+
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
39
|
+
* filter(arr, ['name', 'Alice']);
|
|
40
|
+
* // => [{ id: 1, name: 'Alice' }]
|
|
41
|
+
*/
|
|
42
|
+
declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T[];
|
|
43
|
+
/**
|
|
44
|
+
* Filters the arr, returning elements that contain the given property name.
|
|
45
|
+
*
|
|
46
|
+
* @template T
|
|
47
|
+
* @param {readonly T[]} arr - The array to iterate over.
|
|
48
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
49
|
+
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* const arr = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, age: 28 }];
|
|
53
|
+
* filter(arr, 'name');
|
|
54
|
+
* // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
55
|
+
*/
|
|
56
|
+
declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
|
|
57
|
+
/**
|
|
58
|
+
* Filters items from a object and returns an array of elements that match the given predicate function.
|
|
59
|
+
*
|
|
60
|
+
* @template T
|
|
61
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
62
|
+
* @param {(item: T[keyof T], index: number, arr: T) => unknown} doesMatch - The function invoked per iteration.
|
|
63
|
+
* @returns {T[]} - Returns a new array of elements that satisfy the given predicate function.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const obj = { item1: { a: 0 }, item2: { a: 1 }, item3: { a: 0 } }
|
|
67
|
+
* filter(obj, items => items.a)
|
|
68
|
+
* // => [{ a: 1 }]
|
|
69
|
+
*
|
|
70
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
71
|
+
* filter(obj, item => item > 2)
|
|
72
|
+
* // => [3]
|
|
73
|
+
*/
|
|
74
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: number, object: T) => unknown): T[];
|
|
75
|
+
/**
|
|
76
|
+
* Filters elements in a object that match the properties of the given partial object.
|
|
77
|
+
*
|
|
78
|
+
* @template T
|
|
79
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
80
|
+
* @param {Partial<T[keyof T]>} doesMatch - The partial object to match
|
|
81
|
+
* @returns {T[]} - Returns a new array of elements that match the given properties.pair.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
|
|
85
|
+
* filter(obj, { name: 'Bob' });
|
|
86
|
+
* // => [{ id: 2, name: 'Bob' }]
|
|
87
|
+
*/
|
|
88
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T[];
|
|
89
|
+
/**
|
|
90
|
+
* Filters elements in a arr that match the given key-value pair.
|
|
91
|
+
*
|
|
92
|
+
* @template T
|
|
93
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
94
|
+
* @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
|
|
95
|
+
* @returns {T[]} - Returns a new array of elements that match the given key-value pair.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const obj = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
|
|
99
|
+
* filter(obj, ['name', 'Alice']);
|
|
100
|
+
* // => [{ id: 1, name: 'Alice' }]
|
|
101
|
+
*/
|
|
102
|
+
declare function filter<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T[];
|
|
103
|
+
/**
|
|
104
|
+
* Filters the object, returning elements that contain the given property name.
|
|
105
|
+
*
|
|
106
|
+
* @template T
|
|
107
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to iterate over.
|
|
108
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
109
|
+
* @returns {T[]} - Returns a new array of elements that match the given property name.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, age: 28 } };
|
|
113
|
+
* filter(obj, 'name');
|
|
114
|
+
* // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
115
|
+
*/
|
|
116
|
+
declare function filter<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T[];
|
|
117
|
+
|
|
118
|
+
export { filter };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { identity } from '../_internal/identity.mjs';
|
|
2
|
+
import { property } from '../object/property.mjs';
|
|
3
|
+
import { isArray } from '../predicate/isArray.mjs';
|
|
4
|
+
import { matches } from '../predicate/matches.mjs';
|
|
5
|
+
import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|
6
|
+
|
|
7
|
+
function filter(source, predicate) {
|
|
8
|
+
if (!predicate) {
|
|
9
|
+
predicate = identity;
|
|
10
|
+
}
|
|
11
|
+
const collection = isArray(source) ? source : Object.values(source);
|
|
12
|
+
switch (typeof predicate) {
|
|
13
|
+
case 'function': {
|
|
14
|
+
return collection.filter(predicate);
|
|
15
|
+
}
|
|
16
|
+
case 'object': {
|
|
17
|
+
return isArray(predicate)
|
|
18
|
+
? collection.filter(matchesProperty(predicate[0], predicate[1]))
|
|
19
|
+
: collection.filter(matches(predicate));
|
|
20
|
+
}
|
|
21
|
+
case 'string': {
|
|
22
|
+
return collection.filter(property(predicate));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { filter };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -113,6 +113,7 @@ export { sortBy } from './array/sortBy.mjs';
|
|
|
113
113
|
export { size } from './array/size.mjs';
|
|
114
114
|
export { some } from './array/some.mjs';
|
|
115
115
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
116
|
+
export { filter } from './array/filter.mjs';
|
|
116
117
|
export { ary } from './function/ary.mjs';
|
|
117
118
|
export { bind } from './function/bind.mjs';
|
|
118
119
|
export { bindKey } from './function/bindKey.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -113,6 +113,7 @@ export { sortBy } from './array/sortBy.js';
|
|
|
113
113
|
export { size } from './array/size.js';
|
|
114
114
|
export { some } from './array/some.js';
|
|
115
115
|
export { zipObjectDeep } from './array/zipObjectDeep.js';
|
|
116
|
+
export { filter } from './array/filter.js';
|
|
116
117
|
export { ary } from './function/ary.js';
|
|
117
118
|
export { bind } from './function/bind.js';
|
|
118
119
|
export { bindKey } from './function/bindKey.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -787,6 +787,30 @@ function zipObjectDeep(keys, values) {
|
|
|
787
787
|
return result;
|
|
788
788
|
}
|
|
789
789
|
|
|
790
|
+
function isArray(value) {
|
|
791
|
+
return Array.isArray(value);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
function filter(source, predicate) {
|
|
795
|
+
if (!predicate) {
|
|
796
|
+
predicate = identity;
|
|
797
|
+
}
|
|
798
|
+
const collection = isArray(source) ? source : Object.values(source);
|
|
799
|
+
switch (typeof predicate) {
|
|
800
|
+
case 'function': {
|
|
801
|
+
return collection.filter(predicate);
|
|
802
|
+
}
|
|
803
|
+
case 'object': {
|
|
804
|
+
return isArray(predicate)
|
|
805
|
+
? collection.filter(matchesProperty(predicate[0], predicate[1]))
|
|
806
|
+
: collection.filter(matches(predicate));
|
|
807
|
+
}
|
|
808
|
+
case 'string': {
|
|
809
|
+
return collection.filter(property(predicate));
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
790
814
|
function ary(func, n = func.length, guard) {
|
|
791
815
|
if (guard) {
|
|
792
816
|
n = func.length;
|
|
@@ -1299,10 +1323,6 @@ function fromPairs(pairs) {
|
|
|
1299
1323
|
return result;
|
|
1300
1324
|
}
|
|
1301
1325
|
|
|
1302
|
-
function isArray(value) {
|
|
1303
|
-
return Array.isArray(value);
|
|
1304
|
-
}
|
|
1305
|
-
|
|
1306
1326
|
function isObject(value) {
|
|
1307
1327
|
return value !== null && (typeof value === 'object' || typeof value === 'function');
|
|
1308
1328
|
}
|
|
@@ -1836,6 +1856,7 @@ exports.dropWhile = dropWhile;
|
|
|
1836
1856
|
exports.endsWith = endsWith;
|
|
1837
1857
|
exports.every = every;
|
|
1838
1858
|
exports.fill = fill;
|
|
1859
|
+
exports.filter = filter;
|
|
1839
1860
|
exports.find = find;
|
|
1840
1861
|
exports.findIndex = findIndex;
|
|
1841
1862
|
exports.findLastIndex = findLastIndex;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -114,6 +114,7 @@ export { sortBy } from './array/sortBy.mjs';
|
|
|
114
114
|
export { size } from './array/size.mjs';
|
|
115
115
|
export { some } from './array/some.mjs';
|
|
116
116
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
117
|
+
export { filter } from './array/filter.mjs';
|
|
117
118
|
export { ary } from './function/ary.mjs';
|
|
118
119
|
export { bind } from './function/bind.mjs';
|
|
119
120
|
export { bindKey } from './function/bindKey.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.21.0-dev.
|
|
4
|
+
"version": "1.21.0-dev.684+6e3226e2",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|