@kaumlaut/pure 0.4.0 → 0.5.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/README.md +1 -3
- package/dist/clone/index.d.ts +10 -0
- package/dist/clone/index.d.ts.map +1 -1
- package/dist/clone/index.js +7 -0
- package/dist/fetch-state/index.d.ts +48 -0
- package/dist/fetch-state/index.d.ts.map +1 -1
- package/dist/fetch-state/index.js +29 -0
- package/dist/guard/index.d.ts +79 -0
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +86 -0
- package/dist/maybe/index.d.ts +53 -1
- package/dist/maybe/index.d.ts.map +1 -1
- package/dist/maybe/index.js +71 -0
- package/dist/parse/index.d.ts +6 -0
- package/dist/parse/index.d.ts.map +1 -1
- package/dist/parse/index.js +6 -0
- package/dist/pipe/index.d.ts +15 -2
- package/dist/pipe/index.d.ts.map +1 -1
- package/dist/pipe/index.js +11 -0
- package/dist/result/index.d.ts +40 -1
- package/dist/result/index.d.ts.map +1 -1
- package/dist/result/index.js +30 -0
- package/dist/runtime/effect/fetch/index.d.ts +3 -4
- package/dist/runtime/effect/fetch/index.d.ts.map +1 -1
- package/dist/util/index.d.ts +3 -0
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +3 -0
- package/docs/README.md +23 -0
- package/docs/clone.md +45 -0
- package/docs/fetch-state.md +345 -0
- package/docs/guard.md +729 -0
- package/docs/maybe.md +481 -0
- package/docs/parse.md +47 -0
- package/docs/pipe.md +95 -0
- package/docs/result.md +363 -0
- package/docs/runtime/effect/fetch.md +169 -0
- package/docs/runtime/effect/none.md +19 -0
- package/docs/runtime/effect.md +179 -0
- package/docs/runtime/persistence/none.md +29 -0
- package/docs/runtime/persistence/storage.md +39 -0
- package/docs/runtime/persistence.md +63 -0
- package/docs/runtime.md +167 -0
- package/docs/util.md +33 -0
- package/package.json +9 -7
package/README.md
CHANGED
package/dist/clone/index.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides value cloning capabilities
|
|
3
|
+
* @module clone
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Defines the Cloneable type
|
|
7
|
+
*/
|
|
1
8
|
export type Cloneable = string | number | boolean | null | Cloneable[] | {
|
|
2
9
|
[key: string]: Cloneable;
|
|
3
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Recursively clones values to prevent changing of the original value.
|
|
13
|
+
*/
|
|
4
14
|
export declare function clone<T extends Cloneable>(value: T): T;
|
|
5
15
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/clone/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,EAAE,GACX;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEjC,wBAAgB,KAAK,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAkBtD"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/clone/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,EAAE,GACX;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEjC;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAkBtD"}
|
package/dist/clone/index.js
CHANGED
|
@@ -1,26 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides types and functions to represent fetch request states
|
|
3
|
+
* @module fetch-state
|
|
4
|
+
*/
|
|
1
5
|
import type { Guard } from "@kaumlaut/pure/guard";
|
|
6
|
+
/**
|
|
7
|
+
* Represents a failed fetch request
|
|
8
|
+
*/
|
|
2
9
|
export type Failed = {
|
|
3
10
|
error: Readonly<string>;
|
|
4
11
|
type: "Failed";
|
|
5
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Represents a fetch request that has not been executed
|
|
15
|
+
*/
|
|
6
16
|
export type None = {
|
|
7
17
|
type: "None";
|
|
8
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* Represents a fetch request that is currently still running
|
|
21
|
+
*/
|
|
9
22
|
export type Loading = {
|
|
10
23
|
type: "Loading";
|
|
11
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Represents a fetch request that succeeded
|
|
27
|
+
*/
|
|
12
28
|
export type Success<T> = {
|
|
13
29
|
data: Readonly<T>;
|
|
14
30
|
type: "Success";
|
|
15
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* Represents all possible states of a fetch request
|
|
34
|
+
*/
|
|
16
35
|
export type FetchState<T> = Failed | None | Loading | Success<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether or not the fetch state is Loading via a type guard
|
|
38
|
+
*/
|
|
17
39
|
export declare function isLoading<T>(state: FetchState<T>): state is Loading;
|
|
40
|
+
/**
|
|
41
|
+
* Checks whether or not the fetch state is Failed via a type guard
|
|
42
|
+
*/
|
|
18
43
|
export declare function isFailed<T>(state: FetchState<T>): state is Failed;
|
|
44
|
+
/**
|
|
45
|
+
* Checks whether or not the fetch state is None via a type guard
|
|
46
|
+
*/
|
|
19
47
|
export declare function isNone<T>(state: FetchState<T>): state is None;
|
|
48
|
+
/**
|
|
49
|
+
* Checks whether or not the fetch state is Success via a type guard
|
|
50
|
+
*/
|
|
20
51
|
export declare function isSuccess<T>(state: FetchState<T>): state is Success<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a fetch state of type Failed
|
|
54
|
+
*/
|
|
21
55
|
export declare function fail(error: string): Failed;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a fetch state of type Loading
|
|
58
|
+
*/
|
|
22
59
|
export declare function load(): Loading;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a fetch state of type None
|
|
62
|
+
*/
|
|
23
63
|
export declare function none(): None;
|
|
64
|
+
/**
|
|
65
|
+
* Attempts to create a fetch state of type Success if the given guard passes.
|
|
66
|
+
* Otherwise creates a fetch state of type Failed with the provided error.
|
|
67
|
+
*/
|
|
24
68
|
export declare function attempt<T>(guard: Guard<T>, error?: string): (data: unknown) => Success<T> | Failed;
|
|
69
|
+
/**
|
|
70
|
+
* A Utility function that allows to map the Failed fetch state to any other fetch state.
|
|
71
|
+
* The mapper function is only called if the given fetch state is Failed.
|
|
72
|
+
*/
|
|
25
73
|
export declare function mapFailed<T>(mapper: (state: Failed) => FetchState<T>): (state: FetchState<T>) => FetchState<T>;
|
|
26
74
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch-state/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACxB,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,IAAI,GAAG;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI;IACvB,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAClB,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch-state/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAA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AACH,wBAAgB,IAAI,IAAI,IAAI,CAI3B;AACD;;;GAGG;AACH,wBAAgB,OAAO,CAAC,CAAC,EACvB,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,KAAK,GAAE,MAA6E,GACnF,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAcxC;AACD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,GACvC,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAQzC"}
|
|
@@ -1,31 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether or not the fetch state is Loading via a type guard
|
|
3
|
+
*/
|
|
1
4
|
export function isLoading(state) {
|
|
2
5
|
return state.type === "Loading";
|
|
3
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Checks whether or not the fetch state is Failed via a type guard
|
|
9
|
+
*/
|
|
4
10
|
export function isFailed(state) {
|
|
5
11
|
return state.type === "Failed";
|
|
6
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Checks whether or not the fetch state is None via a type guard
|
|
15
|
+
*/
|
|
7
16
|
export function isNone(state) {
|
|
8
17
|
return state.type === "None";
|
|
9
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Checks whether or not the fetch state is Success via a type guard
|
|
21
|
+
*/
|
|
10
22
|
export function isSuccess(state) {
|
|
11
23
|
return state.type === "Success";
|
|
12
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a fetch state of type Failed
|
|
27
|
+
*/
|
|
13
28
|
export function fail(error) {
|
|
14
29
|
return {
|
|
15
30
|
type: "Failed",
|
|
16
31
|
error,
|
|
17
32
|
};
|
|
18
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates a fetch state of type Loading
|
|
36
|
+
*/
|
|
19
37
|
export function load() {
|
|
20
38
|
return {
|
|
21
39
|
type: "Loading",
|
|
22
40
|
};
|
|
23
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a fetch state of type None
|
|
44
|
+
*/
|
|
24
45
|
export function none() {
|
|
25
46
|
return {
|
|
26
47
|
type: "None",
|
|
27
48
|
};
|
|
28
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Attempts to create a fetch state of type Success if the given guard passes.
|
|
52
|
+
* Otherwise creates a fetch state of type Failed with the provided error.
|
|
53
|
+
*/
|
|
29
54
|
export function attempt(guard, error = "Guard did not pass. Ensure the attempted data has the correct type") {
|
|
30
55
|
return (data) => {
|
|
31
56
|
if (guard(data)) {
|
|
@@ -40,6 +65,10 @@ export function attempt(guard, error = "Guard did not pass. Ensure the attempted
|
|
|
40
65
|
};
|
|
41
66
|
};
|
|
42
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* A Utility function that allows to map the Failed fetch state to any other fetch state.
|
|
70
|
+
* The mapper function is only called if the given fetch state is Failed.
|
|
71
|
+
*/
|
|
43
72
|
export function mapFailed(mapper) {
|
|
44
73
|
return (state) => {
|
|
45
74
|
if (isFailed(state)) {
|
package/dist/guard/index.d.ts
CHANGED
|
@@ -1,26 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a type guard
|
|
3
|
+
*/
|
|
1
4
|
export type Guard<T> = (value: unknown) => value is T;
|
|
5
|
+
/**
|
|
6
|
+
* Confirms that the given value passes all guards.
|
|
7
|
+
*/
|
|
2
8
|
export declare function isAll<T>(guards: Guard<T>[]): (value: unknown) => value is T;
|
|
9
|
+
/**
|
|
10
|
+
* Confirms that the value is a string.
|
|
11
|
+
*/
|
|
3
12
|
export declare function isString(value: unknown): value is string;
|
|
13
|
+
/**
|
|
14
|
+
* Confirms that the value is a string with specified length.
|
|
15
|
+
*/
|
|
4
16
|
export declare function isStringOfLength(length: number): (value: unknown) => value is string;
|
|
17
|
+
/**
|
|
18
|
+
* Confirms that the value is a number.
|
|
19
|
+
*/
|
|
5
20
|
export declare function isNumber(value: unknown): value is number;
|
|
21
|
+
/**
|
|
22
|
+
* Confirms that the value is an integer.
|
|
23
|
+
*/
|
|
6
24
|
export declare function isInt(value: unknown): value is number;
|
|
25
|
+
/**
|
|
26
|
+
* Confirms that the value is a floating point number.
|
|
27
|
+
*/
|
|
7
28
|
export declare function isFloat(value: unknown): value is number;
|
|
29
|
+
/**
|
|
30
|
+
* Confirms that the value is an object containing the specified key.
|
|
31
|
+
*/
|
|
8
32
|
export declare function isObjectWithKey<T extends object>(key: keyof T): (value: unknown) => value is T;
|
|
33
|
+
/**
|
|
34
|
+
* Confirms that the value is an object containing the specified keys.
|
|
35
|
+
*/
|
|
9
36
|
export declare function isObjectWithKeys<T extends object>(keys: (keyof T)[]): (value: unknown) => value is T;
|
|
37
|
+
/**
|
|
38
|
+
* Confirms that the value is an object whose key value pairs match the corresponding type guards.
|
|
39
|
+
* Calls console.debug with an error message to improve debugging when a large type does not match.
|
|
40
|
+
*/
|
|
10
41
|
export declare function isObjectWithKeysMatchingGuard<T extends object>(guards: {
|
|
11
42
|
[K in keyof T]: Guard<T[K]>;
|
|
12
43
|
}): (value: unknown) => value is T;
|
|
44
|
+
/**
|
|
45
|
+
* Confirms the value exactly matched the given string.
|
|
46
|
+
*/
|
|
13
47
|
export declare function isExactString<T extends string>(expectedString: string): (value: unknown) => value is T;
|
|
48
|
+
/**
|
|
49
|
+
* Confirms the value is one of the given valid values.
|
|
50
|
+
*/
|
|
14
51
|
export declare function isOneStringOf<T extends string>(validValues: string[]): (value: unknown) => value is T;
|
|
52
|
+
/**
|
|
53
|
+
* Always passes.
|
|
54
|
+
*/
|
|
15
55
|
export declare function isAlways<T>(value: unknown): value is T;
|
|
56
|
+
/**
|
|
57
|
+
* Never passes.
|
|
58
|
+
*/
|
|
16
59
|
export declare function isNever<T>(value: unknown): value is T;
|
|
60
|
+
/**
|
|
61
|
+
* Confirms the value is a boolean.
|
|
62
|
+
*/
|
|
17
63
|
export declare function isBool(value: unknown): value is boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Confirms the value is null.
|
|
66
|
+
*/
|
|
18
67
|
export declare function isNull(value: unknown): value is null;
|
|
68
|
+
/**
|
|
69
|
+
* Confirms the value is a list of items that all pass the given guard.
|
|
70
|
+
* Calls console.debug with an error message to improve debugging when a large type does not match.
|
|
71
|
+
*/
|
|
19
72
|
export declare function isListOf<T>(guard: Guard<T>): (value: unknown) => value is T[];
|
|
73
|
+
/**
|
|
74
|
+
* Confirms the value passes at least one of the given Guards.
|
|
75
|
+
*/
|
|
20
76
|
export declare function isOneOf<T1, T2>(a: Guard<T1>, b: Guard<T2>): (value: unknown) => value is T1 | T2;
|
|
77
|
+
/**
|
|
78
|
+
* Confirms the value is either null or passes the given Guard.
|
|
79
|
+
*/
|
|
21
80
|
export declare function isNullOr<T>(guard: Guard<T>): (value: unknown) => value is T | null;
|
|
81
|
+
/**
|
|
82
|
+
* Confirms the value is an object where every value matches the given guard.
|
|
83
|
+
* Calls console.debug with an error message to improve debugging when a large type does not match.
|
|
84
|
+
*/
|
|
22
85
|
export declare function isObjectWithAllKeysMatchingGuard<B, T extends {
|
|
23
86
|
[key: string]: B;
|
|
24
87
|
}>(guard: Guard<B>): (value: unknown) => value is T;
|
|
88
|
+
/**
|
|
89
|
+
* Confirms the value is a string and matches the given regular expression.
|
|
90
|
+
*/
|
|
25
91
|
export declare function isStringWithPattern(pattern: RegExp): Guard<string>;
|
|
92
|
+
/**
|
|
93
|
+
* Confirms the value is undefined.
|
|
94
|
+
*/
|
|
95
|
+
export declare function isUndefined(value: unknown): value is undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Confirms the value is a list with atleast one item and all items match the given guard.
|
|
98
|
+
*/
|
|
99
|
+
export declare function isNonEmptyListOf<T>(guard: Guard<T>): (value: unknown) => value is T;
|
|
100
|
+
/**
|
|
101
|
+
* Confirms the value is number between min and max inclusive.
|
|
102
|
+
* Meaning if the value equals min or max the guard passes.
|
|
103
|
+
*/
|
|
104
|
+
export declare function isNumberBetweenInclusive<T>(min: number, max: number): (value: unknown) => value is T;
|
|
26
105
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/guard/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/guard/index.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAAC;AAEtD;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAE3E;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,GACb,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,MAAM,CAGrC;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAMrD;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAMvD;AAMD;;GAEG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAC9C,GAAG,EAAE,MAAM,CAAC,GACX,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAGhC;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EA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C,EAAE,CAAC,EACZ,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC,GACX,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,EAAE,GAAG,EAAE,CAEtC;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GACd,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,GAAG,IAAI,CAEvC;AAED;;;GAGG;AACH,wBAAgB,gCAAgC,CAC9C,CAAC,EACD,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,CAAA;CAAE,EAC9B,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAYjD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAGlE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GACd,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAGhC;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EACxC,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,GACV,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAGhC"}
|
package/dist/guard/index.js
CHANGED
|
@@ -1,22 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides easily composable generic type guards
|
|
3
|
+
* @module guard
|
|
4
|
+
*/
|
|
1
5
|
import { asFloat, asInt } from "@kaumlaut/pure/parse";
|
|
2
6
|
import { isOk } from "@kaumlaut/pure/result";
|
|
7
|
+
/**
|
|
8
|
+
* Confirms that the given value passes all guards.
|
|
9
|
+
*/
|
|
3
10
|
export function isAll(guards) {
|
|
4
11
|
return (value) => guards.every((guard) => guard(value));
|
|
5
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Confirms that the value is a string.
|
|
15
|
+
*/
|
|
6
16
|
export function isString(value) {
|
|
7
17
|
return typeof value === "string";
|
|
8
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Confirms that the value is a string with specified length.
|
|
21
|
+
*/
|
|
9
22
|
export function isStringOfLength(length) {
|
|
10
23
|
return (value) => isString(value) && value.length === length;
|
|
11
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Confirms that the value is a number.
|
|
27
|
+
*/
|
|
12
28
|
export function isNumber(value) {
|
|
13
29
|
return typeof value === "number";
|
|
14
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Confirms that the value is an integer.
|
|
33
|
+
*/
|
|
15
34
|
export function isInt(value) {
|
|
16
35
|
return (isNumber(value) &&
|
|
17
36
|
!numberIncludesCommaSeparator(value) &&
|
|
18
37
|
isOk(asInt(value.toString())));
|
|
19
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Confirms that the value is a floating point number.
|
|
41
|
+
*/
|
|
20
42
|
export function isFloat(value) {
|
|
21
43
|
return (isNumber(value) &&
|
|
22
44
|
numberIncludesCommaSeparator(value) &&
|
|
@@ -25,12 +47,22 @@ export function isFloat(value) {
|
|
|
25
47
|
function numberIncludesCommaSeparator(value) {
|
|
26
48
|
return `${value}`.includes(".");
|
|
27
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Confirms that the value is an object containing the specified key.
|
|
52
|
+
*/
|
|
28
53
|
export function isObjectWithKey(key) {
|
|
29
54
|
return (value) => typeof value === "object" && key in value;
|
|
30
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Confirms that the value is an object containing the specified keys.
|
|
58
|
+
*/
|
|
31
59
|
export function isObjectWithKeys(keys) {
|
|
32
60
|
return (value) => typeof value === "object" && keys.every((key) => key in value);
|
|
33
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Confirms that the value is an object whose key value pairs match the corresponding type guards.
|
|
64
|
+
* Calls console.debug with an error message to improve debugging when a large type does not match.
|
|
65
|
+
*/
|
|
34
66
|
export function isObjectWithKeysMatchingGuard(guards) {
|
|
35
67
|
return (value) => typeof value === "object" &&
|
|
36
68
|
Object.keys(guards).filter((key) => {
|
|
@@ -41,24 +73,46 @@ export function isObjectWithKeysMatchingGuard(guards) {
|
|
|
41
73
|
return result;
|
|
42
74
|
}).length === Object.keys(guards).length;
|
|
43
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Confirms the value exactly matched the given string.
|
|
78
|
+
*/
|
|
44
79
|
export function isExactString(expectedString) {
|
|
45
80
|
return (value) => isString(value) && value === expectedString;
|
|
46
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Confirms the value is one of the given valid values.
|
|
84
|
+
*/
|
|
47
85
|
export function isOneStringOf(validValues) {
|
|
48
86
|
return (value) => isString(value) && validValues.includes(value);
|
|
49
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Always passes.
|
|
90
|
+
*/
|
|
50
91
|
export function isAlways(value) {
|
|
51
92
|
return true;
|
|
52
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Never passes.
|
|
96
|
+
*/
|
|
53
97
|
export function isNever(value) {
|
|
54
98
|
return false;
|
|
55
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Confirms the value is a boolean.
|
|
102
|
+
*/
|
|
56
103
|
export function isBool(value) {
|
|
57
104
|
return typeof value === "boolean";
|
|
58
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Confirms the value is null.
|
|
108
|
+
*/
|
|
59
109
|
export function isNull(value) {
|
|
60
110
|
return value === null;
|
|
61
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Confirms the value is a list of items that all pass the given guard.
|
|
114
|
+
* Calls console.debug with an error message to improve debugging when a large type does not match.
|
|
115
|
+
*/
|
|
62
116
|
export function isListOf(guard) {
|
|
63
117
|
return (value) => Array.isArray(value) &&
|
|
64
118
|
value.filter((item, index) => {
|
|
@@ -69,12 +123,22 @@ export function isListOf(guard) {
|
|
|
69
123
|
return result;
|
|
70
124
|
}).length === value.length;
|
|
71
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Confirms the value passes at least one of the given Guards.
|
|
128
|
+
*/
|
|
72
129
|
export function isOneOf(a, b) {
|
|
73
130
|
return (value) => a(value) || b(value);
|
|
74
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Confirms the value is either null or passes the given Guard.
|
|
134
|
+
*/
|
|
75
135
|
export function isNullOr(guard) {
|
|
76
136
|
return isOneOf(isNull, guard);
|
|
77
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Confirms the value is an object where every value matches the given guard.
|
|
140
|
+
* Calls console.debug with an error message to improve debugging when a large type does not match.
|
|
141
|
+
*/
|
|
78
142
|
export function isObjectWithAllKeysMatchingGuard(guard) {
|
|
79
143
|
return (value) => typeof value === "object" &&
|
|
80
144
|
Object.keys(value).filter((key) => {
|
|
@@ -85,6 +149,28 @@ export function isObjectWithAllKeysMatchingGuard(guard) {
|
|
|
85
149
|
return result;
|
|
86
150
|
}).length === Object.keys(value).length;
|
|
87
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Confirms the value is a string and matches the given regular expression.
|
|
154
|
+
*/
|
|
88
155
|
export function isStringWithPattern(pattern) {
|
|
89
156
|
return (value) => isString(value) && pattern.test(value);
|
|
90
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Confirms the value is undefined.
|
|
160
|
+
*/
|
|
161
|
+
export function isUndefined(value) {
|
|
162
|
+
return value === undefined;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Confirms the value is a list with atleast one item and all items match the given guard.
|
|
166
|
+
*/
|
|
167
|
+
export function isNonEmptyListOf(guard) {
|
|
168
|
+
return (value) => isListOf(guard)(value) && value.length > 0;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Confirms the value is number between min and max inclusive.
|
|
172
|
+
* Meaning if the value equals min or max the guard passes.
|
|
173
|
+
*/
|
|
174
|
+
export function isNumberBetweenInclusive(min, max) {
|
|
175
|
+
return (value) => isNumber(value) && value >= min && value <= max;
|
|
176
|
+
}
|
package/dist/maybe/index.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides types representing the Maybe concept as well as functions to work with it.
|
|
3
|
+
* @module maybe
|
|
4
|
+
*/
|
|
1
5
|
import { Result } from "@kaumlaut/pure/result";
|
|
6
|
+
import { Guard } from "../guard";
|
|
2
7
|
/**
|
|
8
|
+
* Represents a Maybe containing a value.
|
|
3
9
|
* @example
|
|
4
10
|
* ```ts
|
|
5
11
|
* if (isJust(maybe)) {
|
|
@@ -13,7 +19,7 @@ export type Just<T> = {
|
|
|
13
19
|
value: T;
|
|
14
20
|
};
|
|
15
21
|
/**
|
|
16
|
-
*
|
|
22
|
+
* Represents a Maybe not containing a value.
|
|
17
23
|
* @example
|
|
18
24
|
if (isNothing(maybe)) {
|
|
19
25
|
// no value property exists on maybe
|
|
@@ -23,14 +29,60 @@ export type Just<T> = {
|
|
|
23
29
|
export type Nothing = {
|
|
24
30
|
type: "maybe-nothing";
|
|
25
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* Represents the Maybe type.
|
|
34
|
+
*/
|
|
26
35
|
export type Maybe<T> = Just<T> | Nothing;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a maybe not containing a value. (Nothing)
|
|
38
|
+
*/
|
|
27
39
|
export declare function nothing(): Nothing;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a maybe containing a value. (Just)
|
|
42
|
+
*/
|
|
28
43
|
export declare function just<T>(value: T): Just<T>;
|
|
44
|
+
/**
|
|
45
|
+
* A Guard confirming that the given maybe value is a Nothing.
|
|
46
|
+
*/
|
|
29
47
|
export declare function isNothing<T>(maybe: Maybe<T>): maybe is Nothing;
|
|
48
|
+
/**
|
|
49
|
+
* A Guard confirming that the given maybe value is a Just.
|
|
50
|
+
*/
|
|
30
51
|
export declare function isJust<T>(maybe: Maybe<T>): maybe is Just<T>;
|
|
52
|
+
/**
|
|
53
|
+
* A Guard confirming that the given value is a Maybe.
|
|
54
|
+
*/
|
|
31
55
|
export declare function isMaybe<T>(value: unknown): value is Maybe<T>;
|
|
56
|
+
/**
|
|
57
|
+
* Applies the func function to the value if the given Maybe is a Just and returns it wrapped in a Just. Otherwise returns the given Maybe.
|
|
58
|
+
*/
|
|
32
59
|
export declare function map<T, R>(func: (value: T) => R): (maybe: Maybe<T>) => Maybe<R>;
|
|
60
|
+
/**
|
|
61
|
+
* Converts the given Maybe into a Nothing if it is Just but does not pass the given function.
|
|
62
|
+
*/
|
|
33
63
|
export declare function filter<T>(func: (value: T) => boolean): (maybe: Maybe<T>) => Maybe<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Unwraps a Maybe, returning the value if it is a Just or the default value if it is a Nothing.
|
|
66
|
+
*/
|
|
34
67
|
export declare function withDefault<T>(defaultValue: T): (maybe: Maybe<T>) => T;
|
|
68
|
+
/**
|
|
69
|
+
* Converts the given Maybe to a Result.
|
|
70
|
+
* A Nothing becomes an Err with the given error.
|
|
71
|
+
* A Just becomes an Ok with the contained value.
|
|
72
|
+
*/
|
|
35
73
|
export declare function toResult<T, E>(error: E): (maybe: Maybe<T>) => Result<T, E>;
|
|
74
|
+
/**
|
|
75
|
+
* Creates a Just if the Guard passes for the given value. Otherwise Creates a Nothing.
|
|
76
|
+
*/
|
|
77
|
+
export declare function maybeByGuard<T>(guard: Guard<T>): (value: unknown) => Maybe<T>;
|
|
78
|
+
/**
|
|
79
|
+
* Maps the a value contained within a Just using the given function without wrapping it in another Just.
|
|
80
|
+
* Returns the given Maybe if it is a Nothing.
|
|
81
|
+
*/
|
|
82
|
+
export declare function mapToMaybe<T, R>(func: (value: T) => Maybe<R>): (maybe: Maybe<T>) => Maybe<R>;
|
|
83
|
+
/**
|
|
84
|
+
* Maps the a value contained within a Just using the given function without wrapping it in another Just.
|
|
85
|
+
* Returns the given Maybe if it is a Nothing.
|
|
86
|
+
*/
|
|
87
|
+
export declare function tryMap<T, R>(func: (value: T) => R): (maybe: Maybe<T>) => Maybe<R>;
|
|
36
88
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/maybe/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,MAAM,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/maybe/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAW,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC;;;;;;;;;GASG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI;IACpB,IAAI,EAAE,YAAY,CAAC;IACnB,KAAK,EAAE,CAAC,CAAC;CACV,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,eAAe,CAAC;CACvB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;AAEzC;;GAEG;AACH,wBAAgB,OAAO,IAAI,OAAO,CAIjC;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAKzC;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,OAAO,CAE9D;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,C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