react-state-monad 1.0.19 → 1.0.20

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 CHANGED
@@ -27,6 +27,7 @@ __export(index_exports, {
27
27
  useFieldState: () => useFieldState,
28
28
  useNullSafety: () => useNullSafety,
29
29
  useRemapArray: () => useRemapArray,
30
+ useRemapKeysState: () => useRemapKeysState,
30
31
  useStateObject: () => useStateObject
31
32
  });
32
33
  module.exports = __toCommonJS(index_exports);
@@ -40,6 +41,21 @@ function useFieldState(state, field) {
40
41
  // Updates the field with the new value.
41
42
  );
42
43
  }
44
+ function useRemapKeysState(state) {
45
+ if (!state.hasValue) {
46
+ return /* @__PURE__ */ new Map();
47
+ }
48
+ if (Array.isArray(state.value)) {
49
+ console.warn("useRemapKeysState should be used with objects, use useRemapArray for arrays");
50
+ return /* @__PURE__ */ new Map();
51
+ }
52
+ const keys = Object.keys(state.value);
53
+ const map = /* @__PURE__ */ new Map();
54
+ keys.forEach((key) => {
55
+ map.set(key, useFieldState(state, key));
56
+ });
57
+ return map;
58
+ }
43
59
 
44
60
  // src/implementations/emptyState.ts
45
61
  var EmptyState = class _EmptyState {
@@ -175,5 +191,6 @@ var index_default = void 0;
175
191
  useFieldState,
176
192
  useNullSafety,
177
193
  useRemapArray,
194
+ useRemapKeysState,
178
195
  useStateObject
179
196
  });
package/dist/index.d.cts CHANGED
@@ -78,6 +78,16 @@ type ValidFieldFrom<TObject, TField> = {
78
78
  * @returns A new StateObject for the derived field.
79
79
  */
80
80
  declare function useFieldState<TOriginal, TField>(state: StateObject<TOriginal>, field: ValidFieldFrom<TOriginal, TField>): StateObject<TField>;
81
+ /**
82
+ * Hook that remaps the keys of an object within a StateObject to a Map of StateObjects,
83
+ * allowing for independent updates of each key while keeping the overall object state synchronized.
84
+ *
85
+ * @template TOriginal - The type of the original state object.
86
+ * @param state - The StateObject containing the original object.
87
+ * @returns A Map where each key is mapped to a new StateObject representing the value of that key,
88
+ * allowing individual updates while keeping the object state synchronized.
89
+ */
90
+ declare function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Map<string, StateObject<TField>>;
81
91
 
82
92
  /**
83
93
  * Hook that allows you to derive and update a specific element in an array within a StateObject.
@@ -141,4 +151,4 @@ declare function useNullSafety<TOrigin>(state: StateObject<TOrigin | undefined |
141
151
 
142
152
  declare const _default: undefined;
143
153
 
144
- export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useNullSafety, useRemapArray, useStateObject };
154
+ export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useNullSafety, useRemapArray, useRemapKeysState, useStateObject };
package/dist/index.d.ts CHANGED
@@ -78,6 +78,16 @@ type ValidFieldFrom<TObject, TField> = {
78
78
  * @returns A new StateObject for the derived field.
79
79
  */
80
80
  declare function useFieldState<TOriginal, TField>(state: StateObject<TOriginal>, field: ValidFieldFrom<TOriginal, TField>): StateObject<TField>;
81
+ /**
82
+ * Hook that remaps the keys of an object within a StateObject to a Map of StateObjects,
83
+ * allowing for independent updates of each key while keeping the overall object state synchronized.
84
+ *
85
+ * @template TOriginal - The type of the original state object.
86
+ * @param state - The StateObject containing the original object.
87
+ * @returns A Map where each key is mapped to a new StateObject representing the value of that key,
88
+ * allowing individual updates while keeping the object state synchronized.
89
+ */
90
+ declare function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Map<string, StateObject<TField>>;
81
91
 
82
92
  /**
83
93
  * Hook that allows you to derive and update a specific element in an array within a StateObject.
@@ -141,4 +151,4 @@ declare function useNullSafety<TOrigin>(state: StateObject<TOrigin | undefined |
141
151
 
142
152
  declare const _default: undefined;
143
153
 
144
- export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useNullSafety, useRemapArray, useStateObject };
154
+ export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useNullSafety, useRemapArray, useRemapKeysState, useStateObject };
package/dist/index.js CHANGED
@@ -7,6 +7,21 @@ function useFieldState(state, field) {
7
7
  // Updates the field with the new value.
8
8
  );
9
9
  }
10
+ function useRemapKeysState(state) {
11
+ if (!state.hasValue) {
12
+ return /* @__PURE__ */ new Map();
13
+ }
14
+ if (Array.isArray(state.value)) {
15
+ console.warn("useRemapKeysState should be used with objects, use useRemapArray for arrays");
16
+ return /* @__PURE__ */ new Map();
17
+ }
18
+ const keys = Object.keys(state.value);
19
+ const map = /* @__PURE__ */ new Map();
20
+ keys.forEach((key) => {
21
+ map.set(key, useFieldState(state, key));
22
+ });
23
+ return map;
24
+ }
10
25
 
11
26
  // src/implementations/emptyState.ts
12
27
  var EmptyState = class _EmptyState {
@@ -142,5 +157,6 @@ export {
142
157
  useFieldState,
143
158
  useNullSafety,
144
159
  useRemapArray,
160
+ useRemapKeysState,
145
161
  useStateObject
146
162
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-state-monad",
3
3
  "type": "module",
4
- "version": "1.0.19",
4
+ "version": "1.0.20",
5
5
  "description": "A set of hooks to manage/transform/filter states with monads in React",
6
6
  "keywords": [
7
7
  "maybe",