moderndash 2.1.2 → 2.1.4

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
@@ -12,7 +12,7 @@
12
12
  * @template TElem The type of the array elements.
13
13
  * @returns Returns the new array of chunks.
14
14
  */
15
- declare function chunk<TElem>(array: TElem[], size: number): TElem[][];
15
+ declare function chunk<TElem>(array: readonly TElem[], size: number): TElem[][];
16
16
 
17
17
  /**
18
18
  * Creates an object with counts of occurrences of items in the array.
@@ -24,18 +24,18 @@ declare function chunk<TElem>(array: TElem[], size: number): TElem[][];
24
24
  * { 'user': 'fred', 'active': true, age: 40 }
25
25
  * ]
26
26
  *
27
- * count(users, value => value.active);
28
- * // => { 'true': 2, 'false': 1 }
27
+ * count(users, value => value.active ? 'active' : 'inactive');
28
+ * // => { 'active': 2, 'inactive': 1 }
29
29
  *
30
30
  * count(users, value => value.age);
31
- * // => { '36': 2, '40': 1 }
31
+ * // => { 36: 2, 40: 1 }
32
32
  *
33
33
  * @param criteria The criteria to count by.
34
34
  * @param array The array or record to iterate over.
35
35
  * @template TElem The type of the array elements.
36
36
  * @returns Returns the composed aggregate object.
37
37
  */
38
- declare function count<TElem, TKey extends PropertyKey>(array: TElem[], criteria: (value: TElem) => TKey): Record<TKey, number>;
38
+ declare function count<TElem, TKey extends PropertyKey>(array: readonly TElem[], criteria: (value: TElem) => TKey): Record<TKey, number>;
39
39
 
40
40
  /**
41
41
  * This type builds an array with a minimum length.
@@ -77,8 +77,8 @@ type BuildArrayMinLength<TElem, TMinLength extends number, Current extends TElem
77
77
  * @template TElem The type of the array elements.
78
78
  * @returns Returns the new array of filtered values.
79
79
  */
80
- declare function difference<TElem>(...arrays: ArrayMinLength<TElem[], 2>): TElem[];
81
- declare function difference<TElem>(arrayOrCompFn: (a: TElem, b: TElem) => boolean, ...arrays: ArrayMinLength<TElem[], 2>): TElem[];
80
+ declare function difference<TElem>(...arrays: ArrayMinLength<readonly TElem[], 2>): TElem[];
81
+ declare function difference<TElem>(arrayOrCompFn: (a: TElem, b: TElem) => boolean, ...arrays: ArrayMinLength<readonly TElem[], 2>): TElem[];
82
82
 
83
83
  /**
84
84
  * Creates a slice of `array` excluding elements dropped from the end.
@@ -98,7 +98,7 @@ declare function difference<TElem>(arrayOrCompFn: (a: TElem, b: TElem) => boolea
98
98
  * @template TElem The type of the array elements.
99
99
  * @returns Returns the slice of `array`.
100
100
  */
101
- declare function dropRightWhile<TElem>(array: TElem[], predicate: (value: TElem) => boolean): TElem[];
101
+ declare function dropRightWhile<TElem>(array: readonly TElem[], predicate: (value: TElem) => boolean): TElem[];
102
102
 
103
103
  /**
104
104
  * Creates a slice of `array` excluding elements dropped from the beginning.
@@ -118,7 +118,7 @@ declare function dropRightWhile<TElem>(array: TElem[], predicate: (value: TElem)
118
118
  * @template TElem The type of the array elements.
119
119
  * @returns Returns the slice of `array`.
120
120
  */
121
- declare function dropWhile<TElem>(array: TElem[], predicate: (value: TElem) => boolean): TElem[];
121
+ declare function dropWhile<TElem>(array: readonly TElem[], predicate: (value: TElem) => boolean): TElem[];
122
122
 
123
123
  /**
124
124
  * Creates an object with grouped items in the array.
@@ -127,15 +127,15 @@ declare function dropWhile<TElem>(array: TElem[], predicate: (value: TElem) => b
127
127
  * group([6.1, 4.2, 6.3], Math.floor)
128
128
  * // => { 4: [4.2], 6: [6.1, 6.3] }
129
129
  *
130
- * group([6.1, 4.2, 6.3], value => value > 5)
131
- * // => { 'false': [4.2], 'true': [6.1, 6.3] }
130
+ * group([6.1, 4.2, 6.3], value => value > 5 ? '>5' : '<=5')
131
+ * // => { '<=5': [4.2], '>5': [6.1, 6.3] }
132
132
  *
133
133
  * @param collection The array or object to iterate over.
134
- * @param criteria The criteria to group by.
134
+ * @param getGroupKey A function that returns the group id for each item.
135
135
  * @template TElem The type of the array elements.
136
136
  * @returns An object with grouped items.
137
137
  */
138
- declare function group<TElem, TKey extends PropertyKey>(array: TElem[], criteria: (value: TElem) => TKey | boolean): Record<TKey, TElem[]>;
138
+ declare function group<TElem, TKey extends PropertyKey>(array: readonly TElem[], getGroupKey: (elem: TElem) => TKey): Record<TKey, TElem[]>;
139
139
 
140
140
  /**
141
141
  * Create an array with unique values from all input arrays, with order based on the first array.
@@ -158,10 +158,11 @@ declare function group<TElem, TKey extends PropertyKey>(array: TElem[], criteria
158
158
  * intersection((a, b) => a.id === b.id, arr1, arr2)
159
159
  * // => [{ id: 3, name: 'John' }]
160
160
  * @param arrays The arrays to inspect.
161
+ * @template TElem The type of the array elements.
161
162
  * @returns Returns the new array of intersecting values.
162
163
  */
163
- declare function intersection<TArr>(...arrays: ArrayMinLength<TArr[], 2>): TArr[];
164
- declare function intersection<TArr>(arrayOrCompFn: (a: TArr, b: TArr) => boolean, ...arrays: ArrayMinLength<TArr[], 2>): TArr[];
164
+ declare function intersection<TElem>(...arrays: ArrayMinLength<readonly TElem[], 2>): TElem[];
165
+ declare function intersection<TElem>(arrayOrCompFn: (a: TElem, b: TElem) => boolean, ...arrays: ArrayMinLength<readonly TElem[], 2>): TElem[];
165
166
 
166
167
  /**
167
168
  * Generates an iterable sequence of numbers starting from `start`, up to and including `end`,
@@ -192,9 +193,10 @@ declare function range(start: number, end: number, step?: number): Generator<num
192
193
  * shuffle([1, 2, 3, 4])
193
194
  * // => [4, 1, 3, 2]
194
195
  * @param array The array or object to shuffle.
196
+ * @template TElem The type of the array elements.
195
197
  * @returns Returns a new shuffled array.
196
198
  */
197
- declare function shuffle<TElem>(array: TElem[]): TElem[];
199
+ declare function shuffle<TElem>(array: readonly TElem[]): TElem[];
198
200
 
199
201
  /**
200
202
  * Creates a new sorted array in ascending or descending order based on one or multiple sorting criteria.
@@ -215,12 +217,13 @@ declare function shuffle<TElem>(array: TElem[]): TElem[];
215
217
  * @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).
216
218
  * @param orders.order - The order to sort in, either 'asc' or 'desc'.
217
219
  * @param orders.by - The iteratee function to sort based on a specific property.
220
+ * @template TElem The type of the array elements.
218
221
  * @returns Returns a new sorted array.
219
222
  */
220
- declare function sort<TInput>(array: TInput[], ...orders: {
223
+ declare function sort<TElem>(array: readonly TElem[], ...orders: {
221
224
  order?: 'asc' | 'desc';
222
- by?: (item: TInput) => number | bigint | Date | string;
223
- }[]): TInput[];
225
+ by?: (item: TElem) => number | bigint | Date | string;
226
+ }[]): TElem[];
224
227
 
225
228
  /**
226
229
  * Creates a slice of `array` with elements taken from the end.
@@ -237,9 +240,10 @@ declare function sort<TInput>(array: TInput[], ...orders: {
237
240
  * // => objects for ['fred', 'pebbles']
238
241
  * @param predicate The function invoked per iteration.
239
242
  * @param array The array to query.
243
+ * @template TElem The type of the array elements.
240
244
  * @returns Returns the slice of `array`.
241
245
  */
242
- declare function takeRightWhile<TArr>(array: TArr[], predicate: (elem: TArr) => boolean): TArr[];
246
+ declare function takeRightWhile<TElem>(array: readonly TElem[], predicate: (elem: TElem) => boolean): TElem[];
243
247
 
244
248
  /**
245
249
  * Creates a slice of `array` with elements taken from the beginning.
@@ -256,9 +260,10 @@ declare function takeRightWhile<TArr>(array: TArr[], predicate: (elem: TArr) =>
256
260
  * // => objects for ['barney', 'fred']
257
261
  * @param predicate The function invoked per iteration.
258
262
  * @param array The array to query.
263
+ * @template TElem The type of the array elements.
259
264
  * @returns Returns the slice of `array`.
260
265
  */
261
- declare function takeWhile<TArr>(array: TArr[], predicate: (elem: TArr) => boolean): TArr[];
266
+ declare function takeWhile<TElem>(array: readonly TElem[], predicate: (elem: TElem) => boolean): TElem[];
262
267
 
263
268
  /**
264
269
  * Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept.
@@ -273,7 +278,7 @@ declare function takeWhile<TArr>(array: TArr[], predicate: (elem: TArr) => boole
273
278
  * // compare by object values
274
279
  * const users = [
275
280
  * { id: 1, name: 'john' },
276
- * { id: 2, name: 'john' }
281
+ * { id: 2, name: 'john' },
277
282
  * { id: 2, name: 'john' },
278
283
  * ]
279
284
  *
@@ -286,9 +291,10 @@ declare function takeWhile<TArr>(array: TArr[], predicate: (elem: TArr) => boole
286
291
  *
287
292
  * @param array The array to inspect.
288
293
  * @param iteratee The iteratee invoked per element.
294
+ * @template TElem The type of the array elements.
289
295
  * @returns Returns the new duplicate free array.
290
296
  */
291
- declare function unique<TInput>(array: TInput[], compareFn?: (a: TInput, b: TInput) => boolean): TInput[];
297
+ declare function unique<TElem>(array: readonly TElem[], compareFn?: (a: TElem, b: TElem) => boolean): TElem[];
292
298
 
293
299
  type JsonifiableObject = {
294
300
  [Key in string]?: Jsonifiable;
@@ -742,7 +748,7 @@ declare function times<TInput>(func: (index: number) => TInput, n: number): TInp
742
748
  * @param numbers The input array of numbers
743
749
  * @returns The average of the input array, or NaN if the input array is empty
744
750
  */
745
- declare function average(numbers: number[]): number;
751
+ declare function average(numbers: readonly number[]): number;
746
752
 
747
753
  /**
748
754
  * Calculates the median of an array of numbers
@@ -755,7 +761,7 @@ declare function average(numbers: number[]): number;
755
761
  * @param numbers The input array of numbers
756
762
  * @returns The median of the input array
757
763
  */
758
- declare function median(numbers: number[]): number;
764
+ declare function median(numbers: readonly number[]): number;
759
765
 
760
766
  /**
761
767
  * Rounds a number to the given precision.
@@ -781,7 +787,7 @@ declare function round(number: number, precision?: number): number;
781
787
  * @param numbers The input array of numbers
782
788
  * @returns The sum of the input array
783
789
  */
784
- declare function sum(numbers: number[]): number;
790
+ declare function sum(numbers: readonly number[]): number;
785
791
 
786
792
  /**
787
793
  * The type of a plain object.
@@ -813,6 +819,8 @@ type PlainObject = Record<PropertyKey, unknown>;
813
819
  * // => { a: "Yes" }
814
820
  * @param target The target object
815
821
  * @param sources The source objects
822
+ * @template TTarget The type of the target object
823
+ * @template TSources The type of the source objects
816
824
  * @returns A new merged object
817
825
  */
818
826
  declare function merge<TTarget extends PlainObject, TSources extends ArrayMinLength<PlainObject, 1>>(target: TTarget, ...sources: TSources): MergeDeepObjects<[TTarget, ...TSources]>;
@@ -840,8 +848,8 @@ type MergeDeepObjects<A extends readonly [...unknown[]]> = A extends [infer L, .
840
848
  *
841
849
  * @param object The object to filter
842
850
  * @param keysToOmit The keys to exclude from the returned object
851
+ * @template TObj The type of the object
843
852
  * @returns - An object without the specified keys
844
- *
845
853
  */
846
854
  declare function omit<TObj extends PlainObject, Key extends keyof TObj>(object: TObj, keysToOmit: Key[]): Omit<TObj, Key>;
847
855
 
@@ -855,9 +863,10 @@ declare function omit<TObj extends PlainObject, Key extends keyof TObj>(object:
855
863
  * // => { 'a': 1, 'c': 3 }
856
864
  * @param object The source object.
857
865
  * @param keysToPick The property paths to pick.
866
+ * @template TObj The type of the object.
858
867
  * @returns Returns the new object.
859
868
  */
860
- declare function pick<TInput extends PlainObject, Key extends keyof TInput>(object: TInput, keysToPick: Key[]): Pick<TInput, Key>;
869
+ declare function pick<TObj extends PlainObject, Key extends keyof TObj>(object: TObj, keysToPick: Key[]): Pick<TObj, Key>;
861
870
 
862
871
  /**
863
872
  * Sets the value at path of object. If a portion of path doesn’t exist, it’s created.
@@ -870,6 +879,7 @@ declare function pick<TInput extends PlainObject, Key extends keyof TInput>(obje
870
879
  * @param obj The object to modify.
871
880
  * @param path The path of the property to set.
872
881
  * @param value The value to set.
882
+ * @template TObj The type of the object.
873
883
  * @returns The modified object.
874
884
  */
875
885
  declare function set<TObj extends PlainObject>(obj: TObj, path: string, value: unknown): TObj;
package/dist/index.js CHANGED
@@ -23,17 +23,31 @@ function count(array, criteria) {
23
23
  return result;
24
24
  }
25
25
 
26
+ // src/helpers/fastArrayFlat.ts
27
+ function fastArrayFlat(arrays) {
28
+ let result = arrays.shift() ?? [];
29
+ for (const array of arrays) {
30
+ result = [...result, ...array];
31
+ }
32
+ return result;
33
+ }
34
+
26
35
  // src/array/difference.ts
27
36
  function difference(arrayOrCompFn, ...arrays) {
28
37
  const withCompareFn = typeof arrayOrCompFn === "function";
29
- const compareFN = withCompareFn ? arrayOrCompFn : (a, b) => a === b;
30
- const [firstArray, ...restArrays] = withCompareFn ? arrays : [arrayOrCompFn, ...arrays];
38
+ const firstArray = withCompareFn ? arrays.shift() : arrayOrCompFn;
39
+ const combinedRestArray = fastArrayFlat(arrays);
40
+ if (!withCompareFn) {
41
+ const restSet = new Set(combinedRestArray);
42
+ return firstArray.filter((element) => !restSet.has(element));
43
+ }
44
+ const compareFN = arrayOrCompFn;
31
45
  const difference2 = [];
32
- firstArray.forEach((element) => {
33
- if (!restArrays.some((array) => array.some((item) => compareFN(item, element)))) {
46
+ for (const element of firstArray) {
47
+ if (combinedRestArray.every((item) => !compareFN(item, element))) {
34
48
  difference2.push(element);
35
49
  }
36
- });
50
+ }
37
51
  return difference2;
38
52
  }
39
53
 
@@ -53,14 +67,11 @@ function dropWhile(array, predicate) {
53
67
  }
54
68
 
55
69
  // src/array/group.ts
56
- function group(array, criteria) {
70
+ function group(array, getGroupKey) {
57
71
  const result = {};
58
- for (const value of array) {
59
- let key = criteria(value);
60
- if (typeof key === "boolean")
61
- key = key.toString();
62
- result[key] = result[key] ?? [];
63
- result[key].push(value);
72
+ for (const elem of array) {
73
+ const key = getGroupKey(elem);
74
+ (result[key] ??= []).push(elem);
64
75
  }
65
76
  return result;
66
77
  }
@@ -88,28 +99,27 @@ function unique(array, compareFn) {
88
99
  function intersection(arrayOrCompFn, ...arrays) {
89
100
  const withCompareFn = typeof arrayOrCompFn === "function";
90
101
  const firstArray = unique(withCompareFn ? arrays.shift() : arrayOrCompFn);
102
+ const combinedRestArray = fastArrayFlat(arrays);
91
103
  if (!withCompareFn) {
92
- const restSets = arrays.map((array) => new Set(array));
93
- return firstArray.filter((element) => restSets.every((set2) => set2.has(element)));
104
+ const restSet = new Set(combinedRestArray);
105
+ return firstArray.filter((element) => restSet.has(element));
94
106
  }
95
107
  const compareFN = arrayOrCompFn;
96
108
  const intersection2 = [];
97
- firstArray.forEach((element) => {
98
- if (arrays.every((array) => array.some((item) => compareFN(item, element)))) {
109
+ for (const element of firstArray) {
110
+ if (combinedRestArray.some((item) => compareFN(item, element))) {
99
111
  intersection2.push(element);
100
112
  }
101
- });
113
+ }
102
114
  return intersection2;
103
115
  }
104
116
 
105
117
  // src/array/range.ts
106
118
  function* range(start, end, step = 1) {
107
- if (start > end) {
119
+ if (start > end)
108
120
  throw new Error("The start of the range must be less than or equal to the end.");
109
- }
110
- if (step <= 0) {
121
+ if (step <= 0)
111
122
  throw new Error("The step must be greater than 0.");
112
- }
113
123
  for (let i = start; i <= end; i += step) {
114
124
  yield i;
115
125
  }