vest-utils 0.1.0 → 1.0.0-dev-9c596e
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/cjs/vest-utils.development.js +102 -136
- package/dist/cjs/vest-utils.production.js +1 -1
- package/dist/es/vest-utils.development.js +100 -136
- package/dist/es/vest-utils.production.js +1 -1
- package/dist/umd/vest-utils.development.js +102 -136
- package/dist/umd/vest-utils.production.js +1 -1
- package/package.json +1 -1
- package/types/vest-utils.d.ts +32 -20
- package/types/vest-utils.d.ts.map +1 -1
- package/src/__tests__/bindNot.test.ts +0 -37
- package/src/__tests__/bus.test.ts +0 -81
- package/src/__tests__/cache.test.ts +0 -113
- package/src/__tests__/defaultTo.test.ts +0 -50
- package/src/__tests__/deferThrow.test.ts +0 -24
- package/src/__tests__/greaterThan.test.ts +0 -59
- package/src/__tests__/invariant.test.ts +0 -45
- package/src/__tests__/isArray.test.ts +0 -15
- package/src/__tests__/isNull.test.ts +0 -25
- package/src/__tests__/isNumeric.test.ts +0 -26
- package/src/__tests__/isUndefined.test.ts +0 -26
- package/src/__tests__/lengthEquals.test.ts +0 -56
- package/src/__tests__/longerThan.test.ts +0 -56
- package/src/__tests__/mapFirst.test.ts +0 -29
- package/src/__tests__/numberEquals.test.ts +0 -59
- package/src/__tests__/optionalFunctionValue.test.ts +0 -27
- package/src/__tests__/seq.test.ts +0 -28
- package/src/asArray.ts +0 -3
- package/src/assign.ts +0 -1
- package/src/bindNot.ts +0 -3
- package/src/bus.ts +0 -32
- package/src/cache.ts +0 -57
- package/src/callEach.ts +0 -5
- package/src/defaultTo.ts +0 -8
- package/src/deferThrow.ts +0 -5
- package/src/either.ts +0 -3
- package/src/globals.d.ts +0 -3
- package/src/greaterThan.ts +0 -8
- package/src/hasOwnProperty.ts +0 -9
- package/src/invariant.ts +0 -24
- package/src/isArrayValue.ts +0 -11
- package/src/isBooleanValue.ts +0 -3
- package/src/isEmpty.ts +0 -17
- package/src/isFunction.ts +0 -5
- package/src/isNull.ts +0 -7
- package/src/isNullish.ts +0 -9
- package/src/isNumeric.ts +0 -11
- package/src/isPositive.ts +0 -5
- package/src/isPromise.ts +0 -5
- package/src/isStringValue.ts +0 -3
- package/src/isUndefined.ts +0 -7
- package/src/last.ts +0 -7
- package/src/lengthEquals.ts +0 -11
- package/src/longerThan.ts +0 -8
- package/src/mapFirst.ts +0 -25
- package/src/nestedArray.ts +0 -69
- package/src/numberEquals.ts +0 -11
- package/src/optionalFunctionValue.ts +0 -8
- package/src/seq.ts +0 -14
- package/src/utilityTypes.ts +0 -9
- package/src/vest-utils.ts +0 -36
- package/tsconfig.json +0 -46
package/src/bus.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { CB } from 'utilityTypes';
|
|
2
|
-
|
|
3
|
-
export function createBus(): {
|
|
4
|
-
on: (event: string, handler: CB) => OnReturn;
|
|
5
|
-
emit: (event: string, ...args: any[]) => void;
|
|
6
|
-
} {
|
|
7
|
-
const listeners: Record<string, CB[]> = {};
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
emit(event: string, data: any) {
|
|
11
|
-
listener(event).forEach(handler => {
|
|
12
|
-
handler(data);
|
|
13
|
-
});
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
on(event: string, handler: CB): OnReturn {
|
|
17
|
-
listeners[event] = listener(event).concat(handler);
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
off() {
|
|
21
|
-
listeners[event] = listener(event).filter(h => h !== handler);
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
function listener(event: string): CB[] {
|
|
28
|
-
return listeners[event] || [];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
type OnReturn = { off: () => void };
|
package/src/cache.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { lengthEquals } from 'lengthEquals';
|
|
2
|
-
import { longerThan } from 'longerThan';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Creates a cache function
|
|
6
|
-
*/
|
|
7
|
-
export default function createCache<T = unknown>(
|
|
8
|
-
maxSize = 1
|
|
9
|
-
): {
|
|
10
|
-
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T;
|
|
11
|
-
get(deps: unknown[]): [unknown[], T] | null;
|
|
12
|
-
invalidate(item: any): void;
|
|
13
|
-
} {
|
|
14
|
-
const cacheStorage: Array<[unknown[], T]> = [];
|
|
15
|
-
|
|
16
|
-
const cache = (
|
|
17
|
-
deps: unknown[],
|
|
18
|
-
cacheAction: (...args: unknown[]) => T
|
|
19
|
-
): T => {
|
|
20
|
-
const cacheHit = cache.get(deps);
|
|
21
|
-
// cache hit is not null
|
|
22
|
-
if (cacheHit) return cacheHit[1];
|
|
23
|
-
|
|
24
|
-
const result = cacheAction();
|
|
25
|
-
cacheStorage.unshift([deps.concat(), result]);
|
|
26
|
-
|
|
27
|
-
if (longerThan(cacheStorage, maxSize)) cacheStorage.length = maxSize;
|
|
28
|
-
|
|
29
|
-
return result;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// invalidate an item in the cache by its dependencies
|
|
33
|
-
cache.invalidate = (deps: any[]): void => {
|
|
34
|
-
const index = findIndex(deps);
|
|
35
|
-
if (index > -1) cacheStorage.splice(index, 1);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// Retrieves an item from the cache.
|
|
39
|
-
cache.get = (deps: unknown[]): [unknown[], T] | null =>
|
|
40
|
-
cacheStorage[findIndex(deps)] || null;
|
|
41
|
-
|
|
42
|
-
return cache;
|
|
43
|
-
|
|
44
|
-
function findIndex(deps: unknown[]): number {
|
|
45
|
-
return cacheStorage.findIndex(
|
|
46
|
-
([cachedDeps]) =>
|
|
47
|
-
lengthEquals(deps, cachedDeps.length) &&
|
|
48
|
-
deps.every((dep, i) => dep === cachedDeps[i])
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type CacheApi<T = unknown> = {
|
|
54
|
-
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T;
|
|
55
|
-
get(deps: unknown[]): [unknown[], T] | null;
|
|
56
|
-
invalidate(item: any): void;
|
|
57
|
-
};
|
package/src/callEach.ts
DELETED
package/src/defaultTo.ts
DELETED
package/src/deferThrow.ts
DELETED
package/src/either.ts
DELETED
package/src/globals.d.ts
DELETED
package/src/greaterThan.ts
DELETED
package/src/hasOwnProperty.ts
DELETED
package/src/invariant.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import optionalFunctionValue from 'optionalFunctionValue';
|
|
2
|
-
import type { Stringable } from 'utilityTypes';
|
|
3
|
-
|
|
4
|
-
export default function invariant(
|
|
5
|
-
condition: any,
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
7
|
-
message?: String | Stringable
|
|
8
|
-
): asserts condition {
|
|
9
|
-
if (condition) {
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// If message is a string object (rather than string literal)
|
|
14
|
-
// Throw the value directly as a string
|
|
15
|
-
// Alternatively, throw an error with the message
|
|
16
|
-
throw message instanceof String
|
|
17
|
-
? message.valueOf()
|
|
18
|
-
: new Error(message ? optionalFunctionValue(message) : message);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
22
|
-
export function StringObject(value?: Stringable): String {
|
|
23
|
-
return new String(optionalFunctionValue(value));
|
|
24
|
-
}
|
package/src/isArrayValue.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import bindNot from 'bindNot';
|
|
2
|
-
|
|
3
|
-
// The module is named "isArrayValue" since it
|
|
4
|
-
// is conflicting with a nested npm dependency.
|
|
5
|
-
// We may need to revisit this in the future.
|
|
6
|
-
|
|
7
|
-
export function isArray(value: unknown): value is Array<unknown> {
|
|
8
|
-
return Boolean(Array.isArray(value));
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const isNotArray = bindNot(isArray);
|
package/src/isBooleanValue.ts
DELETED
package/src/isEmpty.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import bindNot from 'bindNot';
|
|
2
|
-
import hasOwnProperty from 'hasOwnProperty';
|
|
3
|
-
import { lengthEquals } from 'lengthEquals';
|
|
4
|
-
|
|
5
|
-
export function isEmpty(value: unknown): boolean {
|
|
6
|
-
if (!value) {
|
|
7
|
-
return true;
|
|
8
|
-
} else if (hasOwnProperty(value, 'length')) {
|
|
9
|
-
return lengthEquals(value as string | unknown[], 0);
|
|
10
|
-
} else if (typeof value === 'object') {
|
|
11
|
-
return lengthEquals(Object.keys(value as Record<string, unknown>), 0);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const isNotEmpty = bindNot(isEmpty);
|
package/src/isFunction.ts
DELETED
package/src/isNull.ts
DELETED
package/src/isNullish.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import bindNot from 'bindNot';
|
|
2
|
-
import { isNull } from 'isNull';
|
|
3
|
-
import { isUndefined } from 'isUndefined';
|
|
4
|
-
|
|
5
|
-
export function isNullish(value: any): value is null | undefined {
|
|
6
|
-
return isNull(value) || isUndefined(value);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const isNotNullish = bindNot(isNullish);
|
package/src/isNumeric.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import bindNot from 'bindNot';
|
|
2
|
-
|
|
3
|
-
export function isNumeric(value: string | number): boolean {
|
|
4
|
-
const str = String(value);
|
|
5
|
-
const num = Number(value);
|
|
6
|
-
const result =
|
|
7
|
-
!isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
8
|
-
return Boolean(result);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const isNotNumeric = bindNot(isNumeric);
|
package/src/isPositive.ts
DELETED
package/src/isPromise.ts
DELETED
package/src/isStringValue.ts
DELETED
package/src/isUndefined.ts
DELETED
package/src/last.ts
DELETED
package/src/lengthEquals.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import bindNot from 'bindNot';
|
|
2
|
-
import { numberEquals } from 'numberEquals';
|
|
3
|
-
|
|
4
|
-
export function lengthEquals(
|
|
5
|
-
value: string | unknown[],
|
|
6
|
-
arg1: string | number
|
|
7
|
-
): boolean {
|
|
8
|
-
return numberEquals(value.length, arg1);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const lengthNotEquals = bindNot(lengthEquals);
|
package/src/longerThan.ts
DELETED
package/src/mapFirst.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export default function mapFirst<T>(
|
|
2
|
-
array: T[],
|
|
3
|
-
callback: (
|
|
4
|
-
item: T,
|
|
5
|
-
breakout: (conditional: boolean, value: unknown) => void,
|
|
6
|
-
index: number
|
|
7
|
-
) => unknown
|
|
8
|
-
): any {
|
|
9
|
-
let broke = false;
|
|
10
|
-
let breakoutValue = null;
|
|
11
|
-
for (let i = 0; i < array.length; i++) {
|
|
12
|
-
callback(array[i], breakout, i);
|
|
13
|
-
|
|
14
|
-
if (broke) {
|
|
15
|
-
return breakoutValue;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function breakout(conditional: boolean, value: unknown) {
|
|
20
|
-
if (conditional) {
|
|
21
|
-
broke = true;
|
|
22
|
-
breakoutValue = value;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
package/src/nestedArray.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import asArray from 'asArray';
|
|
2
|
-
import defaultTo from 'defaultTo';
|
|
3
|
-
import { isArray } from 'isArrayValue';
|
|
4
|
-
import { isNotNull } from 'isNull';
|
|
5
|
-
import last from 'last';
|
|
6
|
-
|
|
7
|
-
export type NestedArray<T> = Array<NestedArray<T> | T>;
|
|
8
|
-
|
|
9
|
-
// This is kind of a map/filter in one function.
|
|
10
|
-
// Normally, behaves like a nested-array map,
|
|
11
|
-
// but returning `null` will drop the element from the array
|
|
12
|
-
export function transform<T>(
|
|
13
|
-
array: NestedArray<T>,
|
|
14
|
-
cb: (value: T) => NestedArray<T> | T | null
|
|
15
|
-
): NestedArray<T> {
|
|
16
|
-
const res = [];
|
|
17
|
-
for (const v of array) {
|
|
18
|
-
if (isArray(v)) {
|
|
19
|
-
res.push(transform(v, cb));
|
|
20
|
-
} else {
|
|
21
|
-
const output = cb(v);
|
|
22
|
-
|
|
23
|
-
if (isNotNull(output)) {
|
|
24
|
-
res.push(output);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return res as NestedArray<T>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function valueAtPath<T>(
|
|
32
|
-
array: NestedArray<T>,
|
|
33
|
-
path: number[]
|
|
34
|
-
): T | NestedArray<T> {
|
|
35
|
-
return getCurrent(array, path)[last(path)];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function setValueAtPath<T>(
|
|
39
|
-
array: NestedArray<T>,
|
|
40
|
-
path: number[],
|
|
41
|
-
value: NestedArray<T> | T
|
|
42
|
-
): NestedArray<T> {
|
|
43
|
-
const current = getCurrent(array, path);
|
|
44
|
-
|
|
45
|
-
current[last(path)] = value;
|
|
46
|
-
return array;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function flatten<T>(values: NestedArray<T> | T): T[] {
|
|
50
|
-
return asArray(values).reduce((acc, value) => {
|
|
51
|
-
if (isArray(value)) {
|
|
52
|
-
return (acc as NestedArray<T>).concat(flatten(value));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return asArray(acc).concat(value);
|
|
56
|
-
}, [] as T[]) as T[];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function getCurrent<T>(
|
|
60
|
-
array: NestedArray<T>,
|
|
61
|
-
path: number[]
|
|
62
|
-
): NestedArray<T> {
|
|
63
|
-
let current: NestedArray<T> = array;
|
|
64
|
-
for (const p of path.slice(0, -1)) {
|
|
65
|
-
current[p] = defaultTo(current[p], []);
|
|
66
|
-
current = current[p] as NestedArray<T>;
|
|
67
|
-
}
|
|
68
|
-
return current;
|
|
69
|
-
}
|
package/src/numberEquals.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import bindNot from 'bindNot';
|
|
2
|
-
import { isNumeric } from 'isNumeric';
|
|
3
|
-
|
|
4
|
-
export function numberEquals(
|
|
5
|
-
value: string | number,
|
|
6
|
-
eq: string | number
|
|
7
|
-
): boolean {
|
|
8
|
-
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export const numberNotEquals = bindNot(numberEquals);
|
package/src/seq.ts
DELETED
package/src/utilityTypes.ts
DELETED
package/src/vest-utils.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export { default as cache, CacheApi } from 'cache';
|
|
2
|
-
export { isNullish, isNotNullish } from 'isNullish';
|
|
3
|
-
export * as nestedArray from 'nestedArray';
|
|
4
|
-
export { default as asArray } from 'asArray';
|
|
5
|
-
export { default as callEach } from 'callEach';
|
|
6
|
-
export { default as hasOwnProperty } from 'hasOwnProperty';
|
|
7
|
-
export { default as isPromise } from 'isPromise';
|
|
8
|
-
export { default as optionalFunctionValue } from 'optionalFunctionValue';
|
|
9
|
-
export { default as assign } from 'assign';
|
|
10
|
-
export { default as defaultTo } from 'defaultTo';
|
|
11
|
-
export { default as invariant, StringObject } from 'invariant';
|
|
12
|
-
export { default as isStringValue } from 'isStringValue';
|
|
13
|
-
export { default as bindNot } from 'bindNot';
|
|
14
|
-
export { default as either } from 'either';
|
|
15
|
-
export { default as isBoolean } from 'isBooleanValue';
|
|
16
|
-
export { default as last } from 'last';
|
|
17
|
-
export { default as deferThrow } from 'deferThrow';
|
|
18
|
-
export * as bus from 'bus';
|
|
19
|
-
export { default as seq, genSeq } from 'seq';
|
|
20
|
-
export { default as isFunction } from 'isFunction';
|
|
21
|
-
export { default as mapFirst } from 'mapFirst';
|
|
22
|
-
export { greaterThan } from 'greaterThan';
|
|
23
|
-
export { longerThan } from 'longerThan';
|
|
24
|
-
export { isNumeric, isNotNumeric } from 'isNumeric';
|
|
25
|
-
export { lengthEquals, lengthNotEquals } from 'lengthEquals';
|
|
26
|
-
export { numberEquals, numberNotEquals } from 'numberEquals';
|
|
27
|
-
export { isNull, isNotNull } from 'isNull';
|
|
28
|
-
export { isUndefined, isNotUndefined } from 'isUndefined';
|
|
29
|
-
export { isArray, isNotArray } from 'isArrayValue';
|
|
30
|
-
export { isEmpty, isNotEmpty } from 'isEmpty';
|
|
31
|
-
export { isPositive } from 'isPositive';
|
|
32
|
-
|
|
33
|
-
export type { DropFirst } from 'utilityTypes';
|
|
34
|
-
export type { Stringable } from 'utilityTypes';
|
|
35
|
-
export type { CB } from 'utilityTypes';
|
|
36
|
-
export type { ValueOf } from 'utilityTypes';
|
package/tsconfig.json
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"rootDir": ".",
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"baseUrl": ".",
|
|
6
|
-
"declarationDir": "./types",
|
|
7
|
-
"declarationMap": true,
|
|
8
|
-
"outDir": "./dist",
|
|
9
|
-
"paths": {
|
|
10
|
-
"asArray": ["src/asArray.ts"],
|
|
11
|
-
"assign": ["src/assign.ts"],
|
|
12
|
-
"bindNot": ["src/bindNot.ts"],
|
|
13
|
-
"bus": ["src/bus.ts"],
|
|
14
|
-
"cache": ["src/cache.ts"],
|
|
15
|
-
"callEach": ["src/callEach.ts"],
|
|
16
|
-
"defaultTo": ["src/defaultTo.ts"],
|
|
17
|
-
"deferThrow": ["src/deferThrow.ts"],
|
|
18
|
-
"either": ["src/either.ts"],
|
|
19
|
-
"globals.d": ["src/globals.d.ts"],
|
|
20
|
-
"greaterThan": ["src/greaterThan.ts"],
|
|
21
|
-
"hasOwnProperty": ["src/hasOwnProperty.ts"],
|
|
22
|
-
"invariant": ["src/invariant.ts"],
|
|
23
|
-
"isArrayValue": ["src/isArrayValue.ts"],
|
|
24
|
-
"isBooleanValue": ["src/isBooleanValue.ts"],
|
|
25
|
-
"isEmpty": ["src/isEmpty.ts"],
|
|
26
|
-
"isFunction": ["src/isFunction.ts"],
|
|
27
|
-
"isNull": ["src/isNull.ts"],
|
|
28
|
-
"isNullish": ["src/isNullish.ts"],
|
|
29
|
-
"isNumeric": ["src/isNumeric.ts"],
|
|
30
|
-
"isPositive": ["src/isPositive.ts"],
|
|
31
|
-
"isPromise": ["src/isPromise.ts"],
|
|
32
|
-
"isStringValue": ["src/isStringValue.ts"],
|
|
33
|
-
"isUndefined": ["src/isUndefined.ts"],
|
|
34
|
-
"last": ["src/last.ts"],
|
|
35
|
-
"lengthEquals": ["src/lengthEquals.ts"],
|
|
36
|
-
"longerThan": ["src/longerThan.ts"],
|
|
37
|
-
"mapFirst": ["src/mapFirst.ts"],
|
|
38
|
-
"nestedArray": ["src/nestedArray.ts"],
|
|
39
|
-
"numberEquals": ["src/numberEquals.ts"],
|
|
40
|
-
"optionalFunctionValue": ["src/optionalFunctionValue.ts"],
|
|
41
|
-
"seq": ["src/seq.ts"],
|
|
42
|
-
"utilityTypes": ["src/utilityTypes.ts"],
|
|
43
|
-
"vest-utils": ["src/vest-utils.ts"]
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|