react-state-monad 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs ADDED
@@ -0,0 +1,169 @@
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
+ // 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
+ useRemapArray: () => useRemapArray,
29
+ useStateObject: () => useStateObject
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+
33
+ // src/hooks/useFieldState.ts
34
+ function useFieldState(state, field) {
35
+ return state.map(
36
+ (original) => original[field],
37
+ // Extracts the field value.
38
+ (newField, original) => ({ ...original, [field]: newField })
39
+ // Updates the field with the new value.
40
+ );
41
+ }
42
+
43
+ // src/implementations/emptyState.ts
44
+ var EmptyState = class _EmptyState {
45
+ // No value stored, returns an error when accessed.
46
+ get value() {
47
+ throw new Error("Not implemented");
48
+ }
49
+ get hasValue() {
50
+ return false;
51
+ }
52
+ orElse(orElse) {
53
+ return orElse;
54
+ }
55
+ do() {
56
+ }
57
+ filter() {
58
+ return this;
59
+ }
60
+ set value(_) {
61
+ }
62
+ flatMap() {
63
+ return new _EmptyState();
64
+ }
65
+ map() {
66
+ return new _EmptyState();
67
+ }
68
+ };
69
+
70
+ // src/implementations/validState.ts
71
+ var ValidState = class _ValidState {
72
+ state;
73
+ setter;
74
+ constructor(state, setter) {
75
+ this.state = state;
76
+ this.setter = setter;
77
+ }
78
+ get value() {
79
+ return this.state;
80
+ }
81
+ do(action) {
82
+ action(this.state);
83
+ }
84
+ orElse() {
85
+ return this.state;
86
+ }
87
+ set value(newState) {
88
+ this.setter(newState);
89
+ }
90
+ map(mappingFunction, inverseMappingFunction) {
91
+ const derivedState = mappingFunction(this.state);
92
+ const derivedSetter = (newState) => {
93
+ this.setter(inverseMappingFunction(newState, this.state));
94
+ };
95
+ return new _ValidState(derivedState, derivedSetter);
96
+ }
97
+ flatMap(mappingFunction) {
98
+ return mappingFunction(this.state);
99
+ }
100
+ get hasValue() {
101
+ return true;
102
+ }
103
+ filter(predicate) {
104
+ return predicate(this.state) ? this : new EmptyState();
105
+ }
106
+ };
107
+
108
+ // src/hooks/useElementState.ts
109
+ function useElementState(state, index) {
110
+ if (!state.hasValue || index < 0 || index >= state.value.length) {
111
+ return new EmptyState();
112
+ }
113
+ return new ValidState(
114
+ state.value[index],
115
+ (newElement) => {
116
+ const arrayCopy = [...state.value];
117
+ arrayCopy[index] = newElement;
118
+ state.value = arrayCopy;
119
+ }
120
+ );
121
+ }
122
+
123
+ // src/hooks/useEmptyState.ts
124
+ function useEmptyState() {
125
+ return new EmptyState();
126
+ }
127
+
128
+ // src/hooks/useStateObject.ts
129
+ var import_react = require("react");
130
+ function useStateObject(initialState) {
131
+ const [state, setState] = (0, import_react.useState)(initialState);
132
+ return new ValidState(state, setState);
133
+ }
134
+
135
+ // src/hooks/useRemapArray.ts
136
+ function useRemapArray(state) {
137
+ if (!state.hasValue) return [];
138
+ const count = state.value.length;
139
+ const result = [];
140
+ for (let i = 0; i < count; i++) {
141
+ result.push(
142
+ new ValidState(
143
+ state.value[i],
144
+ // The current value of the element at index i.
145
+ (newElement) => {
146
+ const arrayCopy = [...state.value];
147
+ arrayCopy[i] = newElement;
148
+ state.value = arrayCopy;
149
+ }
150
+ )
151
+ );
152
+ }
153
+ return result;
154
+ }
155
+ function useArrayState(states) {
156
+ return useStateObject(states.filter((state) => state.hasValue).map((state) => state.value));
157
+ }
158
+
159
+ // index.ts
160
+ var index_default = void 0;
161
+ // Annotate the CommonJS export names for ESM import in node:
162
+ 0 && (module.exports = {
163
+ useArrayState,
164
+ useElementState,
165
+ useEmptyState,
166
+ useFieldState,
167
+ useRemapArray,
168
+ useStateObject
169
+ });
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Represents a state object that holds a value of type T, allowing various state operations.
3
+ * This is the main interface for managing state, with operations like `map`, `filter`, and `flatMap`.
4
+ * initialize with useStateObject<T>(initialState: T) hook
5
+ * @template T - The type of the value stored in the state object.
6
+ */
7
+ type StateObject<T> = {
8
+ /**
9
+ * The current value of the state.
10
+ */
11
+ get value(): T;
12
+ /**
13
+ * Returns true if the state has a valid value, false otherwise.
14
+ */
15
+ get hasValue(): boolean;
16
+ /**
17
+ * Performs an action on the current state value.
18
+ *
19
+ * @param action - A function that accepts the current state value and performs some operation.
20
+ */
21
+ do(action: (t: T) => void): void;
22
+ /**
23
+ * Sets a new value for the state.
24
+ *
25
+ * @param newState - The new state value to set.
26
+ */
27
+ set value(newState: T);
28
+ /**
29
+ * Transforms the current state into another state object by applying a mapping function.
30
+ *
31
+ * @template U - The type of the new state value.
32
+ * @param mappingFunction - A function that transforms the current state value into a new value.
33
+ * @param inverseMappingFunction - A function that transforms a new value back to the original state type.
34
+ * @returns A new StateObject with the transformed value.
35
+ */
36
+ map<U>(mappingFunction: (t: T) => U, inverseMappingFunction: (u: U, t: T) => T): StateObject<U>;
37
+ /**
38
+ * Filters the state based on a predicate, returning an empty state if the predicate is not satisfied.
39
+ *
40
+ * @param predicate - A function that tests the current state value.
41
+ * @returns A new StateObject with the original value or an empty state.
42
+ */
43
+ filter(predicate: (t: T) => boolean): StateObject<T>;
44
+ /**
45
+ * Returns the current state value if it exists; otherwise, returns the provided alternative value.
46
+ *
47
+ * @param orElse - The value to return if the state does not have a valid value.
48
+ * @returns The current state value or the provided fallback value.
49
+ */
50
+ orElse(orElse: T): T;
51
+ /**
52
+ * Transforms the current state into another state object by applying a mapping function that returns a new state.
53
+ *
54
+ * @template U - The type of the new state value.
55
+ * @param mappingFunction - A function that transforms the current state value into another state object.
56
+ * @returns A new StateObject based on the result of the mapping function.
57
+ */
58
+ flatMap<U>(mappingFunction: (t: T) => StateObject<U>): StateObject<U>;
59
+ };
60
+
61
+ /**
62
+ * Helper type that ensures a field is a valid key of an object and that the field's type matches the expected type.
63
+ *
64
+ * @template TObject - The object type.
65
+ * @template TField - The expected type of the field.
66
+ */
67
+ type ValidFieldFrom<TObject, TField> = {
68
+ [Key in keyof TObject]: TObject[Key] extends TField ? Key : never;
69
+ }[keyof TObject];
70
+
71
+ /**
72
+ * Hook that derives a field from the state object and creates a new StateObject for the field's value.
73
+ *
74
+ * @template TOriginal - The type of the original state object.
75
+ * @template TField - The type of the field value to be derived.
76
+ * @param state - The StateObject containing the original state.
77
+ * @param field - The field name to be derived from the state.
78
+ * @returns A new StateObject for the derived field.
79
+ */
80
+ declare function useFieldState<TOriginal, TField>(state: StateObject<TOriginal>, field: ValidFieldFrom<TOriginal, TField>): StateObject<TField>;
81
+
82
+ /**
83
+ * Hook that allows you to derive and update a specific element in an array within a StateObject.
84
+ *
85
+ * @template T - The type of the array elements.
86
+ * @param state - The StateObject containing an array.
87
+ * @param index - The index of the element to be derived.
88
+ * @returns A new StateObject representing the element at the given index.
89
+ */
90
+ declare function useElementState<T>(state: StateObject<T[]>, index: number): StateObject<T>;
91
+
92
+ /**
93
+ * Hook that initializes a StateObject with an empty state.
94
+ * This is useful as a fallback when no valid state is available.
95
+ *
96
+ * @template T - The type of the value that could be held by the state.
97
+ * @returns A StateObject representing an empty state.
98
+ */
99
+ declare function useEmptyState<T>(): StateObject<T>;
100
+
101
+ /**
102
+ * Hook that maps each element in an array within a StateObject to a new StateObject,
103
+ * allowing for independent updates of each element while keeping the overall array state synchronized.
104
+ *
105
+ * @template T - The type of the array elements.
106
+ * @param state - The StateObject containing an array.
107
+ * @returns An array of new StateObjects, each representing an element in the original array,
108
+ * allowing individual updates while keeping the array state synchronized.
109
+ */
110
+ declare function useRemapArray<T>(state: StateObject<T[]>): StateObject<T>[];
111
+ /**
112
+ * Hook that takes an array of StateObjects and returns a new StateObject containing that array,
113
+ * allowing for updates to the entire array while keeping it synchronized within a single StateObject.
114
+ *
115
+ * @template T - The type of the elements in the array.
116
+ * @param states - The array of StateObjects.
117
+ * @returns A new StateObject containing the array of StateObjects, allowing for updates to the whole array.
118
+ */
119
+ declare function useArrayState<T>(states: StateObject<T>[]): StateObject<T[]>;
120
+
121
+ /**
122
+ * Hook that initializes a StateObject with the given initial value.
123
+ *
124
+ * @template T - The type of the value to be stored in the state.
125
+ * @param initialState - The initial value of the state.
126
+ * @returns A StateObject representing the initialized state.
127
+ */
128
+ declare function useStateObject<T>(initialState: T): StateObject<T>;
129
+
130
+ declare const _default: undefined;
131
+
132
+ export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useRemapArray, useStateObject };
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Represents a state object that holds a value of type T, allowing various state operations.
3
+ * This is the main interface for managing state, with operations like `map`, `filter`, and `flatMap`.
4
+ * initialize with useStateObject<T>(initialState: T) hook
5
+ * @template T - The type of the value stored in the state object.
6
+ */
7
+ type StateObject<T> = {
8
+ /**
9
+ * The current value of the state.
10
+ */
11
+ get value(): T;
12
+ /**
13
+ * Returns true if the state has a valid value, false otherwise.
14
+ */
15
+ get hasValue(): boolean;
16
+ /**
17
+ * Performs an action on the current state value.
18
+ *
19
+ * @param action - A function that accepts the current state value and performs some operation.
20
+ */
21
+ do(action: (t: T) => void): void;
22
+ /**
23
+ * Sets a new value for the state.
24
+ *
25
+ * @param newState - The new state value to set.
26
+ */
27
+ set value(newState: T);
28
+ /**
29
+ * Transforms the current state into another state object by applying a mapping function.
30
+ *
31
+ * @template U - The type of the new state value.
32
+ * @param mappingFunction - A function that transforms the current state value into a new value.
33
+ * @param inverseMappingFunction - A function that transforms a new value back to the original state type.
34
+ * @returns A new StateObject with the transformed value.
35
+ */
36
+ map<U>(mappingFunction: (t: T) => U, inverseMappingFunction: (u: U, t: T) => T): StateObject<U>;
37
+ /**
38
+ * Filters the state based on a predicate, returning an empty state if the predicate is not satisfied.
39
+ *
40
+ * @param predicate - A function that tests the current state value.
41
+ * @returns A new StateObject with the original value or an empty state.
42
+ */
43
+ filter(predicate: (t: T) => boolean): StateObject<T>;
44
+ /**
45
+ * Returns the current state value if it exists; otherwise, returns the provided alternative value.
46
+ *
47
+ * @param orElse - The value to return if the state does not have a valid value.
48
+ * @returns The current state value or the provided fallback value.
49
+ */
50
+ orElse(orElse: T): T;
51
+ /**
52
+ * Transforms the current state into another state object by applying a mapping function that returns a new state.
53
+ *
54
+ * @template U - The type of the new state value.
55
+ * @param mappingFunction - A function that transforms the current state value into another state object.
56
+ * @returns A new StateObject based on the result of the mapping function.
57
+ */
58
+ flatMap<U>(mappingFunction: (t: T) => StateObject<U>): StateObject<U>;
59
+ };
60
+
61
+ /**
62
+ * Helper type that ensures a field is a valid key of an object and that the field's type matches the expected type.
63
+ *
64
+ * @template TObject - The object type.
65
+ * @template TField - The expected type of the field.
66
+ */
67
+ type ValidFieldFrom<TObject, TField> = {
68
+ [Key in keyof TObject]: TObject[Key] extends TField ? Key : never;
69
+ }[keyof TObject];
70
+
71
+ /**
72
+ * Hook that derives a field from the state object and creates a new StateObject for the field's value.
73
+ *
74
+ * @template TOriginal - The type of the original state object.
75
+ * @template TField - The type of the field value to be derived.
76
+ * @param state - The StateObject containing the original state.
77
+ * @param field - The field name to be derived from the state.
78
+ * @returns A new StateObject for the derived field.
79
+ */
80
+ declare function useFieldState<TOriginal, TField>(state: StateObject<TOriginal>, field: ValidFieldFrom<TOriginal, TField>): StateObject<TField>;
81
+
82
+ /**
83
+ * Hook that allows you to derive and update a specific element in an array within a StateObject.
84
+ *
85
+ * @template T - The type of the array elements.
86
+ * @param state - The StateObject containing an array.
87
+ * @param index - The index of the element to be derived.
88
+ * @returns A new StateObject representing the element at the given index.
89
+ */
90
+ declare function useElementState<T>(state: StateObject<T[]>, index: number): StateObject<T>;
91
+
92
+ /**
93
+ * Hook that initializes a StateObject with an empty state.
94
+ * This is useful as a fallback when no valid state is available.
95
+ *
96
+ * @template T - The type of the value that could be held by the state.
97
+ * @returns A StateObject representing an empty state.
98
+ */
99
+ declare function useEmptyState<T>(): StateObject<T>;
100
+
101
+ /**
102
+ * Hook that maps each element in an array within a StateObject to a new StateObject,
103
+ * allowing for independent updates of each element while keeping the overall array state synchronized.
104
+ *
105
+ * @template T - The type of the array elements.
106
+ * @param state - The StateObject containing an array.
107
+ * @returns An array of new StateObjects, each representing an element in the original array,
108
+ * allowing individual updates while keeping the array state synchronized.
109
+ */
110
+ declare function useRemapArray<T>(state: StateObject<T[]>): StateObject<T>[];
111
+ /**
112
+ * Hook that takes an array of StateObjects and returns a new StateObject containing that array,
113
+ * allowing for updates to the entire array while keeping it synchronized within a single StateObject.
114
+ *
115
+ * @template T - The type of the elements in the array.
116
+ * @param states - The array of StateObjects.
117
+ * @returns A new StateObject containing the array of StateObjects, allowing for updates to the whole array.
118
+ */
119
+ declare function useArrayState<T>(states: StateObject<T>[]): StateObject<T[]>;
120
+
121
+ /**
122
+ * Hook that initializes a StateObject with the given initial value.
123
+ *
124
+ * @template T - The type of the value to be stored in the state.
125
+ * @param initialState - The initial value of the state.
126
+ * @returns A StateObject representing the initialized state.
127
+ */
128
+ declare function useStateObject<T>(initialState: T): StateObject<T>;
129
+
130
+ declare const _default: undefined;
131
+
132
+ export { type StateObject, _default as default, useArrayState, useElementState, useEmptyState, useFieldState, useRemapArray, useStateObject };
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
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
+
11
+ // src/implementations/emptyState.ts
12
+ var EmptyState = class _EmptyState {
13
+ // No value stored, returns an error when accessed.
14
+ get value() {
15
+ throw new Error("Not implemented");
16
+ }
17
+ get hasValue() {
18
+ return false;
19
+ }
20
+ orElse(orElse) {
21
+ return orElse;
22
+ }
23
+ do() {
24
+ }
25
+ filter() {
26
+ return this;
27
+ }
28
+ set value(_) {
29
+ }
30
+ flatMap() {
31
+ return new _EmptyState();
32
+ }
33
+ map() {
34
+ return new _EmptyState();
35
+ }
36
+ };
37
+
38
+ // src/implementations/validState.ts
39
+ var ValidState = class _ValidState {
40
+ state;
41
+ setter;
42
+ constructor(state, setter) {
43
+ this.state = state;
44
+ this.setter = setter;
45
+ }
46
+ get value() {
47
+ return this.state;
48
+ }
49
+ do(action) {
50
+ action(this.state);
51
+ }
52
+ orElse() {
53
+ return this.state;
54
+ }
55
+ set value(newState) {
56
+ this.setter(newState);
57
+ }
58
+ map(mappingFunction, inverseMappingFunction) {
59
+ const derivedState = mappingFunction(this.state);
60
+ const derivedSetter = (newState) => {
61
+ this.setter(inverseMappingFunction(newState, this.state));
62
+ };
63
+ return new _ValidState(derivedState, derivedSetter);
64
+ }
65
+ flatMap(mappingFunction) {
66
+ return mappingFunction(this.state);
67
+ }
68
+ get hasValue() {
69
+ return true;
70
+ }
71
+ filter(predicate) {
72
+ return predicate(this.state) ? this : new EmptyState();
73
+ }
74
+ };
75
+
76
+ // src/hooks/useElementState.ts
77
+ function useElementState(state, index) {
78
+ if (!state.hasValue || index < 0 || index >= state.value.length) {
79
+ return new EmptyState();
80
+ }
81
+ return new ValidState(
82
+ state.value[index],
83
+ (newElement) => {
84
+ const arrayCopy = [...state.value];
85
+ arrayCopy[index] = newElement;
86
+ state.value = arrayCopy;
87
+ }
88
+ );
89
+ }
90
+
91
+ // src/hooks/useEmptyState.ts
92
+ function useEmptyState() {
93
+ return new EmptyState();
94
+ }
95
+
96
+ // src/hooks/useStateObject.ts
97
+ import { useState } from "react";
98
+ function useStateObject(initialState) {
99
+ const [state, setState] = useState(initialState);
100
+ return new ValidState(state, setState);
101
+ }
102
+
103
+ // src/hooks/useRemapArray.ts
104
+ function useRemapArray(state) {
105
+ if (!state.hasValue) return [];
106
+ const count = state.value.length;
107
+ const result = [];
108
+ for (let i = 0; i < count; i++) {
109
+ result.push(
110
+ new ValidState(
111
+ state.value[i],
112
+ // The current value of the element at index i.
113
+ (newElement) => {
114
+ const arrayCopy = [...state.value];
115
+ arrayCopy[i] = newElement;
116
+ state.value = arrayCopy;
117
+ }
118
+ )
119
+ );
120
+ }
121
+ return result;
122
+ }
123
+ function useArrayState(states) {
124
+ return useStateObject(states.filter((state) => state.hasValue).map((state) => state.value));
125
+ }
126
+
127
+ // index.ts
128
+ var index_default = void 0;
129
+ export {
130
+ index_default as default,
131
+ useArrayState,
132
+ useElementState,
133
+ useEmptyState,
134
+ useFieldState,
135
+ useRemapArray,
136
+ useStateObject
137
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-state-monad",
3
3
  "type": "module",
4
- "version": "1.0.8",
4
+ "version": "1.0.9",
5
5
  "description": "A set of hooks to manage/transform/filter states with monads in React",
6
6
  "keywords": [
7
7
  "maybe",
@@ -35,6 +35,4 @@
35
35
  "module": "dist/index.mjs",
36
36
  "types": "dist/types/index.d.ts"
37
37
 
38
-
39
-
40
38
  }