@kaumlaut/pure 1.0.1 → 2.0.0

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # @kaumlaut/pure
2
2
 
3
+ ![example workflow](https://github.com/maxkaemmerer/pure/actions/workflows/node.js.yml/badge.svg)
4
+
3
5
  Types and functions for a state runtime inspired by Elm and functional programming
4
6
 
5
7
  - [API Reference](https://github.com/maxkaemmerer/pure/blob/main/docs/README.md)
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Provides easily composable generic type guards that track errors
3
+ * @module error-aware-guard
4
+ */
5
+ import * as Guard from "@kaumlaut/pure/guard";
6
+ /**
7
+ * Represents a successful ErrorAwareGuard, containing the value
8
+ */
9
+ export type SuccessResult<T> = {
10
+ readonly success: true;
11
+ readonly value: T;
12
+ };
13
+ /**
14
+ * Represents a failed ErrorAwareGuard, containing errors
15
+ */
16
+ export type ErrorResult = {
17
+ readonly success: false;
18
+ readonly errors: string[];
19
+ };
20
+ /**
21
+ * Represents the result of a ErrorAwareGuard
22
+ */
23
+ export type ValidationResult<T> = SuccessResult<T> | ErrorResult;
24
+ /**
25
+ * Represents a function that returns a wrapped value on success or a wrapped error on failure
26
+ */
27
+ export type ErrorAwareGuard<T> = (value: unknown) => ValidationResult<T>;
28
+ /**
29
+ * Creates an ErrorResult with the given errors
30
+ */
31
+ export declare function fail(...errors: string[]): ErrorResult;
32
+ /**
33
+ * Creates an SuccessResult<T> with the given value
34
+ */
35
+ export declare function pass<T>(value: T): SuccessResult<T>;
36
+ /**
37
+ * Takes a ValidationResult<T> and if it is an ErrorResult, prefixes every error with the given prefix.
38
+ */
39
+ export declare function prefixErrors<T>(value: ValidationResult<T>, prefix: string): ValidationResult<T>;
40
+ /**
41
+ * Converts an ErrorAwareGuard<T> to a Guard<T>
42
+ */
43
+ export declare function toGuard<T>(guard: ErrorAwareGuard<T>): Guard.Guard<T>;
44
+ /**
45
+ * Evaluates an ErrorAwareGuard<T> for given value
46
+ */
47
+ export declare function check<T>(value: unknown, guard: ErrorAwareGuard<T>): value is T;
48
+ /**
49
+ * Combines a list of results. Returning a SuccessResult<T> if all of them are SucessResult<T>
50
+ * or returns an ErrorResult with combined errors if any of them are ErrorResult.
51
+ */
52
+ export declare function combineResultsAll<T>(...results: ValidationResult<T>[]): ValidationResult<T>;
53
+ /**
54
+ * Combines two results. Returning a SuccessResult<T> if both of them are SucessResult<T>
55
+ * or returns an ErrorResult with combined errors if one is an ErrorResult.
56
+ */
57
+ export declare function combineTwoResultsAnd<T1, T2>(a: ValidationResult<T1>, b: ValidationResult<T2>): ValidationResult<T1 & T2>;
58
+ /**
59
+ * Combines two results. Returning a SuccessResult<T> if either of them are SucessResult<T>
60
+ * or returns an ErrorResult with combined errors if both are an ErrorResult.
61
+ */
62
+ export declare function combineTwoResultsOneOf<T1, T2>(a: ValidationResult<T1>, b: ValidationResult<T2>): ValidationResult<T1 | T2>;
63
+ /**
64
+ * Combines a list of results. Returning a SuccessResult<T> if either of them are SucessResult<T>
65
+ * or returns an ErrorResult with combined errors if all are an ErrorResult.
66
+ */
67
+ export declare function combineResultsOneOf<T>(...results: ValidationResult<T>[]): ValidationResult<T>;
68
+ /**
69
+ * Converts a Guard<T> to an ErrorAwareGuard<T> with provided error factory function
70
+ */
71
+ export declare function fromGuard<T>(guard: Guard.Guard<T>, createErrors: (value: unknown) => string[]): ErrorAwareGuard<T>;
72
+ /**
73
+ * First applies the predicateGuard and if it is a SuccessResult<T> passes the value to check.
74
+ * If check returns true it creates SuccessResult<T>.
75
+ * Otherwise an ErrorResult is created, containing the errors created by the error factory function.
76
+ */
77
+ export declare function tryGuardIf<T, T2 extends T = T>(predicateGuard: ErrorAwareGuard<T>, check: (value: T) => value is T2, createErrors: (value: T) => string[]): ErrorAwareGuard<T2>;
78
+ /**
79
+ * Confirms that the given value passes both guards.
80
+ */
81
+ export declare function isBoth<T1, T2>(a: ErrorAwareGuard<T1>, b: ErrorAwareGuard<T2>): ErrorAwareGuard<T1 & T2>;
82
+ /**
83
+ * Confirms that the given value passes all guards.
84
+ */
85
+ export declare function isAll<T>(guards: ErrorAwareGuard<T>[]): ErrorAwareGuard<T>;
86
+ /**
87
+ * Confirms the value passes at least one of the given Guards.
88
+ */
89
+ export declare function isOneOf<T1, T2>(a: ErrorAwareGuard<T1>, b: ErrorAwareGuard<T2>): ErrorAwareGuard<T1 | T2>;
90
+ /**
91
+ * Confirms the value is a list of items that all pass the given guard.
92
+ */
93
+ export declare function isListOf<I, T extends I[]>(guard: ErrorAwareGuard<I>): ErrorAwareGuard<T>;
94
+ /**
95
+ * Confirms that the value is a number.
96
+ */
97
+ export declare const isNumber: ErrorAwareGuard<number>;
98
+ /**
99
+ * Confirms that the value is a string.
100
+ */
101
+ export declare const isString: <T extends string>(value: unknown) => ValidationResult<T>;
102
+ /**
103
+ * Confirms that the value is an exact string.
104
+ */
105
+ export declare const isExactString: <T extends string>(expectedString: T) => ErrorAwareGuard<T>;
106
+ /**
107
+ * Confirms that the value is a boolean.
108
+ */
109
+ export declare const isBool: ErrorAwareGuard<boolean>;
110
+ /**
111
+ * Confirms that the value is a float.
112
+ */
113
+ export declare const isFloat: ErrorAwareGuard<number>;
114
+ /**
115
+ * Confirms that the value is an interger.
116
+ */
117
+ export declare const isInt: ErrorAwareGuard<number>;
118
+ /**
119
+ * Confirms that the value is null.
120
+ */
121
+ export declare const isNull: ErrorAwareGuard<null>;
122
+ /**
123
+ * Confirms that the value is undefined.
124
+ */
125
+ export declare const isUndefined: ErrorAwareGuard<undefined>;
126
+ /**
127
+ * Confirms that the value is an object.
128
+ */
129
+ export declare const isObject: ErrorAwareGuard<object>;
130
+ /**
131
+ * Always passes.
132
+ */
133
+ export declare const isAlways: ErrorAwareGuard<unknown>;
134
+ /**
135
+ * Never passes.
136
+ */
137
+ export declare const isNever: ErrorAwareGuard<unknown>;
138
+ /**
139
+ * Confirms that the value is a non-empty string.
140
+ */
141
+ export declare const isNonEmptyString: ErrorAwareGuard<string>;
142
+ /**
143
+ * Confirms that the value is a string with specified length.
144
+ */
145
+ export declare const isStringOfLength: (length: number) => ErrorAwareGuard<string>;
146
+ /**
147
+ * Confirms the value is a string and matches the given regular expression.
148
+ */
149
+ export declare const isStringWithPattern: (pattern: RegExp) => ErrorAwareGuard<string>;
150
+ /**
151
+ * Confirms the value is a list with atleast one item and all items match the given guard.
152
+ */
153
+ export declare const isNonEmptyListOf: <I, T extends I[]>(guard: ErrorAwareGuard<I>) => ErrorAwareGuard<T>;
154
+ /**
155
+ * Confirms the value is number between min and max inclusive.
156
+ * Meaning if the value equals min or max the guard passes.
157
+ */
158
+ export declare const isNumberBetweenInclusive: (min: number, max: number) => ErrorAwareGuard<number>;
159
+ /**
160
+ * Confirms the value is either null or passes the given Guard.
161
+ */
162
+ export declare const isNullOr: <T>(guard: ErrorAwareGuard<T>) => ErrorAwareGuard<T>;
163
+ export declare const isOneStringOf: <T extends string>(validValues: T[]) => ErrorAwareGuard<T>;
164
+ /**
165
+ * Confirms that the value is an object containing the specified key.
166
+ */
167
+ export declare function isObjectWithKey<T extends object, K extends keyof T>(key: K): ErrorAwareGuard<T>;
168
+ /**
169
+ * Confirms that the value is an object containing the specified key the value matching the given guard.
170
+ */
171
+ export declare function isObjectWithKeyMatchingGuard<T extends object, K extends keyof T>(key: K, guard: ErrorAwareGuard<T[K]>): ErrorAwareGuard<T>;
172
+ /**
173
+ * Confirms that the value is an object containing the specified keys.
174
+ */
175
+ export declare function isObjectWithKeys<T extends object, K extends keyof T>(keys: K[]): ErrorAwareGuard<T>;
176
+ /**
177
+ * Confirms that the value is an object whose key value pairs match the corresponding type guards.
178
+ */
179
+ export declare function isObjectWithKeysMatchingGuard<T extends object, K extends keyof T>(guards: {
180
+ [K in keyof T]: ErrorAwareGuard<T[K]>;
181
+ }): ErrorAwareGuard<T>;
182
+ /**
183
+ * Confirms the value is an object where every value matches the given guard.
184
+ */
185
+ export declare function isObjectWithAllKeysMatchingGuard<T extends object, K extends keyof T>(guard: ErrorAwareGuard<T[K]>): ErrorAwareGuard<T>;
186
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/error-aware-guard/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,KAAK,MAAM,sBAAsB,CAAC;AAE9C;;GAEG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAE7E;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC;AAEjE;;GAEG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,gBAAgB,CAAC,CAAC,CAAC,CAAC;AAEzE;;GAEG;AACH,wBAAgB,IAAI,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,WAAW,CAKrD;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAKlD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAC5B,KAAK,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAC1B,MAAM,EAAE,MAAM,GACb,gBAAgB,CAAC,CAAC,CAAC,CASrB;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,C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AAC,EAClE,IAAI,EAAE,CAAC,EAAE,GACR,eAAe,CAAC,CAAC,CAAC,CAEpB;AAED;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,CAAC,EACjB,MAAM,EAAE;KACP,CAAC,IAAI,MAAM,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACtC,GAAG,eAAe,CAAC,CAAC,CAAC,CAOrB;AAED;;GAEG;AACH,wBAAgB,gCAAgC,CAC9C,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,CAAC,EACjB,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAQlD"}
@@ -0,0 +1,313 @@
1
+ /**
2
+ * Provides easily composable generic type guards that track errors
3
+ * @module error-aware-guard
4
+ */
5
+ import * as Guard from "@kaumlaut/pure/guard";
6
+ /**
7
+ * Creates an ErrorResult with the given errors
8
+ */
9
+ export function fail(...errors) {
10
+ return {
11
+ success: false,
12
+ errors: [...errors],
13
+ };
14
+ }
15
+ /**
16
+ * Creates an SuccessResult<T> with the given value
17
+ */
18
+ export function pass(value) {
19
+ return {
20
+ success: true,
21
+ value,
22
+ };
23
+ }
24
+ /**
25
+ * Takes a ValidationResult<T> and if it is an ErrorResult, prefixes every error with the given prefix.
26
+ */
27
+ export function prefixErrors(value, prefix) {
28
+ if (value.success === true) {
29
+ return value;
30
+ }
31
+ return {
32
+ success: false,
33
+ errors: value.errors.map((it) => `${prefix}${it}`),
34
+ };
35
+ }
36
+ /**
37
+ * Converts an ErrorAwareGuard<T> to a Guard<T>
38
+ */
39
+ export function toGuard(guard) {
40
+ return (value) => guard(value).success;
41
+ }
42
+ /**
43
+ * Evaluates an ErrorAwareGuard<T> for given value
44
+ */
45
+ export function check(value, guard) {
46
+ return guard(value).success;
47
+ }
48
+ /**
49
+ * Combines a list of results. Returning a SuccessResult<T> if all of them are SucessResult<T>
50
+ * or returns an ErrorResult with combined errors if any of them are ErrorResult.
51
+ */
52
+ export function combineResultsAll(...results) {
53
+ const successResult = results.find((it) => it.success);
54
+ if (successResult &&
55
+ successResult.success &&
56
+ results.every((it) => it.success)) {
57
+ return pass(successResult.value);
58
+ }
59
+ return fail(...results.reduce((all, result) => [
60
+ ...all,
61
+ ...(result.success === true ? [] : result.errors),
62
+ ], []));
63
+ }
64
+ /**
65
+ * Combines two results. Returning a SuccessResult<T> if both of them are SucessResult<T>
66
+ * or returns an ErrorResult with combined errors if one is an ErrorResult.
67
+ */
68
+ export function combineTwoResultsAnd(a, b) {
69
+ if (a.success === true && b.success === true) {
70
+ return a;
71
+ }
72
+ return fail(...[a, b].reduce((all, result) => [...all, ...result.errors], []));
73
+ }
74
+ /**
75
+ * Combines two results. Returning a SuccessResult<T> if either of them are SucessResult<T>
76
+ * or returns an ErrorResult with combined errors if both are an ErrorResult.
77
+ */
78
+ export function combineTwoResultsOneOf(a, b) {
79
+ if (a.success === true) {
80
+ return a;
81
+ }
82
+ if (b.success === true) {
83
+ return b;
84
+ }
85
+ return fail(...[a, b].reduce((all, result) => [
86
+ ...all,
87
+ ...(result.success === true ? [] : result.errors),
88
+ ], []));
89
+ }
90
+ /**
91
+ * Combines a list of results. Returning a SuccessResult<T> if either of them are SucessResult<T>
92
+ * or returns an ErrorResult with combined errors if all are an ErrorResult.
93
+ */
94
+ export function combineResultsOneOf(...results) {
95
+ const successResult = results.find((it) => it.success);
96
+ if (successResult.success) {
97
+ return pass(successResult.value);
98
+ }
99
+ return fail(...results.reduce((all, result) => [
100
+ ...all,
101
+ ...(result.success === true ? [] : result.errors),
102
+ ], []));
103
+ }
104
+ /**
105
+ * Converts a Guard<T> to an ErrorAwareGuard<T> with provided error factory function
106
+ */
107
+ export function fromGuard(guard, createErrors) {
108
+ return (value) => {
109
+ if (guard(value)) {
110
+ return {
111
+ success: true,
112
+ value,
113
+ };
114
+ }
115
+ return {
116
+ success: false,
117
+ errors: createErrors(value),
118
+ };
119
+ };
120
+ }
121
+ /**
122
+ * First applies the predicateGuard and if it is a SuccessResult<T> passes the value to check.
123
+ * If check returns true it creates SuccessResult<T>.
124
+ * Otherwise an ErrorResult is created, containing the errors created by the error factory function.
125
+ */
126
+ export function tryGuardIf(predicateGuard, check, createErrors) {
127
+ return (value) => {
128
+ const predicateResult = predicateGuard(value);
129
+ if (predicateResult.success === true) {
130
+ if (check(predicateResult.value)) {
131
+ return pass(predicateResult.value);
132
+ }
133
+ return fail(...createErrors(predicateResult.value));
134
+ }
135
+ return predicateResult;
136
+ };
137
+ }
138
+ /**
139
+ * Confirms that the given value passes both guards.
140
+ */
141
+ export function isBoth(a, b) {
142
+ return (value) => {
143
+ const resultA = a(value);
144
+ const resultB = b(value);
145
+ return combineTwoResultsAnd(resultA, resultB);
146
+ };
147
+ }
148
+ /**
149
+ * Confirms that the given value passes all guards.
150
+ */
151
+ export function isAll(guards) {
152
+ return (value) => {
153
+ return combineResultsAll(...guards.map((it) => it(value)));
154
+ };
155
+ }
156
+ /**
157
+ * Confirms the value passes at least one of the given Guards.
158
+ */
159
+ export function isOneOf(a, b) {
160
+ return (value) => {
161
+ const resultA = prefixErrors(a(value), "(A) ");
162
+ const resultB = prefixErrors(b(value), "(B) ");
163
+ const combinedResult = combineTwoResultsOneOf(resultA, resultB);
164
+ if (combinedResult.success === true) {
165
+ return pass(combinedResult.value);
166
+ }
167
+ return fail("Neither A nor B passed", ...combinedResult.errors);
168
+ };
169
+ }
170
+ /**
171
+ * Confirms the value is a list of items that all pass the given guard.
172
+ */
173
+ export function isListOf(guard) {
174
+ return (value) => {
175
+ if (!Array.isArray(value)) {
176
+ return fail("Not a list");
177
+ }
178
+ if (value.length === 0) {
179
+ return pass([]);
180
+ }
181
+ const results = value.map((value, index) => prefixErrors(guard(value), `[${index}] `));
182
+ if (results.every((it) => it.success)) {
183
+ return pass(value);
184
+ }
185
+ return combineResultsAll(fail("Not all items passed the Guard"), ...results.filter((it) => !it.success));
186
+ };
187
+ }
188
+ /**
189
+ * Confirms that the value is a number.
190
+ */
191
+ export const isNumber = fromGuard(Guard.isNumber, () => ["Not a number"]);
192
+ /**
193
+ * Confirms that the value is a string.
194
+ */
195
+ export const isString = (value) => fromGuard((Guard.isString), () => ["Not a string"])(value);
196
+ /**
197
+ * Confirms that the value is an exact string.
198
+ */
199
+ export const isExactString = (expectedString) => tryGuardIf((isString), Guard.isExactString(expectedString), () => [
200
+ `Not the exact string "${expectedString}"`,
201
+ ]);
202
+ /**
203
+ * Confirms that the value is a boolean.
204
+ */
205
+ export const isBool = fromGuard(Guard.isBool, () => ["Not a boolean"]);
206
+ /**
207
+ * Confirms that the value is a float.
208
+ */
209
+ export const isFloat = fromGuard(Guard.isFloat, () => [
210
+ "Not a floating point number",
211
+ ]);
212
+ /**
213
+ * Confirms that the value is an interger.
214
+ */
215
+ export const isInt = fromGuard(Guard.isInt, () => ["Not an integer"]);
216
+ /**
217
+ * Confirms that the value is null.
218
+ */
219
+ export const isNull = fromGuard(Guard.isNull, () => ["Not null"]);
220
+ /**
221
+ * Confirms that the value is undefined.
222
+ */
223
+ export const isUndefined = fromGuard(Guard.isUndefined, () => [
224
+ "Not undefined",
225
+ ]);
226
+ /**
227
+ * Confirms that the value is an object.
228
+ */
229
+ export const isObject = fromGuard(Guard.isObject, () => ["Not an object"]);
230
+ /**
231
+ * Always passes.
232
+ */
233
+ export const isAlways = fromGuard(Guard.isAlways, () => []);
234
+ /**
235
+ * Never passes.
236
+ */
237
+ export const isNever = fromGuard(Guard.isNever, () => ["Never passes"]);
238
+ /**
239
+ * Confirms that the value is a non-empty string.
240
+ */
241
+ export const isNonEmptyString = tryGuardIf(isString, (value) => value.length > 0, () => ["Is empty"]);
242
+ /**
243
+ * Confirms that the value is a string with specified length.
244
+ */
245
+ export const isStringOfLength = (length) => tryGuardIf(isString, (value) => value.length === length, (value) => [
246
+ `String length was ${value.length} instead of expected ${length}`,
247
+ ]);
248
+ /**
249
+ * Confirms the value is a string and matches the given regular expression.
250
+ */
251
+ export const isStringWithPattern = (pattern) => tryGuardIf(isString, (value) => pattern.test(value), (value) => [`String ${value} did not match pattern ${pattern.source}`]);
252
+ /**
253
+ * Confirms the value is a list with atleast one item and all items match the given guard.
254
+ */
255
+ export const isNonEmptyListOf = (guard) => tryGuardIf(isListOf(guard), (value) => value.length > 0, () => ["Not enough items"]);
256
+ /**
257
+ * Confirms the value is number between min and max inclusive.
258
+ * Meaning if the value equals min or max the guard passes.
259
+ */
260
+ export const isNumberBetweenInclusive = (min, max) => tryGuardIf(isNumber, (value) => value >= min && value <= max, (value) => [`${value} is not between ${min} and ${max} (inclusively)`]);
261
+ /**
262
+ * Confirms the value is either null or passes the given Guard.
263
+ */
264
+ export const isNullOr = (guard) => isOneOf(isNull, guard);
265
+ export const isOneStringOf = (validValues) => tryGuardIf((isString), (value) => validValues.includes(value), (value) => [`String ${value} is not one of ${validValues.join("|")}`]);
266
+ /**
267
+ * Confirms that the value is an object containing the specified key.
268
+ */
269
+ export function isObjectWithKey(key) {
270
+ return tryGuardIf(isObject, (value) => key in value, () => [`Object does not have key ${key.toString()}`]);
271
+ }
272
+ /**
273
+ * Confirms that the value is an object containing the specified key the value matching the given guard.
274
+ */
275
+ export function isObjectWithKeyMatchingGuard(key, guard) {
276
+ return (value) => {
277
+ const isObjectWithKeyResult = isObjectWithKey(key)(value);
278
+ if (isObjectWithKeyResult.success === false) {
279
+ return isObjectWithKeyResult;
280
+ }
281
+ const keyValueResult = guard(value[key]);
282
+ if (keyValueResult.success === true) {
283
+ return pass(isObjectWithKeyResult.value);
284
+ }
285
+ return prefixErrors(keyValueResult, `[${key.toString()}] `);
286
+ };
287
+ }
288
+ /**
289
+ * Confirms that the value is an object containing the specified keys.
290
+ */
291
+ export function isObjectWithKeys(keys) {
292
+ return isAll(keys.map((isObjectWithKey)));
293
+ }
294
+ /**
295
+ * Confirms that the value is an object whose key value pairs match the corresponding type guards.
296
+ */
297
+ export function isObjectWithKeysMatchingGuard(guards) {
298
+ return isAll(
299
+ // @ts-expect-error not error just a bad type system
300
+ Object.entries(guards).map(([key, guard]) => {
301
+ return isObjectWithKeyMatchingGuard(key, guard);
302
+ }));
303
+ }
304
+ /**
305
+ * Confirms the value is an object where every value matches the given guard.
306
+ */
307
+ export function isObjectWithAllKeysMatchingGuard(guard) {
308
+ return (value) => isAll(
309
+ // @ts-expect-error not error just a bad type system
310
+ Object.keys(value).map((key) => {
311
+ return isObjectWithKeyMatchingGuard(key, guard);
312
+ }))(value);
313
+ }
@@ -2,12 +2,13 @@
2
2
  * Provides types and functions to represent fetch request states
3
3
  * @module fetch-state
4
4
  */
5
- import { type Guard } from "@kaumlaut/pure/guard";
5
+ import * as Guard from "@kaumlaut/pure/guard";
6
+ import * as ErrorAwareGuard from "@kaumlaut/pure/error-aware-guard";
6
7
  /**
7
8
  * Represents a failed fetch request
8
9
  */
9
10
  export type Failed = {
10
- error: Readonly<string>;
11
+ errors: Readonly<string[]>;
11
12
  type: "Failed";
12
13
  };
13
14
  /**
@@ -52,7 +53,7 @@ export declare function isSuccess<T>(state: FetchState<T>): state is Success<T>;
52
53
  /**
53
54
  * Creates a fetch state of type Failed
54
55
  */
55
- export declare function fail(error: string): Failed;
56
+ export declare function fail(first: string, ...errors: string[]): Failed;
56
57
  /**
57
58
  * Creates a fetch state of type Loading
58
59
  */
@@ -72,7 +73,19 @@ if(isSuccess(value)){
72
73
  console.error(value.error)
73
74
  }
74
75
  */
75
- export declare function attempt<T>(guard: Guard<T>, error?: string): (data: unknown) => Success<T> | Failed;
76
+ export declare function attempt<T>(guard: Guard.Guard<T>, firstError?: string, ...errors: string[]): (data: unknown) => Success<T> | Failed;
77
+ /**
78
+ * Attempts to create a fetch state of type Success if the given guard passes.
79
+ * Otherwise creates a fetch state of type Failed with the errors from the ErrorAwareGuard.
80
+ * @example
81
+ const value = attemptErrorAware(isString)(3);
82
+ if(isSuccess(value)){
83
+ console.log(value.data)
84
+ } else if (isFailed(value)){
85
+ console.error(value.error)
86
+ }
87
+ */
88
+ export declare function attemptErrorAware<T>(guard: ErrorAwareGuard.ErrorAwareGuard<T>): (data: unknown) => Success<T> | Failed;
76
89
  /**
77
90
  * A Utility function that allows to map the Failed fetch state to any other fetch state.
78
91
  * The mapper function is only called if the given fetch state is Failed.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch-state/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACxB,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,IAAI,GAAG;IACjB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI;IACvB,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAClB,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;AACF;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,MAAM,GAAG,IAAI,GAAG,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACjE;;GAEG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,OAAO,CAEnE;AACD;;GAEG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,MAAM,CAEjE;AACD;;GAEG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC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;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,KAAK,EAAE,eAAe,CAAC,eAAe,CAAC,CAAC,CAAC,GACxC,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,CAexC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,GACvC,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAQzC"}
@@ -25,10 +25,10 @@ export function isSuccess(state) {
25
25
  /**
26
26
  * Creates a fetch state of type Failed
27
27
  */
28
- export function fail(error) {
28
+ export function fail(first, ...errors) {
29
29
  return {
30
30
  type: "Failed",
31
- error,
31
+ errors: [first, ...errors],
32
32
  };
33
33
  }
34
34
  /**
@@ -58,7 +58,7 @@ if(isSuccess(value)){
58
58
  console.error(value.error)
59
59
  }
60
60
  */
61
- export function attempt(guard, error = "Guard did not pass. Ensure the attempted data has the correct type") {
61
+ export function attempt(guard, firstError = "Guard did not pass. Ensure the attempted data has the correct type", ...errors) {
62
62
  return (data) => {
63
63
  if (guard(data)) {
64
64
  return {
@@ -68,7 +68,33 @@ export function attempt(guard, error = "Guard did not pass. Ensure the attempted
68
68
  }
69
69
  return {
70
70
  type: "Failed",
71
- error,
71
+ errors: [firstError, ...errors],
72
+ };
73
+ };
74
+ }
75
+ /**
76
+ * Attempts to create a fetch state of type Success if the given guard passes.
77
+ * Otherwise creates a fetch state of type Failed with the errors from the ErrorAwareGuard.
78
+ * @example
79
+ const value = attemptErrorAware(isString)(3);
80
+ if(isSuccess(value)){
81
+ console.log(value.data)
82
+ } else if (isFailed(value)){
83
+ console.error(value.error)
84
+ }
85
+ */
86
+ export function attemptErrorAware(guard) {
87
+ return (data) => {
88
+ const result = guard(data);
89
+ if (result.success === true) {
90
+ return {
91
+ type: "Success",
92
+ data: result.value,
93
+ };
94
+ }
95
+ return {
96
+ type: "Failed",
97
+ errors: result.errors,
72
98
  };
73
99
  };
74
100
  }
@@ -6,14 +6,26 @@ export type Guard<T> = (value: unknown) => value is T;
6
6
  * Confirms that the given value passes all guards.
7
7
  */
8
8
  export declare function isAll<T>(guards: Guard<T>[]): (value: unknown) => value is T;
9
+ /**
10
+ * Confirms that the given value passes both guards.
11
+ */
12
+ export declare function isBoth<T>(a: Guard<T>, b: Guard<T>): (value: unknown) => value is T;
9
13
  /**
10
14
  * Confirms that the value is a string.
11
15
  */
12
- export declare function isString(value: unknown): value is string;
16
+ export declare function isString<T extends string>(value: unknown): value is T;
17
+ /**
18
+ * Confirms that the value is a non-empty string.
19
+ */
20
+ export declare function isNonEmptyString(value: unknown): value is string;
13
21
  /**
14
22
  * Confirms that the value is a string with specified length.
15
23
  */
16
24
  export declare function isStringOfLength(length: number): (value: unknown) => value is string;
25
+ /**
26
+ * Confirms that the value is an object.
27
+ */
28
+ export declare function isObject(value: unknown): value is object;
17
29
  /**
18
30
  * Confirms that the value is a number.
19
31
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/guard/index.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAAC;AAEtD;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAE3E;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,GACb,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI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AAI,CAAC,CAGhC;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EACxC,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,GACV,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,CAGhC"}
@@ -10,18 +10,36 @@ import { isOk } from "@kaumlaut/pure/result";
10
10
  export function isAll(guards) {
11
11
  return (value) => guards.every((guard) => guard(value));
12
12
  }
13
+ /**
14
+ * Confirms that the given value passes both guards.
15
+ */
16
+ export function isBoth(a, b) {
17
+ return (value) => a(value) && b(value);
18
+ }
13
19
  /**
14
20
  * Confirms that the value is a string.
15
21
  */
16
22
  export function isString(value) {
17
23
  return typeof value === "string";
18
24
  }
25
+ /**
26
+ * Confirms that the value is a non-empty string.
27
+ */
28
+ export function isNonEmptyString(value) {
29
+ return typeof value === "string" && value.length > 0;
30
+ }
19
31
  /**
20
32
  * Confirms that the value is a string with specified length.
21
33
  */
22
34
  export function isStringOfLength(length) {
23
35
  return (value) => isString(value) && value.length === length;
24
36
  }
37
+ /**
38
+ * Confirms that the value is an object.
39
+ */
40
+ export function isObject(value) {
41
+ return typeof value === "object";
42
+ }
25
43
  /**
26
44
  * Confirms that the value is a number.
27
45
  */
@@ -0,0 +1,2 @@
1
+ export * as Guard from "./guard";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * as Guard from "./guard";
@@ -3,7 +3,8 @@
3
3
  * @module maybe
4
4
  */
5
5
  import { Result } from "@kaumlaut/pure/result";
6
- import { Guard } from "../guard";
6
+ import * as ErrorAwareGuard from "@kaumlaut/pure/error-aware-guard";
7
+ import * as Guard from "@kaumlaut/pure/guard";
7
8
  /**
8
9
  * Represents a Maybe containing a value.
9
10
  * @example
@@ -74,7 +75,11 @@ export declare function toResult<T, E>(error: E): (maybe: Maybe<T>) => Result<T,
74
75
  /**
75
76
  * Creates a Just if the Guard passes for the given value. Otherwise Creates a Nothing.
76
77
  */
77
- export declare function maybeByGuard<T>(guard: Guard<T>): (value: unknown) => Maybe<T>;
78
+ export declare function maybeByGuard<T>(guard: Guard.Guard<T>): (value: unknown) => Maybe<T>;
79
+ /**
80
+ * Creates a Just if the ErrorAwareGuard passes for the given value. Otherwise Creates a Nothing.
81
+ */
82
+ export declare function maybeByErrorAwareGuard<T>(guard: ErrorAwareGuard.ErrorAwareGuard<T>): (value: unknown) => Maybe<T>;
78
83
  /**
79
84
  * Maps the a value contained within a Just using the given function without wrapping it in another Just.
80
85
  * Returns the given Maybe if it is a Nothing.
@@ -85,4 +90,17 @@ export declare function mapToMaybe<T, R>(func: (value: T) => Maybe<R>): (maybe:
85
90
  * Returns the given Maybe if it is a Nothing.
86
91
  */
87
92
  export declare function tryMap<T, R>(func: (value: T) => R): (maybe: Maybe<T>) => Maybe<R>;
93
+ /**
94
+ * Returns true if both values are either Just with matching predicate return values
95
+ * Or if both values are Nothing
96
+ */
97
+ export declare function eqBy<T, T2>(by: (value: T) => T2): (a: Maybe<T>, b: Maybe<T>) => boolean;
98
+ /**
99
+ * Returns true if both values are Nothing
100
+ */
101
+ export declare function bothNothing<T>(a: Maybe<T>, b: Maybe<T>): boolean;
102
+ /**
103
+ * Creates a Maybe<T> from a list and an index. Just if the key exists, Nothing if it doesn't
104
+ */
105
+ export declare function fromListAndIndex<T>(index: number, list: T[]): Maybe<T>;
88
106
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/maybe/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAW,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC;;;;;;;;;GASG;AACH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AAC,CAAC,CAAC,GAAG,OAAO,CAEhE;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAEtE"}
@@ -91,6 +91,18 @@ export function maybeByGuard(guard) {
91
91
  return guard(value) ? just(value) : nothing();
92
92
  };
93
93
  }
94
+ /**
95
+ * Creates a Just if the ErrorAwareGuard passes for the given value. Otherwise Creates a Nothing.
96
+ */
97
+ export function maybeByErrorAwareGuard(guard) {
98
+ return (value) => {
99
+ const result = guard(value);
100
+ if (result.success === true) {
101
+ return just(result.value);
102
+ }
103
+ return nothing();
104
+ };
105
+ }
94
106
  /**
95
107
  * Maps the a value contained within a Just using the given function without wrapping it in another Just.
96
108
  * Returns the given Maybe if it is a Nothing.
@@ -121,3 +133,27 @@ export function tryMap(func) {
121
133
  return maybe;
122
134
  };
123
135
  }
136
+ /**
137
+ * Returns true if both values are either Just with matching predicate return values
138
+ * Or if both values are Nothing
139
+ */
140
+ export function eqBy(by) {
141
+ return (a, b) => {
142
+ if (isJust(a) && isJust(b)) {
143
+ return by(a.value) === by(b.value);
144
+ }
145
+ return isNothing(a) && isNothing(b);
146
+ };
147
+ }
148
+ /**
149
+ * Returns true if both values are Nothing
150
+ */
151
+ export function bothNothing(a, b) {
152
+ return isNothing(a) && isNothing(b);
153
+ }
154
+ /**
155
+ * Creates a Maybe<T> from a list and an index. Just if the key exists, Nothing if it doesn't
156
+ */
157
+ export function fromListAndIndex(index, list) {
158
+ return index in list ? just(list[index]) : nothing();
159
+ }
@@ -1,4 +1,4 @@
1
- import { Guard } from "@kaumlaut/pure/guard";
1
+ import * as Guard from "@kaumlaut/pure/guard";
2
2
  import { ReceiveEffectResult, Effect } from "@kaumlaut/pure/runtime/effect";
3
3
  import { Message } from "@kaumlaut/pure/runtime";
4
4
  export type HttpError = {
@@ -13,5 +13,5 @@ export declare function isNotFound(error: FetchError): boolean;
13
13
  export declare function isHttp(error: FetchError): error is HttpError;
14
14
  export declare function isClient(error: FetchError): boolean;
15
15
  export declare function isServer(error: FetchError): boolean;
16
- export declare function fetchJson<T, M extends Message>(url: string, headers: [string, string][], method: string, guard: Guard<T>, receiver: ReceiveEffectResult<T, FetchError, M>): Effect<M>;
16
+ export declare function fetchJson<T, M extends Message>(url: string, headers: [string, string][], method: string, guard: Guard.Guard<T>, receiver: ReceiveEffectResult<T, FetchError, M>): Effect<M>;
17
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/runtime/effect/fetch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE7C,OAAO,EACL,mBAAmB,EACnB,MAAM,EAEP,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEjD,MAAM,MAAM,SAAS,GAAG;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,SAAS,GAAG,qBAAqB,CAAC;AAE3D,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAErD;AAED,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,KAAK,IAAI,SAAS,CAK5D;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAEnD;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAEnD;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,OAAO,EAC5C,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EAC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"}
@@ -1,6 +1,6 @@
1
1
  import { Effect } from "@kaumlaut/pure/runtime/effect";
2
- import { Cloneable } from "../clone";
3
- import { Maybe } from "../maybe";
2
+ import { Cloneable } from "@kaumlaut/pure/clone";
3
+ import { Maybe } from "@kaumlaut/pure/maybe";
4
4
  export type Message<N extends string = string, T = any> = {
5
5
  value: T;
6
6
  name: N;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,+BAA+B,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAGjC,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,GAAG,IAAI;IACxD,KAAK,EAAE,CAAC,CAAC;IACT,IAAI,EAAE,CAAC,CAAC;CACT,CAAC;AAEF,MAAM,MAAM,MAAM,CAAC,KAAK,SAAS,SAAS,EAAE,aAAa,SAAS,OAAO,IAAI,CAC3E,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,EACtB,GAAG,EAAE,aAAa,KACf,CAAC,KAAK,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;AAEpC,MAAM,MAAM,IAAI,CAAC,KAAK,SAAS,SAAS,EAAE,aAAa,SAAS,OAAO,IAAI,CACzE,cAAc,EAAE,KAAK,CAAC,KAAK,CAAC,KACzB,CAAC,KAAK,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;AAEpC,MAAM,MAAM,MAAM,CAAC,aAAa,SAAS,OAAO,EAAE,KAAK,IAAI;IACzD,aAAa,GAAG,IAAI;IACpB,KAAK,EAAE,KAAK;IACZ,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;KACrB,GAAG,IAAI;CACT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AC;QACb,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;KACrB,GAAG,IAAI;CACT,CAAC;AAEF,MAAM,MAAM,OAAO,CACjB,aAAa,SAAS,OAAO,EAC7B,KAAK,SAAS,SAAS,IACrB,MAAM,CAAC,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC;AAEnC,wBAAgB,EAAE,CAAC,aAAa,SAAS,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EACnE,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,OAAO,GACf,OAAO,IAAI,aAAa,CAE1B"}
@@ -1,9 +1,9 @@
1
1
  import { Init, Message } from "@kaumlaut/pure/runtime";
2
- import { Guard } from "@kaumlaut/pure/guard";
2
+ import * as Guard from "@kaumlaut/pure/guard";
3
3
  import { Result } from "@kaumlaut/pure/result";
4
4
  import { Cloneable } from "@kaumlaut/pure/clone";
5
5
  export type Persistence<Model extends Cloneable, CustomMessage extends Message> = {
6
6
  persist(model: Model): void;
7
- read(guard: Guard<Model>, init: Init<Model, CustomMessage>): Result<Model, string>;
7
+ read(guard: Guard.Guard<Model>, init: Init<Model, CustomMessage>): Result<Model, string>;
8
8
  };
9
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/runtime/persistence/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,MAAM,MAAM,WAAW,CACrB,KAAK,SAAS,SAAS,EACvB,aAAa,SAAS,OAAO,IAC3B;IACF,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAC5B,IAAI,CACF,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,EACnB,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,GAC/B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;CAC1B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/runtime/persistence/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,KAAK,KAAK,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,MAAM,MAAM,WAAW,CACrB,KAAK,SAAS,SAAS,EACvB,aAAa,SAAS,OAAO,IAC3B;IACF,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAC5B,IAAI,CACF,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,EACzB,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,GAC/B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;CAC1B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaumlaut/pure",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "Types and functions for a state runtime inspired by Elm and functional programming",
5
5
  "author": "Max Kaemmerer",
6
6
  "license": "MIT",
@@ -27,7 +27,7 @@
27
27
  "docs": "typedoc",
28
28
  "lint": "eslint -c eslint.config.js --fix",
29
29
  "format": "prettier . --write",
30
- "test": "vitest",
30
+ "test": "npm run build && vitest",
31
31
  "release-minor": "npm run docs && npm run build && git add ./docs && git commit -m 'update docs' && npm version minor && git push --tags && npm publish --access public",
32
32
  "release-patch": "npm run docs && npm run build && git add ./docs && git commit -m 'update docs' && npm version patch && git push --tags && npm publish --access public",
33
33
  "release-major": "npm run docs && npm run build && git add ./docs && git commit -m 'update docs' && npm version major && git push --tags && npm publish --access public"
@@ -42,9 +42,7 @@
42
42
  "typedoc-plugin-markdown": "^4.8.1",
43
43
  "typescript": "^5.8.2",
44
44
  "typescript-eslint": "^8.26.1",
45
- "vitest": "^3.0.8"
46
- },
47
- "dependencies": {
45
+ "vitest": "^3.0.8",
48
46
  "vite-tsconfig-paths": "^5.1.4"
49
47
  }
50
48
  }