es-toolkit 1.25.0 → 1.25.1-dev.796

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.
@@ -2,7 +2,7 @@
2
2
  * Checks if all elements in an array are truthy.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to check through.
5
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
6
6
  * @returns {boolean} - `true` if all elements are truthy, or `false` if at least one element is falsy.
7
7
  *
8
8
  * @example
@@ -14,12 +14,12 @@
14
14
  * const resultWithFalsy = every(itemsWithFalsy);
15
15
  * console.log(resultWithFalsy); // false
16
16
  */
17
- declare function every<T>(arr: readonly T[]): boolean;
17
+ declare function every<T>(arr: ArrayLike<T> | null | undefined): boolean;
18
18
  /**
19
19
  * Checks if every item in an array matches the given predicate function.
20
20
  *
21
21
  * @template T
22
- * @param {T[]} arr - The array to check through.
22
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
23
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
24
  * @returns {boolean} - `true` if every item matches the predicate, or `false` if at least one item does not match.
25
25
  *
@@ -29,12 +29,12 @@ declare function every<T>(arr: readonly T[]): boolean;
29
29
  * const result = every(items, (item) => item > 0);
30
30
  * console.log(result); // true
31
31
  */
32
- declare function every<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
32
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
33
33
  /**
34
34
  * Checks if every item in an array matches the given partial object.
35
35
  *
36
36
  * @template T
37
- * @param {T[]} arr - The array to check through.
37
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
38
38
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
39
39
  * @returns {boolean} - `true` if every item matches the partial object, or `false` if at least one item does not match.
40
40
  *
@@ -44,12 +44,12 @@ declare function every<T>(arr: readonly T[], doesMatch: (item: T, index: number,
44
44
  * const result = every(items, { name: 'Bob' });
45
45
  * console.log(result); // false
46
46
  */
47
- declare function every<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
47
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): boolean;
48
48
  /**
49
49
  * Checks if every item in an array matches a property with a specific value.
50
50
  *
51
51
  * @template T
52
- * @param {T[]} arr - The array to check through.
52
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
53
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
54
  * @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
55
55
  *
@@ -59,12 +59,12 @@ declare function every<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
59
59
  * const result = every(items, ['name', 'Alice']);
60
60
  * console.log(result); // false
61
61
  */
62
- declare function every<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): boolean;
62
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): boolean;
63
63
  /**
64
64
  * Checks if every item in an array has a specific property, where the property name is provided as a string.
65
65
  *
66
66
  * @template T
67
- * @param {T[]} arr - The array to check through.
67
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
68
68
  * @param {string} propertyToCheck - The property name to check.
69
69
  * @returns {boolean} - `true` if every item has the specified property, or `false` if at least one item does not match.
70
70
  *
@@ -74,12 +74,12 @@ declare function every<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknow
74
74
  * const result = every(items, 'name');
75
75
  * console.log(result); // true
76
76
  */
77
- declare function every<T>(arr: readonly T[], propertyToCheck: string): boolean;
77
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): boolean;
78
78
  /**
79
79
  * Checks if every item in an object matches the given predicate function.
80
80
  *
81
81
  * @template T
82
- * @param {T} object - The object to check through.
82
+ * @param {T | null | undefined} object - The object to check through.
83
83
  * @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.
84
84
  * @returns {boolean} - `true` if every property value matches the predicate, or `false` if at least one does not match.
85
85
  *
@@ -89,12 +89,12 @@ declare function every<T>(arr: readonly T[], propertyToCheck: string): boolean;
89
89
  * const result = every(obj, (value) => value > 0);
90
90
  * console.log(result); // true
91
91
  */
92
- declare function every<T extends Record<string, unknown>>(object: T, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): boolean;
92
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): boolean;
93
93
  /**
94
94
  * Checks if every item in an object matches the given partial value.
95
95
  *
96
96
  * @template T
97
- * @param {T} object - The object to check through.
97
+ * @param {T | null | undefined} object - The object to check through.
98
98
  * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
99
99
  * @returns {boolean} - `true` if every property value matches the partial value, or `false` if at least one does not match.
100
100
  *
@@ -104,12 +104,12 @@ declare function every<T extends Record<string, unknown>>(object: T, doesMatch:
104
104
  * const result = every(obj, { name: 'Bob' });
105
105
  * console.log(result); // false
106
106
  */
107
- declare function every<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): boolean;
107
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): boolean;
108
108
  /**
109
109
  * Checks if every item in an object matches a property with a specific value.
110
110
  *
111
111
  * @template T
112
- * @param {T[]} object - The object to check through.
112
+ * @param {T | null | undefined} object - The object to check through.
113
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
114
  * @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
115
115
  *
@@ -119,12 +119,12 @@ declare function every<T extends Record<string, unknown>>(object: T, doesMatch:
119
119
  * const result = every(obj, ['name', 'Alice']);
120
120
  * console.log(result); // false
121
121
  */
122
- declare function every<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): boolean;
122
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T, unknown]): boolean;
123
123
  /**
124
124
  * Checks if every item in an object has a specific property, where the property name is provided as a string.
125
125
  *
126
126
  * @template T
127
- * @param {T} object - The object to check through.
127
+ * @param {T | null | undefined} object - The object to check through.
128
128
  * @param {string} propertyToCheck - The property name to check.
129
129
  * @returns {boolean} - `true` if every property value has the specified property, or `false` if at least one does not match.
130
130
  *
@@ -134,6 +134,6 @@ declare function every<T extends Record<string, unknown>>(object: T, doesMatchPr
134
134
  * const result = every(obj, 'name');
135
135
  * console.log(result); // true
136
136
  */
137
- declare function every<T extends Record<string, unknown>>(object: T, propertyToCheck: string): boolean;
137
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): boolean;
138
138
 
139
139
  export { every };
@@ -2,7 +2,7 @@
2
2
  * Checks if all elements in an array are truthy.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to check through.
5
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
6
6
  * @returns {boolean} - `true` if all elements are truthy, or `false` if at least one element is falsy.
7
7
  *
8
8
  * @example
@@ -14,12 +14,12 @@
14
14
  * const resultWithFalsy = every(itemsWithFalsy);
15
15
  * console.log(resultWithFalsy); // false
16
16
  */
17
- declare function every<T>(arr: readonly T[]): boolean;
17
+ declare function every<T>(arr: ArrayLike<T> | null | undefined): boolean;
18
18
  /**
19
19
  * Checks if every item in an array matches the given predicate function.
20
20
  *
21
21
  * @template T
22
- * @param {T[]} arr - The array to check through.
22
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
23
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
24
  * @returns {boolean} - `true` if every item matches the predicate, or `false` if at least one item does not match.
25
25
  *
@@ -29,12 +29,12 @@ declare function every<T>(arr: readonly T[]): boolean;
29
29
  * const result = every(items, (item) => item > 0);
30
30
  * console.log(result); // true
31
31
  */
32
- declare function every<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
32
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): boolean;
33
33
  /**
34
34
  * Checks if every item in an array matches the given partial object.
35
35
  *
36
36
  * @template T
37
- * @param {T[]} arr - The array to check through.
37
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
38
38
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
39
39
  * @returns {boolean} - `true` if every item matches the partial object, or `false` if at least one item does not match.
40
40
  *
@@ -44,12 +44,12 @@ declare function every<T>(arr: readonly T[], doesMatch: (item: T, index: number,
44
44
  * const result = every(items, { name: 'Bob' });
45
45
  * console.log(result); // false
46
46
  */
47
- declare function every<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
47
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): boolean;
48
48
  /**
49
49
  * Checks if every item in an array matches a property with a specific value.
50
50
  *
51
51
  * @template T
52
- * @param {T[]} arr - The array to check through.
52
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
53
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
54
  * @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
55
55
  *
@@ -59,12 +59,12 @@ declare function every<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
59
59
  * const result = every(items, ['name', 'Alice']);
60
60
  * console.log(result); // false
61
61
  */
62
- declare function every<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): boolean;
62
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): boolean;
63
63
  /**
64
64
  * Checks if every item in an array has a specific property, where the property name is provided as a string.
65
65
  *
66
66
  * @template T
67
- * @param {T[]} arr - The array to check through.
67
+ * @param {ArrayLike<T> | null | undefined} arr - The array to check through.
68
68
  * @param {string} propertyToCheck - The property name to check.
69
69
  * @returns {boolean} - `true` if every item has the specified property, or `false` if at least one item does not match.
70
70
  *
@@ -74,12 +74,12 @@ declare function every<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknow
74
74
  * const result = every(items, 'name');
75
75
  * console.log(result); // true
76
76
  */
77
- declare function every<T>(arr: readonly T[], propertyToCheck: string): boolean;
77
+ declare function every<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): boolean;
78
78
  /**
79
79
  * Checks if every item in an object matches the given predicate function.
80
80
  *
81
81
  * @template T
82
- * @param {T} object - The object to check through.
82
+ * @param {T | null | undefined} object - The object to check through.
83
83
  * @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.
84
84
  * @returns {boolean} - `true` if every property value matches the predicate, or `false` if at least one does not match.
85
85
  *
@@ -89,12 +89,12 @@ declare function every<T>(arr: readonly T[], propertyToCheck: string): boolean;
89
89
  * const result = every(obj, (value) => value > 0);
90
90
  * console.log(result); // true
91
91
  */
92
- declare function every<T extends Record<string, unknown>>(object: T, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): boolean;
92
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): boolean;
93
93
  /**
94
94
  * Checks if every item in an object matches the given partial value.
95
95
  *
96
96
  * @template T
97
- * @param {T} object - The object to check through.
97
+ * @param {T | null | undefined} object - The object to check through.
98
98
  * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
99
99
  * @returns {boolean} - `true` if every property value matches the partial value, or `false` if at least one does not match.
100
100
  *
@@ -104,12 +104,12 @@ declare function every<T extends Record<string, unknown>>(object: T, doesMatch:
104
104
  * const result = every(obj, { name: 'Bob' });
105
105
  * console.log(result); // false
106
106
  */
107
- declare function every<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): boolean;
107
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): boolean;
108
108
  /**
109
109
  * Checks if every item in an object matches a property with a specific value.
110
110
  *
111
111
  * @template T
112
- * @param {T[]} object - The object to check through.
112
+ * @param {T | null | undefined} object - The object to check through.
113
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
114
  * @returns {boolean} - `true` if every item has the specified property value, or `false` if at least one item does not match.
115
115
  *
@@ -119,12 +119,12 @@ declare function every<T extends Record<string, unknown>>(object: T, doesMatch:
119
119
  * const result = every(obj, ['name', 'Alice']);
120
120
  * console.log(result); // false
121
121
  */
122
- declare function every<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): boolean;
122
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T, unknown]): boolean;
123
123
  /**
124
124
  * Checks if every item in an object has a specific property, where the property name is provided as a string.
125
125
  *
126
126
  * @template T
127
- * @param {T} object - The object to check through.
127
+ * @param {T | null | undefined} object - The object to check through.
128
128
  * @param {string} propertyToCheck - The property name to check.
129
129
  * @returns {boolean} - `true` if every property value has the specified property, or `false` if at least one does not match.
130
130
  *
@@ -134,6 +134,6 @@ declare function every<T extends Record<string, unknown>>(object: T, doesMatchPr
134
134
  * const result = every(obj, 'name');
135
135
  * console.log(result); // true
136
136
  */
137
- declare function every<T extends Record<string, unknown>>(object: T, propertyToCheck: string): boolean;
137
+ declare function every<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): boolean;
138
138
 
139
139
  export { every };
@@ -5,12 +5,9 @@ import { matchesProperty } from '../predicate/matchesProperty.mjs';
5
5
 
6
6
  function every(source, doesMatch) {
7
7
  if (!source) {
8
- source = [];
9
- }
10
- let values = source;
11
- if (!Array.isArray(source)) {
12
- values = Object.values(source);
8
+ return true;
13
9
  }
10
+ const values = Array.isArray(source) ? source : Object.values(source);
14
11
  if (!doesMatch) {
15
12
  doesMatch = identity;
16
13
  }
@@ -5,10 +5,37 @@
5
5
  * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
6
6
  * entire array.
7
7
  *
8
- * @template T, U, S, V
9
- * @param {Array<T | U>} array - The array to fill.
10
- * @param {U} value - The value to fill the array with.
11
- * @returns {Array<T | U>} The array with the filled values.
8
+ * @template T
9
+ * @param {unknown[] | null | undefined} array - The array to fill.
10
+ * @param {T} value - The value to fill the array with.
11
+ * @returns {T[]} The array with the filled values.
12
+ *
13
+ * @example
14
+ * const array = [1, 2, 3];
15
+ * const result = fill(array, 'a');
16
+ * // => ['a', 'a', 'a']
17
+ *
18
+ * const result = fill(Array(3), 2);
19
+ * // => [2, 2, 2]
20
+ *
21
+ * const result = fill([4, 6, 8, 10], '*', 1, 3);
22
+ * // => [4, '*', '*', 10]
23
+ *
24
+ * const result = fill(array, '*', -2, -1);
25
+ * // => [1, '*', 3]
26
+ */
27
+ declare function fill<T>(array: unknown[] | null | undefined, value?: T): T[];
28
+ /**
29
+ * Fills the whole array with a specified value.
30
+ *
31
+ * This function mutates the original array and replaces its elements with the provided value, starting from the specified
32
+ * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
33
+ * entire array.
34
+ *
35
+ * @template T
36
+ * @param {ArrayLike<unknown> | null | undefined} array - The array to fill.
37
+ * @param {T} value - The value to fill the array with.
38
+ * @returns {ArrayLike<T>} The array with the filled values.
12
39
  *
13
40
  * @example
14
41
  * const array = [1, 2, 3];
@@ -24,18 +51,19 @@
24
51
  * const result = fill(array, '*', -2, -1);
25
52
  * // => [1, '*', 3]
26
53
  */
27
- declare function fill<T>(array: unknown[], value?: T): T[];
54
+ declare function fill<T>(array: ArrayLike<unknown> | null | undefined, value?: T): ArrayLike<T>;
28
55
  /**
29
- * Fills elements of an array with a specified value from the start position to the end.
56
+ * Fills elements of an array with a specified value from the start position up to, but not including, the end position.
30
57
  *
31
58
  * This function mutates the original array and replaces its elements with the provided value, starting from the specified
32
59
  * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
33
60
  * entire array.
34
61
  *
35
- * @template T, U, S, V
36
- * @param {Array<T | U>} array - The array to fill.
62
+ * @template T, U
63
+ * @param {Array<T | U> | null | undefined} array - The array to fill.
37
64
  * @param {U} value - The value to fill the array with.
38
- * @param {S} [start=0] - The start position. Defaults to 0.
65
+ * @param {number} [start=0] - The start position. Defaults to 0.
66
+ * @param {number} [end=arr.length] - The end position. Defaults to the array's length.
39
67
  * @returns {Array<T | U>} The array with the filled values.
40
68
  *
41
69
  * @example
@@ -52,7 +80,7 @@ declare function fill<T>(array: unknown[], value?: T): T[];
52
80
  * const result = fill(array, '*', -2, -1);
53
81
  * // => [1, '*', 3]
54
82
  */
55
- declare function fill<T, U, S>(array: Array<T | U>, value: U, start: S): Array<T | U>;
83
+ declare function fill<T, U>(array: Array<T | U> | null | undefined, value: U, start?: number, end?: number): Array<T | U>;
56
84
  /**
57
85
  * Fills elements of an array with a specified value from the start position up to, but not including, the end position.
58
86
  *
@@ -60,12 +88,12 @@ declare function fill<T, U, S>(array: Array<T | U>, value: U, start: S): Array<T
60
88
  * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
61
89
  * entire array.
62
90
  *
63
- * @template T, U, S, V
64
- * @param {Array<T | U>} array - The array to fill.
91
+ * @template T, U
92
+ * @param {ArrayLike<T | U> | null | undefined} array - The array to fill.
65
93
  * @param {U} value - The value to fill the array with.
66
- * @param {S} [start=0] - The start position. Defaults to 0.
67
- * @param {V} [end=arr.length] - The end position. Defaults to the array's length.
68
- * @returns {Array<T | U>} The array with the filled values.
94
+ * @param {number} [start=0] - The start position. Defaults to 0.
95
+ * @param {number} [end=arr.length] - The end position. Defaults to the array's length.
96
+ * @returns {ArrayLike<T | U>} The array with the filled values.
69
97
  *
70
98
  * @example
71
99
  * const array = [1, 2, 3];
@@ -81,6 +109,6 @@ declare function fill<T, U, S>(array: Array<T | U>, value: U, start: S): Array<T
81
109
  * const result = fill(array, '*', -2, -1);
82
110
  * // => [1, '*', 3]
83
111
  */
84
- declare function fill<T, U, S, V>(array: Array<T | U>, value: U, start: S, end: V): Array<T | U>;
112
+ declare function fill<T, U>(array: ArrayLike<T | U> | null | undefined, value: U, start?: number, end?: number): ArrayLike<T | U>;
85
113
 
86
114
  export { fill };
@@ -5,10 +5,37 @@
5
5
  * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
6
6
  * entire array.
7
7
  *
8
- * @template T, U, S, V
9
- * @param {Array<T | U>} array - The array to fill.
10
- * @param {U} value - The value to fill the array with.
11
- * @returns {Array<T | U>} The array with the filled values.
8
+ * @template T
9
+ * @param {unknown[] | null | undefined} array - The array to fill.
10
+ * @param {T} value - The value to fill the array with.
11
+ * @returns {T[]} The array with the filled values.
12
+ *
13
+ * @example
14
+ * const array = [1, 2, 3];
15
+ * const result = fill(array, 'a');
16
+ * // => ['a', 'a', 'a']
17
+ *
18
+ * const result = fill(Array(3), 2);
19
+ * // => [2, 2, 2]
20
+ *
21
+ * const result = fill([4, 6, 8, 10], '*', 1, 3);
22
+ * // => [4, '*', '*', 10]
23
+ *
24
+ * const result = fill(array, '*', -2, -1);
25
+ * // => [1, '*', 3]
26
+ */
27
+ declare function fill<T>(array: unknown[] | null | undefined, value?: T): T[];
28
+ /**
29
+ * Fills the whole array with a specified value.
30
+ *
31
+ * This function mutates the original array and replaces its elements with the provided value, starting from the specified
32
+ * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
33
+ * entire array.
34
+ *
35
+ * @template T
36
+ * @param {ArrayLike<unknown> | null | undefined} array - The array to fill.
37
+ * @param {T} value - The value to fill the array with.
38
+ * @returns {ArrayLike<T>} The array with the filled values.
12
39
  *
13
40
  * @example
14
41
  * const array = [1, 2, 3];
@@ -24,18 +51,19 @@
24
51
  * const result = fill(array, '*', -2, -1);
25
52
  * // => [1, '*', 3]
26
53
  */
27
- declare function fill<T>(array: unknown[], value?: T): T[];
54
+ declare function fill<T>(array: ArrayLike<unknown> | null | undefined, value?: T): ArrayLike<T>;
28
55
  /**
29
- * Fills elements of an array with a specified value from the start position to the end.
56
+ * Fills elements of an array with a specified value from the start position up to, but not including, the end position.
30
57
  *
31
58
  * This function mutates the original array and replaces its elements with the provided value, starting from the specified
32
59
  * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
33
60
  * entire array.
34
61
  *
35
- * @template T, U, S, V
36
- * @param {Array<T | U>} array - The array to fill.
62
+ * @template T, U
63
+ * @param {Array<T | U> | null | undefined} array - The array to fill.
37
64
  * @param {U} value - The value to fill the array with.
38
- * @param {S} [start=0] - The start position. Defaults to 0.
65
+ * @param {number} [start=0] - The start position. Defaults to 0.
66
+ * @param {number} [end=arr.length] - The end position. Defaults to the array's length.
39
67
  * @returns {Array<T | U>} The array with the filled values.
40
68
  *
41
69
  * @example
@@ -52,7 +80,7 @@ declare function fill<T>(array: unknown[], value?: T): T[];
52
80
  * const result = fill(array, '*', -2, -1);
53
81
  * // => [1, '*', 3]
54
82
  */
55
- declare function fill<T, U, S>(array: Array<T | U>, value: U, start: S): Array<T | U>;
83
+ declare function fill<T, U>(array: Array<T | U> | null | undefined, value: U, start?: number, end?: number): Array<T | U>;
56
84
  /**
57
85
  * Fills elements of an array with a specified value from the start position up to, but not including, the end position.
58
86
  *
@@ -60,12 +88,12 @@ declare function fill<T, U, S>(array: Array<T | U>, value: U, start: S): Array<T
60
88
  * start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the
61
89
  * entire array.
62
90
  *
63
- * @template T, U, S, V
64
- * @param {Array<T | U>} array - The array to fill.
91
+ * @template T, U
92
+ * @param {ArrayLike<T | U> | null | undefined} array - The array to fill.
65
93
  * @param {U} value - The value to fill the array with.
66
- * @param {S} [start=0] - The start position. Defaults to 0.
67
- * @param {V} [end=arr.length] - The end position. Defaults to the array's length.
68
- * @returns {Array<T | U>} The array with the filled values.
94
+ * @param {number} [start=0] - The start position. Defaults to 0.
95
+ * @param {number} [end=arr.length] - The end position. Defaults to the array's length.
96
+ * @returns {ArrayLike<T | U>} The array with the filled values.
69
97
  *
70
98
  * @example
71
99
  * const array = [1, 2, 3];
@@ -81,6 +109,6 @@ declare function fill<T, U, S>(array: Array<T | U>, value: U, start: S): Array<T
81
109
  * const result = fill(array, '*', -2, -1);
82
110
  * // => [1, '*', 3]
83
111
  */
84
- declare function fill<T, U, S, V>(array: Array<T | U>, value: U, start: S, end: V): Array<T | U>;
112
+ declare function fill<T, U>(array: ArrayLike<T | U> | null | undefined, value: U, start?: number, end?: number): ArrayLike<T | U>;
85
113
 
86
114
  export { fill };
@@ -1,6 +1,14 @@
1
1
  import { fill as fill$1 } from '../../array/fill.mjs';
2
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
3
+ import { isString } from '../predicate/isString.mjs';
2
4
 
3
- function fill(array, value, start = 0, end = array.length) {
5
+ function fill(array, value, start = 0, end = array ? array.length : 0) {
6
+ if (!isArrayLike(array)) {
7
+ return [];
8
+ }
9
+ if (isString(array)) {
10
+ return array;
11
+ }
4
12
  start = Math.floor(start);
5
13
  end = Math.floor(end);
6
14
  if (!start) {
@@ -2,7 +2,7 @@
2
2
  * Filters items from a array and returns an array of elements.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to iterate over.
5
+ * @param {ArrayLike<T> | null | undefined} 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
8
  *
@@ -10,12 +10,12 @@
10
10
  * filter([1, 2, 3], n => n % 2 === 0)
11
11
  * // => [2]
12
12
  */
13
- declare function filter<T>(arr: readonly T[], doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): T[];
13
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): T[];
14
14
  /**
15
15
  * Filters elements in a arr that match the properties of the given partial object.
16
16
  *
17
17
  * @template T
18
- * @param {T[]} arr - The array to iterate over.
18
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
19
19
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
20
20
  * @returns {T[]} - Returns a new array of elements that match the given properties.
21
21
  *
@@ -24,12 +24,12 @@ declare function filter<T>(arr: readonly T[], doesMatch?: (item: T, index: numbe
24
24
  * filter(arr, { name: 'Bob' });
25
25
  * // => [{ id: 2, name: 'Bob' }]
26
26
  */
27
- declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
27
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): T[];
28
28
  /**
29
29
  * Filters elements in a arr that match the given key-value pair.
30
30
  *
31
31
  * @template T
32
- * @param {T[]} arr - The array to iterate over.
32
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
33
33
  * @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
34
34
  * @returns {T[]} - Returns a new array of elements that match the given key-value pair.
35
35
  *
@@ -38,12 +38,12 @@ declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
38
38
  * filter(arr, ['name', 'Alice']);
39
39
  * // => [{ id: 1, name: 'Alice' }]
40
40
  */
41
- declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T[];
41
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): T[];
42
42
  /**
43
43
  * Filters the arr, returning elements that contain the given property name.
44
44
  *
45
45
  * @template T
46
- * @param {T[]} arr - The array to iterate over.
46
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
47
47
  * @param {string} propertyToCheck - The property name to check.
48
48
  * @returns {T[]} - Returns a new array of elements that match the given property name.
49
49
  *
@@ -52,7 +52,7 @@ declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unkno
52
52
  * filter(arr, 'name');
53
53
  * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
54
54
  */
55
- declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
55
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): T[];
56
56
  /**
57
57
  * Filters items from a object and returns an array of elements that match the given predicate function.
58
58
  *
@@ -70,12 +70,12 @@ declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
70
70
  * filter(obj, value => value > 2)
71
71
  * // => [3]
72
72
  */
73
- declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): T[];
73
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): T[];
74
74
  /**
75
75
  * Filters elements in a object that match the properties of the given partial object.
76
76
  *
77
77
  * @template T
78
- * @param {T} object - The object to iterate over.
78
+ * @param {T | null | undefined} object - The object to iterate over.
79
79
  * @param {Partial<T[keyof T]>} doesMatch - The partial object to match
80
80
  * @returns {T[]} - Returns a new array of elements that match the given properties.pair.
81
81
  *
@@ -84,12 +84,12 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatch:
84
84
  * filter(obj, { name: 'Bob' });
85
85
  * // => [{ id: 2, name: 'Bob' }]
86
86
  */
87
- declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T[];
87
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): T[];
88
88
  /**
89
89
  * Filters elements in a arr that match the given key-value pair.
90
90
  *
91
91
  * @template T
92
- * @param {T} object - The object to iterate over.
92
+ * @param {T | null | undefined} object - The object to iterate over.
93
93
  * @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
94
94
  * @returns {T[]} - Returns a new array of elements that match the given key-value pair.
95
95
  *
@@ -98,12 +98,12 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatch:
98
98
  * filter(obj, ['name', 'Alice']);
99
99
  * // => [{ id: 1, name: 'Alice' }]
100
100
  */
101
- declare function filter<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T[];
101
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T, unknown]): T[];
102
102
  /**
103
103
  * Filters the object, returning elements that contain the given property name.
104
104
  *
105
105
  * @template T
106
- * @param {T} object - The object to iterate over.
106
+ * @param {T | null | undefined} object - The object to iterate over.
107
107
  * @param {string} propertyToCheck - The property name to check.
108
108
  * @returns {T[]} - Returns a new array of elements that match the given property name.
109
109
  *
@@ -112,6 +112,6 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatchP
112
112
  * filter(obj, 'name');
113
113
  * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
114
114
  */
115
- declare function filter<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T[];
115
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): T[];
116
116
 
117
117
  export { filter };