react-state-monad 1.0.27 → 1.0.28

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.d.cts CHANGED
@@ -86,7 +86,7 @@ declare function useFieldState<TOriginal, TField>(state: StateObject<TOriginal>,
86
86
  * @param state - The StateObject containing the original state.
87
87
  * @returns A record where each key is mapped to a new StateObject for the corresponding field.
88
88
  */
89
- declare function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Record<string, StateObject<TField>>;
89
+ declare function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Record<keyof TOriginal, StateObject<TField>>;
90
90
 
91
91
  /**
92
92
  * Hook that allows you to derive and update a specific element in an array within a StateObject.
package/dist/index.d.ts CHANGED
@@ -86,7 +86,7 @@ declare function useFieldState<TOriginal, TField>(state: StateObject<TOriginal>,
86
86
  * @param state - The StateObject containing the original state.
87
87
  * @returns A record where each key is mapped to a new StateObject for the corresponding field.
88
88
  */
89
- declare function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Record<string, StateObject<TField>>;
89
+ declare function useRemapKeysState<TOriginal extends object, TField>(state: StateObject<TOriginal>): Record<keyof TOriginal, StateObject<TField>>;
90
90
 
91
91
  /**
92
92
  * Hook that allows you to derive and update a specific element in an array within a StateObject.
package/dist/index.js CHANGED
@@ -1,3 +1,37 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => index_default,
24
+ useArrayState: () => useArrayState,
25
+ useElementState: () => useElementState,
26
+ useEmptyState: () => useEmptyState,
27
+ useFieldState: () => useFieldState,
28
+ useNullSafety: () => useNullSafety,
29
+ useRemapArray: () => useRemapArray,
30
+ useRemapKeysState: () => useRemapKeysState,
31
+ useStateObject: () => useStateObject
32
+ });
33
+ module.exports = __toCommonJS(index_exports);
34
+
1
35
  // src/hooks/useFieldState.ts
2
36
  function useFieldState(state, field) {
3
37
  return state.map(
@@ -16,10 +50,13 @@ function useRemapKeysState(state) {
16
50
  return {};
17
51
  }
18
52
  const keys = Object.keys(state.value);
19
- return keys.reduce((acc, key) => {
20
- acc[key] = useFieldState(state, key);
21
- return acc;
22
- }, {});
53
+ return keys.reduce(
54
+ (acc, key) => {
55
+ acc[key] = useFieldState(state, key);
56
+ return acc;
57
+ },
58
+ {}
59
+ );
23
60
  }
24
61
 
25
62
  // src/implementations/emptyState.ts
@@ -108,9 +145,9 @@ function useEmptyState() {
108
145
  }
109
146
 
110
147
  // src/hooks/useStateObject.ts
111
- import { useState } from "react";
148
+ var import_react = require("react");
112
149
  function useStateObject(initialState) {
113
- const [state, setState] = useState(initialState);
150
+ const [state, setState] = (0, import_react.useState)(initialState);
114
151
  return new ValidState(state, setState);
115
152
  }
116
153
 
@@ -148,8 +185,8 @@ function useNullSafety(state) {
148
185
 
149
186
  // src/index.ts
150
187
  var index_default = void 0;
151
- export {
152
- index_default as default,
188
+ // Annotate the CommonJS export names for ESM import in node:
189
+ 0 && (module.exports = {
153
190
  useArrayState,
154
191
  useElementState,
155
192
  useEmptyState,
@@ -158,4 +195,4 @@ export {
158
195
  useRemapArray,
159
196
  useRemapKeysState,
160
197
  useStateObject
161
- };
198
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,164 @@
1
+ // src/hooks/useFieldState.ts
2
+ function useFieldState(state, field) {
3
+ return state.map(
4
+ (original) => original[field],
5
+ // Extracts the field value.
6
+ (newField, original) => ({ ...original, [field]: newField })
7
+ // Updates the field with the new value.
8
+ );
9
+ }
10
+ function useRemapKeysState(state) {
11
+ if (!state.hasValue) {
12
+ return {};
13
+ }
14
+ if (Array.isArray(state.value)) {
15
+ console.warn("useRemapKeysState should be used with objects, use useRemapArray for arrays");
16
+ return {};
17
+ }
18
+ const keys = Object.keys(state.value);
19
+ return keys.reduce(
20
+ (acc, key) => {
21
+ acc[key] = useFieldState(state, key);
22
+ return acc;
23
+ },
24
+ {}
25
+ );
26
+ }
27
+
28
+ // src/implementations/emptyState.ts
29
+ var EmptyState = class _EmptyState {
30
+ // No value stored, returns an error when accessed.
31
+ get value() {
32
+ throw new Error("Not implemented");
33
+ }
34
+ get hasValue() {
35
+ return false;
36
+ }
37
+ orElse(orElse) {
38
+ return orElse;
39
+ }
40
+ do() {
41
+ }
42
+ filter() {
43
+ return this;
44
+ }
45
+ set value(_) {
46
+ }
47
+ flatMap() {
48
+ return new _EmptyState();
49
+ }
50
+ map() {
51
+ return new _EmptyState();
52
+ }
53
+ };
54
+
55
+ // src/implementations/validState.ts
56
+ var ValidState = class _ValidState {
57
+ state;
58
+ setter;
59
+ constructor(state, setter) {
60
+ this.state = state;
61
+ this.setter = setter;
62
+ }
63
+ get value() {
64
+ return this.state;
65
+ }
66
+ do(action) {
67
+ action(this.state);
68
+ }
69
+ orElse() {
70
+ return this.state;
71
+ }
72
+ set value(newState) {
73
+ this.setter(newState);
74
+ }
75
+ map(mappingFunction, inverseMappingFunction) {
76
+ const derivedState = mappingFunction(this.state);
77
+ const derivedSetter = (newState) => {
78
+ this.setter(inverseMappingFunction(newState, this.state));
79
+ };
80
+ return new _ValidState(derivedState, derivedSetter);
81
+ }
82
+ flatMap(mappingFunction) {
83
+ return mappingFunction(this.state);
84
+ }
85
+ get hasValue() {
86
+ return true;
87
+ }
88
+ filter(predicate) {
89
+ return predicate(this.state) ? this : new EmptyState();
90
+ }
91
+ };
92
+
93
+ // src/hooks/useElementState.ts
94
+ function useElementState(state, index) {
95
+ if (!state.hasValue || index < 0 || index >= state.value.length) {
96
+ return new EmptyState();
97
+ }
98
+ return new ValidState(
99
+ state.value[index],
100
+ (newElement) => {
101
+ const arrayCopy = [...state.value];
102
+ arrayCopy[index] = newElement;
103
+ state.value = arrayCopy;
104
+ }
105
+ );
106
+ }
107
+
108
+ // src/hooks/useEmptyState.ts
109
+ function useEmptyState() {
110
+ return new EmptyState();
111
+ }
112
+
113
+ // src/hooks/useStateObject.ts
114
+ import { useState } from "react";
115
+ function useStateObject(initialState) {
116
+ const [state, setState] = useState(initialState);
117
+ return new ValidState(state, setState);
118
+ }
119
+
120
+ // src/hooks/useRemapArray.ts
121
+ function useRemapArray(state) {
122
+ if (!state.hasValue) return [];
123
+ const count = state.value.length;
124
+ const result = [];
125
+ for (let i = 0; i < count; i++) {
126
+ result.push(
127
+ new ValidState(
128
+ state.value[i],
129
+ // The current value of the element at index i.
130
+ (newElement) => {
131
+ const arrayCopy = [...state.value];
132
+ arrayCopy[i] = newElement;
133
+ state.value = arrayCopy;
134
+ }
135
+ )
136
+ );
137
+ }
138
+ return result;
139
+ }
140
+ function useArrayState(states) {
141
+ return useStateObject(states.filter((state) => state.hasValue).map((state) => state.value));
142
+ }
143
+
144
+ // src/hooks/useNullSafety.ts
145
+ function useNullSafety(state) {
146
+ if (!state.hasValue) return new EmptyState();
147
+ if (state.value === void 0) return new EmptyState();
148
+ if (state.value === null) return new EmptyState();
149
+ return new ValidState(state.value, (value) => state.value = value);
150
+ }
151
+
152
+ // src/index.ts
153
+ var index_default = void 0;
154
+ export {
155
+ index_default as default,
156
+ useArrayState,
157
+ useElementState,
158
+ useEmptyState,
159
+ useFieldState,
160
+ useNullSafety,
161
+ useRemapArray,
162
+ useRemapKeysState,
163
+ useStateObject
164
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-state-monad",
3
3
  "type": "module",
4
- "version": "1.0.27",
4
+ "version": "1.0.28",
5
5
  "description": "A set of hooks to manage/transform/filter states with monads in React",
6
6
  "keywords": [
7
7
  "maybe",
@@ -14,7 +14,7 @@
14
14
  ],
15
15
  "scripts": {
16
16
  "validateTypes": "tsc --noEmit",
17
- "build": "tsup src/index.ts --format cjs,esm --dts --dts-resolve",
17
+ "build": "tsup",
18
18
  "cleanBuild": "rm -rf dist"
19
19
  },
20
20
  "dependencies": {
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/react": "^19.0.7",
25
- "tsup": "^8.3.5",
25
+ "tsup": "^8.5.0",
26
26
  "typescript": "^5.7.3"
27
27
  },
28
28
  "author": {
@@ -45,4 +45,4 @@
45
45
  "dist"
46
46
  ],
47
47
  "sideEffects": false
48
- }
48
+ }