moderndash 0.0.9 → 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.cjs +711 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +161 -79
- package/dist/index.js +15 -35
- package/dist/index.js.map +1 -1
- package/package.json +8 -3
- package/src/array/difference.ts +2 -2
- package/src/array/differenceBy.ts +1 -1
- package/src/array/index.ts +0 -3
- package/src/array/intersectionBy.ts +1 -1
- package/src/collection/countBy.ts +4 -4
- package/src/collection/groupBy.ts +5 -6
- package/src/function/after.ts +10 -4
- package/src/function/before.ts +8 -4
- package/src/function/debounce.ts +12 -11
- package/src/function/index.ts +1 -0
- package/src/function/memoize.ts +7 -3
- package/src/function/once.ts +6 -3
- package/src/function/throttle.ts +5 -3
- package/src/function/times.ts +26 -0
- package/src/helpers/collections.ts +2 -2
- package/src/lang/isEmpty.ts +33 -5
- package/src/lang/isEqual.ts +26 -0
- package/src/lang/isEqualWith.ts +30 -1
- package/src/types.ts +8 -2
- package/src/array/union.ts +0 -16
- package/src/array/unionBy.ts +0 -26
- package/src/array/unionWith.ts +0 -31
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ArrayOrRecord } from '../types';
|
|
2
2
|
|
|
3
|
-
export function getValuesFromCollection<T>(collection:
|
|
3
|
+
export function getValuesFromCollection<T>(collection: ArrayOrRecord<T>): T[] {
|
|
4
4
|
return Array.isArray(collection) ? collection : Object.values(collection);
|
|
5
5
|
}
|
package/src/lang/isEmpty.ts
CHANGED
|
@@ -1,4 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Checks if `value` is an empty object, collection, map, or set.
|
|
3
|
+
*
|
|
4
|
+
* Objects are considered empty if they have no own enumerable string keyed
|
|
5
|
+
* properties.
|
|
6
|
+
*
|
|
7
|
+
* Array-like values such as `arguments` objects, arrays, buffers, strings, or
|
|
8
|
+
* Similarly, maps and sets are considered empty if they have a `size` of `0`.
|
|
9
|
+
*
|
|
10
|
+
* @category Lang
|
|
11
|
+
* @param value - The value to check.
|
|
12
|
+
* @returns Returns `true` if `value` is empty, else `false`.
|
|
13
|
+
* @example
|
|
14
|
+
* isEmpty(null)
|
|
15
|
+
* // => true
|
|
16
|
+
*
|
|
17
|
+
* isEmpty({})
|
|
18
|
+
* // => true
|
|
19
|
+
*
|
|
20
|
+
* isEmpty("")
|
|
21
|
+
* // => true
|
|
22
|
+
*
|
|
23
|
+
* isEmpty([1, 2, 3])
|
|
24
|
+
* // => false
|
|
25
|
+
*
|
|
26
|
+
* isEmpty('abc')
|
|
27
|
+
* // => false
|
|
28
|
+
*
|
|
29
|
+
* isEmpty({ 'a': 1 })
|
|
30
|
+
* // => false
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
export function isEmpty(value: string | object | null | undefined): boolean {
|
|
2
34
|
if (value === null || value === undefined) {
|
|
3
35
|
return true;
|
|
4
36
|
}
|
|
@@ -7,10 +39,6 @@ export function isEmpty(value: unknown): boolean {
|
|
|
7
39
|
return value.length === 0;
|
|
8
40
|
}
|
|
9
41
|
|
|
10
|
-
if (typeof value === 'number') {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
42
|
if (value instanceof Map || value instanceof Set) {
|
|
15
43
|
return value.size === 0;
|
|
16
44
|
}
|
package/src/lang/isEqual.ts
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { RecordKey } from '../types';
|
|
2
2
|
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Performs a deep comparison between two values to determine if they are
|
|
6
|
+
* equivalent.
|
|
7
|
+
*
|
|
8
|
+
* **Note:** This method supports comparing arrays, array buffers, booleans,
|
|
9
|
+
* date objects, error objects, maps, numbers, `Object` objects, regexes,
|
|
10
|
+
* sets, strings, symbols, and typed arrays. `Object` objects are compared
|
|
11
|
+
* by their own, not inherited, enumerable properties. Functions and DOM
|
|
12
|
+
* nodes are compared by strict equality, i.e. `===`.
|
|
13
|
+
*
|
|
14
|
+
* @category Lang
|
|
15
|
+
* @param value1 - The value to compare.
|
|
16
|
+
* @param value2 - The other value to compare.
|
|
17
|
+
* @returns Returns `true` if the values are equivalent, else `false`.
|
|
18
|
+
* @example
|
|
19
|
+
* var object = { 'a': 1 };
|
|
20
|
+
* var other = { 'a': 1 };
|
|
21
|
+
*
|
|
22
|
+
* _.isEqual(object, other);
|
|
23
|
+
* // => true
|
|
24
|
+
*
|
|
25
|
+
* object === other;
|
|
26
|
+
* // => false
|
|
27
|
+
*/
|
|
28
|
+
|
|
3
29
|
export function isEqual(value1: unknown, value2: unknown): boolean {
|
|
4
30
|
if (value1 === value2) return true;
|
|
5
31
|
|
package/src/lang/isEqualWith.ts
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
import { isEqual } from '@lang/isEqual';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* This method is like `_.isEqual` except that it accepts `customizer` which
|
|
5
|
+
* is invoked to compare values. If `customizer` returns `undefined`, comparisons
|
|
6
|
+
* are handled by the method instead. The `customizer` is invoked with up to
|
|
7
|
+
* six arguments: (objValue, othValue [, index|key, object, other, stack]).
|
|
8
|
+
*
|
|
9
|
+
* @category Lang
|
|
10
|
+
* @param value1 - The value to compare.
|
|
11
|
+
* @param value2 - The other value to compare.
|
|
12
|
+
* @param customizer - The function to customize comparisons.
|
|
13
|
+
* @returns Returns `true` if the values are equivalent, else `false`.
|
|
14
|
+
* @example
|
|
15
|
+
* function isGreeting(value) {
|
|
16
|
+
* return /^h(?:i|ello)$/.test(value);
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* function customizer(objValue, othValue) {
|
|
20
|
+
* if (isGreeting(objValue) && isGreeting(othValue)) {
|
|
21
|
+
* return true;
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* var array = ['hello', 'goodbye'];
|
|
26
|
+
* var other = ['hi', 'goodbye'];
|
|
27
|
+
*
|
|
28
|
+
* isEqualWith(array, other, customizer);
|
|
29
|
+
* // => true
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export function isEqualWith<T>(a: T, b: T, customizer: (value: T) => unknown): boolean {
|
|
4
33
|
return isEqual(customizer(a), customizer(b));
|
|
5
34
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
export type MinimumTwoArrays<
|
|
1
|
+
export type MinimumTwoArrays<TInput> = [TInput[], TInput[], ...TInput[][]];
|
|
2
2
|
|
|
3
3
|
export type IterateeFunction<T> = (value: T) => unknown;
|
|
4
4
|
export type PropertyShorthand<T> = keyof T;
|
|
5
5
|
|
|
6
6
|
export type RecordKey = string | number | symbol;
|
|
7
|
+
export type ArrayOrRecord<TInput> = TInput[] | Record<RecordKey, TInput>;
|
|
7
8
|
|
|
8
|
-
export type Collection<T> = T[] | Record<RecordKey, T>;
|
|
9
9
|
export type NoUnion<T, U = T> = T extends U ? [U] extends [T] ? T : never : never;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @description Generic function type, should fit any function
|
|
13
|
+
* @typeParam TFunc - The input function type
|
|
14
|
+
*/
|
|
15
|
+
export type GenericFunction<TFunc extends (...args: Parameters<TFunc>) => ReturnType<TFunc>> = (...args: Parameters<TFunc>) => ReturnType<TFunc>;
|
package/src/array/union.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { MinimumTwoArrays } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Creates an array of unique values, in order, from all given arrays using for equality comparisons.
|
|
5
|
-
*
|
|
6
|
-
* @category Array
|
|
7
|
-
* @param arrays - The arrays to inspect.
|
|
8
|
-
* @returns Returns the new array of combined values.
|
|
9
|
-
* @example
|
|
10
|
-
* union([2, 3], [1, 2])
|
|
11
|
-
* // => [2, 3, 1]
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export function union<TInput>(...arrays: MinimumTwoArrays<TInput>): TInput[] {
|
|
15
|
-
return [...new Set(arrays.flat())];
|
|
16
|
-
}
|
package/src/array/unionBy.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { IterateeFunction, MinimumTwoArrays, PropertyShorthand } from '../types';
|
|
2
|
-
|
|
3
|
-
import { unionWith } from '@array/unionWith';
|
|
4
|
-
import { getIterateFunction } from '@helpers/shortHands';
|
|
5
|
-
import { isEqualWith } from '@lang/isEqualWith';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* This method is like `union` except that it accepts `iteratee` which is
|
|
9
|
-
* invoked for each element of each `arrays` to generate the criterion by
|
|
10
|
-
* which uniqueness is computed. Result values are chosen from the first
|
|
11
|
-
* array in which the value occurs. The iteratee is invoked with one argument:
|
|
12
|
-
* (value).
|
|
13
|
-
*
|
|
14
|
-
* @category Array
|
|
15
|
-
* @param arrays - The arrays to inspect.
|
|
16
|
-
* @param iteratee - The iteratee invoked per element. Or property shorthand.
|
|
17
|
-
* @returns Returns the new array of combined values.
|
|
18
|
-
* @example
|
|
19
|
-
* unionBy(Math.floor, [2.1], [1.2, 2.3])
|
|
20
|
-
* // => [2.1, 1.2]
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export function unionBy<T>(iteratee: IterateeFunction<T> | PropertyShorthand<T>, ...arrays: MinimumTwoArrays<T>): T[] {
|
|
24
|
-
const iterateeFunction = getIterateFunction(iteratee);
|
|
25
|
-
return unionWith((a, b) => isEqualWith(iterateeFunction, a, b), ...arrays);
|
|
26
|
-
}
|
package/src/array/unionWith.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { MinimumTwoArrays } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This method is like `union` except that it accepts `comparator` which
|
|
5
|
-
* is invoked to compare elements of `arrays`. Result values are chosen from
|
|
6
|
-
* the first array in which the value occurs. The comparator is invoked
|
|
7
|
-
* with two arguments: (arrVal, othVal).
|
|
8
|
-
*
|
|
9
|
-
* @category Array
|
|
10
|
-
* @param comparator - The comparator invoked per element.
|
|
11
|
-
* @param arrays - The arrays to inspect.
|
|
12
|
-
* @returns Returns the new array of combined values.
|
|
13
|
-
* @example
|
|
14
|
-
* const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
|
|
15
|
-
* const others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }]
|
|
16
|
-
*
|
|
17
|
-
* unionWith(isEqual, objects, others)
|
|
18
|
-
* // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
export function unionWith<TInput>(comparator: (a: TInput, b: TInput) => boolean, ...arrays: MinimumTwoArrays<TInput>): TInput[] {
|
|
22
|
-
return arrays.reduce((acc, current) => {
|
|
23
|
-
for (const item of current) {
|
|
24
|
-
if (acc.some(x => comparator(x, item))) {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
acc.push(item);
|
|
28
|
-
}
|
|
29
|
-
return acc;
|
|
30
|
-
}, []);
|
|
31
|
-
}
|