react-state-monad 1.0.20 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-state-monad",
3
3
  "type": "module",
4
- "version": "1.0.20",
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