react-state-monad 1.0.16 → 1.0.18
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/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/index.ts +1 -0
- /package/src/hooks/{useStateCast.ts → useNullSafety.ts} +0 -0
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
package/src/index.ts
CHANGED
File without changes
|