moderndash 0.0.10 → 0.0.11

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
@@ -14,12 +14,17 @@
14
14
  */
15
15
  declare function chunk<TInput>(chunkSize: number, array: TInput[]): TInput[][];
16
16
 
17
- type MinimumTwoArrays<T> = [T[], T[], ...T[][]];
17
+ type MinimumTwoArrays<TInput> = [TInput[], TInput[], ...TInput[][]];
18
18
  type IterateeFunction<T> = (value: T) => unknown;
19
19
  type PropertyShorthand<T> = keyof T;
20
20
  type RecordKey = string | number | symbol;
21
- type ArrayOrRecord<T> = T[] | Record<RecordKey, T>;
22
- type NoUnion<T, U = T> = T extends U ? [U] extends [T] ? T : never : never;
21
+ type ArrayOrRecord<TInput> = TInput[] | Record<RecordKey, TInput>;
22
+ type NoUnion<T, U = T> = T extends U ? [U] extends [T] ? T : never : never;
23
+ /**
24
+ * @description Generic function type, should fit any function
25
+ * @typeParam TFunc - The input function type
26
+ */
27
+ type GenericFunction<TFunc extends (...args: Parameters<TFunc>) => ReturnType<TFunc>> = (...args: Parameters<TFunc>) => ReturnType<TFunc>;
23
28
 
24
29
  /**
25
30
  * 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.
@@ -31,9 +36,9 @@ type NoUnion<T, U = T> = T extends U ? [U] extends [T] ? T : never : never;
31
36
  * @returns Returns the new array of filtered values.
32
37
  * @example
33
38
  * difference([2, 1], [2, 3])
34
- * // =\> [1]
39
+ * // => [1]
35
40
  */
36
- declare function difference<T>(...arrays: MinimumTwoArrays<T>): T[];
41
+ declare function difference<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[];
37
42
 
38
43
  /**
39
44
  * This method is like `difference` except that it accepts `iteratee` which
@@ -250,54 +255,6 @@ declare function takeRightWhile<T>(predicate: (elem: T) => boolean, array: T[]):
250
255
  */
251
256
  declare function takeWhile<T>(predicate: (elem: T) => boolean, array: T[]): T[];
252
257
 
253
- /**
254
- * Creates an array of unique values, in order, from all given arrays using for equality comparisons.
255
- *
256
- * @category Array
257
- * @param arrays - The arrays to inspect.
258
- * @returns Returns the new array of combined values.
259
- * @example
260
- * union([2, 3], [1, 2])
261
- * // => [2, 3, 1]
262
- */
263
- declare function union<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[];
264
-
265
- /**
266
- * This method is like `union` except that it accepts `iteratee` which is
267
- * invoked for each element of each `arrays` to generate the criterion by
268
- * which uniqueness is computed. Result values are chosen from the first
269
- * array in which the value occurs. The iteratee is invoked with one argument:
270
- * (value).
271
- *
272
- * @category Array
273
- * @param arrays - The arrays to inspect.
274
- * @param iteratee - The iteratee invoked per element. Or property shorthand.
275
- * @returns Returns the new array of combined values.
276
- * @example
277
- * unionBy(Math.floor, [2.1], [1.2, 2.3])
278
- * // => [2.1, 1.2]
279
- */
280
- declare function unionBy<T>(iteratee: IterateeFunction<T> | PropertyShorthand<T>, ...arrays: MinimumTwoArrays<T>): T[];
281
-
282
- /**
283
- * This method is like `union` except that it accepts `comparator` which
284
- * is invoked to compare elements of `arrays`. Result values are chosen from
285
- * the first array in which the value occurs. The comparator is invoked
286
- * with two arguments: (arrVal, othVal).
287
- *
288
- * @category Array
289
- * @param comparator - The comparator invoked per element.
290
- * @param arrays - The arrays to inspect.
291
- * @returns Returns the new array of combined values.
292
- * @example
293
- * const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
294
- * const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
295
- *
296
- * unionWith(isEqual, objects, others)
297
- * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
298
- */
299
- declare function unionWith<TInput>(comparator: (a: TInput, b: TInput) => boolean, ...arrays: MinimumTwoArrays<TInput>): TInput[];
300
-
301
258
  /**
302
259
  * Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept.
303
260
  * The order of result values is determined by the order they occur in the array.
@@ -456,16 +413,19 @@ declare function sortBy<T, U>(iteratee: (item: T) => NoUnion<number | bigint | D
456
413
  * @param func The function to restrict.
457
414
  * @returns Returns the new restricted function.
458
415
  * @example
459
- * const caution = () => alert("Caution!");
416
+ * const caution = () => console.log("Caution!");
460
417
  *
461
- * // Display alert only after it has been called 5 times
462
- * after(5, caution)
418
+ * const afterFN = after(2, caution);
419
+ *
420
+ * afterFN()
421
+ * afterFN()
422
+ * afterFN()
423
+ * // => `caution` is invoked after called twice
463
424
  */
464
- declare function after<TFunc extends (...args: Parameters<TFunc>) => ReturnType<TFunc>>(n: number, func: TFunc): (...args: Parameters<TFunc>) => ReturnType<TFunc> | undefined;
425
+ declare function after<TFunc extends GenericFunction<TFunc>>(n: number, func: TFunc): (...args: Parameters<TFunc>) => ReturnType<TFunc> | undefined;
465
426
 
466
427
  /**
467
- * Creates a function that invokes `func`, with the `this` binding and arguments
468
- * of the created function, while it's called less than `n` times. Subsequent
428
+ * Creates a function that invokes `func`, while it's called less than `n` times. Subsequent
469
429
  * calls to the created function return the result of the last `func` invocation.
470
430
  *
471
431
  * @category Function
@@ -473,20 +433,24 @@ declare function after<TFunc extends (...args: Parameters<TFunc>) => ReturnType<
473
433
  * @param func - The function to restrict.
474
434
  * @returns Returns the new restricted function.
475
435
  * @example
476
- * const caution = () => alert("Caution!");
436
+ * const caution = () => console.log("Caution!");
477
437
  *
478
438
  * // Only call caution two times
479
- * before(2, caution)
439
+ * const reducedCaution = before(2, caution)
440
+ *
441
+ * reducedCaution()
442
+ * reducedCaution()
443
+ * reducedCaution()
444
+ * // => `caution` is invoked twice
480
445
  */
481
446
  declare function before<TFunc extends (...args: Parameters<TFunc>) => ReturnType<TFunc>>(n: number, func: TFunc): TFunc;
482
447
 
483
- declare function debounce<T extends (...args: Parameters<T>) => ReturnType<T>>(fn: T, wait?: number, options?: {
448
+ declare function debounce<TFunc extends GenericFunction<TFunc>>(fn: TFunc, wait?: number, options?: {
484
449
  leading?: boolean;
485
450
  maxWait?: number;
486
451
  trailing?: boolean;
487
- }): (this: ThisParameterType<T>, ...args: Parameters<T>) => ReturnType<T>;
452
+ }): (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>) => ReturnType<TFunc>;
488
453
 
489
- type Cache = Map<string | symbol, unknown>;
490
454
  /**
491
455
  * Creates a function that memoizes the result of `func`. If `resolver` is
492
456
  * provided, it determines the cache key for storing the result based on the
@@ -502,6 +466,8 @@ type Cache = Map<string | symbol, unknown>;
502
466
  * @category Function
503
467
  * @param func - The function to have its output memoized.
504
468
  * @param resolver - The function to resolve the cache key.
469
+ * @typeParam TFunc - The input function type
470
+ * @typeParam Cache - The cache map type
505
471
  * @returns Returns the new memoized function.
506
472
  * @example
507
473
  * const object = \{ 'a': 1, 'b': 2 \}
@@ -525,22 +491,139 @@ type Cache = Map<string | symbol, unknown>;
525
491
  * // Replace `memoize.Cache`.
526
492
  * memoize.Cache = WeakMap
527
493
  */
528
- declare function memoize<TFunc extends (...args: Parameters<TFunc>) => ReturnType<TFunc>>(func: TFunc, resolver?: ((...args: Parameters<TFunc>) => string | symbol)): TFunc & {
494
+ declare function memoize<TFunc extends GenericFunction<TFunc>, Cache extends Map<string | symbol, ReturnType<TFunc>>>(func: TFunc, resolver?: ((...args: Parameters<TFunc>) => string | symbol)): TFunc & {
529
495
  cache: Cache;
530
496
  };
531
497
 
532
- declare function once<TFunc extends (...args: Parameters<TFunc>) => ReturnType<TFunc>>(func: TFunc): TFunc;
498
+ /**
499
+ * Creates a function that is restricted to invoking `func` once. Repeat calls
500
+ * to the function return the value of the first invocation. The `func` is
501
+ * invoked with the `this` binding and arguments of the created function.
502
+ *
503
+ * @category Function
504
+ * @param func - The function to restrict.
505
+ * @returns Returns the new restricted function.
506
+ * @example
507
+ * const initialize = once(() => console.log('initialize'))
508
+ * initialize()
509
+ * initialize()
510
+ * // => `createApplication` is invoked once
511
+ */
512
+ declare function once<TFunc extends GenericFunction<TFunc>>(func: TFunc): TFunc;
533
513
 
534
- declare function throttle<T extends (...args: Parameters<T>) => ReturnType<T>>(func: T, wait?: number, options?: {
514
+ declare function throttle<TFunc extends GenericFunction<TFunc>>(func: TFunc, wait?: number, options?: {
535
515
  leading?: boolean;
536
516
  trailing?: boolean;
537
- }): (this: ThisParameterType<T>, ...args: Parameters<T>) => ReturnType<T>;
517
+ }): (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>) => ReturnType<TFunc>;
538
518
 
539
- declare function isEmpty(value: unknown): boolean;
519
+ /**
520
+ * Invokes the iteratee `n` times, returning an array of the results of
521
+ * each invocation. The function is invoked with one argument: (index).
522
+ *
523
+ * @category Function
524
+ * @param n - The number of times to invoke `func`.
525
+ * @param iteratee - The function invoked per iteration.
526
+ * @returns Returns the array of results.
527
+ * @example
528
+ * times(3, index => console.log("Run", index)))
529
+ * // => "Run 0" | "Run 1" | "Run 2"
530
+ *
531
+ * times(3, Math.random)
532
+ * // => [0.123, 0.456, 0.789]
533
+ *
534
+ * times(4, () => 0)
535
+ * // => [0, 0, 0, 0]
536
+ */
537
+ declare function times<T>(n: number, func: (index: number) => T): T[];
538
+
539
+ /**
540
+ * Checks if `value` is an empty object, collection, map, or set.
541
+ *
542
+ * Objects are considered empty if they have no own enumerable string keyed
543
+ * properties.
544
+ *
545
+ * Array-like values such as `arguments` objects, arrays, buffers, strings, or
546
+ * Similarly, maps and sets are considered empty if they have a `size` of `0`.
547
+ *
548
+ * @category Lang
549
+ * @param value - The value to check.
550
+ * @returns Returns `true` if `value` is empty, else `false`.
551
+ * @example
552
+ * isEmpty(null)
553
+ * // => true
554
+ *
555
+ * isEmpty({})
556
+ * // => true
557
+ *
558
+ * isEmpty("")
559
+ * // => true
560
+ *
561
+ * isEmpty([1, 2, 3])
562
+ * // => false
563
+ *
564
+ * isEmpty('abc')
565
+ * // => false
566
+ *
567
+ * isEmpty({ 'a': 1 })
568
+ * // => false
569
+ */
570
+ declare function isEmpty(value: string | object | null | undefined): boolean;
540
571
 
572
+ /**
573
+ * Performs a deep comparison between two values to determine if they are
574
+ * equivalent.
575
+ *
576
+ * **Note:** This method supports comparing arrays, array buffers, booleans,
577
+ * date objects, error objects, maps, numbers, `Object` objects, regexes,
578
+ * sets, strings, symbols, and typed arrays. `Object` objects are compared
579
+ * by their own, not inherited, enumerable properties. Functions and DOM
580
+ * nodes are compared by strict equality, i.e. `===`.
581
+ *
582
+ * @category Lang
583
+ * @param value1 - The value to compare.
584
+ * @param value2 - The other value to compare.
585
+ * @returns Returns `true` if the values are equivalent, else `false`.
586
+ * @example
587
+ * var object = { 'a': 1 };
588
+ * var other = { 'a': 1 };
589
+ *
590
+ * _.isEqual(object, other);
591
+ * // => true
592
+ *
593
+ * object === other;
594
+ * // => false
595
+ */
541
596
  declare function isEqual(value1: unknown, value2: unknown): boolean;
542
597
 
543
- declare function isEqualWith<T>(customizer: (value: T) => unknown, a: T, b: T): boolean;
598
+ /**
599
+ * This method is like `_.isEqual` except that it accepts `customizer` which
600
+ * is invoked to compare values. If `customizer` returns `undefined`, comparisons
601
+ * are handled by the method instead. The `customizer` is invoked with up to
602
+ * six arguments: (objValue, othValue [, index|key, object, other, stack]).
603
+ *
604
+ * @category Lang
605
+ * @param value1 - The value to compare.
606
+ * @param value2 - The other value to compare.
607
+ * @param customizer - The function to customize comparisons.
608
+ * @returns Returns `true` if the values are equivalent, else `false`.
609
+ * @example
610
+ * function isGreeting(value) {
611
+ * return /^h(?:i|ello)$/.test(value);
612
+ * }
613
+ *
614
+ * function customizer(objValue, othValue) {
615
+ * if (isGreeting(objValue) && isGreeting(othValue)) {
616
+ * return true;
617
+ * }
618
+ * }
619
+ *
620
+ * var array = ['hello', 'goodbye'];
621
+ * var other = ['hi', 'goodbye'];
622
+ *
623
+ * isEqualWith(array, other, customizer);
624
+ * // => true
625
+ */
626
+ declare function isEqualWith<T>(a: T, b: T, customizer: (value: T) => unknown): boolean;
544
627
 
545
628
  declare function isPlainObject(value: unknown): value is object;
546
629
 
@@ -582,4 +665,4 @@ declare function stripSpecialChars(str: string): string;
582
665
 
583
666
  declare function unescapeHTML(html: string): string;
584
667
 
585
- export { ArrayOrRecord, IterateeFunction, MinimumTwoArrays, NoUnion, PropertyShorthand, RecordKey, after, before, camelCase, capitalize, chunk, countBy, debounce, deburr, difference, differenceBy, differenceWith, dropRightWhile, dropWhile, escape, escapeRegExp, groupBy, intersection, intersectionBy, intersectionWith, isEmpty, isEqual, isEqualWith, isPlainObject, kebabCase, memoize, once, pascalCase, pick, sample, sampleSize, shuffle, snakeCase, sortBy, startCase, stripSpecialChars, takeRightWhile, takeWhile, throttle, unescapeHTML, union, unionBy, unionWith, uniq, uniqBy, uniqWith, unzip, unzipWith, zip, zipWith };
668
+ export { ArrayOrRecord, GenericFunction, IterateeFunction, MinimumTwoArrays, NoUnion, PropertyShorthand, RecordKey, after, before, camelCase, capitalize, chunk, countBy, debounce, deburr, difference, differenceBy, differenceWith, dropRightWhile, dropWhile, escape, escapeRegExp, groupBy, intersection, intersectionBy, intersectionWith, isEmpty, isEqual, isEqualWith, isPlainObject, kebabCase, memoize, once, pascalCase, pick, sample, sampleSize, shuffle, snakeCase, sortBy, startCase, stripSpecialChars, takeRightWhile, takeWhile, throttle, times, unescapeHTML, uniq, uniqBy, uniqWith, unzip, unzipWith, zip, zipWith };
package/dist/index.js CHANGED
@@ -89,14 +89,14 @@ function getIterateFunction(iteratee) {
89
89
  }
90
90
 
91
91
  // src/lang/isEqualWith.ts
92
- function isEqualWith(customizer, a, b) {
92
+ function isEqualWith(a, b, customizer) {
93
93
  return isEqual(customizer(a), customizer(b));
94
94
  }
95
95
 
96
96
  // src/array/differenceBy.ts
97
97
  function differenceBy(iteratee, ...arrays) {
98
98
  const iterateeFunction = getIterateFunction(iteratee);
99
- return differenceWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
99
+ return differenceWith((a, b) => isEqualWith(a, b, iterateeFunction), ...arrays);
100
100
  }
101
101
 
102
102
  // src/array/dropRightWhile.ts
@@ -134,7 +134,7 @@ function intersection(...arrays) {
134
134
  // src/array/intersectionBy.ts
135
135
  function intersectionBy(iteratee, ...arrays) {
136
136
  const iterateeFunction = getIterateFunction(iteratee);
137
- return intersectionWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
137
+ return intersectionWith((a, b) => isEqualWith(a, b, iterateeFunction), ...arrays);
138
138
  }
139
139
 
140
140
  // src/array/sample.ts
@@ -200,30 +200,6 @@ function takeWhile(predicate, array) {
200
200
  return result;
201
201
  }
202
202
 
203
- // src/array/union.ts
204
- function union(...arrays) {
205
- return [...new Set(arrays.flat())];
206
- }
207
-
208
- // src/array/unionWith.ts
209
- function unionWith(comparator, ...arrays) {
210
- return arrays.reduce((acc, current) => {
211
- for (const item of current) {
212
- if (acc.some((x) => comparator(x, item))) {
213
- continue;
214
- }
215
- acc.push(item);
216
- }
217
- return acc;
218
- }, []);
219
- }
220
-
221
- // src/array/unionBy.ts
222
- function unionBy(iteratee, ...arrays) {
223
- const iterateeFunction = getIterateFunction(iteratee);
224
- return unionWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
225
- }
226
-
227
203
  // src/array/uniqWith.ts
228
204
  function uniqWith(comparator, array) {
229
205
  return array.filter((value, index, self) => {
@@ -462,6 +438,15 @@ function throttle(func, wait = 0, options = {}) {
462
438
  });
463
439
  }
464
440
 
441
+ // src/function/times.ts
442
+ function times(n, func) {
443
+ const result = [];
444
+ for (let i = 0; i < n; i++) {
445
+ result.push(func(i));
446
+ }
447
+ return result;
448
+ }
449
+
465
450
  // src/lang/isEmpty.ts
466
451
  function isEmpty(value) {
467
452
  if (value === null || value === void 0) {
@@ -470,9 +455,6 @@ function isEmpty(value) {
470
455
  if (typeof value === "string" || Array.isArray(value)) {
471
456
  return value.length === 0;
472
457
  }
473
- if (typeof value === "number") {
474
- return false;
475
- }
476
458
  if (value instanceof Map || value instanceof Set) {
477
459
  return value.size === 0;
478
460
  }
@@ -643,10 +625,8 @@ export {
643
625
  takeRightWhile,
644
626
  takeWhile,
645
627
  throttle,
628
+ times,
646
629
  unescapeHTML,
647
- union,
648
- unionBy,
649
- unionWith,
650
630
  uniq,
651
631
  uniqBy,
652
632
  uniqWith,