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.
Files changed (61) hide show
  1. package/dist/cjs/vest-utils.development.js +102 -136
  2. package/dist/cjs/vest-utils.production.js +1 -1
  3. package/dist/es/vest-utils.development.js +100 -136
  4. package/dist/es/vest-utils.production.js +1 -1
  5. package/dist/umd/vest-utils.development.js +102 -136
  6. package/dist/umd/vest-utils.production.js +1 -1
  7. package/package.json +1 -1
  8. package/types/vest-utils.d.ts +32 -20
  9. package/types/vest-utils.d.ts.map +1 -1
  10. package/src/__tests__/bindNot.test.ts +0 -37
  11. package/src/__tests__/bus.test.ts +0 -81
  12. package/src/__tests__/cache.test.ts +0 -113
  13. package/src/__tests__/defaultTo.test.ts +0 -50
  14. package/src/__tests__/deferThrow.test.ts +0 -24
  15. package/src/__tests__/greaterThan.test.ts +0 -59
  16. package/src/__tests__/invariant.test.ts +0 -45
  17. package/src/__tests__/isArray.test.ts +0 -15
  18. package/src/__tests__/isNull.test.ts +0 -25
  19. package/src/__tests__/isNumeric.test.ts +0 -26
  20. package/src/__tests__/isUndefined.test.ts +0 -26
  21. package/src/__tests__/lengthEquals.test.ts +0 -56
  22. package/src/__tests__/longerThan.test.ts +0 -56
  23. package/src/__tests__/mapFirst.test.ts +0 -29
  24. package/src/__tests__/numberEquals.test.ts +0 -59
  25. package/src/__tests__/optionalFunctionValue.test.ts +0 -27
  26. package/src/__tests__/seq.test.ts +0 -28
  27. package/src/asArray.ts +0 -3
  28. package/src/assign.ts +0 -1
  29. package/src/bindNot.ts +0 -3
  30. package/src/bus.ts +0 -32
  31. package/src/cache.ts +0 -57
  32. package/src/callEach.ts +0 -5
  33. package/src/defaultTo.ts +0 -8
  34. package/src/deferThrow.ts +0 -5
  35. package/src/either.ts +0 -3
  36. package/src/globals.d.ts +0 -3
  37. package/src/greaterThan.ts +0 -8
  38. package/src/hasOwnProperty.ts +0 -9
  39. package/src/invariant.ts +0 -24
  40. package/src/isArrayValue.ts +0 -11
  41. package/src/isBooleanValue.ts +0 -3
  42. package/src/isEmpty.ts +0 -17
  43. package/src/isFunction.ts +0 -5
  44. package/src/isNull.ts +0 -7
  45. package/src/isNullish.ts +0 -9
  46. package/src/isNumeric.ts +0 -11
  47. package/src/isPositive.ts +0 -5
  48. package/src/isPromise.ts +0 -5
  49. package/src/isStringValue.ts +0 -3
  50. package/src/isUndefined.ts +0 -7
  51. package/src/last.ts +0 -7
  52. package/src/lengthEquals.ts +0 -11
  53. package/src/longerThan.ts +0 -8
  54. package/src/mapFirst.ts +0 -25
  55. package/src/nestedArray.ts +0 -69
  56. package/src/numberEquals.ts +0 -11
  57. package/src/optionalFunctionValue.ts +0 -8
  58. package/src/seq.ts +0 -14
  59. package/src/utilityTypes.ts +0 -9
  60. package/src/vest-utils.ts +0 -36
  61. 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
@@ -1,5 +0,0 @@
1
- import type { CB } from 'utilityTypes';
2
-
3
- export default function callEach(arr: CB[]): void {
4
- return arr.forEach(fn => fn());
5
- }
package/src/defaultTo.ts DELETED
@@ -1,8 +0,0 @@
1
- import optionalFunctionValue from 'optionalFunctionValue';
2
-
3
- export default function defaultTo<T>(
4
- value: T | ((...args: any[]) => T),
5
- defaultValue: T | (() => T)
6
- ): T {
7
- return optionalFunctionValue(value) ?? optionalFunctionValue(defaultValue);
8
- }
package/src/deferThrow.ts DELETED
@@ -1,5 +0,0 @@
1
- export default function deferThrow(message?: string): void {
2
- setTimeout(() => {
3
- throw new Error(message);
4
- }, 0);
5
- }
package/src/either.ts DELETED
@@ -1,3 +0,0 @@
1
- export default function either(a: unknown, b: unknown): boolean {
2
- return !!a !== !!b;
3
- }
package/src/globals.d.ts DELETED
@@ -1,3 +0,0 @@
1
- declare const __DEV__: boolean;
2
- declare const __LIB_VERSION__: string;
3
- declare const LIBRARY_NAME: string;
@@ -1,8 +0,0 @@
1
- import { isNumeric } from 'isNumeric';
2
-
3
- export function greaterThan(
4
- value: number | string,
5
- gt: number | string
6
- ): boolean {
7
- return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
8
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * A safe hasOwnProperty access
3
- */
4
- export default function hasOwnProperty<T>(
5
- obj: T,
6
- key: string | number | symbol
7
- ): key is keyof T {
8
- return Object.prototype.hasOwnProperty.call(obj, key);
9
- }
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
- }
@@ -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);
@@ -1,3 +0,0 @@
1
- export default function isBoolean(value: unknown): value is boolean {
2
- return !!value === value;
3
- }
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
@@ -1,5 +0,0 @@
1
- export default function isFunction(
2
- value: unknown
3
- ): value is (...args: unknown[]) => unknown {
4
- return typeof value === 'function';
5
- }
package/src/isNull.ts DELETED
@@ -1,7 +0,0 @@
1
- import bindNot from 'bindNot';
2
-
3
- export function isNull(value: unknown): value is null {
4
- return value === null;
5
- }
6
-
7
- export const isNotNull = bindNot(isNull);
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
@@ -1,5 +0,0 @@
1
- import { greaterThan } from 'greaterThan';
2
-
3
- export function isPositive(value: number | string): boolean {
4
- return greaterThan(value, 0);
5
- }
package/src/isPromise.ts DELETED
@@ -1,5 +0,0 @@
1
- import isFunction from 'isFunction';
2
-
3
- export default function isPromise(value: any): value is Promise<unknown> {
4
- return value && isFunction(value.then);
5
- }
@@ -1,3 +0,0 @@
1
- export default function isStringValue(v: unknown): v is string {
2
- return String(v) === v;
3
- }
@@ -1,7 +0,0 @@
1
- import bindNot from 'bindNot';
2
-
3
- export function isUndefined(value?: unknown): value is undefined {
4
- return value === undefined;
5
- }
6
-
7
- export const isNotUndefined = bindNot(isUndefined);
package/src/last.ts DELETED
@@ -1,7 +0,0 @@
1
- import asArray from 'asArray';
2
-
3
- export default function last<T>(values: T | T[]): T {
4
- const valuesArray = asArray(values);
5
-
6
- return valuesArray[valuesArray.length - 1];
7
- }
@@ -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
@@ -1,8 +0,0 @@
1
- import { greaterThan } from 'greaterThan';
2
-
3
- export function longerThan(
4
- value: string | unknown[],
5
- arg1: string | number
6
- ): boolean {
7
- return greaterThan(value.length, arg1);
8
- }
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
- }
@@ -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
- }
@@ -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);
@@ -1,8 +0,0 @@
1
- import isFunction from 'isFunction';
2
-
3
- export default function optionalFunctionValue<T>(
4
- value: T | ((...args: any[]) => T),
5
- ...args: unknown[]
6
- ): T {
7
- return isFunction(value) ? value(...args) : value;
8
- }
package/src/seq.ts DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * @returns a unique numeric id.
3
- */
4
-
5
- const seq = genSeq();
6
-
7
- export default seq;
8
-
9
- export function genSeq(namespace?: string): () => string {
10
- return (
11
- (n: number) => () =>
12
- `${namespace ? namespace + '_' : ''}${n++}`
13
- )(0);
14
- }
@@ -1,9 +0,0 @@
1
- export type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
2
- ? U
3
- : never;
4
-
5
- export type Stringable = string | ((...args: any[]) => string);
6
-
7
- export type CB = (...args: any[]) => any;
8
-
9
- export type ValueOf<T> = T[keyof T];
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
- }