@terrygonguet/utils 0.0.13 → 0.1.0
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/LICENSE +21 -21
- package/README.md +2 -2
- package/dist/async.d.ts +15 -0
- package/dist/async.js +60 -48
- package/dist/index.d.ts +18 -0
- package/dist/index.js +46 -39
- package/package.json +45 -63
- package/dist/functional.js +0 -266
- 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/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 {};
|