@terrygonguet/utils 0.0.14 → 0.1.1
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/async.d.ts +15 -0
- package/dist/async.js +77 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +62 -0
- package/package.json +20 -29
- package/src/async.ts +0 -112
- package/src/functional/index.ts +0 -80
- package/src/functional/maybe.ts +0 -115
- package/src/functional/result.ts +0 -151
- package/src/index.ts +0 -63
- package/src/vite-env.d.ts +0 -1
- package/types/async.d.ts +0 -14
- package/types/functional/index.d.ts +0 -19
- package/types/functional/maybe.d.ts +0 -45
- package/types/functional/result.d.ts +0 -58
- package/types/index.d.ts +0 -13
package/dist/async.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare function pause(ms: number): Promise<void>;
|
|
2
|
+
interface RetryOptions {
|
|
3
|
+
count?: number;
|
|
4
|
+
delay?: number | ((retryCount: number, error: any) => number);
|
|
5
|
+
}
|
|
6
|
+
declare function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>;
|
|
7
|
+
declare function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
8
|
+
interface AsyncMapOptions {
|
|
9
|
+
concurrent?: number;
|
|
10
|
+
}
|
|
11
|
+
declare function asyncMap<T, U>(f: AsyncMapFn<T, U>, options?: AsyncMapOptions): (data: T[]) => Promise<U[]>;
|
|
12
|
+
declare function asyncMap<T, U>(data: T[], f: AsyncMapFn<T, U>, options?: AsyncMapOptions): Promise<U[]>;
|
|
13
|
+
type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>;
|
|
14
|
+
|
|
15
|
+
export { asyncMap, pause, retry };
|
package/dist/async.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/async.ts
|
|
2
|
+
function pause(ms) {
|
|
3
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
4
|
+
}
|
|
5
|
+
function retry(providerOrOptions, options) {
|
|
6
|
+
if (typeof providerOrOptions == "function")
|
|
7
|
+
return _retry(providerOrOptions, options);
|
|
8
|
+
else
|
|
9
|
+
return (provider) => _retry(provider, providerOrOptions);
|
|
10
|
+
}
|
|
11
|
+
async function _retry(provider, { count = Infinity, delay } = {}) {
|
|
12
|
+
let retryCount = 0;
|
|
13
|
+
let lastError;
|
|
14
|
+
do {
|
|
15
|
+
try {
|
|
16
|
+
return await provider();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
lastError = error;
|
|
19
|
+
retryCount++;
|
|
20
|
+
switch (typeof delay) {
|
|
21
|
+
case "number":
|
|
22
|
+
await pause(delay);
|
|
23
|
+
break;
|
|
24
|
+
case "function":
|
|
25
|
+
await pause(delay(retryCount, error));
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
} while (retryCount <= count);
|
|
30
|
+
throw lastError;
|
|
31
|
+
}
|
|
32
|
+
function asyncMap(dataOrF, ForOptions, options) {
|
|
33
|
+
if (typeof dataOrF == "function" && typeof ForOptions != "function") {
|
|
34
|
+
const f = dataOrF;
|
|
35
|
+
const options2 = ForOptions;
|
|
36
|
+
return (data) => asyncMap_(data, f, options2);
|
|
37
|
+
} else if (Array.isArray(dataOrF) && typeof ForOptions == "function") {
|
|
38
|
+
const data = dataOrF;
|
|
39
|
+
const f = ForOptions;
|
|
40
|
+
return asyncMap_(data, f, options);
|
|
41
|
+
} else throw new Error("Invalid arguments passed to asyncMap");
|
|
42
|
+
}
|
|
43
|
+
async function asyncMap_(data, f, { concurrent = 5 } = {}) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const resolved = [];
|
|
46
|
+
const inFlight = [];
|
|
47
|
+
let next = 0;
|
|
48
|
+
function queue(i) {
|
|
49
|
+
if (resolved.length == data.length && inFlight.length == 0)
|
|
50
|
+
return finish();
|
|
51
|
+
if (i >= data.length) return;
|
|
52
|
+
const el = data[i];
|
|
53
|
+
inFlight.push([
|
|
54
|
+
i,
|
|
55
|
+
f(el, i, data).then((result) => {
|
|
56
|
+
resolved.push([i, result]);
|
|
57
|
+
const j = inFlight.findIndex(([j2]) => i == j2);
|
|
58
|
+
inFlight.splice(j, 1);
|
|
59
|
+
queue(next);
|
|
60
|
+
}, reject)
|
|
61
|
+
]);
|
|
62
|
+
next++;
|
|
63
|
+
}
|
|
64
|
+
function finish() {
|
|
65
|
+
const sorted = resolved.sort(([a], [b]) => a - b);
|
|
66
|
+
resolve(sorted.map(([, value]) => value));
|
|
67
|
+
}
|
|
68
|
+
for (let i = 0; i < concurrent; i++) {
|
|
69
|
+
queue(next);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
asyncMap,
|
|
75
|
+
pause,
|
|
76
|
+
retry
|
|
77
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Behaviour is undefined when max < min
|
|
3
|
+
*/
|
|
4
|
+
declare function clamp(value: number, min: number, max: number): number;
|
|
5
|
+
type JSONReviver = (key: string, value: any) => any;
|
|
6
|
+
/**
|
|
7
|
+
* This function does no runtime type checking,
|
|
8
|
+
* make sure that the parsed value is valid
|
|
9
|
+
*/
|
|
10
|
+
declare function safeParse<T>(str: string, defaultValue: T, reviver?: JSONReviver): T;
|
|
11
|
+
declare function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver;
|
|
12
|
+
declare function createNoopProxy<T>(): T;
|
|
13
|
+
declare function noop(): void;
|
|
14
|
+
declare function exhaustive(_: never): never;
|
|
15
|
+
declare function hash(message: string): Promise<ArrayBuffer>;
|
|
16
|
+
declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
|
|
17
|
+
|
|
18
|
+
export { clamp, composeJSONRevivers, createNoopProxy, exhaustive, hash, noop, range, safeParse };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function clamp(value, min, max) {
|
|
3
|
+
return Math.min(Math.max(value, min), max);
|
|
4
|
+
}
|
|
5
|
+
function safeParse(str, defaultValue, reviver) {
|
|
6
|
+
try {
|
|
7
|
+
return JSON.parse(str, reviver);
|
|
8
|
+
} catch (_) {
|
|
9
|
+
return defaultValue;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function composeJSONRevivers(...revivers) {
|
|
13
|
+
return function(key, value) {
|
|
14
|
+
for (const reviver of revivers) {
|
|
15
|
+
value = reviver(key, value);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function createNoopProxy() {
|
|
21
|
+
const noop2 = () => proxy;
|
|
22
|
+
const no = () => false;
|
|
23
|
+
const yes = () => true;
|
|
24
|
+
const proxy = new Proxy(() => {
|
|
25
|
+
}, {
|
|
26
|
+
get: noop2,
|
|
27
|
+
set: noop2,
|
|
28
|
+
apply: noop2,
|
|
29
|
+
construct: noop2,
|
|
30
|
+
deleteProperty: yes,
|
|
31
|
+
has: yes,
|
|
32
|
+
preventExtensions: no,
|
|
33
|
+
defineProperty: no
|
|
34
|
+
});
|
|
35
|
+
return proxy;
|
|
36
|
+
}
|
|
37
|
+
function noop() {
|
|
38
|
+
}
|
|
39
|
+
function exhaustive(_) {
|
|
40
|
+
throw new Error("This should never be called");
|
|
41
|
+
}
|
|
42
|
+
async function hash(message) {
|
|
43
|
+
const encoder = new TextEncoder();
|
|
44
|
+
const data = encoder.encode(message);
|
|
45
|
+
const hash2 = await crypto.subtle.digest("SHA-1", data);
|
|
46
|
+
return hash2;
|
|
47
|
+
}
|
|
48
|
+
function* range(start, end, step = 1) {
|
|
49
|
+
for (let i = start; i < end; i += step) {
|
|
50
|
+
yield i;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
clamp,
|
|
55
|
+
composeJSONRevivers,
|
|
56
|
+
createNoopProxy,
|
|
57
|
+
exhaustive,
|
|
58
|
+
hash,
|
|
59
|
+
noop,
|
|
60
|
+
range,
|
|
61
|
+
safeParse
|
|
62
|
+
};
|
package/package.json
CHANGED
|
@@ -1,49 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terrygonguet/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"
|
|
6
|
-
"author": {
|
|
7
|
-
"email": "terry@gonguet.com",
|
|
8
|
-
"name": "Terry Gonguet",
|
|
9
|
-
"url": "https://terry.gonguet.com/"
|
|
10
|
-
},
|
|
5
|
+
"license": "MIT",
|
|
11
6
|
"scripts": {
|
|
12
7
|
"dev": "vite",
|
|
13
|
-
"build": "
|
|
14
|
-
"test": "vitest test --
|
|
8
|
+
"build": "tsup",
|
|
9
|
+
"test": "vitest test --update",
|
|
15
10
|
"coverage": "vitest run --coverage"
|
|
16
11
|
},
|
|
17
|
-
"devDependencies": {
|
|
18
|
-
"@vitest/coverage-v8": "^0.32.0",
|
|
19
|
-
"@vitest/ui": "^0.32.0",
|
|
20
|
-
"prettier": "^2.8.8",
|
|
21
|
-
"typescript": "^5.0.2",
|
|
22
|
-
"vite": "^4.3.9",
|
|
23
|
-
"vitest": "^0.32.0"
|
|
24
|
-
},
|
|
25
12
|
"files": [
|
|
26
|
-
"dist
|
|
27
|
-
"src/**/!(*.test).ts",
|
|
28
|
-
"types/**/!(*.test).d.ts"
|
|
13
|
+
"dist"
|
|
29
14
|
],
|
|
30
15
|
"exports": {
|
|
31
16
|
".": {
|
|
32
|
-
"types": "./
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
33
18
|
"import": "./dist/index.js"
|
|
34
19
|
},
|
|
35
20
|
"./async": {
|
|
36
|
-
"types": "./
|
|
21
|
+
"types": "./dist/async.d.ts",
|
|
37
22
|
"import": "./dist/async.js"
|
|
38
|
-
},
|
|
39
|
-
"./functional": {
|
|
40
|
-
"types": "./types/functional/index.d.ts",
|
|
41
|
-
"import": "./dist/functional.js"
|
|
42
23
|
}
|
|
43
24
|
},
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@vitest/coverage-v8": "^0.32.0",
|
|
27
|
+
"@vitest/ui": "^0.32.0",
|
|
28
|
+
"prettier": "^2.8.8",
|
|
29
|
+
"tsup": "^8.1.0",
|
|
30
|
+
"typescript": "^5.0.2",
|
|
31
|
+
"vite": "^4.3.9",
|
|
32
|
+
"vitest": "^0.32.0"
|
|
33
|
+
},
|
|
34
|
+
"author": {
|
|
35
|
+
"email": "terry@gonguet.com",
|
|
36
|
+
"name": "Terry Gonguet",
|
|
37
|
+
"url": "https://terry.gonguet.com/"
|
|
47
38
|
},
|
|
48
39
|
"repository": {
|
|
49
40
|
"type": "git",
|
package/src/async.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { findIndex } from "./functional/index.ts"
|
|
2
|
-
|
|
3
|
-
export function pause(ms: number): Promise<void> {
|
|
4
|
-
return new Promise(resolve => setTimeout(resolve, ms))
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface RetryOptions {
|
|
8
|
-
count?: number
|
|
9
|
-
delay?: number | ((retryCount: number, error: any) => number)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>
|
|
13
|
-
export function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>
|
|
14
|
-
export function retry<T>(
|
|
15
|
-
providerOrOptions: RetryOptions | (() => Promise<T>),
|
|
16
|
-
options?: RetryOptions,
|
|
17
|
-
) {
|
|
18
|
-
if (typeof providerOrOptions == "function") return _retry(providerOrOptions, options)
|
|
19
|
-
else return (provider: () => Promise<T>) => _retry(provider, providerOrOptions)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async function _retry<T>(
|
|
23
|
-
provider: () => Promise<T>,
|
|
24
|
-
{ count = Infinity, delay }: RetryOptions = {},
|
|
25
|
-
): Promise<T> {
|
|
26
|
-
let retryCount = 0
|
|
27
|
-
let lastError: any
|
|
28
|
-
do {
|
|
29
|
-
try {
|
|
30
|
-
return await provider()
|
|
31
|
-
} catch (error) {
|
|
32
|
-
lastError = error
|
|
33
|
-
retryCount++
|
|
34
|
-
switch (typeof delay) {
|
|
35
|
-
case "number":
|
|
36
|
-
await pause(delay)
|
|
37
|
-
break
|
|
38
|
-
case "function":
|
|
39
|
-
await pause(delay(retryCount, error))
|
|
40
|
-
break
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
} while (retryCount <= count)
|
|
44
|
-
throw lastError
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
interface AsyncMapOptions {
|
|
48
|
-
concurrent?: number
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function asyncMap<T, U>(
|
|
52
|
-
f: AsyncMapFn<T, U>,
|
|
53
|
-
options?: AsyncMapOptions,
|
|
54
|
-
): (data: T[]) => Promise<U[]>
|
|
55
|
-
export function asyncMap<T, U>(
|
|
56
|
-
data: T[],
|
|
57
|
-
f: AsyncMapFn<T, U>,
|
|
58
|
-
options?: AsyncMapOptions,
|
|
59
|
-
): Promise<U[]>
|
|
60
|
-
export function asyncMap<T, U>(
|
|
61
|
-
dataOrF: T[] | AsyncMapFn<T, U>,
|
|
62
|
-
ForOptions: AsyncMapFn<T, U> | AsyncMapOptions | undefined,
|
|
63
|
-
options?: AsyncMapOptions,
|
|
64
|
-
): Promise<U[]> | ((data: T[]) => Promise<U[]>) {
|
|
65
|
-
if (typeof dataOrF == "function" && typeof ForOptions != "function") {
|
|
66
|
-
const f = dataOrF
|
|
67
|
-
const options = ForOptions
|
|
68
|
-
return (data: T[]) => asyncMap_(data, f, options)
|
|
69
|
-
} else if (Array.isArray(dataOrF) && typeof ForOptions == "function") {
|
|
70
|
-
const data = dataOrF
|
|
71
|
-
const f = ForOptions
|
|
72
|
-
return asyncMap_(data, f, options)
|
|
73
|
-
} else throw new Error("Invalid arguments passed to asyncMap")
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>
|
|
77
|
-
|
|
78
|
-
async function asyncMap_<T, U>(
|
|
79
|
-
data: T[],
|
|
80
|
-
f: AsyncMapFn<T, U>,
|
|
81
|
-
{ concurrent = 5 }: AsyncMapOptions = {},
|
|
82
|
-
): Promise<U[]> {
|
|
83
|
-
return new Promise((resolve, reject) => {
|
|
84
|
-
const resolved: [i: number, result: U][] = []
|
|
85
|
-
const inFlight: [i: number, promise: Promise<void>][] = []
|
|
86
|
-
|
|
87
|
-
let next = 0
|
|
88
|
-
function queue(i: number) {
|
|
89
|
-
if (resolved.length == data.length && inFlight.length == 0) return finish()
|
|
90
|
-
if (i >= data.length) return
|
|
91
|
-
const el = data[i]
|
|
92
|
-
inFlight.push([
|
|
93
|
-
i,
|
|
94
|
-
f(el, i, data).then(result => {
|
|
95
|
-
resolved.push([i, result])
|
|
96
|
-
findIndex(inFlight, ([j]) => i == j).map(j => inFlight.splice(j, 1))
|
|
97
|
-
queue(next)
|
|
98
|
-
}, reject),
|
|
99
|
-
])
|
|
100
|
-
next++
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function finish() {
|
|
104
|
-
const sorted = resolved.sort(([a], [b]) => a - b)
|
|
105
|
-
resolve(sorted.map(([, value]) => value))
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
for (let i = 0; i < concurrent; i++) {
|
|
109
|
-
queue(next)
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
}
|
package/src/functional/index.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Maybe } from "./maybe.ts"
|
|
2
|
-
|
|
3
|
-
export * from "./maybe.ts"
|
|
4
|
-
export * from "./result.ts"
|
|
5
|
-
export { default as compose } from "just-compose"
|
|
6
|
-
export { default as pipe } from "just-pipe"
|
|
7
|
-
|
|
8
|
-
export function identity<T>(value: T) {
|
|
9
|
-
return value
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function constant<T>(value: T) {
|
|
13
|
-
return () => value
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function at(idx: number): <T>(data: T[]) => Maybe<T>
|
|
17
|
-
export function at<T>(data: T[], idx: number): Maybe<T>
|
|
18
|
-
export function at<T>(dataOrIdx: T[] | number, idx?: number) {
|
|
19
|
-
if (typeof dataOrIdx == "number") return (data: T[]) => Maybe.from(data.at(dataOrIdx))
|
|
20
|
-
else return Maybe.from(dataOrIdx.at(idx!))
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type Predicate<T> = (value: T, idx: number, arr: T[]) => boolean
|
|
24
|
-
|
|
25
|
-
export function find<T>(predicate: Predicate<T>): (data: T[]) => Maybe<T>
|
|
26
|
-
export function find<T>(data: T[], predicate: Predicate<T>): Maybe<T>
|
|
27
|
-
export function find<T>(dataOrPredicate: T[] | Predicate<T>, predicate?: Predicate<T>) {
|
|
28
|
-
if (typeof dataOrPredicate == "function")
|
|
29
|
-
return (data: T[]) => Maybe.from(data.find(dataOrPredicate))
|
|
30
|
-
else return Maybe.from(dataOrPredicate.find(predicate!))
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function findIndex<T>(predicate: Predicate<T>): (data: T[]) => Maybe<number>
|
|
34
|
-
export function findIndex<T>(data: T[], predicate: Predicate<T>): Maybe<number>
|
|
35
|
-
export function findIndex<T>(dataOrPredicate: T[] | Predicate<T>, predicate?: Predicate<T>) {
|
|
36
|
-
if (typeof dataOrPredicate == "function") {
|
|
37
|
-
return (data: T[]) => {
|
|
38
|
-
const idx = data.findIndex(dataOrPredicate)
|
|
39
|
-
return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
const idx = dataOrPredicate.findIndex(predicate!)
|
|
43
|
-
return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function prop<T extends {}, K1 extends keyof T = keyof T>(data: T, key1: K1): Maybe<T[K1]>
|
|
48
|
-
export function prop<
|
|
49
|
-
T extends {},
|
|
50
|
-
K1 extends keyof T = keyof T,
|
|
51
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
52
|
-
>(data: T, key1: K1, key2: K2): Maybe<T[K1][K2]>
|
|
53
|
-
export function prop<
|
|
54
|
-
T extends {},
|
|
55
|
-
K1 extends keyof T = keyof T,
|
|
56
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
57
|
-
K3 extends keyof T[K1][K2] = keyof T[K1][K2],
|
|
58
|
-
>(data: T, key1: K1, key2: K2, key: K3): Maybe<T[K1][K2][K3]>
|
|
59
|
-
export function prop<
|
|
60
|
-
T extends {},
|
|
61
|
-
K1 extends keyof T = keyof T,
|
|
62
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
63
|
-
K3 extends keyof T[K1][K2] = keyof T[K1][K2],
|
|
64
|
-
K4 extends keyof T[K1][K2][K3] = keyof T[K1][K2][K3],
|
|
65
|
-
>(data: T, key1: K1, key2: K2, key: K3, key4: K4): Maybe<T[K1][K2][K3][K4]>
|
|
66
|
-
export function prop<
|
|
67
|
-
T extends {},
|
|
68
|
-
K1 extends keyof T = keyof T,
|
|
69
|
-
K2 extends keyof T[K1] = keyof T[K1],
|
|
70
|
-
K3 extends keyof T[K1][K2] = keyof T[K1][K2],
|
|
71
|
-
K4 extends keyof T[K1][K2][K3] = keyof T[K1][K2][K3],
|
|
72
|
-
K5 extends keyof T[K1][K2][K3][K4] = keyof T[K1][K2][K3][K4],
|
|
73
|
-
>(data: T, key1: K1, key2: K2, key: K3, key4: K4, key5: K5): Maybe<T[K1][K2][K3][K4][K5]>
|
|
74
|
-
|
|
75
|
-
export function prop(data: any, ...path: string[]) {
|
|
76
|
-
for (const key of path) {
|
|
77
|
-
data = data?.[key]
|
|
78
|
-
}
|
|
79
|
-
return Maybe.from(data)
|
|
80
|
-
}
|
package/src/functional/maybe.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { compose, constant, identity } from "./index.ts"
|
|
2
|
-
import { Result } from "./result.ts"
|
|
3
|
-
|
|
4
|
-
const $_kind = "@terrygonguet/utils/functional/maybe"
|
|
5
|
-
const $_variant_Some = "@terrygonguet/utils/functional/maybe/Some"
|
|
6
|
-
const $_variant_None = "@terrygonguet/utils/functional/maybe/None"
|
|
7
|
-
|
|
8
|
-
export interface Maybe<T> {
|
|
9
|
-
isSome(): this is Some<T>
|
|
10
|
-
isNone(): this is None<T>
|
|
11
|
-
orDefault(defaultValue: T): T
|
|
12
|
-
map<U>(f: (value: T) => U): Maybe<U>
|
|
13
|
-
flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>
|
|
14
|
-
toResult(): Result<T, undefined>
|
|
15
|
-
toResult<U>(mapNone: () => U): Result<T, U>
|
|
16
|
-
toJSON(): Object
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
class Some<T> implements Maybe<T> {
|
|
20
|
-
value!: T
|
|
21
|
-
|
|
22
|
-
constructor(value: T) {
|
|
23
|
-
Object.defineProperties(this, {
|
|
24
|
-
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
25
|
-
$_variant: { value: $_variant_Some, enumerable: false, writable: false },
|
|
26
|
-
value: { value, writable: false },
|
|
27
|
-
})
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
isSome(): true {
|
|
31
|
-
return true
|
|
32
|
-
}
|
|
33
|
-
isNone(): false {
|
|
34
|
-
return false
|
|
35
|
-
}
|
|
36
|
-
orDefault() {
|
|
37
|
-
return this.value
|
|
38
|
-
}
|
|
39
|
-
map<U>(f: (value: T) => U) {
|
|
40
|
-
return new Some(f(this.value))
|
|
41
|
-
}
|
|
42
|
-
flatMap<U>(f: (value: T) => Maybe<U>) {
|
|
43
|
-
return f(this.value)
|
|
44
|
-
}
|
|
45
|
-
toResult<U>(): Result<T, U> {
|
|
46
|
-
return Result.Success(this.value)
|
|
47
|
-
}
|
|
48
|
-
toJSON(): Object {
|
|
49
|
-
return { $_kind, $_variant: $_variant_Some, value: this.value }
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
class None<T> implements Maybe<T> {
|
|
54
|
-
constructor() {}
|
|
55
|
-
|
|
56
|
-
isSome(): false {
|
|
57
|
-
return false
|
|
58
|
-
}
|
|
59
|
-
isNone(): true {
|
|
60
|
-
return true
|
|
61
|
-
}
|
|
62
|
-
orDefault(defaultValue: T) {
|
|
63
|
-
return defaultValue
|
|
64
|
-
}
|
|
65
|
-
map<U>() {
|
|
66
|
-
return this as unknown as Maybe<U>
|
|
67
|
-
}
|
|
68
|
-
flatMap<U>() {
|
|
69
|
-
return this as unknown as Maybe<U>
|
|
70
|
-
}
|
|
71
|
-
toResult(): Result<T, undefined>
|
|
72
|
-
toResult<U>(mapNone: () => U): Result<T, U>
|
|
73
|
-
toResult<U>(mapNone?: () => U) {
|
|
74
|
-
return mapNone ? Result.Failure<T, U>(mapNone?.()) : Result.Failure<T, undefined>(undefined)
|
|
75
|
-
}
|
|
76
|
-
toJSON(): Object {
|
|
77
|
-
return { $_kind, $_variant: $_variant_None }
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const none = new None()
|
|
82
|
-
export const Maybe = {
|
|
83
|
-
Some<T>(value: T): Maybe<T> {
|
|
84
|
-
return new Some(value)
|
|
85
|
-
},
|
|
86
|
-
None<T>() {
|
|
87
|
-
return none as Maybe<T>
|
|
88
|
-
},
|
|
89
|
-
from<T>(value: T | undefined | null): Maybe<T> {
|
|
90
|
-
switch (value) {
|
|
91
|
-
case null:
|
|
92
|
-
case undefined:
|
|
93
|
-
return this.None()
|
|
94
|
-
default:
|
|
95
|
-
return new Some(value!)
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
/**
|
|
99
|
-
* CAUTION: this method swallows errors and simply returns None!
|
|
100
|
-
* Use `Result.fromPromise()` if you need error details.
|
|
101
|
-
*/
|
|
102
|
-
fromPromise<T>(promise: Promise<T>, onResolve: (value: T) => T = identity): Promise<Maybe<T>> {
|
|
103
|
-
return promise.then(compose(onResolve, Maybe.from), constant(this.None()))
|
|
104
|
-
},
|
|
105
|
-
JSONReviver(_key: string, value: any) {
|
|
106
|
-
if (value?.$_kind == $_kind) {
|
|
107
|
-
const $_variant = value?.$_variant
|
|
108
|
-
if ($_variant == $_variant_Some) return new Some<unknown>(value?.value)
|
|
109
|
-
else if ($_variant == $_variant_None) return Maybe.None()
|
|
110
|
-
else return value
|
|
111
|
-
} else return value
|
|
112
|
-
},
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export type { Some, None }
|
package/src/functional/result.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { compose, identity } from "./index.ts"
|
|
2
|
-
import { Maybe } from "./maybe.ts"
|
|
3
|
-
|
|
4
|
-
const $_kind = "@terrygonguet/utils/functional/result"
|
|
5
|
-
const $_variant_Success = "@terrygonguet/utils/functional/result/Success"
|
|
6
|
-
const $_variant_Failure = "@terrygonguet/utils/functional/result/Failure"
|
|
7
|
-
|
|
8
|
-
export interface Result<S, F> {
|
|
9
|
-
isSuccess(): this is Success<S, F>
|
|
10
|
-
isFailure(): this is Failure<S, F>
|
|
11
|
-
merge<T>(whenSuccess: (value: S) => T, whenFailure: (reason: F) => T): T
|
|
12
|
-
match(onSuccess: (value: S) => void, onFailure: (reason: F) => void): void
|
|
13
|
-
map<S2>(f: (value: S) => S2): Result<S2, F>
|
|
14
|
-
flatMap<S2, F2>(f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>
|
|
15
|
-
toJSON(): Object
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class Success<S, F> implements Result<S, F> {
|
|
19
|
-
value!: S
|
|
20
|
-
|
|
21
|
-
constructor(value: S) {
|
|
22
|
-
Object.defineProperties(this, {
|
|
23
|
-
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
24
|
-
$_variant: { value: $_variant_Success, enumerable: false, writable: false },
|
|
25
|
-
value: { value, writable: false },
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
isSuccess(): true {
|
|
30
|
-
return true
|
|
31
|
-
}
|
|
32
|
-
isFailure(): false {
|
|
33
|
-
return false
|
|
34
|
-
}
|
|
35
|
-
merge<T>(whenSuccess: (value: S) => T) {
|
|
36
|
-
return whenSuccess(this.value)
|
|
37
|
-
}
|
|
38
|
-
match(onSuccess: (value: S) => void) {
|
|
39
|
-
return onSuccess(this.value)
|
|
40
|
-
}
|
|
41
|
-
map<S2>(f: (value: S) => S2) {
|
|
42
|
-
return new Success<S2, F>(f(this.value))
|
|
43
|
-
}
|
|
44
|
-
flatMap<S2, F2>(f: (value: S) => Result<S2, F2>): Result<S2, F | F2> {
|
|
45
|
-
return f(this.value)
|
|
46
|
-
}
|
|
47
|
-
toJSON(this: Success<S, F>) {
|
|
48
|
-
return { $_kind, $_variant: $_variant_Success, value: this.value }
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
class Failure<S, F> implements Result<S, F> {
|
|
53
|
-
reason!: F
|
|
54
|
-
|
|
55
|
-
constructor(reason: F) {
|
|
56
|
-
Object.defineProperties(this, {
|
|
57
|
-
$_kind: { value: $_kind, enumerable: false, writable: false },
|
|
58
|
-
$_variant: { value: $_variant_Success, enumerable: false, writable: false },
|
|
59
|
-
reason: { value: reason, writable: false },
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
isSuccess(): false {
|
|
64
|
-
return false
|
|
65
|
-
}
|
|
66
|
-
isFailure(): true {
|
|
67
|
-
return true
|
|
68
|
-
}
|
|
69
|
-
merge<T>(_: (value: S) => T, whenFailure: (reason: F) => T) {
|
|
70
|
-
return whenFailure(this.reason)
|
|
71
|
-
}
|
|
72
|
-
match(_: (value: S) => void, onFailure: (reason: F) => void) {
|
|
73
|
-
return onFailure(this.reason)
|
|
74
|
-
}
|
|
75
|
-
map<S2>() {
|
|
76
|
-
return this as unknown as Result<S2, F>
|
|
77
|
-
}
|
|
78
|
-
flatMap<S2, F2>() {
|
|
79
|
-
return this as unknown as Result<S2, F | F2>
|
|
80
|
-
}
|
|
81
|
-
toJSON() {
|
|
82
|
-
return { $_kind, $_variant: $_variant_Failure, reason: this.reason }
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>
|
|
87
|
-
function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>
|
|
88
|
-
function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone?: () => F) {
|
|
89
|
-
return mapNone ? maybe.toResult(mapNone) : maybe.toResult()
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export const Result = {
|
|
93
|
-
Success<S, F>(value: S): Result<S, F> {
|
|
94
|
-
return new Success<S, F>(value)
|
|
95
|
-
},
|
|
96
|
-
Failure<S, F>(reason: F): Result<S, F> {
|
|
97
|
-
return new Failure<S, F>(reason)
|
|
98
|
-
},
|
|
99
|
-
try<S, F>(tryFn: () => S) {
|
|
100
|
-
return new TryCatch<S, F>(tryFn)
|
|
101
|
-
},
|
|
102
|
-
fromPromise<S, F>(
|
|
103
|
-
promise: Promise<S>,
|
|
104
|
-
onResolve: (value: S) => S,
|
|
105
|
-
onReject: (reason: unknown) => F,
|
|
106
|
-
): Promise<Result<S, F>> {
|
|
107
|
-
return promise.then(
|
|
108
|
-
compose(onResolve, this.Success<S, F>),
|
|
109
|
-
compose(onReject, this.Failure<S, F>),
|
|
110
|
-
)
|
|
111
|
-
},
|
|
112
|
-
fromMaybe: resultFromMaybe,
|
|
113
|
-
JSONReviver(_key: string, value: any) {
|
|
114
|
-
if (value?.$_kind == $_kind) {
|
|
115
|
-
const $_variant = value?.$_variant
|
|
116
|
-
if ($_variant == $_variant_Success) return new Success<unknown, unknown>(value?.value)
|
|
117
|
-
else if ($_variant == $_variant_Failure)
|
|
118
|
-
return new Failure<unknown, unknown>(value?.reason)
|
|
119
|
-
else return value
|
|
120
|
-
} else return value
|
|
121
|
-
},
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
class TryCatch<S, F> {
|
|
125
|
-
tryFn: () => S
|
|
126
|
-
catchFn: (err: unknown) => F
|
|
127
|
-
|
|
128
|
-
constructor(tryFn: () => S) {
|
|
129
|
-
this.tryFn = tryFn
|
|
130
|
-
this.catchFn = identity as any
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
catch(catchFn: (err: unknown) => F) {
|
|
134
|
-
this.catchFn = catchFn
|
|
135
|
-
return this
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F> {
|
|
139
|
-
try {
|
|
140
|
-
const result = Result.Success<S, F>(this.tryFn())
|
|
141
|
-
finallyFn?.(result)
|
|
142
|
-
return result
|
|
143
|
-
} catch (error) {
|
|
144
|
-
const result = Result.Failure<S, F>(this.catchFn(error))
|
|
145
|
-
finallyFn?.(result)
|
|
146
|
-
return result
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export type { Success, Failure }
|
package/src/index.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { constant } from "./functional/index.ts"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Behaviour is undefined when max < min
|
|
5
|
-
*/
|
|
6
|
-
export function clamp(value: number, min: number, max: number) {
|
|
7
|
-
return Math.min(Math.max(value, min), max)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type JSONReviver = (key: string, value: any) => any
|
|
11
|
-
|
|
12
|
-
export function safeParse<T>(str: string, defaultValue: T, reviver?: JSONReviver) {
|
|
13
|
-
try {
|
|
14
|
-
return JSON.parse(str, reviver)
|
|
15
|
-
} catch (_) {
|
|
16
|
-
return defaultValue
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver {
|
|
21
|
-
return function (key, value) {
|
|
22
|
-
for (const reviver of revivers) {
|
|
23
|
-
value = reviver(key, value)
|
|
24
|
-
}
|
|
25
|
-
return value
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function createNoopProxy<T>() {
|
|
30
|
-
const noop = () => proxy
|
|
31
|
-
const no = constant(false)
|
|
32
|
-
const yes = constant(true)
|
|
33
|
-
const proxy: any = new Proxy(() => {}, {
|
|
34
|
-
get: noop,
|
|
35
|
-
set: noop,
|
|
36
|
-
apply: noop,
|
|
37
|
-
construct: noop,
|
|
38
|
-
deleteProperty: yes,
|
|
39
|
-
has: yes,
|
|
40
|
-
preventExtensions: no,
|
|
41
|
-
defineProperty: no,
|
|
42
|
-
})
|
|
43
|
-
return proxy as T
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function noop() {}
|
|
47
|
-
|
|
48
|
-
export function exhaustive(_: never): never {
|
|
49
|
-
throw new Error("This should never be called")
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function hash(message: string) {
|
|
53
|
-
const encoder = new TextEncoder()
|
|
54
|
-
const data = encoder.encode(message)
|
|
55
|
-
const hash = await crypto.subtle.digest("SHA-1", data)
|
|
56
|
-
return hash
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function* range(start: number, end: number, step = 1) {
|
|
60
|
-
for (let i = start; i < end; i += step) {
|
|
61
|
-
yield i
|
|
62
|
-
}
|
|
63
|
-
}
|
package/src/vite-env.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/// <reference types="vite/client" />
|
package/types/async.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export declare function pause(ms: number): Promise<void>;
|
|
2
|
-
interface RetryOptions {
|
|
3
|
-
count?: number;
|
|
4
|
-
delay?: number | ((retryCount: number, error: any) => number);
|
|
5
|
-
}
|
|
6
|
-
export declare function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>;
|
|
7
|
-
export declare function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>;
|
|
8
|
-
interface AsyncMapOptions {
|
|
9
|
-
concurrent?: number;
|
|
10
|
-
}
|
|
11
|
-
export declare function asyncMap<T, U>(f: AsyncMapFn<T, U>, options?: AsyncMapOptions): (data: T[]) => Promise<U[]>;
|
|
12
|
-
export declare function asyncMap<T, U>(data: T[], f: AsyncMapFn<T, U>, options?: AsyncMapOptions): Promise<U[]>;
|
|
13
|
-
type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>;
|
|
14
|
-
export {};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Maybe } from "./maybe.ts";
|
|
2
|
-
export * from "./maybe.ts";
|
|
3
|
-
export * from "./result.ts";
|
|
4
|
-
export { default as compose } from "just-compose";
|
|
5
|
-
export { default as pipe } from "just-pipe";
|
|
6
|
-
export declare function identity<T>(value: T): T;
|
|
7
|
-
export declare function constant<T>(value: T): () => T;
|
|
8
|
-
export declare function at(idx: number): <T>(data: T[]) => Maybe<T>;
|
|
9
|
-
export declare function at<T>(data: T[], idx: number): Maybe<T>;
|
|
10
|
-
type Predicate<T> = (value: T, idx: number, arr: T[]) => boolean;
|
|
11
|
-
export declare function find<T>(predicate: Predicate<T>): (data: T[]) => Maybe<T>;
|
|
12
|
-
export declare function find<T>(data: T[], predicate: Predicate<T>): Maybe<T>;
|
|
13
|
-
export declare function findIndex<T>(predicate: Predicate<T>): (data: T[]) => Maybe<number>;
|
|
14
|
-
export declare function findIndex<T>(data: T[], predicate: Predicate<T>): Maybe<number>;
|
|
15
|
-
export declare function prop<T extends {}, K1 extends keyof T = keyof T>(data: T, key1: K1): Maybe<T[K1]>;
|
|
16
|
-
export declare function prop<T extends {}, K1 extends keyof T = keyof T, K2 extends keyof T[K1] = keyof T[K1]>(data: T, key1: K1, key2: K2): Maybe<T[K1][K2]>;
|
|
17
|
-
export declare function prop<T extends {}, K1 extends keyof T = keyof T, K2 extends keyof T[K1] = keyof T[K1], K3 extends keyof T[K1][K2] = keyof T[K1][K2]>(data: T, key1: K1, key2: K2, key: K3): Maybe<T[K1][K2][K3]>;
|
|
18
|
-
export declare function prop<T extends {}, K1 extends keyof T = keyof T, K2 extends keyof T[K1] = keyof T[K1], K3 extends keyof T[K1][K2] = keyof T[K1][K2], K4 extends keyof T[K1][K2][K3] = keyof T[K1][K2][K3]>(data: T, key1: K1, key2: K2, key: K3, key4: K4): Maybe<T[K1][K2][K3][K4]>;
|
|
19
|
-
export declare function prop<T extends {}, K1 extends keyof T = keyof T, K2 extends keyof T[K1] = keyof T[K1], K3 extends keyof T[K1][K2] = keyof T[K1][K2], K4 extends keyof T[K1][K2][K3] = keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4] = keyof T[K1][K2][K3][K4]>(data: T, key1: K1, key2: K2, key: K3, key4: K4, key5: K5): Maybe<T[K1][K2][K3][K4][K5]>;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { Result } from "./result.ts";
|
|
2
|
-
export interface Maybe<T> {
|
|
3
|
-
isSome(): this is Some<T>;
|
|
4
|
-
isNone(): this is None<T>;
|
|
5
|
-
orDefault(defaultValue: T): T;
|
|
6
|
-
map<U>(f: (value: T) => U): Maybe<U>;
|
|
7
|
-
flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>;
|
|
8
|
-
toResult(): Result<T, undefined>;
|
|
9
|
-
toResult<U>(mapNone: () => U): Result<T, U>;
|
|
10
|
-
toJSON(): Object;
|
|
11
|
-
}
|
|
12
|
-
declare class Some<T> implements Maybe<T> {
|
|
13
|
-
value: T;
|
|
14
|
-
constructor(value: T);
|
|
15
|
-
isSome(): true;
|
|
16
|
-
isNone(): false;
|
|
17
|
-
orDefault(): T;
|
|
18
|
-
map<U>(f: (value: T) => U): Some<U>;
|
|
19
|
-
flatMap<U>(f: (value: T) => Maybe<U>): Maybe<U>;
|
|
20
|
-
toResult<U>(): Result<T, U>;
|
|
21
|
-
toJSON(): Object;
|
|
22
|
-
}
|
|
23
|
-
declare class None<T> implements Maybe<T> {
|
|
24
|
-
constructor();
|
|
25
|
-
isSome(): false;
|
|
26
|
-
isNone(): true;
|
|
27
|
-
orDefault(defaultValue: T): T;
|
|
28
|
-
map<U>(): Maybe<U>;
|
|
29
|
-
flatMap<U>(): Maybe<U>;
|
|
30
|
-
toResult(): Result<T, undefined>;
|
|
31
|
-
toResult<U>(mapNone: () => U): Result<T, U>;
|
|
32
|
-
toJSON(): Object;
|
|
33
|
-
}
|
|
34
|
-
export declare const Maybe: {
|
|
35
|
-
Some<T>(value: T): Maybe<T>;
|
|
36
|
-
None<T_1>(): Maybe<T_1>;
|
|
37
|
-
from<T_2>(value: T_2 | null | undefined): Maybe<T_2>;
|
|
38
|
-
/**
|
|
39
|
-
* CAUTION: this method swallows errors and simply returns None!
|
|
40
|
-
* Use `Result.fromPromise()` if you need error details.
|
|
41
|
-
*/
|
|
42
|
-
fromPromise<T_3>(promise: Promise<T_3>, onResolve?: (value: T_3) => T_3): Promise<Maybe<T_3>>;
|
|
43
|
-
JSONReviver(_key: string, value: any): any;
|
|
44
|
-
};
|
|
45
|
-
export type { Some, None };
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Maybe } from "./maybe.ts";
|
|
2
|
-
export interface Result<S, F> {
|
|
3
|
-
isSuccess(): this is Success<S, F>;
|
|
4
|
-
isFailure(): this is Failure<S, F>;
|
|
5
|
-
merge<T>(whenSuccess: (value: S) => T, whenFailure: (reason: F) => T): T;
|
|
6
|
-
match(onSuccess: (value: S) => void, onFailure: (reason: F) => void): void;
|
|
7
|
-
map<S2>(f: (value: S) => S2): Result<S2, F>;
|
|
8
|
-
flatMap<S2, F2>(f: (value: S) => Result<S2, F | F2>): Result<S2, F | F2>;
|
|
9
|
-
toJSON(): Object;
|
|
10
|
-
}
|
|
11
|
-
declare class Success<S, F> implements Result<S, F> {
|
|
12
|
-
value: S;
|
|
13
|
-
constructor(value: S);
|
|
14
|
-
isSuccess(): true;
|
|
15
|
-
isFailure(): false;
|
|
16
|
-
merge<T>(whenSuccess: (value: S) => T): T;
|
|
17
|
-
match(onSuccess: (value: S) => void): void;
|
|
18
|
-
map<S2>(f: (value: S) => S2): Success<S2, F>;
|
|
19
|
-
flatMap<S2, F2>(f: (value: S) => Result<S2, F2>): Result<S2, F | F2>;
|
|
20
|
-
toJSON(this: Success<S, F>): {
|
|
21
|
-
$_kind: string;
|
|
22
|
-
$_variant: string;
|
|
23
|
-
value: S;
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
declare class Failure<S, F> implements Result<S, F> {
|
|
27
|
-
reason: F;
|
|
28
|
-
constructor(reason: F);
|
|
29
|
-
isSuccess(): false;
|
|
30
|
-
isFailure(): true;
|
|
31
|
-
merge<T>(_: (value: S) => T, whenFailure: (reason: F) => T): T;
|
|
32
|
-
match(_: (value: S) => void, onFailure: (reason: F) => void): void;
|
|
33
|
-
map<S2>(): Result<S2, F>;
|
|
34
|
-
flatMap<S2, F2>(): Result<S2, F | F2>;
|
|
35
|
-
toJSON(): {
|
|
36
|
-
$_kind: string;
|
|
37
|
-
$_variant: string;
|
|
38
|
-
reason: F;
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
declare function resultFromMaybe<S>(maybe: Maybe<S>): Result<S, undefined>;
|
|
42
|
-
declare function resultFromMaybe<S, F>(maybe: Maybe<S>, mapNone: () => F): Result<S, F>;
|
|
43
|
-
export declare const Result: {
|
|
44
|
-
Success<S, F>(value: S): Result<S, F>;
|
|
45
|
-
Failure<S_1, F_1>(reason: F_1): Result<S_1, F_1>;
|
|
46
|
-
try<S_2, F_2>(tryFn: () => S_2): TryCatch<S_2, F_2>;
|
|
47
|
-
fromPromise<S_3, F_3>(promise: Promise<S_3>, onResolve: (value: S_3) => S_3, onReject: (reason: unknown) => F_3): Promise<Result<S_3, F_3>>;
|
|
48
|
-
fromMaybe: typeof resultFromMaybe;
|
|
49
|
-
JSONReviver(_key: string, value: any): any;
|
|
50
|
-
};
|
|
51
|
-
declare class TryCatch<S, F> {
|
|
52
|
-
tryFn: () => S;
|
|
53
|
-
catchFn: (err: unknown) => F;
|
|
54
|
-
constructor(tryFn: () => S);
|
|
55
|
-
catch(catchFn: (err: unknown) => F): this;
|
|
56
|
-
exec(finallyFn?: (result: Result<S, F>) => void): Result<S, F>;
|
|
57
|
-
}
|
|
58
|
-
export type { Success, Failure };
|
package/types/index.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Behaviour is undefined when max < min
|
|
3
|
-
*/
|
|
4
|
-
export declare function clamp(value: number, min: number, max: number): number;
|
|
5
|
-
type JSONReviver = (key: string, value: any) => any;
|
|
6
|
-
export declare function safeParse<T>(str: string, defaultValue: T, reviver?: JSONReviver): any;
|
|
7
|
-
export declare function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver;
|
|
8
|
-
export declare function createNoopProxy<T>(): T;
|
|
9
|
-
export declare function noop(): void;
|
|
10
|
-
export declare function exhaustive(_: never): never;
|
|
11
|
-
export declare function hash(message: string): Promise<ArrayBuffer>;
|
|
12
|
-
export declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
|
|
13
|
-
export {};
|