react-state-monad 1.0.16 → 1.0.18

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-state-monad",
3
3
  "type": "module",
4
- "version": "1.0.16",
4
+ "version": "1.0.18",
5
5
  "description": "A set of hooks to manage/transform/filter states with monads in React",
6
6
  "keywords": [
7
7
  "maybe",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ export * from "./hooks/useEmptyState";
4
4
  export * from "./hooks/useFieldState";
5
5
  export * from "./hooks/useRemapArray";
6
6
  export * from "./hooks/useStateObject";
7
+ export * from "./hooks/useNullSafety";
7
8
  export * from "./stateObject";
8
9
 
9
10
  export default this;
File without changes