react-state-monad 1.0.17 → 1.0.19

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-state-monad",
3
3
  "type": "module",
4
- "version": "1.0.17",
4
+ "version": "1.0.19",
5
5
  "description": "A set of hooks to manage/transform/filter states with monads in React",
6
6
  "keywords": [
7
7
  "maybe",
@@ -18,4 +18,41 @@ export function useFieldState<TOriginal, TField>(
18
18
  (original) => original[field] as TField, // Extracts the field value.
19
19
  (newField, original) => ({...original, [field]: newField} as TOriginal) // Updates the field with the new value.
20
20
  );
21
- }
21
+ }
22
+
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.
26
+ *
27
+ * @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.
31
+ */
32
+
33
+ export function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Map<string, StateObject<TField>> {
34
+ // si state no tiene valor, retornar un invalid
35
+
36
+ if (!state.hasValue) {
37
+ return new Map<string, StateObject<TField>>();
38
+ }
39
+
40
+ if (Array.isArray(state.value)) {
41
+ console.warn('useRemapKeysState should be used with objects, use useRemapArray for arrays');
42
+ return new Map<string, StateObject<TField>>();
43
+ }
44
+
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
+ });
52
+
53
+ return map;
54
+ }
55
+
56
+
57
+
58
+