react-state-monad 1.0.24 → 1.0.25
Sign up to get free protection for your applications and to get access to all the features.
- package/.github/workflows/publish.yml +72 -72
- package/LICENSE +673 -673
- package/README.md +274 -274
- package/package.json +1 -1
- package/src/hooks/types.ts +12 -12
- package/src/hooks/useElementState.ts +25 -25
- package/src/hooks/useEmptyState.ts +12 -12
- package/src/hooks/useFieldState.ts +54 -53
- package/src/hooks/useNullSafety.ts +23 -23
- package/src/hooks/useRemapArray.ts +50 -50
- package/src/hooks/useStateObject.ts +14 -14
- package/src/implementations/emptyState.ts +42 -42
- package/src/implementations/validState.ts +59 -59
- package/src/index.ts +10 -10
- package/src/stateObject.ts +70 -70
- package/tsconfig.json +15 -15
package/src/stateObject.ts
CHANGED
@@ -1,71 +1,71 @@
|
|
1
|
-
/**
|
2
|
-
* Represents a state object that holds a value of type T, allowing various state operations.
|
3
|
-
* This is the main interface for managing state, with operations like `map`, `filter`, and `flatMap`.
|
4
|
-
* initialize with useStateObject<T>(initialState: T) hook
|
5
|
-
* @template T - The type of the value stored in the state object.
|
6
|
-
*/
|
7
|
-
export type StateObject<T> = {
|
8
|
-
/**
|
9
|
-
* The current value of the state.
|
10
|
-
*/
|
11
|
-
get value(): T;
|
12
|
-
|
13
|
-
/**
|
14
|
-
* Returns true if the state has a valid value, false otherwise.
|
15
|
-
*/
|
16
|
-
get hasValue(): boolean;
|
17
|
-
|
18
|
-
/**
|
19
|
-
* Performs an action on the current state value.
|
20
|
-
*
|
21
|
-
* @param action - A function that accepts the current state value and performs some operation.
|
22
|
-
*/
|
23
|
-
do(action: (t: T) => void): void;
|
24
|
-
|
25
|
-
/**
|
26
|
-
* Sets a new value for the state.
|
27
|
-
*
|
28
|
-
* @param newState - The new state value to set.
|
29
|
-
*/
|
30
|
-
set value(newState: T);
|
31
|
-
|
32
|
-
/**
|
33
|
-
* Transforms the current state into another state object by applying a mapping function.
|
34
|
-
*
|
35
|
-
* @template U - The type of the new state value.
|
36
|
-
* @param mappingFunction - A function that transforms the current state value into a new value.
|
37
|
-
* @param inverseMappingFunction - A function that transforms a new value back to the original state type.
|
38
|
-
* @returns A new StateObject with the transformed value.
|
39
|
-
*/
|
40
|
-
map<U>(
|
41
|
-
mappingFunction: (t: T) => U,
|
42
|
-
inverseMappingFunction: (u: U, t: T) => T
|
43
|
-
): StateObject<U>;
|
44
|
-
|
45
|
-
/**
|
46
|
-
* Filters the state based on a predicate, returning an empty state if the predicate is not satisfied.
|
47
|
-
*
|
48
|
-
* @param predicate - A function that tests the current state value.
|
49
|
-
* @returns A new StateObject with the original value or an empty state.
|
50
|
-
*/
|
51
|
-
filter(predicate: (t: T) => boolean): StateObject<T>;
|
52
|
-
|
53
|
-
/**
|
54
|
-
* Returns the current state value if it exists; otherwise, returns the provided alternative value.
|
55
|
-
*
|
56
|
-
* @param orElse - The value to return if the state does not have a valid value.
|
57
|
-
* @returns The current state value or the provided fallback value.
|
58
|
-
*/
|
59
|
-
orElse(orElse: T): T;
|
60
|
-
|
61
|
-
/**
|
62
|
-
* Transforms the current state into another state object by applying a mapping function that returns a new state.
|
63
|
-
*
|
64
|
-
* @template U - The type of the new state value.
|
65
|
-
* @param mappingFunction - A function that transforms the current state value into another state object.
|
66
|
-
* @returns A new StateObject based on the result of the mapping function.
|
67
|
-
*/
|
68
|
-
flatMap<U>(
|
69
|
-
mappingFunction: (t: T) => StateObject<U>
|
70
|
-
): StateObject<U>;
|
1
|
+
/**
|
2
|
+
* Represents a state object that holds a value of type T, allowing various state operations.
|
3
|
+
* This is the main interface for managing state, with operations like `map`, `filter`, and `flatMap`.
|
4
|
+
* initialize with useStateObject<T>(initialState: T) hook
|
5
|
+
* @template T - The type of the value stored in the state object.
|
6
|
+
*/
|
7
|
+
export type StateObject<T> = {
|
8
|
+
/**
|
9
|
+
* The current value of the state.
|
10
|
+
*/
|
11
|
+
get value(): T;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Returns true if the state has a valid value, false otherwise.
|
15
|
+
*/
|
16
|
+
get hasValue(): boolean;
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Performs an action on the current state value.
|
20
|
+
*
|
21
|
+
* @param action - A function that accepts the current state value and performs some operation.
|
22
|
+
*/
|
23
|
+
do(action: (t: T) => void): void;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Sets a new value for the state.
|
27
|
+
*
|
28
|
+
* @param newState - The new state value to set.
|
29
|
+
*/
|
30
|
+
set value(newState: T);
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Transforms the current state into another state object by applying a mapping function.
|
34
|
+
*
|
35
|
+
* @template U - The type of the new state value.
|
36
|
+
* @param mappingFunction - A function that transforms the current state value into a new value.
|
37
|
+
* @param inverseMappingFunction - A function that transforms a new value back to the original state type.
|
38
|
+
* @returns A new StateObject with the transformed value.
|
39
|
+
*/
|
40
|
+
map<U>(
|
41
|
+
mappingFunction: (t: T) => U,
|
42
|
+
inverseMappingFunction: (u: U, t: T) => T
|
43
|
+
): StateObject<U>;
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Filters the state based on a predicate, returning an empty state if the predicate is not satisfied.
|
47
|
+
*
|
48
|
+
* @param predicate - A function that tests the current state value.
|
49
|
+
* @returns A new StateObject with the original value or an empty state.
|
50
|
+
*/
|
51
|
+
filter(predicate: (t: T) => boolean): StateObject<T>;
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Returns the current state value if it exists; otherwise, returns the provided alternative value.
|
55
|
+
*
|
56
|
+
* @param orElse - The value to return if the state does not have a valid value.
|
57
|
+
* @returns The current state value or the provided fallback value.
|
58
|
+
*/
|
59
|
+
orElse(orElse: T): T;
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Transforms the current state into another state object by applying a mapping function that returns a new state.
|
63
|
+
*
|
64
|
+
* @template U - The type of the new state value.
|
65
|
+
* @param mappingFunction - A function that transforms the current state value into another state object.
|
66
|
+
* @returns A new StateObject based on the result of the mapping function.
|
67
|
+
*/
|
68
|
+
flatMap<U>(
|
69
|
+
mappingFunction: (t: T) => StateObject<U>
|
70
|
+
): StateObject<U>;
|
71
71
|
}
|
package/tsconfig.json
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
{
|
2
|
-
"compilerOptions": {
|
3
|
-
"module": "ESNext",
|
4
|
-
"target": "ESNext",
|
5
|
-
"declaration": true,
|
6
|
-
"declarationDir": "./",
|
7
|
-
"outDir": "dist",
|
8
|
-
"moduleResolution": "Node",
|
9
|
-
"esModuleInterop": true,
|
10
|
-
"strict": true
|
11
|
-
},
|
12
|
-
"include": [
|
13
|
-
"src/**/*",
|
14
|
-
"index.ts"
|
15
|
-
]
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"module": "ESNext",
|
4
|
+
"target": "ESNext",
|
5
|
+
"declaration": true,
|
6
|
+
"declarationDir": "./",
|
7
|
+
"outDir": "dist",
|
8
|
+
"moduleResolution": "Node",
|
9
|
+
"esModuleInterop": true,
|
10
|
+
"strict": true
|
11
|
+
},
|
12
|
+
"include": [
|
13
|
+
"src/**/*",
|
14
|
+
"index.ts"
|
15
|
+
]
|
16
16
|
}
|