@terrygonguet/utils 0.0.12 → 0.0.14

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 CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Terry Gonguet
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Terry Gonguet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,2 +1,2 @@
1
- # utils
2
- Okay fine I'll make my own
1
+ # utils
2
+ Okay fine I'll make my own
package/package.json CHANGED
@@ -1,63 +1,55 @@
1
- {
2
- "name": "@terrygonguet/utils",
3
- "version": "0.0.12",
4
- "type": "module",
5
- "module": "./dist/index.js",
6
- "author": {
7
- "email": "terry@gonguet.com",
8
- "name": "Terry Gonguet",
9
- "url": "https://terry.gonguet.com/"
10
- },
11
- "scripts": {
12
- "dev": "vite",
13
- "build": "tsc && vite build",
14
- "test": "vitest test --ui --watch --update",
15
- "coverage": "vitest run --coverage"
16
- },
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
- "files": [
26
- "dist/**/*.js",
27
- "src/**/!(*.test).ts",
28
- "types/**/!(*.test).d.ts"
29
- ],
30
- "exports": {
31
- ".": {
32
- "import": "./dist/index.js"
33
- },
34
- "./async": {
35
- "import": "./dist/async.js"
36
- },
37
- "./functional": {
38
- "import": "./dist/functional.js"
39
- }
40
- },
41
- "typesVersions": {
42
- "*": {
43
- "async": [
44
- "./types/async.d.ts"
45
- ],
46
- "functional": [
47
- "./types/functional/index.d.ts"
48
- ]
49
- }
50
- },
51
- "types": "./types/index.d.ts",
52
- "dependencies": {
53
- "just-compose": "^2.3.0",
54
- "just-pipe": "^1.0.0"
55
- },
56
- "repository": {
57
- "type": "git",
58
- "url": "https://github.com/terrygonguet/utils.git"
59
- },
60
- "bugs": {
61
- "url": "https://github.com/terrygonguet/utils/issues"
62
- }
63
- }
1
+ {
2
+ "name": "@terrygonguet/utils",
3
+ "version": "0.0.14",
4
+ "type": "module",
5
+ "module": "./dist/index.js",
6
+ "author": {
7
+ "email": "terry@gonguet.com",
8
+ "name": "Terry Gonguet",
9
+ "url": "https://terry.gonguet.com/"
10
+ },
11
+ "scripts": {
12
+ "dev": "vite",
13
+ "build": "tsc && vite build",
14
+ "test": "vitest test --ui --watch --update",
15
+ "coverage": "vitest run --coverage"
16
+ },
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
+ "files": [
26
+ "dist/**/*.js",
27
+ "src/**/!(*.test).ts",
28
+ "types/**/!(*.test).d.ts"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./types/index.d.ts",
33
+ "import": "./dist/index.js"
34
+ },
35
+ "./async": {
36
+ "types": "./types/async.d.ts",
37
+ "import": "./dist/async.js"
38
+ },
39
+ "./functional": {
40
+ "types": "./types/functional/index.d.ts",
41
+ "import": "./dist/functional.js"
42
+ }
43
+ },
44
+ "dependencies": {
45
+ "just-compose": "^2.3.0",
46
+ "just-pipe": "^1.0.0"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "https://github.com/terrygonguet/utils.git"
51
+ },
52
+ "bugs": {
53
+ "url": "https://github.com/terrygonguet/utils/issues"
54
+ }
55
+ }
@@ -43,3 +43,38 @@ export function findIndex<T>(dataOrPredicate: T[] | Predicate<T>, predicate?: Pr
43
43
  return idx == -1 ? Maybe.None<number>() : Maybe.Some(idx)
44
44
  }
45
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/index.ts CHANGED
@@ -1,63 +1,63 @@
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
- }
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 CHANGED
@@ -1 +1 @@
1
- /// <reference types="vite/client" />
1
+ /// <reference types="vite/client" />
package/types/async.d.ts CHANGED
@@ -1,14 +1,14 @@
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
+ 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,14 +1,19 @@
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>;
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 +1,45 @@
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
+ 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 +1,58 @@
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 };
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 CHANGED
@@ -1,13 +1,13 @@
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 {};
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 {};
package/dist/async.js DELETED
@@ -1,65 +0,0 @@
1
- import { findIndex as g } from "./functional.js";
2
- function h(n) {
3
- return new Promise((t) => setTimeout(t, n));
4
- }
5
- function x(n, t) {
6
- return typeof n == "function" ? p(n, t) : (r) => p(r, n);
7
- }
8
- async function p(n, { count: t = 1 / 0, delay: r } = {}) {
9
- let o = 0, s;
10
- do
11
- try {
12
- return await n();
13
- } catch (c) {
14
- switch (s = c, o++, typeof r) {
15
- case "number":
16
- await h(r);
17
- break;
18
- case "function":
19
- await h(r(o, c));
20
- break;
21
- }
22
- }
23
- while (o <= t);
24
- throw s;
25
- }
26
- function I(n, t, r) {
27
- if (typeof n == "function" && typeof t != "function") {
28
- const o = n, s = t;
29
- return (c) => w(c, o, s);
30
- } else {
31
- if (Array.isArray(n) && typeof t == "function")
32
- return w(n, t, r);
33
- throw new Error("Invalid arguments passed to asyncMap");
34
- }
35
- }
36
- async function w(n, t, { concurrent: r = 5 } = {}) {
37
- return new Promise((o, s) => {
38
- const c = [], u = [];
39
- let i = 0;
40
- function y(e) {
41
- if (c.length == n.length && u.length == 0)
42
- return m();
43
- if (e >= n.length)
44
- return;
45
- const f = n[e];
46
- u.push([
47
- e,
48
- t(f, e, n).then((a) => {
49
- c.push([e, a]), g(u, ([l]) => e == l).map((l) => u.splice(l, 1)), y(i);
50
- }, s)
51
- ]), i++;
52
- }
53
- function m() {
54
- const e = c.sort(([f], [a]) => f - a);
55
- o(e.map(([, f]) => f));
56
- }
57
- for (let e = 0; e < r; e++)
58
- y(i);
59
- });
60
- }
61
- export {
62
- I as asyncMap,
63
- h as pause,
64
- x as retry
65
- };
@@ -1,260 +0,0 @@
1
- var d = Object.defineProperty;
2
- var S = (r, e, t) => e in r ? d(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var u = (r, e, t) => (S(r, typeof e != "symbol" ? e + "" : e, t), t);
4
- const a = "@terrygonguet/utils/functional/result", l = "@terrygonguet/utils/functional/result/Success", w = "@terrygonguet/utils/functional/result/Failure";
5
- class f {
6
- constructor(e) {
7
- u(this, "value");
8
- Object.defineProperties(this, {
9
- $_kind: { value: a, enumerable: !1, writable: !1 },
10
- $_variant: { value: l, enumerable: !1, writable: !1 },
11
- value: { value: e, writable: !1 }
12
- });
13
- }
14
- isSuccess() {
15
- return !0;
16
- }
17
- isFailure() {
18
- return !1;
19
- }
20
- merge(e) {
21
- return e(this.value);
22
- }
23
- match(e) {
24
- return e(this.value);
25
- }
26
- map(e) {
27
- return new f(e(this.value));
28
- }
29
- flatMap(e) {
30
- return e(this.value);
31
- }
32
- toJSON() {
33
- return { $_kind: a, $_variant: l, value: this.value };
34
- }
35
- }
36
- class g {
37
- constructor(e) {
38
- u(this, "reason");
39
- Object.defineProperties(this, {
40
- $_kind: { value: a, enumerable: !1, writable: !1 },
41
- $_variant: { value: l, enumerable: !1, writable: !1 },
42
- reason: { value: e, writable: !1 }
43
- });
44
- }
45
- isSuccess() {
46
- return !1;
47
- }
48
- isFailure() {
49
- return !0;
50
- }
51
- merge(e, t) {
52
- return t(this.reason);
53
- }
54
- match(e, t) {
55
- return t(this.reason);
56
- }
57
- map() {
58
- return this;
59
- }
60
- flatMap() {
61
- return this;
62
- }
63
- toJSON() {
64
- return { $_kind: a, $_variant: w, reason: this.reason };
65
- }
66
- }
67
- function v(r, e) {
68
- return e ? r.toResult(e) : r.toResult();
69
- }
70
- const c = {
71
- Success(r) {
72
- return new f(r);
73
- },
74
- Failure(r) {
75
- return new g(r);
76
- },
77
- try(r) {
78
- return new y(r);
79
- },
80
- fromPromise(r, e, t) {
81
- return r.then(
82
- b(e, this.Success),
83
- b(t, this.Failure)
84
- );
85
- },
86
- fromMaybe: v,
87
- JSONReviver(r, e) {
88
- if ((e == null ? void 0 : e.$_kind) == a) {
89
- const t = e == null ? void 0 : e.$_variant;
90
- return t == l ? new f(e == null ? void 0 : e.value) : t == w ? new g(e == null ? void 0 : e.reason) : e;
91
- } else
92
- return e;
93
- }
94
- };
95
- class y {
96
- constructor(e) {
97
- u(this, "tryFn");
98
- u(this, "catchFn");
99
- this.tryFn = e, this.catchFn = p;
100
- }
101
- catch(e) {
102
- return this.catchFn = e, this;
103
- }
104
- exec(e) {
105
- try {
106
- const t = c.Success(this.tryFn());
107
- return e == null || e(t), t;
108
- } catch (t) {
109
- const n = c.Failure(this.catchFn(t));
110
- return e == null || e(n), n;
111
- }
112
- }
113
- }
114
- const h = "@terrygonguet/utils/functional/maybe", _ = "@terrygonguet/utils/functional/maybe/Some", $ = "@terrygonguet/utils/functional/maybe/None";
115
- class o {
116
- constructor(e) {
117
- u(this, "value");
118
- Object.defineProperties(this, {
119
- $_kind: { value: h, enumerable: !1, writable: !1 },
120
- $_variant: { value: _, enumerable: !1, writable: !1 },
121
- value: { value: e, writable: !1 }
122
- });
123
- }
124
- isSome() {
125
- return !0;
126
- }
127
- isNone() {
128
- return !1;
129
- }
130
- orDefault() {
131
- return this.value;
132
- }
133
- map(e) {
134
- return new o(e(this.value));
135
- }
136
- flatMap(e) {
137
- return e(this.value);
138
- }
139
- toResult() {
140
- return c.Success(this.value);
141
- }
142
- toJSON() {
143
- return { $_kind: h, $_variant: _, value: this.value };
144
- }
145
- }
146
- class k {
147
- constructor() {
148
- }
149
- isSome() {
150
- return !1;
151
- }
152
- isNone() {
153
- return !0;
154
- }
155
- orDefault(e) {
156
- return e;
157
- }
158
- map() {
159
- return this;
160
- }
161
- flatMap() {
162
- return this;
163
- }
164
- toResult(e) {
165
- return e ? c.Failure(e == null ? void 0 : e()) : c.Failure(void 0);
166
- }
167
- toJSON() {
168
- return { $_kind: h, $_variant: $ };
169
- }
170
- }
171
- const N = new k(), s = {
172
- Some(r) {
173
- return new o(r);
174
- },
175
- None() {
176
- return N;
177
- },
178
- from(r) {
179
- switch (r) {
180
- case null:
181
- case void 0:
182
- return this.None();
183
- default:
184
- return new o(r);
185
- }
186
- },
187
- /**
188
- * CAUTION: this method swallows errors and simply returns None!
189
- * Use `Result.fromPromise()` if you need error details.
190
- */
191
- fromPromise(r, e = p) {
192
- return r.then(b(e, s.from), M(this.None()));
193
- },
194
- JSONReviver(r, e) {
195
- if ((e == null ? void 0 : e.$_kind) == h) {
196
- const t = e == null ? void 0 : e.$_variant;
197
- return t == _ ? new o(e == null ? void 0 : e.value) : t == $ ? s.None() : e;
198
- } else
199
- return e;
200
- }
201
- };
202
- var b = F;
203
- function F(r, e) {
204
- if (!arguments.length)
205
- throw new Error(
206
- "expected at least one (and probably more) function arguments"
207
- );
208
- var t = arguments;
209
- return function() {
210
- for (var n = t[0].apply(this, arguments), i = t.length, m = 1; m < i; m++)
211
- n = t[m].call(this, n);
212
- return n;
213
- };
214
- }
215
- var J = x;
216
- function x(r, ...e) {
217
- if (!arguments.length)
218
- throw new Error("expected one value argument and least one function argument");
219
- if (!e.length)
220
- throw new Error(
221
- "expected at least one (and probably more) function arguments"
222
- );
223
- for (var t = e[0](r), n = e.length, i = 1; i < n; i++)
224
- t = e[i](t);
225
- return t;
226
- }
227
- function p(r) {
228
- return r;
229
- }
230
- function M(r) {
231
- return () => r;
232
- }
233
- function j(r, e) {
234
- return typeof r == "number" ? (t) => s.from(t.at(r)) : s.from(r.at(e));
235
- }
236
- function E(r, e) {
237
- return typeof r == "function" ? (t) => s.from(t.find(r)) : s.from(r.find(e));
238
- }
239
- function O(r, e) {
240
- if (typeof r == "function")
241
- return (t) => {
242
- const n = t.findIndex(r);
243
- return n == -1 ? s.None() : s.Some(n);
244
- };
245
- {
246
- const t = r.findIndex(e);
247
- return t == -1 ? s.None() : s.Some(t);
248
- }
249
- }
250
- export {
251
- s as Maybe,
252
- c as Result,
253
- j as at,
254
- b as compose,
255
- M as constant,
256
- E as find,
257
- O as findIndex,
258
- p as identity,
259
- J as pipe
260
- };
package/dist/index.js DELETED
@@ -1,55 +0,0 @@
1
- import { constant as r } from "./functional.js";
2
- function s(n, t, e) {
3
- return Math.min(Math.max(n, t), e);
4
- }
5
- function a(n, t, e) {
6
- try {
7
- return JSON.parse(n, e);
8
- } catch {
9
- return t;
10
- }
11
- }
12
- function i(...n) {
13
- return function(t, e) {
14
- for (const o of n)
15
- e = o(t, e);
16
- return e;
17
- };
18
- }
19
- function u() {
20
- const n = () => o, t = r(!1), e = r(!0), o = new Proxy(() => {
21
- }, {
22
- get: n,
23
- set: n,
24
- apply: n,
25
- construct: n,
26
- deleteProperty: e,
27
- has: e,
28
- preventExtensions: t,
29
- defineProperty: t
30
- });
31
- return o;
32
- }
33
- function f() {
34
- }
35
- function p(n) {
36
- throw new Error("This should never be called");
37
- }
38
- async function h(n) {
39
- const e = new TextEncoder().encode(n);
40
- return await crypto.subtle.digest("SHA-1", e);
41
- }
42
- function* d(n, t, e = 1) {
43
- for (let o = n; o < t; o += e)
44
- yield o;
45
- }
46
- export {
47
- s as clamp,
48
- i as composeJSONRevivers,
49
- u as createNoopProxy,
50
- p as exhaustive,
51
- h as hash,
52
- f as noop,
53
- d as range,
54
- a as safeParse
55
- };