react-state-monad 1.0.15 → 1.0.17
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -0
- package/dist/index.cjs +10 -0
- package/dist/index.d.cts +13 -1
- package/dist/index.d.ts +13 -1
- package/dist/index.js +9 -0
- package/package.json +1 -1
- package/src/hooks/useNullSafety.ts +24 -0
- package/src/index.ts +1 -0
package/README.md
CHANGED
@@ -134,6 +134,22 @@ Parameters:
|
|
134
134
|
Returns an array of new `StateObject`s, each representing an element in the original array. This allows individual
|
135
135
|
updates while keeping the array state synchronized. If the state has no value, it returns an empty array.
|
136
136
|
|
137
|
+
|
138
|
+
### `useNullSafety<TOrigin>`
|
139
|
+
|
140
|
+
This hook ensures a `StateObject` contains a defined, non-null value. If the `StateObject`'s value is `undefined` or `null`, it returns an `EmptyState`. Otherwise, it returns a `ValidState` with the value and a setter to update the value.
|
141
|
+
|
142
|
+
Parameters:
|
143
|
+
|
144
|
+
- `state`: The `StateObject` which may contain a value, `undefined`, or `null`.
|
145
|
+
|
146
|
+
Returns a `StateObject` of type `TOrigin` representing the value if it is defined and non-null, otherwise an `EmptyState`.
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
137
153
|
### Complete Example
|
138
154
|
|
139
155
|
Here's a more complete example demonstrating the usage of `useStateObject`, `useFieldState`, `useElementState`,
|
package/dist/index.cjs
CHANGED
@@ -25,6 +25,7 @@ __export(index_exports, {
|
|
25
25
|
useElementState: () => useElementState,
|
26
26
|
useEmptyState: () => useEmptyState,
|
27
27
|
useFieldState: () => useFieldState,
|
28
|
+
useNullSafety: () => useNullSafety,
|
28
29
|
useRemapArray: () => useRemapArray,
|
29
30
|
useStateObject: () => useStateObject
|
30
31
|
});
|
@@ -156,6 +157,14 @@ function useArrayState(states) {
|
|
156
157
|
return useStateObject(states.filter((state) => state.hasValue).map((state) => state.value));
|
157
158
|
}
|
158
159
|
|
160
|
+
// src/hooks/useNullSafety.ts
|
161
|
+
function useNullSafety(state) {
|
162
|
+
if (!state.hasValue) return new EmptyState();
|
163
|
+
if (state.value === void 0) return new EmptyState();
|
164
|
+
if (state.value === null) return new EmptyState();
|
165
|
+
return new ValidState(state.value, (value) => state.value = value);
|
166
|
+
}
|
167
|
+
|
159
168
|
// src/index.ts
|
160
169
|
var index_default = void 0;
|
161
170
|
// Annotate the CommonJS export names for ESM import in node:
|
@@ -164,6 +173,7 @@ var index_default = void 0;
|
|
164
173
|
useElementState,
|
165
174
|
useEmptyState,
|
166
175
|
useFieldState,
|
176
|
+
useNullSafety,
|
167
177
|
useRemapArray,
|
168
178
|
useStateObject
|
169
179
|
});
|
package/dist/index.d.cts
CHANGED
@@ -127,6 +127,18 @@ declare function useArrayState<T>(states: StateObject<T>[]): StateObject<T[]>;
|
|
127
127
|
*/
|
128
128
|
declare function useStateObject<T>(initialState: T): StateObject<T>;
|
129
129
|
|
130
|
+
/**
|
131
|
+
* Hook that ensures a StateObject contains a defined, non-null value.
|
132
|
+
* If the StateObject's value is `undefined` or `null`, it returns an EmptyState.
|
133
|
+
* Otherwise, it returns a ValidState with the value and a setter to update the value.
|
134
|
+
*
|
135
|
+
* @template TOrigin - The type of the value contained in the StateObject.
|
136
|
+
* @param state - The StateObject which may contain a value, `undefined`, or `null`.
|
137
|
+
* @returns A new StateObject containing the value if it is defined and non-null,
|
138
|
+
* otherwise an EmptyState.
|
139
|
+
*/
|
140
|
+
declare function useNullSafety<TOrigin>(state: StateObject<TOrigin | undefined | null>): StateObject<TOrigin>;
|
141
|
+
|
130
142
|
declare const _default: undefined;
|
131
143
|
|
132
|
-
export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useRemapArray, useStateObject };
|
144
|
+
export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useNullSafety, useRemapArray, useStateObject };
|
package/dist/index.d.ts
CHANGED
@@ -127,6 +127,18 @@ declare function useArrayState<T>(states: StateObject<T>[]): StateObject<T[]>;
|
|
127
127
|
*/
|
128
128
|
declare function useStateObject<T>(initialState: T): StateObject<T>;
|
129
129
|
|
130
|
+
/**
|
131
|
+
* Hook that ensures a StateObject contains a defined, non-null value.
|
132
|
+
* If the StateObject's value is `undefined` or `null`, it returns an EmptyState.
|
133
|
+
* Otherwise, it returns a ValidState with the value and a setter to update the value.
|
134
|
+
*
|
135
|
+
* @template TOrigin - The type of the value contained in the StateObject.
|
136
|
+
* @param state - The StateObject which may contain a value, `undefined`, or `null`.
|
137
|
+
* @returns A new StateObject containing the value if it is defined and non-null,
|
138
|
+
* otherwise an EmptyState.
|
139
|
+
*/
|
140
|
+
declare function useNullSafety<TOrigin>(state: StateObject<TOrigin | undefined | null>): StateObject<TOrigin>;
|
141
|
+
|
130
142
|
declare const _default: undefined;
|
131
143
|
|
132
|
-
export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useRemapArray, useStateObject };
|
144
|
+
export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useNullSafety, useRemapArray, useStateObject };
|
package/dist/index.js
CHANGED
@@ -124,6 +124,14 @@ function useArrayState(states) {
|
|
124
124
|
return useStateObject(states.filter((state) => state.hasValue).map((state) => state.value));
|
125
125
|
}
|
126
126
|
|
127
|
+
// src/hooks/useNullSafety.ts
|
128
|
+
function useNullSafety(state) {
|
129
|
+
if (!state.hasValue) return new EmptyState();
|
130
|
+
if (state.value === void 0) return new EmptyState();
|
131
|
+
if (state.value === null) return new EmptyState();
|
132
|
+
return new ValidState(state.value, (value) => state.value = value);
|
133
|
+
}
|
134
|
+
|
127
135
|
// src/index.ts
|
128
136
|
var index_default = void 0;
|
129
137
|
export {
|
@@ -132,6 +140,7 @@ export {
|
|
132
140
|
useElementState,
|
133
141
|
useEmptyState,
|
134
142
|
useFieldState,
|
143
|
+
useNullSafety,
|
135
144
|
useRemapArray,
|
136
145
|
useStateObject
|
137
146
|
};
|
package/package.json
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
import {StateObject} from "../stateObject";
|
2
|
+
import {ValidState} from "../implementations/validState";
|
3
|
+
import {EmptyState} from "../implementations/emptyState";
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Hook that ensures a StateObject contains a defined, non-null value.
|
7
|
+
* If the StateObject's value is `undefined` or `null`, it returns an EmptyState.
|
8
|
+
* Otherwise, it returns a ValidState with the value and a setter to update the value.
|
9
|
+
*
|
10
|
+
* @template TOrigin - The type of the value contained in the StateObject.
|
11
|
+
* @param state - The StateObject which may contain a value, `undefined`, or `null`.
|
12
|
+
* @returns A new StateObject containing the value if it is defined and non-null,
|
13
|
+
* otherwise an EmptyState.
|
14
|
+
*/
|
15
|
+
export function useNullSafety<TOrigin>(state: StateObject<TOrigin | undefined | null>): StateObject<TOrigin> {
|
16
|
+
|
17
|
+
if (!state.hasValue) return new EmptyState<TOrigin>();
|
18
|
+
|
19
|
+
if (state.value === undefined) return new EmptyState<TOrigin>();
|
20
|
+
|
21
|
+
if (state.value === null) return new EmptyState<TOrigin>();
|
22
|
+
|
23
|
+
return new ValidState<TOrigin>(state.value, (value: TOrigin) => state.value = value);
|
24
|
+
}
|
package/src/index.ts
CHANGED