moderndash 0.3.0 → 0.3.2

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
@@ -6,17 +6,15 @@
6
6
  * @param chunkSize - The array to process.
7
7
  * @param array - The length of each chunk
8
8
  * @example
9
- * chunk(2, ['a', 'b', 'c', 'd'])
9
+ * chunk(['a', 'b', 'c', 'd'], 2)
10
10
  * // => [['a', 'b'], ['c', 'd']]
11
11
  *
12
- * chunk(3, ['a', 'b', 'c', 'd'])
12
+ * chunk(['a', 'b', 'c', 'd'], 3)
13
13
  * // => [['a', 'b', 'c'], ['d']]
14
14
  */
15
- declare function chunk<TInput>(chunkSize: number, array: TInput[]): TInput[][];
15
+ declare function chunk<TInput>(array: TInput[], chunkSize: number): TInput[][];
16
16
 
17
17
  type MinimumTwoArrays<TInput> = [TInput[], TInput[], ...TInput[][]];
18
- type IterateeFunction<T> = (value: T) => unknown;
19
- type PropertyShorthand<T> = keyof T;
20
18
  type RecordKey = string | number | symbol;
21
19
  /**
22
20
  * @description Generic function type, should fit any function
@@ -46,55 +44,31 @@ type GenericFunction<TFunc extends (...args: any) => any> = (...args: Parameters
46
44
  declare function count<TInput, TKey extends RecordKey>(array: TInput[], iteratee: (value: TInput) => TKey): Record<TKey, number>;
47
45
 
48
46
  /**
49
- * Creates an array of `array` values not included in the other given arrays. The order and references of result values are determined by the first array.
50
- *
51
- * **Note:** Unlike `pullAll`, this method returns a new array.
47
+ * Creates an array values not included in the other given arrays.
48
+ * The order and references of result values are determined by the first array.
52
49
  *
53
- * @category Array
54
- * @param arrays - First array is inspected, others are excluded.
55
- * @returns Returns the new array of filtered values.
50
+ * An compare function is optinal to specify how the elements of the arrays are compared.
51
+ * Default compare function is {@link isEqual}.
56
52
  * @example
57
53
  * difference([2, 1], [2, 3])
58
54
  * // => [1]
59
- */
60
- declare function difference<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[];
61
-
62
- /**
63
- * This method is like `difference` except that it accepts `iteratee` which
64
- * is invoked for each element of `array` and `values` to generate the criterion
65
- * by which they're compared. The order and references of result values are
66
- * determined by the first array.
67
55
  *
68
- * **Note:** Unlike `pullAllBy`, this method returns a new array.
56
+ * // ---- Custom compare function ----
57
+ * intersection((a, b) => Math.floor(a) === Math.floor(b), [1.2, 3.1], [1.3, 2.4])
58
+ * // => [3.1]
69
59
  *
70
- * @category Array
71
- * @param iteratee - The iteratee invoked per element. Or property shorthand.
72
- * @param arrays - First array to inspect. Others are excluded.
73
-
74
- * @returns Returns the new array of filtered values.
75
- * @example
76
- * differenceBy(Math.floor, [2.1, 1.2], [2.3, 3.4])
77
- * // => [1.2]
78
- * differenceBy('x', [{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }])
79
- * // => [{ 'x': 2 }]
80
- */
81
- declare function differenceBy<T>(iteratee: IterateeFunction<T> | PropertyShorthand<T>, ...arrays: MinimumTwoArrays<T>): T[];
82
-
83
- /**
84
- * This method is like `difference` except that it accepts `comparator`
85
- * which is invoked to compare elements of `array` to `values`.
86
- * The order and references of result values are determined by the first array.
87
- * The comparator is invoked with two arguments: (arrVal, othVal).
60
+ * // ---- Only compare by id ----
61
+ * const arr1 = [{ id: 1, name: 'Yeet' }, { id: 3, name: 'John' }];
62
+ * const arr2 = [{ id: 3, 'Carl' }, { id: 4, name: 'Max' }];
88
63
  *
89
- * @example
90
- * differenceWith(isEqual, [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }], [{ 'x': 1, 'y': 2 }])
91
- * // => [{ 'x': 2, 'y': 1 }]
64
+ * difference((a, b) => a.id === b.id, arr1, arr2)
65
+ * // => [{ id: 1, name: 'Yeet' }]
92
66
  * @category Array
93
- * @param comparator - The comparator invoked per element.
94
- * @param arrays - First array to inspect. Others are excluded.
67
+ * @param arrays - First array is inspected, others are excluded.
95
68
  * @returns Returns the new array of filtered values.
96
69
  */
97
- declare function differenceWith<T>(comparator: (a: T, b: T) => boolean, ...arrays: MinimumTwoArrays<T>): T[];
70
+ declare function difference<TArr>(...arrays: MinimumTwoArrays<TArr>): TArr[];
71
+ declare function difference<TArr>(arrayOrCompFn: (a: TArr, b: TArr) => boolean, ...arrays: MinimumTwoArrays<TArr>): TArr[];
98
72
 
99
73
  /**
100
74
  * Creates a slice of `array` excluding elements dropped from the end.
@@ -162,50 +136,29 @@ declare function group<T, U extends RecordKey>(array: T[], iteratee: (value: T)
162
136
  * Creates an array of unique values that are included in all given arrays.
163
137
  * The order and references of result values are determined by the first array.
164
138
  *
165
- * @category Array
166
- * @param arrays - The arrays to inspect.
167
- * @returns Returns the new array of intersecting values.
139
+ * An compare function is optinal to specify how the elements of the arrays are compared.
140
+ * Default compare function is {@link isEqual}.
141
+ *
168
142
  * @example
169
143
  * intersection([2, 1], [2, 3])
170
144
  * // => [2]
171
- */
172
- declare function intersection<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[];
173
-
174
- /**
175
- * This method is like `intersection` except that it accepts `iteratee`
176
- * which is invoked for each element of each `arrays` to generate the criterion
177
- * by which they're compared. The order and references of result values are
178
- * determined by the first array. The iteratee is invoked with one argument:
179
- * (value).
180
145
  *
181
- * @example
182
- * intersectionBy(Math.floor, [2.1, 1.2], [2.3, 3.4])
183
- * // => [2.1]
184
- * @category Array
185
- * @param iteratee - The iteratee invoked per element. Or property shorthand.
186
- * @param arrays - The arrays to inspect.
187
- * @returns Returns the new array of intersecting values.
188
- */
189
- declare function intersectionBy<TInput>(iteratee: IterateeFunction<TInput> | PropertyShorthand<TInput>, ...arrays: MinimumTwoArrays<TInput>): TInput[];
190
-
191
- /**
192
- * This method is like `intersection` except that it accepts `comparator`
193
- * which is invoked to compare elements of `arrays`. The order and references
194
- * of result values are determined by the first array. The comparator is
195
- * invoked with two arguments: (arrVal, othVal).
146
+ * // ---- Custom compare function ----
147
+ * intersection((a, b) => Math.floor(a) === Math.floor(b), [1.2, 1.1], [1.3, 2.4])
148
+ * // => [1.2]
196
149
  *
197
- * @example
198
- * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
199
- * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
150
+ * // ---- Only compare by id ----
151
+ * const arr1 = [{ id: 1, name: 'Yeet' }, { id: 3, name: 'John' }];
152
+ * const arr2 = [{ id: 3, 'Carl' }, { id: 4, name: 'Max' }];
200
153
  *
201
- * intersectionWith(isEqual, objects, others)
202
- * // => [{ 'x': 1, 'y': 2 }]
154
+ * intersection((a, b) => a.id === b.id, arr1, arr2)
155
+ * // => [{ id: 3, name: 'John' }]
203
156
  * @category Array
204
- * @param comparator - The comparator invoked per element.
205
157
  * @param arrays - The arrays to inspect.
206
158
  * @returns Returns the new array of intersecting values.
207
159
  */
208
- declare function intersectionWith<T>(comparator: (a: T, b: T) => boolean, ...arrays: MinimumTwoArrays<T>): T[];
160
+ declare function intersection<TArr>(...arrays: MinimumTwoArrays<TArr>): TArr[];
161
+ declare function intersection<TArr>(arrayOrCompFn: (a: TArr, b: TArr) => boolean, ...arrays: MinimumTwoArrays<TArr>): TArr[];
209
162
 
210
163
  /**
211
164
  * Gets a random element from `array`.
@@ -313,7 +266,7 @@ declare function takeWhile<T>(array: T[], predicate: (elem: T) => boolean): T[];
313
266
  * Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept.
314
267
  * The order of result values is determined by the order they occur in the array.
315
268
  *
316
- * An iteratee function is optional, to specify the value to be compared.
269
+ * An compare function is optinal to specify how the array is compared.
317
270
  *
318
271
  * @example
319
272
  * unique([2, 1, 2])
@@ -324,7 +277,7 @@ declare function takeWhile<T>(array: T[], predicate: (elem: T) => boolean): T[];
324
277
  * { id: 1, name: 'c' }
325
278
  * ]
326
279
  *
327
- * unique(users, value => value.id)
280
+ * unique(users, (a, b) => a.id === b.id)
328
281
  * // => [{ id: 1, name: 'a' }]
329
282
  *
330
283
  *
@@ -333,70 +286,7 @@ declare function takeWhile<T>(array: T[], predicate: (elem: T) => boolean): T[];
333
286
  * @param iteratee - The iteratee invoked per element.
334
287
  * @returns Returns the new duplicate free array.
335
288
  */
336
- declare function unique<TInput>(array: TInput[], iteratee?: IterateeFunction<TInput>): TInput[];
337
-
338
- /**
339
- * This method is like `zip` except that it accepts an array of grouped
340
- * elements and creates an array regrouping the elements to their pre-zip configuration.
341
- *
342
- * @example
343
- * const zipped = zip(['a', 'b'], [1, 2], [true, false])
344
- * // => [['a', 1, true], ['b', 2, false]]
345
- *
346
- * unzip(zipped)
347
- * // => [['a', 'b'], [1, 2], [true, false]]
348
- * @category Array
349
- * @param array - The array of grouped elements to process.
350
- * @returns Returns the new array of regrouped elements.
351
- */
352
- declare function unzip<TInput extends unknown[]>(array: TInput[]): TInput[];
353
-
354
- /**
355
- * This method is like `unzip` except that it accepts `iteratee` to specify how regrouped values should be combined.
356
- *
357
- * @example
358
- * const zipped = zip([1, 2], [10, 20], [100, 200])
359
- * // => [[1, 10, 100], [2, 20, 200]]
360
- *
361
- * unzipWith(add, zipped)
362
- * // => [3, 30, 300]
363
- * @category Array
364
- * @param iteratee - The function to combine regrouped values.
365
- * @param array - The array of grouped elements to process.
366
- * @returns Returns the new array of regrouped elements.
367
- */
368
- declare function unzipWith<TInput extends unknown[], TOutput>(array: TInput[], iteratee: (...t: TInput) => TOutput): TOutput[];
369
-
370
- /**
371
- * Creates an array of grouped elements, the first of which contains the first elements of the given arrays,
372
- * the second of which contains the second elements of the given arrays, and so on.
373
- *
374
- * @category Array
375
- * @param arrays - The arrays to process.
376
- * @returns Returns the new array of grouped elements.
377
- * @example
378
- * zip(['a', 'b'], [1, 2], [true, false])
379
- * // => [['a', 1, true], ['b', 2, false]]
380
- */
381
- declare function zip<TInput>(...arrays: TInput[][]): TInput[][];
382
-
383
- type UnZip<A extends readonly unknown[]> = {
384
- [K in keyof A]: readonly A[K][];
385
- };
386
- /**
387
- * This method is like `zip` except that it accepts `iteratee` to specify
388
- * how grouped values should be combined. The iteratee is invoked with the
389
- * elements of each group: (...group).
390
- *
391
- * @category Array
392
- * @param combineFunc - The function to combine grouped values.
393
- * @param arrays - The arrays to process.
394
- * @returns Returns the new array of grouped elements.
395
- * @example
396
- * zipWith([1, 2], [10, 20], [100, 200], (a, b, c) => a + b + c)
397
- * // => [111, 222]
398
- */
399
- declare function zipWith<Args extends unknown[], TOutput>(combineFunc: (...args: Args) => TOutput, ...arrays: UnZip<Args>): TOutput[];
289
+ declare function unique<TInput>(array: TInput[], compareFn?: (a: TInput, b: TInput) => boolean): TInput[];
400
290
 
401
291
  /**
402
292
  * The opposite of `before`. This method creates a function that invokes `func` once it's called `n` or more times.
@@ -730,11 +620,13 @@ declare function unescapeHtml(str: string): string;
730
620
  /**
731
621
  * A `number` that is an integer.
732
622
  * You can't pass a `bigint` as they are already guaranteed to be integers.
623
+ *
733
624
  * Use-case: Validating and documenting parameters.
734
625
  * @example
735
- * function setYear<T extends number>(length: Integer<T>) {
736
- * // ...
737
- * };
626
+ * function setYear<T extends number>(x: Integer<T>){};
627
+ *
628
+ * setYear(1); // OK
629
+ * setYear(1.1); // Error
738
630
  * @category type
739
631
  */
740
632
  type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;
@@ -744,9 +636,10 @@ type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;
744
636
  * You can't pass a `bigint` as they are already guaranteed to be integers.
745
637
  * Use-case: Validating and documenting parameters.
746
638
  * @example
747
- * function setPercentage<T extends number>(length: Float<T>) {
748
- * // ...
749
- * };
639
+ * function setPercentage<T extends number>(x: Float<T>) {};
640
+ *
641
+ * setPercentage(1.1); // OK
642
+ * setPercentage(1); // Error
750
643
  *
751
644
  * @category type
752
645
  */
@@ -754,32 +647,40 @@ type Float<T extends number> = T extends Integer<T> ? never : T;
754
647
 
755
648
  /**
756
649
  * A negative `number`/`bigint` (`-∞ < x < 0`)
650
+ *
757
651
  * Use-case: Validating and documenting parameters.
758
652
  * @example
759
- * function setLength<T extends number>(length: Negative<T>) {
760
- * // ...
761
- * };
653
+ * function setNegative<T extends number>(x: Negative<T>) {};
654
+ *
655
+ * setNegative(-1.2); // OK
656
+ * setNegative(1); // Error
657
+ *
658
+ * function setNegInt<T extends number>(x: Negative<Integer<T>>) {};
762
659
  *
763
- * function setLength<T extends number>(length: Negative<Integer<T>>) {
764
- * // ...
765
- * };
660
+ * setNegInt(-1); // OK
661
+ * setNegInt(1); // Error
662
+ * setNegInt(-1.1); // Error
766
663
  * @category type
767
664
  */
768
665
  type Negative<T extends number | bigint> = T extends 0 | 0n ? never : `${T}` extends `-${string}` ? T : never;
769
666
 
770
667
  /**
771
- A positive `number`/`bigint` (`0 < x < ∞`).
772
- Use-case: Validating and documenting parameters.
773
- @example
774
- function setLength<T extends number>(length: Positive<T>) {
775
- // ...
776
- };
777
-
778
- function setLength<T extends number>(length: Positive<Integer<T>>) {
779
- // ...
780
- };
781
-
782
- @category type
668
+ * A positive `number`/`bigint` (`0 < x < ∞`).
669
+ *
670
+ * Use-case: Validating and documenting parameters.
671
+ * @example
672
+ * function setPositive<T extends number>(x: Positive<T>) {};
673
+ *
674
+ * setPositive(1.2); // OK
675
+ * setPositive(-1); // Error
676
+ *
677
+ * function setPosInt<T extends number>(x: Positive<Integer<T>>) {};
678
+ *
679
+ * setPosInt(1); // OK
680
+ * setPosInt(-1); // Error
681
+ * setPosInt(1.1); // Error
682
+ *
683
+ * @category type
783
684
  */
784
685
  type Positive<T extends number | bigint> = T extends 0 | 0n ? never : Negative<T> extends never ? T : never;
785
686
 
@@ -857,4 +758,4 @@ declare function isPlainObject(value: unknown): value is object;
857
758
  */
858
759
  declare function isUrl(str: string): boolean;
859
760
 
860
- export { Float, Integer, Negative, Positive, after, before, camelCase, capitalize, chunk, count, debounce, deburr, difference, differenceBy, differenceWith, dropRightWhile, dropWhile, escapeHtml, escapeRegExp, group, intersection, intersectionBy, intersectionWith, isEmpty, isEqual, isPlainObject, isUrl, kebabCase, memoize, omit, once, pascalCase, pick, sample, sampleSize, shuffle, sleep, snakeCase, sort, startCase, stripSpecial, takeRightWhile, takeWhile, throttle, times, unescapeHtml, unique, unzip, unzipWith, zip, zipWith };
761
+ export { Float, Integer, Negative, Positive, after, before, camelCase, capitalize, chunk, count, debounce, deburr, difference, dropRightWhile, dropWhile, escapeHtml, escapeRegExp, group, intersection, isEmpty, isEqual, isPlainObject, isUrl, kebabCase, memoize, omit, once, pascalCase, pick, sample, sampleSize, shuffle, sleep, snakeCase, sort, startCase, stripSpecial, takeRightWhile, takeWhile, throttle, times, unescapeHtml, unique };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/array/chunk.ts
2
- function chunk(chunkSize, array) {
2
+ function chunk(array, chunkSize) {
3
3
  const sizeInteger = Math.trunc(chunkSize);
4
4
  if (array.length === 0 || sizeInteger < 1) {
5
5
  return [];
@@ -27,18 +27,6 @@ function count(array, iteratee) {
27
27
  return result;
28
28
  }
29
29
 
30
- // src/array/differenceWith.ts
31
- function differenceWith(comparator, ...arrays) {
32
- const difference2 = [];
33
- const [firstArray, ...restArrays] = arrays;
34
- firstArray.forEach((element) => {
35
- if (!restArrays.some((array) => array.some((item) => comparator(item, element)))) {
36
- difference2.push(element);
37
- }
38
- });
39
- return difference2;
40
- }
41
-
42
30
  // src/validate/isEqual.ts
43
31
  function isEqual(value1, value2) {
44
32
  if (value1 === value2)
@@ -79,33 +67,17 @@ function isSameArray(value1, value2) {
79
67
  }
80
68
 
81
69
  // src/array/difference.ts
82
- function difference(...arrays) {
83
- return differenceWith(isEqual, ...arrays);
84
- }
85
-
86
- // src/helpers/typeofChecks.ts
87
- function isObjectKey(key) {
88
- return typeof key === "string" || typeof key === "number" || typeof key === "symbol";
89
- }
90
-
91
- // src/helpers/shortHands.ts
92
- function getPropertyShorthand(key) {
93
- return (object) => object[key];
94
- }
95
- function getIterateFunction(iteratee) {
96
- if (typeof iteratee === "function") {
97
- return iteratee;
98
- } else if (isObjectKey(iteratee)) {
99
- return getPropertyShorthand(iteratee);
100
- } else {
101
- throw new TypeError("Expected iteratee to be a function or a property name");
102
- }
103
- }
104
-
105
- // src/array/differenceBy.ts
106
- function differenceBy(iteratee, ...arrays) {
107
- const iterateeFn = getIterateFunction(iteratee);
108
- return differenceWith((a, b) => isEqual(iterateeFn(a), iterateeFn(b)), ...arrays);
70
+ function difference(arrayOrCompFn, ...arrays) {
71
+ const withCompareFn = typeof arrayOrCompFn === "function";
72
+ const compareFN = withCompareFn ? arrayOrCompFn : isEqual;
73
+ const [firstArray, ...restArrays] = withCompareFn ? arrays : [arrayOrCompFn, ...arrays];
74
+ const difference2 = [];
75
+ firstArray.forEach((element) => {
76
+ if (!restArrays.some((array) => array.some((item) => compareFN(item, element)))) {
77
+ difference2.push(element);
78
+ }
79
+ });
80
+ return difference2;
109
81
  }
110
82
 
111
83
  // src/array/dropRightWhile.ts
@@ -134,29 +106,20 @@ function group(array, iteratee) {
134
106
  return result;
135
107
  }
136
108
 
137
- // src/array/intersectionWith.ts
138
- function intersectionWith(comparator, ...arrays) {
109
+ // src/array/intersection.ts
110
+ function intersection(arrayOrCompFn, ...arrays) {
111
+ const withCompareFn = typeof arrayOrCompFn === "function";
112
+ const compareFN = withCompareFn ? arrayOrCompFn : isEqual;
113
+ const [firstArray, ...restArrays] = withCompareFn ? arrays : [arrayOrCompFn, ...arrays];
139
114
  const intersection2 = [];
140
- const [firstArray, ...restArrays] = arrays;
141
115
  firstArray.forEach((element) => {
142
- if (restArrays.every((array) => array.some((item) => comparator(item, element)))) {
116
+ if (restArrays.every((array) => array.some((item) => compareFN(item, element)))) {
143
117
  intersection2.push(element);
144
118
  }
145
119
  });
146
120
  return intersection2;
147
121
  }
148
122
 
149
- // src/array/intersection.ts
150
- function intersection(...arrays) {
151
- return intersectionWith(isEqual, ...arrays);
152
- }
153
-
154
- // src/array/intersectionBy.ts
155
- function intersectionBy(iteratee, ...arrays) {
156
- const iterateeFn = getIterateFunction(iteratee);
157
- return intersectionWith((a, b) => isEqual(iterateeFn(a), iterateeFn(b)), ...arrays);
158
- }
159
-
160
123
  // src/array/sample.ts
161
124
  function sample(array) {
162
125
  if (array.length === 0) {
@@ -236,42 +199,12 @@ function takeWhile(array, predicate) {
236
199
  }
237
200
 
238
201
  // src/array/unique.ts
239
- function unique(array, iteratee) {
240
- const compareFn = (a, b) => isEqual(iteratee ? iteratee(a) : a, iteratee ? iteratee(b) : b);
202
+ function unique(array, compareFn = (a, b) => isEqual(a, b)) {
241
203
  return array.filter((value, index, self) => {
242
204
  return self.findIndex((otherValue) => compareFn(value, otherValue)) === index;
243
205
  });
244
206
  }
245
207
 
246
- // src/array/unzipWith.ts
247
- function unzipWith(array, iteratee) {
248
- const result = [];
249
- for (const elements of array) {
250
- result.push(iteratee(...elements));
251
- }
252
- return result;
253
- }
254
-
255
- // src/array/unzip.ts
256
- function unzip(array) {
257
- return unzipWith(array, (...t) => t);
258
- }
259
-
260
- // src/array/zipWith.ts
261
- function zipWith(combineFunc, ...arrays) {
262
- const len = Math.min(...arrays.map((a) => a.length));
263
- const zipped = [];
264
- for (let i = 0; i < len; i++) {
265
- zipped[i] = combineFunc(...arrays.map((a) => a[i]));
266
- }
267
- return zipped;
268
- }
269
-
270
- // src/array/zip.ts
271
- function zip(...arrays) {
272
- return zipWith((...t) => t, ...arrays);
273
- }
274
-
275
208
  // src/function/after.ts
276
209
  function after(n, func) {
277
210
  let count2 = 1;
@@ -596,16 +529,12 @@ export {
596
529
  debounce,
597
530
  deburr,
598
531
  difference,
599
- differenceBy,
600
- differenceWith,
601
532
  dropRightWhile,
602
533
  dropWhile,
603
534
  escapeHtml,
604
535
  escapeRegExp,
605
536
  group,
606
537
  intersection,
607
- intersectionBy,
608
- intersectionWith,
609
538
  isEmpty,
610
539
  isEqual,
611
540
  isPlainObject,
@@ -629,10 +558,6 @@ export {
629
558
  throttle,
630
559
  times,
631
560
  unescapeHtml,
632
- unique,
633
- unzip,
634
- unzipWith,
635
- zip,
636
- zipWith
561
+ unique
637
562
  };
638
563
  //# sourceMappingURL=index.js.map