es-toolkit 1.21.0 → 1.22.0-dev.690
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/CHANGELOG.md +13 -0
- package/dist/_chunk/{rest-pUyjvl.js → flowRight-BzdOZX.js} +16 -0
- package/dist/_chunk/{isWeakSet-1xFSnK.js → isWeakSet-clQklw.js} +10 -0
- package/dist/_chunk/pad-Cw2pvt.js +207 -0
- package/dist/_chunk/{toMerged-D-sLFv.js → toMerged-2WPeoI.js} +5 -2
- package/dist/_chunk/{zipWith-DEcUS4.js → zipWith-EOU_KZ.js} +1 -1
- package/dist/array/dropWhile.d.mts +1 -1
- package/dist/array/dropWhile.d.ts +1 -1
- package/dist/array/dropWhile.mjs +1 -1
- package/dist/array/index.js +1 -1
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/dropWhile.d.mts +40 -0
- package/dist/compat/array/dropWhile.d.ts +40 -0
- package/dist/compat/array/dropWhile.mjs +27 -0
- package/dist/compat/array/every.d.mts +139 -0
- package/dist/compat/array/every.d.ts +139 -0
- package/dist/compat/array/every.mjs +49 -0
- 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/array/find.d.mts +1 -1
- package/dist/compat/array/find.d.ts +1 -1
- package/dist/compat/array/includes.d.mts +62 -0
- package/dist/compat/array/includes.d.ts +62 -0
- package/dist/compat/array/includes.mjs +42 -0
- package/dist/compat/function/flip.d.mts +20 -0
- package/dist/compat/function/flip.d.ts +20 -0
- package/dist/compat/function/flip.mjs +7 -0
- package/dist/compat/index.d.mts +9 -1
- package/dist/compat/index.d.ts +9 -1
- package/dist/compat/index.js +247 -97
- package/dist/compat/index.mjs +9 -1
- package/dist/compat/string/startCase.mjs +16 -2
- package/dist/compat/util/toInteger.d.mts +3 -0
- package/dist/compat/util/toInteger.d.ts +3 -0
- package/dist/function/flow.d.mts +132 -0
- package/dist/function/flow.d.ts +132 -0
- package/dist/function/flow.mjs +11 -0
- package/dist/function/flowRight.d.mts +144 -0
- package/dist/function/flowRight.d.ts +144 -0
- package/dist/function/flowRight.mjs +7 -0
- package/dist/function/index.d.mts +2 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.js +17 -14
- package/dist/function/index.mjs +2 -0
- package/dist/function/throttle.mjs +1 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +39 -34
- package/dist/index.mjs +4 -0
- package/dist/object/clone.mjs +4 -1
- package/dist/object/cloneDeep.mjs +1 -1
- package/dist/object/index.js +1 -1
- package/dist/predicate/index.d.mts +2 -0
- package/dist/predicate/index.d.ts +2 -0
- package/dist/predicate/index.js +3 -1
- package/dist/predicate/index.mjs +2 -0
- package/dist/predicate/isMap.d.mts +20 -0
- package/dist/predicate/isMap.d.ts +20 -0
- package/dist/predicate/isMap.mjs +5 -0
- package/dist/predicate/isSet.d.mts +20 -0
- package/dist/predicate/isSet.d.ts +20 -0
- package/dist/predicate/isSet.mjs +5 -0
- package/dist/string/index.js +20 -204
- package/package.json +2 -2
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drops elements from the beginning of an array while the predicate function returns truthy.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of elements in the array.
|
|
5
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
6
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} canContinueDropping - A predicate function that determines
|
|
7
|
+
* whether to continue dropping elements. The function is called with each element, index, and array, and dropping
|
|
8
|
+
* continues as long as it returns true.
|
|
9
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
10
|
+
*/
|
|
11
|
+
declare function dropWhile<T>(arr: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => unknown): T[];
|
|
12
|
+
/**
|
|
13
|
+
* Drops elements from the beginning of an array while the specified object properties match.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of elements in the array.
|
|
16
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
17
|
+
* @param {Partial<T>} objectToDrop - An object specifying the properties to match for dropping elements.
|
|
18
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
19
|
+
*/
|
|
20
|
+
declare function dropWhile<T>(arr: readonly T[], objectToDrop: Partial<T>): T[];
|
|
21
|
+
/**
|
|
22
|
+
* Drops elements from the beginning of an array while the specified property matches a given value.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The type of elements in the array.
|
|
25
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
26
|
+
* @param {[keyof T, unknown]} propertyToDrop - A tuple containing the property key and the value to match for dropping elements.
|
|
27
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
28
|
+
*/
|
|
29
|
+
declare function dropWhile<T>(arr: readonly T[], propertyToDrop: [keyof T, unknown]): T[];
|
|
30
|
+
/**
|
|
31
|
+
* Drops elements from the beginning of an array while the specified property name matches.
|
|
32
|
+
*
|
|
33
|
+
* @template T - The type of elements in the array.
|
|
34
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
35
|
+
* @param {string} propertyToDrop - The name of the property to match for dropping elements.
|
|
36
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
37
|
+
*/
|
|
38
|
+
declare function dropWhile<T>(arr: readonly T[], propertyToDrop: string): T[];
|
|
39
|
+
|
|
40
|
+
export { dropWhile };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drops elements from the beginning of an array while the predicate function returns truthy.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of elements in the array.
|
|
5
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
6
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} canContinueDropping - A predicate function that determines
|
|
7
|
+
* whether to continue dropping elements. The function is called with each element, index, and array, and dropping
|
|
8
|
+
* continues as long as it returns true.
|
|
9
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
10
|
+
*/
|
|
11
|
+
declare function dropWhile<T>(arr: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => unknown): T[];
|
|
12
|
+
/**
|
|
13
|
+
* Drops elements from the beginning of an array while the specified object properties match.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of elements in the array.
|
|
16
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
17
|
+
* @param {Partial<T>} objectToDrop - An object specifying the properties to match for dropping elements.
|
|
18
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
19
|
+
*/
|
|
20
|
+
declare function dropWhile<T>(arr: readonly T[], objectToDrop: Partial<T>): T[];
|
|
21
|
+
/**
|
|
22
|
+
* Drops elements from the beginning of an array while the specified property matches a given value.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The type of elements in the array.
|
|
25
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
26
|
+
* @param {[keyof T, unknown]} propertyToDrop - A tuple containing the property key and the value to match for dropping elements.
|
|
27
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
28
|
+
*/
|
|
29
|
+
declare function dropWhile<T>(arr: readonly T[], propertyToDrop: [keyof T, unknown]): T[];
|
|
30
|
+
/**
|
|
31
|
+
* Drops elements from the beginning of an array while the specified property name matches.
|
|
32
|
+
*
|
|
33
|
+
* @template T - The type of elements in the array.
|
|
34
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
35
|
+
* @param {string} propertyToDrop - The name of the property to match for dropping elements.
|
|
36
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
37
|
+
*/
|
|
38
|
+
declare function dropWhile<T>(arr: readonly T[], propertyToDrop: string): T[];
|
|
39
|
+
|
|
40
|
+
export { dropWhile };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { dropWhile as dropWhile$1 } from '../../array/dropWhile.mjs';
|
|
2
|
+
import { property } from '../object/property.mjs';
|
|
3
|
+
import { matches } from '../predicate/matches.mjs';
|
|
4
|
+
import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|
5
|
+
|
|
6
|
+
function dropWhile(arr, predicate) {
|
|
7
|
+
switch (typeof predicate) {
|
|
8
|
+
case 'function': {
|
|
9
|
+
return dropWhile$1(arr, (item, index, arr) => Boolean(predicate(item, index, arr)));
|
|
10
|
+
}
|
|
11
|
+
case 'object': {
|
|
12
|
+
if (Array.isArray(predicate) && predicate.length === 2) {
|
|
13
|
+
const key = predicate[0];
|
|
14
|
+
const value = predicate[1];
|
|
15
|
+
return dropWhile$1(arr, matchesProperty(key, value));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return dropWhile$1(arr, matches(predicate));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
case 'string': {
|
|
22
|
+
return dropWhile$1(arr, property(predicate));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { dropWhile };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if all elements in an array are truthy.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T[]} arr - The array to check through.
|
|
6
|
+
* @returns {boolean} - `true` if all elements are truthy, or `false` if at least one element is falsy.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const items = [1, 2, 3, 4];
|
|
10
|
+
* const result = every(items);
|
|
11
|
+
* console.log(result); // true
|
|
12
|
+
*
|
|
13
|
+
* const itemsWithFalsy = [1, 0, 3, 4];
|
|
14
|
+
* const resultWithFalsy = every(itemsWithFalsy);
|
|
15
|
+
* console.log(resultWithFalsy); // false
|
|
16
|
+
*/
|
|
17
|
+
declare function every<T>(arr: readonly T[]): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Checks if every item in an array matches the given predicate function.
|
|
20
|
+
*
|
|
21
|
+
* @template T
|
|
22
|
+
* @param {T[]} arr - The array to check through.
|
|
23
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
|
|
24
|
+
* @returns {boolean} - `true` if every item matches the predicate, or `false` if at least one item does not match.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Using a predicate function
|
|
28
|
+
* const items = [1, 2, 3, 4, 5];
|
|
29
|
+
* const result = every(items, (item) => item > 0);
|
|
30
|
+
* console.log(result); // true
|
|
31
|
+
*/
|
|
32
|
+
declare function every<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Checks if every item in an array matches the given partial object.
|
|
35
|
+
*
|
|
36
|
+
* @template T
|
|
37
|
+
* @param {T[]} arr - The array to check through.
|
|
38
|
+
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
39
|
+
* @returns {boolean} - `true` if every item matches the partial object, or `false` if at least one item does not match.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // Using a partial object
|
|
43
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
44
|
+
* const result = every(items, { name: 'Bob' });
|
|
45
|
+
* console.log(result); // false
|
|
46
|
+
*/
|
|
47
|
+
declare function every<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if every item in an array matches a property with a specific value.
|
|
50
|
+
*
|
|
51
|
+
* @template T
|
|
52
|
+
* @param {readonly T[]} arr - The array to check through.
|
|
53
|
+
* @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
54
|
+
* @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Using a property-value pair
|
|
58
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
59
|
+
* const result = every(items, ['name', 'Alice']);
|
|
60
|
+
* console.log(result); // false
|
|
61
|
+
*/
|
|
62
|
+
declare function every<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Checks if every item in an array has a specific property, where the property name is provided as a string.
|
|
65
|
+
*
|
|
66
|
+
* @template T
|
|
67
|
+
* @param {readonly T[]} arr - The array to check through.
|
|
68
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
69
|
+
* @returns {boolean} - `true` if every item has the specified property, or `false` if at least one item does not match.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Using a property name
|
|
73
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
74
|
+
* const result = every(items, 'name');
|
|
75
|
+
* console.log(result); // true
|
|
76
|
+
*/
|
|
77
|
+
declare function every<T>(arr: readonly T[], propertyToCheck: string): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Checks if every item in an object matches the given predicate function.
|
|
80
|
+
*
|
|
81
|
+
* @template T
|
|
82
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to check through.
|
|
83
|
+
* @param {(item: T[keyof T], index: keyof T, arr: T) => unknown} doesMatch - A function that takes an item, its key, and the object, and returns a truthy value if the item matches the criteria.
|
|
84
|
+
* @returns {boolean} - `true` if every property value matches the predicate, or `false` if at least one does not match.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* // Using a predicate function
|
|
88
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
89
|
+
* const result = every(obj, (item) => item > 0);
|
|
90
|
+
* console.log(result); // true
|
|
91
|
+
*/
|
|
92
|
+
declare function every<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Checks if every item in an object matches the given partial value.
|
|
95
|
+
*
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to check through.
|
|
98
|
+
* @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
|
|
99
|
+
* @returns {boolean} - `true` if every property value matches the partial value, or `false` if at least one does not match.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* // Using a partial value
|
|
103
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
|
|
104
|
+
* const result = every(obj, { name: 'Bob' });
|
|
105
|
+
* console.log(result); // false
|
|
106
|
+
*/
|
|
107
|
+
declare function every<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Checks if every item in an object matches a property with a specific value.
|
|
110
|
+
*
|
|
111
|
+
* @template T
|
|
112
|
+
* @param {readonly T[]} object - The object to check through.
|
|
113
|
+
* @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
114
|
+
* @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Using a property-value pair
|
|
118
|
+
* const items = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
|
|
119
|
+
* const result = every(items, ['name', 'Alice']);
|
|
120
|
+
* console.log(result); // false
|
|
121
|
+
*/
|
|
122
|
+
declare function every<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Checks if every item in an object has a specific property, where the property name is provided as a string.
|
|
125
|
+
*
|
|
126
|
+
* @template T
|
|
127
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to check through.
|
|
128
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
129
|
+
* @returns {boolean} - `true` if every property value has the specified property, or `false` if at least one does not match.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* // Using a property name
|
|
133
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
|
|
134
|
+
* const result = every(obj, 'name');
|
|
135
|
+
* console.log(result); // true
|
|
136
|
+
*/
|
|
137
|
+
declare function every<T extends Record<string, unknown>>(object: T, propertyToCheck: string): boolean;
|
|
138
|
+
|
|
139
|
+
export { every };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if all elements in an array are truthy.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T[]} arr - The array to check through.
|
|
6
|
+
* @returns {boolean} - `true` if all elements are truthy, or `false` if at least one element is falsy.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const items = [1, 2, 3, 4];
|
|
10
|
+
* const result = every(items);
|
|
11
|
+
* console.log(result); // true
|
|
12
|
+
*
|
|
13
|
+
* const itemsWithFalsy = [1, 0, 3, 4];
|
|
14
|
+
* const resultWithFalsy = every(itemsWithFalsy);
|
|
15
|
+
* console.log(resultWithFalsy); // false
|
|
16
|
+
*/
|
|
17
|
+
declare function every<T>(arr: readonly T[]): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Checks if every item in an array matches the given predicate function.
|
|
20
|
+
*
|
|
21
|
+
* @template T
|
|
22
|
+
* @param {T[]} arr - The array to check through.
|
|
23
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
|
|
24
|
+
* @returns {boolean} - `true` if every item matches the predicate, or `false` if at least one item does not match.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Using a predicate function
|
|
28
|
+
* const items = [1, 2, 3, 4, 5];
|
|
29
|
+
* const result = every(items, (item) => item > 0);
|
|
30
|
+
* console.log(result); // true
|
|
31
|
+
*/
|
|
32
|
+
declare function every<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Checks if every item in an array matches the given partial object.
|
|
35
|
+
*
|
|
36
|
+
* @template T
|
|
37
|
+
* @param {T[]} arr - The array to check through.
|
|
38
|
+
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
39
|
+
* @returns {boolean} - `true` if every item matches the partial object, or `false` if at least one item does not match.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // Using a partial object
|
|
43
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
44
|
+
* const result = every(items, { name: 'Bob' });
|
|
45
|
+
* console.log(result); // false
|
|
46
|
+
*/
|
|
47
|
+
declare function every<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if every item in an array matches a property with a specific value.
|
|
50
|
+
*
|
|
51
|
+
* @template T
|
|
52
|
+
* @param {readonly T[]} arr - The array to check through.
|
|
53
|
+
* @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
54
|
+
* @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Using a property-value pair
|
|
58
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
59
|
+
* const result = every(items, ['name', 'Alice']);
|
|
60
|
+
* console.log(result); // false
|
|
61
|
+
*/
|
|
62
|
+
declare function every<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Checks if every item in an array has a specific property, where the property name is provided as a string.
|
|
65
|
+
*
|
|
66
|
+
* @template T
|
|
67
|
+
* @param {readonly T[]} arr - The array to check through.
|
|
68
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
69
|
+
* @returns {boolean} - `true` if every item has the specified property, or `false` if at least one item does not match.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Using a property name
|
|
73
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
74
|
+
* const result = every(items, 'name');
|
|
75
|
+
* console.log(result); // true
|
|
76
|
+
*/
|
|
77
|
+
declare function every<T>(arr: readonly T[], propertyToCheck: string): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Checks if every item in an object matches the given predicate function.
|
|
80
|
+
*
|
|
81
|
+
* @template T
|
|
82
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to check through.
|
|
83
|
+
* @param {(item: T[keyof T], index: keyof T, arr: T) => unknown} doesMatch - A function that takes an item, its key, and the object, and returns a truthy value if the item matches the criteria.
|
|
84
|
+
* @returns {boolean} - `true` if every property value matches the predicate, or `false` if at least one does not match.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* // Using a predicate function
|
|
88
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
89
|
+
* const result = every(obj, (item) => item > 0);
|
|
90
|
+
* console.log(result); // true
|
|
91
|
+
*/
|
|
92
|
+
declare function every<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Checks if every item in an object matches the given partial value.
|
|
95
|
+
*
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to check through.
|
|
98
|
+
* @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
|
|
99
|
+
* @returns {boolean} - `true` if every property value matches the partial value, or `false` if at least one does not match.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* // Using a partial value
|
|
103
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
|
|
104
|
+
* const result = every(obj, { name: 'Bob' });
|
|
105
|
+
* console.log(result); // false
|
|
106
|
+
*/
|
|
107
|
+
declare function every<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Checks if every item in an object matches a property with a specific value.
|
|
110
|
+
*
|
|
111
|
+
* @template T
|
|
112
|
+
* @param {readonly T[]} object - The object to check through.
|
|
113
|
+
* @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
114
|
+
* @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Using a property-value pair
|
|
118
|
+
* const items = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
|
|
119
|
+
* const result = every(items, ['name', 'Alice']);
|
|
120
|
+
* console.log(result); // false
|
|
121
|
+
*/
|
|
122
|
+
declare function every<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Checks if every item in an object has a specific property, where the property name is provided as a string.
|
|
125
|
+
*
|
|
126
|
+
* @template T
|
|
127
|
+
* @param {T extends Record<string, unknown> ? T : never} object - The object to check through.
|
|
128
|
+
* @param {string} propertyToCheck - The property name to check.
|
|
129
|
+
* @returns {boolean} - `true` if every property value has the specified property, or `false` if at least one does not match.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* // Using a property name
|
|
133
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
|
|
134
|
+
* const result = every(obj, 'name');
|
|
135
|
+
* console.log(result); // true
|
|
136
|
+
*/
|
|
137
|
+
declare function every<T extends Record<string, unknown>>(object: T, propertyToCheck: string): boolean;
|
|
138
|
+
|
|
139
|
+
export { every };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { identity } from '../_internal/identity.mjs';
|
|
2
|
+
import { property } from '../object/property.mjs';
|
|
3
|
+
import { matches } from '../predicate/matches.mjs';
|
|
4
|
+
import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|
5
|
+
|
|
6
|
+
function every(source, doesMatch) {
|
|
7
|
+
if (!source) {
|
|
8
|
+
source = [];
|
|
9
|
+
}
|
|
10
|
+
let values = source;
|
|
11
|
+
if (!Array.isArray(source)) {
|
|
12
|
+
values = Object.values(source);
|
|
13
|
+
}
|
|
14
|
+
if (!doesMatch) {
|
|
15
|
+
doesMatch = identity;
|
|
16
|
+
}
|
|
17
|
+
switch (typeof doesMatch) {
|
|
18
|
+
case 'function': {
|
|
19
|
+
if (!Array.isArray(source)) {
|
|
20
|
+
const entries = Object.entries(source);
|
|
21
|
+
for (let i = 0; i < entries.length; i++) {
|
|
22
|
+
const entry = entries[i];
|
|
23
|
+
const key = entry[0];
|
|
24
|
+
const value = entry[1];
|
|
25
|
+
if (!doesMatch(value, key, source)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return values.every(doesMatch);
|
|
32
|
+
}
|
|
33
|
+
case 'object': {
|
|
34
|
+
if (Array.isArray(doesMatch) && doesMatch.length === 2) {
|
|
35
|
+
const key = doesMatch[0];
|
|
36
|
+
const value = doesMatch[1];
|
|
37
|
+
return values.every(matchesProperty(key, value));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return values.every(matches(doesMatch));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
case 'string': {
|
|
44
|
+
return values.every(property(doesMatch));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { every };
|
|
@@ -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 };
|