moderndash 3.3.2 → 3.5.0

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/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Call, Tuples, Objects } from 'hotscript';
2
+
1
3
  /**
2
4
  * Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
3
5
  *
@@ -8,9 +10,9 @@
8
10
  * chunk(['a', 'b', 'c', 'd'], 3)
9
11
  * // => [['a', 'b', 'c'], ['d']]
10
12
  * @param chunkSize The length of each chunk
11
- * @param array The array to chunk.
12
- * @template TElem The type of the array elements.
13
- * @returns Returns the new array of chunks.
13
+ * @param array The array to chunk
14
+ * @template TElem The type of the array elements
15
+ * @returns Returns the new array of chunks
14
16
  */
15
17
  declare function chunk<TElem>(array: readonly TElem[], chunkSize: number): TElem[][];
16
18
 
@@ -30,10 +32,10 @@ declare function chunk<TElem>(array: readonly TElem[], chunkSize: number): TElem
30
32
  * count(users, value => value.age);
31
33
  * // => { 36: 2, 40: 1 }
32
34
  *
33
- * @param criteria The criteria to count by.
34
- * @param array The array or record to iterate over.
35
- * @template TElem The type of the array elements.
36
- * @returns Returns the composed aggregate object.
35
+ * @param criteria The criteria to count by
36
+ * @param array The array or record to iterate over
37
+ * @template TElem The type of the array elements
38
+ * @returns Returns the composed aggregate object
37
39
  */
38
40
  declare function count<TElem, TKey extends PropertyKey>(array: readonly TElem[], criteria: (value: TElem) => TKey): Record<TKey, number>;
39
41
 
@@ -76,10 +78,10 @@ type ArrayTail<TArray extends unknown[]> = TArray extends [unknown, ...infer U]
76
78
  *
77
79
  * difference(arr1, arr2, (a, b) => a.id === b.id)
78
80
  * // => [{ id: 1, name: 'Yeet' }]
79
- * @param arraysOrCompareFn Two or more arrays with an optional compare function at the end.
80
- * @template TElem The type of the array elements.
81
- * @template TArrays The type of the arrays provided.
82
- * @returns Returns the new array of filtered values.
81
+ * @param arraysOrCompareFn Two or more arrays with an optional compare function at the end
82
+ * @template TElem The type of the array elements
83
+ * @template TArrays The type of the arrays provided
84
+ * @returns Returns the new array of filtered values
83
85
  */
84
86
  declare function difference<TElem>(...arraysOrCompareFn: ArrayMinLength<TElem[], 2>): TElem[];
85
87
  declare function difference<TArrays extends ArrayMinLength<unknown[], 2>>(...arraysOrCompareFn: [...TArrays, CompareFunction<TArrays>]): TArrays[0];
@@ -97,10 +99,10 @@ declare function difference<TArrays extends ArrayMinLength<unknown[], 2>>(...arr
97
99
  *
98
100
  * dropRightWhile(users, user => user.active)
99
101
  * // => objects for ['barney']
100
- * @param predicate The function invoked per iteration.
101
- * @param array The array to query.
102
- * @template TElem The type of the array elements.
103
- * @returns Returns the slice of `array`.
102
+ * @param predicate The function invoked per iteration
103
+ * @param array The array to query
104
+ * @template TElem The type of the array elements
105
+ * @returns Returns the slice of `array`
104
106
  */
105
107
  declare function dropRightWhile<TElem>(array: readonly TElem[], predicate: (value: TElem) => boolean): TElem[];
106
108
 
@@ -117,10 +119,10 @@ declare function dropRightWhile<TElem>(array: readonly TElem[], predicate: (valu
117
119
  *
118
120
  * dropWhile(users, user => user.active)
119
121
  * // => objects for ['pebbles']
120
- * @param predicate The function invoked per iteration.
121
- * @param array The array to query.
122
- * @template TElem The type of the array elements.
123
- * @returns Returns the slice of `array`.
122
+ * @param predicate The function invoked per iteration
123
+ * @param array The array to query
124
+ * @template TElem The type of the array elements
125
+ * @returns Returns the slice of `array`
124
126
  */
125
127
  declare function dropWhile<TElem>(array: readonly TElem[], predicate: (value: TElem) => boolean): TElem[];
126
128
 
@@ -134,10 +136,10 @@ declare function dropWhile<TElem>(array: readonly TElem[], predicate: (value: TE
134
136
  * group([6.1, 4.2, 6.3], value => value > 5 ? '>5' : '<=5')
135
137
  * // => { '<=5': [4.2], '>5': [6.1, 6.3] }
136
138
  *
137
- * @param collection The array or object to iterate over.
138
- * @param getGroupKey A function that returns the group id for each item.
139
- * @template TElem The type of the array elements.
140
- * @returns An object with grouped items.
139
+ * @param collection The array or object to iterate over
140
+ * @param getGroupKey A function that returns the group id for each item
141
+ * @template TElem The type of the array elements
142
+ * @returns An object with grouped items
141
143
  */
142
144
  declare function group<TElem, TKey extends PropertyKey>(array: readonly TElem[], getGroupKey: (elem: TElem) => TKey): Record<TKey, TElem[]>;
143
145
 
@@ -146,6 +148,7 @@ declare function group<TElem, TKey extends PropertyKey>(array: readonly TElem[],
146
148
  * The order of the values is based on the first array.
147
149
  *
148
150
  * Optionally, use a compare function for element comparison (default is `===`).
151
+ *
149
152
  * @example
150
153
  * intersection([2, 1], [2, 3], [6, 2])
151
154
  * // => [2]
@@ -162,17 +165,35 @@ declare function group<TElem, TKey extends PropertyKey>(array: readonly TElem[],
162
165
  *
163
166
  * intersection(arr1, arr2, (a, b) => a.id === b.id)
164
167
  * // => [{ id: 3, name: 'John' }]
165
- * @param arraysOrCompareFn Two or more arrays with an optional compare function at the end.
166
- * @template TElem The type of the array elements.
167
- * @template TArrays The type of the arrays provided.
168
- * @returns Returns the new array of intersecting values.
168
+ *
169
+ * @param arraysOrCompareFn Two or more arrays with an optional compare function at the end
170
+ * @template TElem Type of the array elements
171
+ * @template TArrays Type of the arrays provided
172
+ * @returns New array of intersecting values
169
173
  */
170
174
  declare function intersection<TElem>(...arraysOrCompareFn: ArrayMinLength<TElem[], 2>): TElem[];
171
175
  declare function intersection<TArrays extends ArrayMinLength<unknown[], 2>>(...arraysOrCompareFn: [...TArrays, CompareFunction<TArrays>]): TArrays[0];
172
176
 
173
177
  /**
174
- * Generates an iterable sequence of numbers starting from `start`, up to and including `end`,
175
- * with a given `step` between each number.
178
+ * Moves an element within an array.
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * move([1, 2, 3, 4, 5], 0, 2);
183
+ * // => [2, 3, 1, 4, 5]
184
+ * ```
185
+ *
186
+ * @param array The input array
187
+ * @param fromIndex Index of the element to move
188
+ * @param toIndex Target index for the element
189
+ * @throws If index is out of bounds
190
+ * @returns The modified array with the moved element
191
+ * @template TArr Type of the array elements
192
+ */
193
+ declare function move<TArr>(array: TArr[], fromIndex: number, toIndex: number): TArr[];
194
+
195
+ /**
196
+ * Generates iterable sequence including numbers from start to end, with step increments.
176
197
  *
177
198
  * @example
178
199
  * for (const num of range(1, 5)) {
@@ -184,11 +205,11 @@ declare function intersection<TArrays extends ArrayMinLength<unknown[], 2>>(...a
184
205
  * const arr = [...range(0, 10, 2)];
185
206
  * // => [0, 2, 4, 6, 8, 10]
186
207
  *
187
- * @param start The starting number of the sequence.
188
- * @param end The end number of the sequence.
189
- * @param step The step between each number in the sequence. Defaults to 1.
190
- *
191
- * @returns An iterable sequence of numbers between `start` and `end`, inclusive, with increments of `step`.
208
+ * @param start Start number of sequence
209
+ * @param end End number of sequence
210
+ * @param step Step between numbers, default: 1
211
+ * @throws If range is negative or step is 0
212
+ * @returns Generator of numbers within the range
192
213
  */
193
214
  declare function range(start: number, end: number, step?: number): Generator<number>;
194
215
 
@@ -198,14 +219,14 @@ declare function range(start: number, end: number, step?: number): Generator<num
198
219
  * @example
199
220
  * shuffle([1, 2, 3, 4])
200
221
  * // => [4, 1, 3, 2]
201
- * @param array The array or object to shuffle.
202
- * @template TElem The type of the array elements.
203
- * @returns Returns a new shuffled array.
222
+ * @param array Array to shuffle
223
+ * @template TElem The type of the array elements
224
+ * @returns A new shuffled array
204
225
  */
205
226
  declare function shuffle<TElem>(array: readonly TElem[]): TElem[];
206
227
 
207
228
  /**
208
- * Creates a new sorted array in ascending or descending order based on one or multiple sorting criteria.
229
+ * Creates new array sorted in ascending/descending order with single or multiple criteria.
209
230
  *
210
231
  * @example
211
232
  * sort([1, 2, 3, 4], { order: 'desc' })
@@ -219,14 +240,15 @@ declare function shuffle<TElem>(array: readonly TElem[]): TElem[];
219
240
  * { order: 'desc', by: item => item.b }
220
241
  * )
221
242
  * // => [{ a: 1, b: 2 }, { a: 1, b: 1 }, { a: 2, b: 1 }]
222
- * @param array The array to sort.
223
- * @param orders The sorting criteria, one or multiple objects with properties order (either 'asc' or 'desc') and by (iteratee function to sort based on a specific property).
224
- * @param orders.order - The order to sort in, either 'asc' or 'desc'.
225
- * @param orders.by - The iteratee function to sort based on a specific property.
226
- * @template TElem The type of the array elements.
227
- * @returns Returns a new sorted array.
243
+ *
244
+ * @param array Array to sort
245
+ * @param criteria Criteria to sort by
246
+ * @param criteria.order Order to sort in, either 'asc' or 'desc'
247
+ * @param criteria.by Iteratee function to sort based on a specific property
248
+ * @template TElem Type of the array elements
249
+ * @returns New sorted array
228
250
  */
229
- declare function sort<TElem>(array: readonly TElem[], ...orders: {
251
+ declare function sort<TElem>(array: readonly TElem[], ...criteria: {
230
252
  order?: "asc" | "desc";
231
253
  by?: (item: TElem) => number | bigint | Date | string;
232
254
  }[]): TElem[];
@@ -272,10 +294,9 @@ declare function takeRightWhile<TElem>(array: readonly TElem[], predicate: (elem
272
294
  declare function takeWhile<TElem>(array: readonly TElem[], predicate: (elem: TElem) => boolean): TElem[];
273
295
 
274
296
  /**
275
- * Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept.
276
- * The order of result values is determined by the order they occur in the array.
297
+ * Creates unique array retaining first occurrence of elements.
277
298
  *
278
- * A compare function is optional to specify how the array is compared (default is `===`).
299
+ * A compare function is optional (default is `===`).
279
300
  *
280
301
  * @example
281
302
  * unique([2, 1, 2])
@@ -295,10 +316,10 @@ declare function takeWhile<TElem>(array: readonly TElem[], predicate: (elem: TEl
295
316
  * unique(users, (a, b) => a.name === b.name)
296
317
  * // => [{ id: 1, name: 'john' }]
297
318
  *
298
- * @param array The array to inspect.
299
- * @param iteratee The iteratee invoked per element.
300
- * @template TElem The type of the array elements.
301
- * @returns Returns the new duplicate free array.
319
+ * @param array Array to inspect
320
+ * @param iteratee Iteratee invoked per element
321
+ * @template TElem Type of the array elements
322
+ * @returns A new unique array
302
323
  */
303
324
  declare function unique<TElem>(array: readonly TElem[], compareFn?: (a: TElem, b: TElem) => boolean): TElem[];
304
325
 
@@ -795,7 +816,8 @@ declare function round(number: number, precision?: number): number;
795
816
  * @param numbers The input array of numbers
796
817
  * @returns The sum of the input array
797
818
  */
798
- declare function sum(numbers: readonly number[]): number;
819
+ declare function sum(numbers: number[]): number;
820
+ declare function sum<TNum extends readonly number[]>(numbers: TNum): Call<Tuples.Sum, TNum>;
799
821
 
800
822
  /**
801
823
  * The type of a plain object.
@@ -811,6 +833,8 @@ declare function sum(numbers: readonly number[]): number;
811
833
  */
812
834
  type PlainObject = Record<PropertyKey, unknown>;
813
835
 
836
+ type StringIfNever<Type> = [Type] extends [never] ? string : Type;
837
+ type Paths$1<TObj> = StringIfNever<Call<Objects.AllPaths, TObj>>;
814
838
  /**
815
839
  * Flattens an object into a single level object.
816
840
  *
@@ -823,7 +847,7 @@ type PlainObject = Record<PropertyKey, unknown>;
823
847
  * @template TObj The type of the object to flatten.
824
848
  * @returns A new object with flattened keys.
825
849
  */
826
- declare function flatKeys<TObj extends PlainObject>(obj: TObj): Record<string, unknown>;
850
+ declare function flatKeys<TObj extends PlainObject>(obj: TObj): Record<Paths$1<TObj>, unknown>;
827
851
 
828
852
  /**
829
853
  * This function combines two or more objects into a single new object. Arrays and other types are overwritten.
@@ -890,6 +914,8 @@ declare function omit<TObj extends PlainObject, Key extends keyof TObj>(object:
890
914
  */
891
915
  declare function pick<TObj extends PlainObject, Key extends keyof TObj>(object: TObj, keysToPick: Key[]): Pick<TObj, Key>;
892
916
 
917
+ type Paths<TObj> = Call<Objects.AllPaths, TObj> | string & {};
918
+ type UpdateObj<TObj extends PlainObject, TPath extends string, TVal> = Call<Objects.Update<TPath, TVal>, TObj>;
893
919
  /**
894
920
  * Sets the value at path of object. If a portion of path doesn’t exist, it’s created.
895
921
  *
@@ -914,9 +940,11 @@ declare function pick<TObj extends PlainObject, Key extends keyof TObj>(object:
914
940
  * @param path The path of the property to set.
915
941
  * @param value The value to set.
916
942
  * @template TObj The type of the object.
943
+ * @template TPath The type of the object path.
944
+ * @template TVal The type of the value to set.
917
945
  * @returns The modified object.
918
946
  */
919
- declare function set(obj: PlainObject, path: string, value: unknown): PlainObject;
947
+ declare function set<TObj extends PlainObject, TPath extends Paths<TObj>, TVal>(obj: TObj, path: TPath, value: TVal): UpdateObj<TObj, TPath, TVal>;
920
948
 
921
949
  /**
922
950
  * A class for managing a queue of async functions that runs a set number concurrently.
@@ -1182,6 +1210,22 @@ declare function kebabCase(str: string): string;
1182
1210
  */
1183
1211
  declare function pascalCase(str: string): string;
1184
1212
 
1213
+ /**
1214
+ * Replaces the last occurrence of a string.
1215
+ *
1216
+ * @example
1217
+ * ```typescript
1218
+ * replaceLast("Foo Bar Bar", "Bar", "Boo");
1219
+ * // => "Foo Bar Boo"
1220
+ * ```
1221
+ *
1222
+ * @param str The string to replace in.
1223
+ * @param searchFor The string to search for.
1224
+ * @param replaceWith The string to replace with.
1225
+ * @returns The replaced string.
1226
+ */
1227
+ declare function replaceLast(str: string, searchFor: string, replaceWith: string): string;
1228
+
1185
1229
  /**
1186
1230
  * Converts a string to snake_case.
1187
1231
  *
@@ -1370,4 +1414,4 @@ declare function isPlainObject(value: unknown): value is PlainObject;
1370
1414
  */
1371
1415
  declare function isUrl(str: string): boolean;
1372
1416
 
1373
- export { ArrayMinLength, GenericFunction, Jsonifiable, PlainObject, Queue, average, camelCase, capitalize, chunk, count, debounce, deburr, decDebounce, decMaxCalls, decMemoize, decMinCalls, decThrottle, difference, dropRightWhile, dropWhile, escapeHtml, escapeRegExp, flatKeys, group, hash, intersection, isEmpty, isEqual, isPlainObject, isUrl, kebabCase, maxCalls, median, memoize, merge, minCalls, omit, pascalCase, pick, races, randomElem, randomFloat, randomInt, randomString, range, retry, round, set, shuffle, sleep, snakeCase, sort, splitWords, sum, takeRightWhile, takeWhile, throttle, timeout, times, titleCase, toDecorator, trim, trimEnd, trimStart, tryCatch, unescapeHtml, unique };
1417
+ export { ArrayMinLength, GenericFunction, Jsonifiable, PlainObject, Queue, average, camelCase, capitalize, chunk, count, debounce, deburr, decDebounce, decMaxCalls, decMemoize, decMinCalls, decThrottle, difference, dropRightWhile, dropWhile, escapeHtml, escapeRegExp, flatKeys, group, hash, intersection, isEmpty, isEqual, isPlainObject, isUrl, kebabCase, maxCalls, median, memoize, merge, minCalls, move, omit, pascalCase, pick, races, randomElem, randomFloat, randomInt, randomString, range, replaceLast, retry, round, set, shuffle, sleep, snakeCase, sort, splitWords, sum, takeRightWhile, takeWhile, throttle, timeout, times, titleCase, toDecorator, trim, trimEnd, trimStart, tryCatch, unescapeHtml, unique };
package/dist/index.js CHANGED
@@ -1,9 +1,8 @@
1
1
  // src/array/chunk.ts
2
2
  function chunk(array, chunkSize) {
3
3
  const intSize = Math.trunc(chunkSize);
4
- if (array.length === 0 || intSize < 1) {
4
+ if (array.length === 0 || intSize < 1)
5
5
  return [];
6
- }
7
6
  let index = 0;
8
7
  let resultIndex = 0;
9
8
  const result = new Array(Math.ceil(array.length / intSize));
@@ -116,6 +115,25 @@ function intersection(...arraysOrCompareFn) {
116
115
  return intersection2;
117
116
  }
118
117
 
118
+ // src/array/move.ts
119
+ function move(array, fromIndex, toIndex) {
120
+ if (fromIndex < 0 || fromIndex >= array.length)
121
+ throw new Error(`Invalid 'fromIndex': ${fromIndex}. Must be between 0 and ${array.length - 1}.`);
122
+ if (toIndex < 0 || toIndex >= array.length)
123
+ throw new Error(`Invalid 'toIndex': ${toIndex}. Must be between 0 and ${array.length - 1}.`);
124
+ if (fromIndex === toIndex)
125
+ return array;
126
+ const item = array[fromIndex];
127
+ if (fromIndex < toIndex)
128
+ for (let index = fromIndex; index < toIndex; index++)
129
+ array[index] = array[index + 1];
130
+ else
131
+ for (let index = fromIndex; index > toIndex; index--)
132
+ array[index] = array[index - 1];
133
+ array[toIndex] = item;
134
+ return array;
135
+ }
136
+
119
137
  // src/array/range.ts
120
138
  function* range(start, end, step = 1) {
121
139
  if (start > end)
@@ -138,9 +156,9 @@ function shuffle(array) {
138
156
  }
139
157
 
140
158
  // src/array/sort.ts
141
- function sort(array, ...orders) {
159
+ function sort(array, ...criteria) {
142
160
  return [...array].sort((a, b) => {
143
- for (const { order, by } of orders) {
161
+ for (const { order, by } of criteria) {
144
162
  const aValue = by ? by(a) : a;
145
163
  const bValue = by ? by(b) : b;
146
164
  if (aValue < bValue) {
@@ -468,7 +486,7 @@ function omit(object, keysToOmit) {
468
486
  }
469
487
 
470
488
  // src/object/set.ts
471
- var validPathRegex = /^(?:[^.[\]]+(?:\[\d+])*(?:\.|\[\d+]))+(?:[^.[\]]+(?:\[\d+])*)+$/;
489
+ var validPathRegex = /^[^.[\]]+(?:\.[^.[\]]+)*(?:\[\d+])*(?:\.[^.[\]]+(?:\[\d+])*)*$/;
472
490
  var pathSplitRegex = /\.|(?=\[)/g;
473
491
  var matchBracketsRegex = /[[\]]/g;
474
492
  function set(obj, path, value) {
@@ -714,6 +732,14 @@ function pascalCase(str) {
714
732
  return pascalCase2;
715
733
  }
716
734
 
735
+ // src/string/replaceLast.ts
736
+ function replaceLast(str, searchFor, replaceWith) {
737
+ const index = str.lastIndexOf(searchFor);
738
+ if (index === -1)
739
+ return str;
740
+ return str.slice(0, index) + replaceWith + str.slice(index + searchFor.length);
741
+ }
742
+
717
743
  // src/string/snakeCase.ts
718
744
  function snakeCase(str) {
719
745
  str = deburr(str);
@@ -894,6 +920,7 @@ export {
894
920
  memoize,
895
921
  merge,
896
922
  minCalls,
923
+ move,
897
924
  omit,
898
925
  pascalCase,
899
926
  pick,
@@ -903,6 +930,7 @@ export {
903
930
  randomInt,
904
931
  randomString,
905
932
  range,
933
+ replaceLast,
906
934
  retry,
907
935
  round,
908
936
  set,