react-state-monad 1.0.19 → 1.0.21

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -145,8 +145,15 @@ Parameters:
145
145
 
146
146
  Returns a `StateObject` of type `TOrigin` representing the value if it is defined and non-null, otherwise an `EmptyState`.
147
147
 
148
+ ### `useRemapKeysState<TOriginal, TField>`
148
149
 
150
+ This hook remaps the keys of a state object to a record of `StateObject`s, allowing for independent updates of each key while keeping the overall object state synchronized.
149
151
 
152
+ Parameters:
153
+
154
+ - `state`: The `StateObject` containing the original state.
155
+
156
+ Returns a record where each key is mapped to a new `StateObject` representing the value of that key, allowing individual updates while keeping the object state synchronized. If the state has no value or is an array, it returns an empty object.
150
157
 
151
158
 
152
159
 
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.21",
5
5
  "description": "A set of hooks to manage/transform/filter states with monads in React",
6
6
  "keywords": [
7
7
  "maybe",
@@ -21,36 +21,32 @@ export function useFieldState<TOriginal, TField>(
21
21
  }
22
22
 
23
23
  /**
24
- * Hook that remaps the keys of an object within a StateObject to a Map of StateObjects,
25
- * allowing for independent updates of each key while keeping the overall object state synchronized.
24
+ * Hook that remaps the keys of a state object to a record of StateObjects.
26
25
  *
27
26
  * @template TOriginal - The type of the original state object.
28
- * @param state - The StateObject containing the original object.
29
- * @returns A Map where each key is mapped to a new StateObject representing the value of that key,
30
- * allowing individual updates while keeping the object state synchronized.
27
+ * @template TField - The type of the field value to be derived.
28
+ * @param state - The StateObject containing the original state.
29
+ * @returns A record where each key is mapped to a new StateObject for the corresponding field.
31
30
  */
32
31
 
33
- export function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Map<string, StateObject<TField>> {
32
+ export function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Record<string, StateObject<TField>> {
34
33
  // si state no tiene valor, retornar un invalid
35
34
 
36
35
  if (!state.hasValue) {
37
- return new Map<string, StateObject<TField>>();
36
+ return {} as Record<string, StateObject<TField>>;
38
37
  }
39
38
 
40
39
  if (Array.isArray(state.value)) {
41
40
  console.warn('useRemapKeysState should be used with objects, use useRemapArray for arrays');
42
- return new Map<string, StateObject<TField>>();
41
+ return {} as Record<string, StateObject<TField>>;
43
42
  }
44
43
 
45
- const keys = Object.keys(state.value);
46
-
47
- const map = new Map<string, StateObject<TField>>();
48
-
49
- keys.forEach((key) => {
50
- map.set(key, useFieldState(state, key as ValidFieldFrom<TOriginal, TField>));
51
- });
44
+ const keys = Object.keys(state.value);
52
45
 
53
- return map;
46
+ return keys.reduce((acc, key) => {
47
+ acc[key] = useFieldState(state, key as ValidFieldFrom<TOriginal, TField>);
48
+ return acc;
49
+ }, {} as Record<string, StateObject<TField>>);
54
50
  }
55
51
 
56
52