@tempots/std 0.24.0 → 0.25.1

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.
@@ -0,0 +1,486 @@
1
+ import { AsyncResult as n } from "./async-result.js";
2
+ const i = {
3
+ /**
4
+ * Creates a valid `Validation`.
5
+ * @returns A `Validation` that is `Valid`.
6
+ */
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ valid: { type: "valid" },
9
+ /**
10
+ * Creates an invalid `Validation`.
11
+ * @param error - The error associated with the invalid value.
12
+ * @returns A `Validation` that is `Invalid`.
13
+ */
14
+ invalid(e) {
15
+ return { type: "invalid", error: e };
16
+ },
17
+ /**
18
+ * Checks if a `Validation` is `Valid`.
19
+ * @param r - The `Validation` to check.
20
+ * @returns `true` if the `Validation` is `Valid`, otherwise `false`.
21
+ */
22
+ isValid(e) {
23
+ return e.type === "valid";
24
+ },
25
+ /**
26
+ * Checks if a `Validation` is `Invalid`.
27
+ * @param r - The `Validation` to check.
28
+ * @returns `true` if the `Validation` is `Invalid`, otherwise `false`.
29
+ */
30
+ isInvalid(e) {
31
+ return e.type === "invalid";
32
+ },
33
+ /**
34
+ * Maps the value of a `Validation` to a new value.
35
+ * @param r - The `Validation` to map.
36
+ * @param valid - The mapping function for a valid value.
37
+ * @param invalid - The mapping function for an invalid value.
38
+ * @returns The mapped value.
39
+ */
40
+ match: (e, r, u) => i.isValid(e) ? r() : u(e.error),
41
+ /**
42
+ * Maps the value of a `Validation` to a new `Validation`.
43
+ * @param validation - The `Validation` to map.
44
+ * @param value - The value to map.
45
+ * @returns A new `Validation` with the mapped value.
46
+ */
47
+ toResult: (e, r) => i.match(
48
+ e,
49
+ () => s.success(r),
50
+ (u) => s.failure(u)
51
+ ),
52
+ /**
53
+ * Executes side effects based on the state of the validation.
54
+ * Unlike `match`, all handlers are optional, allowing you to react only to specific states.
55
+ * @param v - The validation.
56
+ * @param handlers - An object with optional handlers for each state.
57
+ * @returns The validation that was passed in, allowing for chaining.
58
+ * @public
59
+ */
60
+ effect: (e, r) => (e.type === "valid" ? r.valid?.() : r.invalid?.(e.error), e),
61
+ /**
62
+ * Execute a function when the `Validation` is valid.
63
+ *
64
+ * @param r - The `Validation` to check.
65
+ * @param apply - The function to execute when the `Validation` is valid.
66
+ * @returns The `Validation` object.
67
+ */
68
+ whenValid: (e, r) => (i.isValid(e) && r(), e),
69
+ /**
70
+ * Execute a function when the `Validation` is invalid.
71
+ *
72
+ * @param r - The `Validation` to check.
73
+ * @param apply - The function to execute when the `Validation` is invalid.
74
+ * @returns The `Validation` object.
75
+ */
76
+ whenInvalid: (e, r) => (i.isInvalid(e) && r(e.error), e),
77
+ /**
78
+ * Maps the error of an invalid `Validation` to a new error using the provided function.
79
+ * For valid validations, the validation is preserved unchanged.
80
+ * @param v - The `Validation` to map the error of.
81
+ * @param f - The mapping function to apply to the error.
82
+ * @returns A new `Validation` with the mapped error if invalid, otherwise the original valid.
83
+ * @public
84
+ */
85
+ mapError: (e, r) => e.type === "invalid" ? i.invalid(r(e.error)) : e,
86
+ /**
87
+ * Maps the error of an invalid `Validation` to a new `Validation` using the provided function.
88
+ * This allows recovery from errors by returning a valid validation.
89
+ * @param v - The `Validation` to recover from.
90
+ * @param f - The recovery function that returns a new `Validation`.
91
+ * @returns The result of the recovery function if invalid, otherwise the original valid.
92
+ * @public
93
+ */
94
+ flatMapError: (e, r) => e.type === "invalid" ? r(e.error) : e,
95
+ /**
96
+ * Combines two validations. Both must be valid for the result to be valid.
97
+ * If both are invalid, errors are combined using the provided function.
98
+ * @param v1 - The first validation.
99
+ * @param v2 - The second validation.
100
+ * @param combineErrors - The function to combine two errors.
101
+ * @returns A combined validation.
102
+ * @public
103
+ */
104
+ combine: (e, r, u) => i.isValid(e) && i.isValid(r) ? i.valid : i.isInvalid(e) && i.isInvalid(r) ? i.invalid(u(e.error, r.error)) : i.isInvalid(e) ? e : r,
105
+ /**
106
+ * Combines multiple validations into a single validation.
107
+ * All must be valid for the result to be valid.
108
+ * Returns the first invalid validation if any.
109
+ * @param validations - The validations to combine.
110
+ * @returns A single validation that is valid only if all inputs are valid.
111
+ * @public
112
+ */
113
+ all: (e) => {
114
+ for (const r of e)
115
+ if (i.isInvalid(r))
116
+ return r;
117
+ return i.valid;
118
+ },
119
+ /**
120
+ * Combines multiple validations, accumulating all errors.
121
+ * All must be valid for the result to be valid.
122
+ * If any are invalid, all errors are collected into an array.
123
+ * @param validations - The validations to combine.
124
+ * @returns A validation that is valid only if all inputs are valid, otherwise contains all errors.
125
+ * @public
126
+ */
127
+ allErrors: (e) => {
128
+ const r = [];
129
+ for (const u of e)
130
+ i.isInvalid(u) && r.push(u.error);
131
+ return r.length > 0 ? i.invalid(r) : i.valid;
132
+ },
133
+ /**
134
+ * Compares two validations for equality.
135
+ * @param v1 - The first validation.
136
+ * @param v2 - The second validation.
137
+ * @param errorEquals - Optional custom equality function for errors. Defaults to strict equality.
138
+ * @returns `true` if the validations are equal, `false` otherwise.
139
+ * @public
140
+ */
141
+ equals: (e, r, u = (t, l) => t === l) => e.type === "valid" && r.type === "valid" ? !0 : e.type === "invalid" && r.type === "invalid" ? u(e.error, r.error) : !1,
142
+ /**
143
+ * Recovers from an invalid validation by returning a valid validation.
144
+ * @param v - The `Validation` to recover from.
145
+ * @returns A valid validation regardless of the input.
146
+ * @public
147
+ */
148
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
149
+ recover: (e) => i.valid,
150
+ /**
151
+ * Gets the error if the validation is invalid, otherwise returns undefined.
152
+ * @param v - The validation to get the error from.
153
+ * @returns The error if invalid, otherwise undefined.
154
+ * @public
155
+ */
156
+ getError: (e) => {
157
+ if (i.isInvalid(e))
158
+ return e.error;
159
+ },
160
+ /**
161
+ * Gets the error if the validation is invalid, otherwise returns the default value.
162
+ * @param v - The validation to get the error from.
163
+ * @param defaultError - The default error to return if valid.
164
+ * @returns The error if invalid, otherwise the default error.
165
+ * @public
166
+ */
167
+ getErrorOrElse: (e, r) => i.isInvalid(e) ? e.error : r
168
+ }, s = {
169
+ /**
170
+ * Creates a successful `Result`.
171
+ * @param value - The value to wrap in a `Success` type.
172
+ * @returns A `Result` that is a `Success`.
173
+ * @public
174
+ */
175
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
176
+ success(e) {
177
+ return { type: "Success", value: e };
178
+ },
179
+ /**
180
+ * Creates a failure `Result`.
181
+ * @param error - The error to wrap in a `Failure` type.
182
+ * @returns A `Result` that is a `Failure`.
183
+ * @public
184
+ */
185
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
186
+ failure(e) {
187
+ return { type: "Failure", error: e };
188
+ },
189
+ /**
190
+ * Maps the value of a `Result` to a new value.
191
+ * @param r - The `Result` to map.
192
+ * @param f - The mapping function.
193
+ * @returns A new `Result` with the mapped value.
194
+ * @public
195
+ */
196
+ map: (e, r) => e.type === "Success" ? s.success(r(e.value)) : e,
197
+ /**
198
+ * Maps the value of a `Result` to a new `Result`.
199
+ * @param r - The `Result` to map.
200
+ * @param f - The mapping function.
201
+ * @returns A new `Result` with the mapped value.
202
+ * @public
203
+ */
204
+ flatMap: (e, r) => e.type === "Success" ? r(e.value) : e,
205
+ /**
206
+ * Converts a `Result` to an `AsyncResult`.
207
+ * @param r - The `Result` to convert.
208
+ * @returns An `AsyncResult` that is equivalent to the input `Result`.
209
+ * @public
210
+ */
211
+ toAsync(e) {
212
+ return s.match(
213
+ e,
214
+ (r) => n.success(r),
215
+ (r) => n.failure(r)
216
+ );
217
+ },
218
+ /**
219
+ * Converts a `Result` to a `Validation`.
220
+ * @param r - The `Result` to convert.
221
+ * @returns A `Validation` that is equivalent to the input `Result`.
222
+ * @public
223
+ */
224
+ toValidation(e) {
225
+ return s.match(
226
+ e,
227
+ () => i.valid,
228
+ (r) => i.invalid(r)
229
+ );
230
+ },
231
+ /**
232
+ * Checks if a `Result` is a success.
233
+ * @param r - The `Result` to check.
234
+ * @returns `true` if the `Result` is a `Success`, `false` otherwise.
235
+ * @public
236
+ */
237
+ isSuccess(e) {
238
+ return e.type === "Success";
239
+ },
240
+ /**
241
+ * Checks if a `Result` is a failure.
242
+ * @param r - The `Result` to check.
243
+ * @returns `true` if the `Result` is a `Failure`, `false` otherwise.
244
+ * @public
245
+ */
246
+ isFailure(e) {
247
+ return e.type === "Failure";
248
+ },
249
+ /**
250
+ * Gets the value of a `Result` if it is a `Success`, otherwise returns the provided default value.
251
+ * @param r - The `Result` to get the value from.
252
+ * @param alt - The default value to return if the `Result` is a `Failure`.
253
+ * @returns The value of the `Result` if it is a `Success`, otherwise the default value.
254
+ * @public
255
+ */
256
+ getOrElse(e, r) {
257
+ return s.isSuccess(e) ? e.value : r;
258
+ },
259
+ /**
260
+ * Gets the value of a `Result` if it is a `Success`, otherwise returns the result of the provided function.
261
+ * @param r - The `Result` to get the value from.
262
+ * @param altf - The function to call if the `Result` is a `Failure`.
263
+ * @returns The value of the `Result` if it is a `Success`, otherwise the result of the function.
264
+ * @public
265
+ */
266
+ getOrElseLazy(e, r) {
267
+ return s.isSuccess(e) ? e.value : r();
268
+ },
269
+ /**
270
+ * Gets the value of a `Result` if it is a `Success`, otherwise returns `null`.
271
+ * @param r - The `Result` to get the value from.
272
+ * @returns The value of the `Result` if it is a `Success`, otherwise `null`.
273
+ * @public
274
+ */
275
+ getOrNull(e) {
276
+ return s.isSuccess(e) ? e.value : null;
277
+ },
278
+ /**
279
+ * Gets the value of a `Result` if it is a `Success`, otherwise returns `undefined`.
280
+ * @param r - The `Result` to get the value from.
281
+ * @returns The value of the `Result` if it is a `Success`, otherwise `undefined`.
282
+ * @public
283
+ */
284
+ getOrUndefined(e) {
285
+ return s.isSuccess(e) ? e.value : void 0;
286
+ },
287
+ /**
288
+ * Gets the value of a `Result` if it is a `Success`, otherwise it throws the error contained in the `Failure`.
289
+ * @param r - The `Result` to get the value from.
290
+ * @returns The value of the `Result` if it is a `Success`.
291
+ */
292
+ getUnsafe: (e) => {
293
+ if (s.isSuccess(e))
294
+ return e.value;
295
+ throw e.error;
296
+ },
297
+ /**
298
+ * Based on the state of the result, it picks the appropriate function to call and returns the result.
299
+ * @param success - The function to call if the result is a success.
300
+ * @param failure - The function to call if the result is a failure.
301
+ * @returns The result of calling the appropriate function based on the state of the result.
302
+ * @public
303
+ */
304
+ match: (e, r, u) => s.isSuccess(e) ? r(e.value) : u(e.error),
305
+ /**
306
+ * Executes side effects based on the state of the result.
307
+ * Unlike `match`, all handlers are optional, allowing you to react only to specific states.
308
+ * @param r - The result.
309
+ * @param handlers - An object with optional handlers for each state.
310
+ * @returns The result that was passed in, allowing for chaining.
311
+ * @public
312
+ */
313
+ effect: (e, r) => (e.type === "Success" ? r.success?.(e.value) : r.failure?.(e.error), e),
314
+ /**
315
+ * Calls the provided function if the result is a success.
316
+ * @param apply - The function to call if the result is a success.
317
+ * @returns A function that takes a `Result` and calls the provided function if the result is a success.
318
+ * @public
319
+ */
320
+ whenSuccess: (e, r) => (s.isSuccess(e) && r(e.value), e),
321
+ /**
322
+ * Calls the provided function if the result is a failure.
323
+ * @param apply - The function to call if the result is a failure.
324
+ * @returns The result that was passed in.
325
+ * @public
326
+ */
327
+ whenFailure: (e, r) => (s.isFailure(e) && r(e.error), e),
328
+ /**
329
+ * Combines two results into a single result.
330
+ * @param r1 - The first result.
331
+ * @param r2 - The second result.
332
+ * @param combineV - The function to combine two values.
333
+ * @param combineE - The function to combine two errors.
334
+ * @returns The combined result.
335
+ * @public
336
+ */
337
+ combine: (e, r, u, t) => s.match(
338
+ e,
339
+ (l) => s.match(
340
+ r,
341
+ (a) => s.success(u(l, a)),
342
+ (a) => s.failure(a)
343
+ ),
344
+ (l) => s.match(
345
+ r,
346
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
347
+ (a) => s.failure(l),
348
+ (a) => s.failure(t(l, a))
349
+ )
350
+ ),
351
+ /**
352
+ * Compares two results for equality.
353
+ * @param r1 - The first result.
354
+ * @param r2 - The second result.
355
+ * @param options - The options to use for comparison. By default, uses strict equality.
356
+ * @returns `true` if the results are equal, `false` otherwise.
357
+ */
358
+ equals: (e, r, u = {
359
+ valueEquals: (t, l) => t === l,
360
+ errorEquals: (t, l) => t === l
361
+ }) => e.type === "Success" && r.type === "Success" ? u.valueEquals(e.value, r.value) : e.type === "Failure" && r.type === "Failure" ? u.errorEquals(e.error, r.error) : !1,
362
+ /**
363
+ * Combines multiple results into a single result.
364
+ * @param results - The results to combine.
365
+ * @returns A single result that is a success if all the input results are successes, otherwise a failure.
366
+ */
367
+ all: (e) => {
368
+ const r = [];
369
+ for (const u of e)
370
+ if (s.isSuccess(u))
371
+ r.push(u.value);
372
+ else
373
+ return u;
374
+ return s.success(r);
375
+ },
376
+ /**
377
+ * Maps the error of a failed `Result` to a new error using the provided function.
378
+ * For success results, the result is preserved unchanged.
379
+ * @param r - The `Result` to map the error of.
380
+ * @param f - The mapping function to apply to the error.
381
+ * @returns A new `Result` with the mapped error if failed, otherwise the original success.
382
+ * @public
383
+ */
384
+ mapError: (e, r) => e.type === "Failure" ? s.failure(r(e.error)) : e,
385
+ /**
386
+ * Maps the error of a failed `Result` to a new `Result` using the provided function.
387
+ * This allows recovery from errors by returning a new successful result.
388
+ * @param r - The `Result` to recover from.
389
+ * @param f - The recovery function that returns a new `Result`.
390
+ * @returns The result of the recovery function if failed, otherwise the original success.
391
+ * @public
392
+ */
393
+ flatMapError: (e, r) => e.type === "Failure" ? r(e.error) : e,
394
+ /**
395
+ * Recovers from a failure by providing an alternative value.
396
+ * @param r - The `Result` to recover from.
397
+ * @param f - The function that provides an alternative value given the error.
398
+ * @returns A successful `Result` with the alternative value if failed, otherwise the original success.
399
+ * @public
400
+ */
401
+ recover: (e, r) => e.type === "Failure" ? { type: "Success", value: r(e.error) } : e,
402
+ /**
403
+ * Applies a function wrapped in a `Result` to a value wrapped in a `Result`.
404
+ * Useful for applying multiple arguments to a function in a safe way.
405
+ * @param resultFn - The `Result` containing the function.
406
+ * @param resultVal - The `Result` containing the value.
407
+ * @returns A new `Result` with the result of applying the function to the value.
408
+ * @public
409
+ */
410
+ ap: (e, r) => s.isSuccess(e) && s.isSuccess(r) ? s.success(e.value(r.value)) : s.isFailure(e) ? s.failure(e.error) : s.failure(r.error),
411
+ /**
412
+ * Maps two `Result` values using a function.
413
+ * @param r1 - The first `Result`.
414
+ * @param r2 - The second `Result`.
415
+ * @param f - The function to apply to both values.
416
+ * @returns A new `Result` with the result of applying the function to both values.
417
+ * @public
418
+ */
419
+ map2: (e, r, u) => s.isSuccess(e) && s.isSuccess(r) ? s.success(u(e.value, r.value)) : s.isFailure(e) ? s.failure(e.error) : s.failure(r.error),
420
+ /**
421
+ * Maps three `Result` values using a function.
422
+ * @param r1 - The first `Result`.
423
+ * @param r2 - The second `Result`.
424
+ * @param r3 - The third `Result`.
425
+ * @param f - The function to apply to all three values.
426
+ * @returns A new `Result` with the result of applying the function to all three values.
427
+ * @public
428
+ */
429
+ map3: (e, r, u, t) => s.isSuccess(e) && s.isSuccess(r) && s.isSuccess(u) ? s.success(t(e.value, r.value, u.value)) : s.isFailure(e) ? s.failure(e.error) : s.isFailure(r) ? s.failure(r.error) : s.failure(u.error),
430
+ /**
431
+ * Converts a Promise to a Result.
432
+ * @param p - The Promise to convert.
433
+ * @returns A Promise that resolves to a Result.
434
+ * @public
435
+ */
436
+ ofPromise: async (e) => {
437
+ try {
438
+ const r = await e;
439
+ return s.success(r);
440
+ } catch (r) {
441
+ return s.failure(r instanceof Error ? r : new Error(String(r)));
442
+ }
443
+ },
444
+ /**
445
+ * Swaps the success and failure values of a Result.
446
+ * A success becomes a failure with the value as the error,
447
+ * and a failure becomes a success with the error as the value.
448
+ * @param r - The Result to swap.
449
+ * @returns A new Result with swapped success and failure.
450
+ * @public
451
+ */
452
+ swap: (e) => s.isSuccess(e) ? s.failure(e.value) : s.success(e.error),
453
+ /**
454
+ * Converts a nullable value to a Result.
455
+ * @param value - The nullable value.
456
+ * @param error - The error to use if the value is null or undefined.
457
+ * @returns A Result containing the value if not null/undefined, otherwise a failure.
458
+ * @public
459
+ */
460
+ fromNullable: (e, r) => e == null ? s.failure(r) : s.success(e),
461
+ /**
462
+ * Converts a nullable value to a Result using a lazy error function.
463
+ * @param value - The nullable value.
464
+ * @param errorFn - The function to call to get the error if the value is null or undefined.
465
+ * @returns A Result containing the value if not null/undefined, otherwise a failure.
466
+ * @public
467
+ */
468
+ fromNullableLazy: (e, r) => e == null ? s.failure(r()) : s.success(e),
469
+ /**
470
+ * Wraps a function that may throw into a function that returns a Result.
471
+ * @param f - The function that may throw.
472
+ * @returns A function that returns a Result instead of throwing.
473
+ * @public
474
+ */
475
+ tryCatch: (e) => {
476
+ try {
477
+ return s.success(e());
478
+ } catch (r) {
479
+ return s.failure(r instanceof Error ? r : new Error(String(r)));
480
+ }
481
+ }
482
+ };
483
+ export {
484
+ s as R,
485
+ i as V
486
+ };
@@ -0,0 +1 @@
1
+ "use strict";const n=require("./async-result.cjs"),i={valid:{type:"valid"},invalid(e){return{type:"invalid",error:e}},isValid(e){return e.type==="valid"},isInvalid(e){return e.type==="invalid"},match:(e,r,u)=>i.isValid(e)?r():u(e.error),toResult:(e,r)=>i.match(e,()=>s.success(r),u=>s.failure(u)),effect:(e,r)=>(e.type==="valid"?r.valid?.():r.invalid?.(e.error),e),whenValid:(e,r)=>(i.isValid(e)&&r(),e),whenInvalid:(e,r)=>(i.isInvalid(e)&&r(e.error),e),mapError:(e,r)=>e.type==="invalid"?i.invalid(r(e.error)):e,flatMapError:(e,r)=>e.type==="invalid"?r(e.error):e,combine:(e,r,u)=>i.isValid(e)&&i.isValid(r)?i.valid:i.isInvalid(e)&&i.isInvalid(r)?i.invalid(u(e.error,r.error)):i.isInvalid(e)?e:r,all:e=>{for(const r of e)if(i.isInvalid(r))return r;return i.valid},allErrors:e=>{const r=[];for(const u of e)i.isInvalid(u)&&r.push(u.error);return r.length>0?i.invalid(r):i.valid},equals:(e,r,u=(t,l)=>t===l)=>e.type==="valid"&&r.type==="valid"?!0:e.type==="invalid"&&r.type==="invalid"?u(e.error,r.error):!1,recover:e=>i.valid,getError:e=>{if(i.isInvalid(e))return e.error},getErrorOrElse:(e,r)=>i.isInvalid(e)?e.error:r},s={success(e){return{type:"Success",value:e}},failure(e){return{type:"Failure",error:e}},map:(e,r)=>e.type==="Success"?s.success(r(e.value)):e,flatMap:(e,r)=>e.type==="Success"?r(e.value):e,toAsync(e){return s.match(e,r=>n.AsyncResult.success(r),r=>n.AsyncResult.failure(r))},toValidation(e){return s.match(e,()=>i.valid,r=>i.invalid(r))},isSuccess(e){return e.type==="Success"},isFailure(e){return e.type==="Failure"},getOrElse(e,r){return s.isSuccess(e)?e.value:r},getOrElseLazy(e,r){return s.isSuccess(e)?e.value:r()},getOrNull(e){return s.isSuccess(e)?e.value:null},getOrUndefined(e){return s.isSuccess(e)?e.value:void 0},getUnsafe:e=>{if(s.isSuccess(e))return e.value;throw e.error},match:(e,r,u)=>s.isSuccess(e)?r(e.value):u(e.error),effect:(e,r)=>(e.type==="Success"?r.success?.(e.value):r.failure?.(e.error),e),whenSuccess:(e,r)=>(s.isSuccess(e)&&r(e.value),e),whenFailure:(e,r)=>(s.isFailure(e)&&r(e.error),e),combine:(e,r,u,t)=>s.match(e,l=>s.match(r,a=>s.success(u(l,a)),a=>s.failure(a)),l=>s.match(r,a=>s.failure(l),a=>s.failure(t(l,a)))),equals:(e,r,u={valueEquals:(t,l)=>t===l,errorEquals:(t,l)=>t===l})=>e.type==="Success"&&r.type==="Success"?u.valueEquals(e.value,r.value):e.type==="Failure"&&r.type==="Failure"?u.errorEquals(e.error,r.error):!1,all:e=>{const r=[];for(const u of e)if(s.isSuccess(u))r.push(u.value);else return u;return s.success(r)},mapError:(e,r)=>e.type==="Failure"?s.failure(r(e.error)):e,flatMapError:(e,r)=>e.type==="Failure"?r(e.error):e,recover:(e,r)=>e.type==="Failure"?{type:"Success",value:r(e.error)}:e,ap:(e,r)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(e.value(r.value)):s.isFailure(e)?s.failure(e.error):s.failure(r.error),map2:(e,r,u)=>s.isSuccess(e)&&s.isSuccess(r)?s.success(u(e.value,r.value)):s.isFailure(e)?s.failure(e.error):s.failure(r.error),map3:(e,r,u,t)=>s.isSuccess(e)&&s.isSuccess(r)&&s.isSuccess(u)?s.success(t(e.value,r.value,u.value)):s.isFailure(e)?s.failure(e.error):s.isFailure(r)?s.failure(r.error):s.failure(u.error),ofPromise:async e=>{try{const r=await e;return s.success(r)}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}},swap:e=>s.isSuccess(e)?s.failure(e.value):s.success(e.error),fromNullable:(e,r)=>e==null?s.failure(r):s.success(e),fromNullableLazy:(e,r)=>e==null?s.failure(r()):s.success(e),tryCatch:e=>{try{return s.success(e())}catch(r){return s.failure(r instanceof Error?r:new Error(String(r)))}}};exports.Result=s;exports.Validation=i;
package/result.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./async-result.cjs");const e=require("./result-CdwVhaAc.cjs");exports.Result=e.Result;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./async-result.cjs");const e=require("./result-bSKKrSLB.cjs");exports.Result=e.Result;
package/result.d.ts CHANGED
@@ -140,6 +140,18 @@ export declare const Result: {
140
140
  * @public
141
141
  */
142
142
  match: <V1, V2, E>(r: Result<V1, E>, success: (value: V1) => V2, failure: (error: E) => V2) => V2;
143
+ /**
144
+ * Executes side effects based on the state of the result.
145
+ * Unlike `match`, all handlers are optional, allowing you to react only to specific states.
146
+ * @param r - The result.
147
+ * @param handlers - An object with optional handlers for each state.
148
+ * @returns The result that was passed in, allowing for chaining.
149
+ * @public
150
+ */
151
+ effect: <V, E>(r: Result<V, E>, handlers: {
152
+ success?: (value: V) => void;
153
+ failure?: (error: E) => void;
154
+ }) => Result<V, E>;
143
155
  /**
144
156
  * Calls the provided function if the result is a success.
145
157
  * @param apply - The function to call if the result is a success.
@@ -147,6 +159,12 @@ export declare const Result: {
147
159
  * @public
148
160
  */
149
161
  whenSuccess: <V, E>(r: Result<V, E>, apply: (v: V) => void) => Result<V, E>;
162
+ /**
163
+ * Calls the provided function if the result is a failure.
164
+ * @param apply - The function to call if the result is a failure.
165
+ * @returns The result that was passed in.
166
+ * @public
167
+ */
150
168
  whenFailure: <V, E>(r: Result<V, E>, apply: (e: E) => void) => Result<V, E>;
151
169
  /**
152
170
  * Combines two results into a single result.
@@ -175,4 +193,97 @@ export declare const Result: {
175
193
  * @returns A single result that is a success if all the input results are successes, otherwise a failure.
176
194
  */
177
195
  all: <V, E>(results: Result<V, E>[]) => Result<V[], E>;
196
+ /**
197
+ * Maps the error of a failed `Result` to a new error using the provided function.
198
+ * For success results, the result is preserved unchanged.
199
+ * @param r - The `Result` to map the error of.
200
+ * @param f - The mapping function to apply to the error.
201
+ * @returns A new `Result` with the mapped error if failed, otherwise the original success.
202
+ * @public
203
+ */
204
+ mapError: <V, E, F>(r: Result<V, E>, f: (error: E) => F) => Result<V, F>;
205
+ /**
206
+ * Maps the error of a failed `Result` to a new `Result` using the provided function.
207
+ * This allows recovery from errors by returning a new successful result.
208
+ * @param r - The `Result` to recover from.
209
+ * @param f - The recovery function that returns a new `Result`.
210
+ * @returns The result of the recovery function if failed, otherwise the original success.
211
+ * @public
212
+ */
213
+ flatMapError: <V, E, F>(r: Result<V, E>, f: (error: E) => Result<V, F>) => Result<V, F>;
214
+ /**
215
+ * Recovers from a failure by providing an alternative value.
216
+ * @param r - The `Result` to recover from.
217
+ * @param f - The function that provides an alternative value given the error.
218
+ * @returns A successful `Result` with the alternative value if failed, otherwise the original success.
219
+ * @public
220
+ */
221
+ recover: <V, E>(r: Result<V, E>, f: (error: E) => V) => Result<V, never>;
222
+ /**
223
+ * Applies a function wrapped in a `Result` to a value wrapped in a `Result`.
224
+ * Useful for applying multiple arguments to a function in a safe way.
225
+ * @param resultFn - The `Result` containing the function.
226
+ * @param resultVal - The `Result` containing the value.
227
+ * @returns A new `Result` with the result of applying the function to the value.
228
+ * @public
229
+ */
230
+ ap: <V, U, E>(resultFn: Result<(v: V) => U, E>, resultVal: Result<V, E>) => Result<U, E>;
231
+ /**
232
+ * Maps two `Result` values using a function.
233
+ * @param r1 - The first `Result`.
234
+ * @param r2 - The second `Result`.
235
+ * @param f - The function to apply to both values.
236
+ * @returns A new `Result` with the result of applying the function to both values.
237
+ * @public
238
+ */
239
+ map2: <V1, V2, U, E>(r1: Result<V1, E>, r2: Result<V2, E>, f: (v1: V1, v2: V2) => U) => Result<U, E>;
240
+ /**
241
+ * Maps three `Result` values using a function.
242
+ * @param r1 - The first `Result`.
243
+ * @param r2 - The second `Result`.
244
+ * @param r3 - The third `Result`.
245
+ * @param f - The function to apply to all three values.
246
+ * @returns A new `Result` with the result of applying the function to all three values.
247
+ * @public
248
+ */
249
+ map3: <V1, V2, V3, U, E>(r1: Result<V1, E>, r2: Result<V2, E>, r3: Result<V3, E>, f: (v1: V1, v2: V2, v3: V3) => U) => Result<U, E>;
250
+ /**
251
+ * Converts a Promise to a Result.
252
+ * @param p - The Promise to convert.
253
+ * @returns A Promise that resolves to a Result.
254
+ * @public
255
+ */
256
+ ofPromise: <V>(p: Promise<V>) => Promise<Result<V, Error>>;
257
+ /**
258
+ * Swaps the success and failure values of a Result.
259
+ * A success becomes a failure with the value as the error,
260
+ * and a failure becomes a success with the error as the value.
261
+ * @param r - The Result to swap.
262
+ * @returns A new Result with swapped success and failure.
263
+ * @public
264
+ */
265
+ swap: <V, E>(r: Result<V, E>) => Result<E, V>;
266
+ /**
267
+ * Converts a nullable value to a Result.
268
+ * @param value - The nullable value.
269
+ * @param error - The error to use if the value is null or undefined.
270
+ * @returns A Result containing the value if not null/undefined, otherwise a failure.
271
+ * @public
272
+ */
273
+ fromNullable: <V, E>(value: V | null | undefined, error: E) => Result<V, E>;
274
+ /**
275
+ * Converts a nullable value to a Result using a lazy error function.
276
+ * @param value - The nullable value.
277
+ * @param errorFn - The function to call to get the error if the value is null or undefined.
278
+ * @returns A Result containing the value if not null/undefined, otherwise a failure.
279
+ * @public
280
+ */
281
+ fromNullableLazy: <V, E>(value: V | null | undefined, errorFn: () => E) => Result<V, E>;
282
+ /**
283
+ * Wraps a function that may throw into a function that returns a Result.
284
+ * @param f - The function that may throw.
285
+ * @returns A function that returns a Result instead of throwing.
286
+ * @public
287
+ */
288
+ tryCatch: <V>(f: () => V) => Result<V, Error>;
178
289
  };
package/result.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./async-result.js";
2
- import { R as e } from "./result-CGd0jCdl.js";
2
+ import { R as e } from "./result-7Po2Mxhx.js";
3
3
  export {
4
4
  e as Result
5
5
  };
package/string.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("./error.cjs"),f=require("./regexp.cjs"),T=(t,e)=>{const n=t.indexOf(e);return n<0?"":t.substring(n+e.length)},z=(t,e)=>{const n=t.lastIndexOf(e);return n<0?"":t.substring(n+e.length)},j=(t,e)=>{const n=t.indexOf(e);return n<0?"":t.substring(0,n)},M=(t,e)=>{const n=t.lastIndexOf(e);return n<0?"":t.substring(0,n)},a=t=>t.substring(0,1).toUpperCase()+t.substring(1),p=t=>t.toUpperCase(),N=(t,e=!1)=>e?f.mapRegExp(a(t),Ut,p):f.mapRegExp(a(t),Mt,p),_=t=>t.replace(Ht,`
2
- `),U=(t,e)=>t==null&&e==null?0:t==null?-1:e==null?1:d(t.toLowerCase(),e.toLowerCase()),k=(t,e)=>t.substring(t.length-e.length).toLowerCase()===e.toLowerCase(),F=(t,e)=>t.substring(0,e.length).toLowerCase()===e.toLowerCase(),H=(t,e)=>S(t.toLowerCase(),e.map(n=>n.toLowerCase())),$=(t,e)=>I(t.toLowerCase(),e.map(n=>n.toLowerCase())),D=t=>t.trim().replace(E," "),d=(t,e)=>t<e?-1:t>e?1:0,g=(t,e)=>t.toLowerCase().includes(e.toLowerCase()),R=(t,e)=>t.split(e).length-1,P=(t,e)=>e.some(n=>g(t,n)),Z=(t,e)=>e.some(n=>t.includes(n)),q=(t,e)=>e.every(n=>g(t,n)),Q=(t,e)=>e.every(n=>t.includes(n)),G=t=>t.replace(/_/g,"-"),K=(t,e)=>{if(t===e)return-1;const n=Math.min(t.length,e.length);for(let r=0;r<n;r++)if(t.substring(r,r+1)!==e.substring(r,r+1))return r;return n},C=(t,e=20,n="…")=>{const r=t.length,s=n.length;return r>e?e<s?n.slice(s-e,e):t.slice(0,e-s)+n:t},J=(t,e=20,n="…")=>{const r=t.length,s=n.length;if(r>e){if(e<=s)return C(t,e,n);const c=Math.ceil((e-s)/2),u=Math.floor((e-s)/2);return t.slice(0,c)+n+t.slice(r-u)}else return t},S=(t,e)=>e.some(n=>t.endsWith(n)),V=(t,e)=>Array.from(t).filter(e).join(""),X=(t,e)=>w(t).filter(e).map(r=>String.fromCharCode(r)).join(""),Y=(t,e=2166136261)=>{let n=e;for(let r=0,s=t.length;r<s;r++)n^=t.charCodeAt(r),n+=(n<<1)+(n<<4)+(n<<7)+(n<<8)+(n<<24);return n>>>0},v=t=>t!=null&&t.length>0,tt=t=>W(t).split("_").join(" "),et=t=>t.length>0&&!_t.test(t),nt=t=>kt.test(t),rt=t=>!Nt.test(t),st=t=>t.toLowerCase()===t,it=t=>t.toUpperCase()===t,ot=(t,e)=>t!=null&&t!==""?t:e,ct=t=>Ft.test(t),ut=t=>t==null||t==="",at=t=>t.substring(0,1).toLowerCase()+t.substring(1),A=(t,e=1)=>{const n=Math.floor((t.length-e+1)*Math.random());return t.substring(n,n+e)},b=(t,e)=>Array.from({length:e},()=>A(t)).join(""),lt=t=>b(jt,t),gt=(t,e)=>Array.from(e).map(t),ft=(t,e)=>t.split(e).join(""),pt=(t,e)=>t.endsWith(e)?t.substring(0,t.length-e.length):t,ht=(t,e,n)=>t.substring(0,e)+t.substring(e+n),dt=(t,e)=>t.startsWith(e)?t.substring(e.length):t,Ct=(t,e)=>{const n=t.indexOf(e);return n<0?t:t.substring(0,n)+t.substring(n+e.length)},St=t=>{const e=Array.from(t);return e.reverse(),e.join("")},m=(t,e="'")=>e==="'"?t.includes("'")?t.includes('"')?"'"+t.split("'").join("\\'")+"'":'"'+t+'"':"'"+t+"'":t.includes('"')?t.includes("'")?'"'+t.split('"').join('\\"')+'"':"'"+t+"'":'"'+t+'"',L=(t,e="'")=>e+t.split(e).join("\\"+e)+e,At=(t,e="'")=>t.indexOf(`
3
- `)>=0?L(t,"`"):m(t,e),bt=(t,e)=>{const n=t.indexOf(e);return n<0?[t]:[t.substring(0,n),t.substring(n+e.length)]},I=(t,e)=>e.some(n=>t.startsWith(n)),mt=(t,e,n=e)=>`${e}${t}${n}`,w=t=>Array.from({length:t.length},(e,n)=>t.charCodeAt(n)),Lt=(t,e)=>{const n=[];for(;t.length>0;)n.push(t.substring(0,e)),t=t.substring(e);return n},It=t=>t.split(B),wt=(t,e)=>y(x(t,e),e),x=(t,e)=>{let n=0;for(let r=0;r<t.length&&e.includes(t.charAt(r));r++)n++;return t.substring(n)},y=(t,e)=>{const n=t.length;let r=n,s;for(let c=0;c<n&&(s=n-c-1,e.includes(t.charAt(s)));c++)r=s;return t.substring(0,r)},W=t=>(t=t.replace(/::/g,"/"),t=t.replace(/([A-Z]+)([A-Z][a-z])/g,"$1_$2"),t=t.replace(/([a-z\d])([A-Z])/g,"$1_$2"),t=t.replace(/-/g,"_"),t.toLowerCase()),xt=t=>t.substring(0,1).toUpperCase()+t.substring(1),yt=(t,e=78,n="",r=`
4
- `)=>t.split(B).map(s=>O(s.replace(E," ").trim(),e,n,r)).join(r),l=(t,e)=>{if(e<0||e>=t.length)return!1;const n=t.charCodeAt(e);return n===9||n===10||n===11||n===12||n===13||n===32},Wt=t=>{if(typeof Buffer<"u")return Buffer.from(t).toString("base64");if(typeof btoa<"u")return btoa(t);throw new h.MissingImplementationError("No implementation found for base64 encoding")},Ot=t=>{if(typeof Buffer<"u")return Buffer.from(t,"base64").toString("utf8");if(typeof atob<"u")return atob(t);throw new h.MissingImplementationError("No implementation found for base64 decoding")},O=(t,e,n,r)=>{const s=[],c=t.length,u=n.length;let o=0;for(e-=u;;){if(o+e>=c-u){s.push(t.substring(o));break}let i=0;for(;!l(t,o+e-i)&&i<e;)i++;if(i===e){for(i=0;!l(t,o+e+i)&&o+e+i<c;)i++;s.push(t.substring(o,o+e+i)),o+=e+i+1}else s.push(t.substring(o,o+e-i)),o+=e-i+1}return n+s.join(r+n)},Et=(t,e,n)=>{const r=n-t.length;return r>0?e.repeat(r)+t:t},Bt=(t,e,n)=>{const r=n-t.length;return r>0?t+e.repeat(r):t},Tt=(t,e)=>{const n=t.lastIndexOf(e);return n>=0?[t.substring(0,n),t.substring(n+e.length)]:[t]},zt=(t,e)=>{const n=t.indexOf(e);return n>=0?[t.substring(0,n),t.substring(n+e.length)]:[t]},jt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",Mt=/[^a-zA-Z]([a-z])/g,Nt=/[^\t\n\r ]/,_t=/[^a-zA-Z]/,Ut=/[ \t\r\n][a-z]/g,kt=/^[a-z0-9]+$/i,Ft=/^[0-9]+$/,E=/[ \t\r\n]+/g,B=/\r\n|\n\r|\n|\r/g,Ht=/\r\n|\n\r|\r/g;exports.canonicalizeNewlines=_;exports.capitalize=a;exports.capitalizeWords=N;exports.chunkString=Lt;exports.collapseText=D;exports.compareCaseInsensitive=U;exports.compareStrings=d;exports.containsAllText=Q;exports.containsAllTextCaseInsensitive=q;exports.containsAnyText=Z;exports.containsAnyTextCaseInsensitive=P;exports.countStringOccurrences=R;exports.dasherize=G;exports.decodeBase64=Ot;exports.deleteFirstFromString=Ct;exports.deleteStringAfter=pt;exports.deleteStringBefore=dt;exports.deleteSubstring=ft;exports.ellipsis=C;exports.ellipsisMiddle=J;exports.encodeBase64=Wt;exports.filterCharcodes=X;exports.filterChars=V;exports.humanize=tt;exports.ifEmptyString=ot;exports.isAlpha=et;exports.isAlphaNum=nt;exports.isBreakingWhitespace=rt;exports.isDigitsOnly=ct;exports.isEmptyString=ut;exports.isLowerCase=st;exports.isSpaceAt=l;exports.isUpperCase=it;exports.jsQuote=At;exports.lowerCaseFirst=at;exports.lpad=Et;exports.mapChars=gt;exports.quote=L;exports.randomStringSequence=b;exports.randomStringSequenceBase64=lt;exports.randomSubString=A;exports.reverseString=St;exports.rpad=Bt;exports.smartQuote=m;exports.splitStringOnFirst=zt;exports.splitStringOnLast=Tt;exports.splitStringOnce=bt;exports.stringEndsWithAny=S;exports.stringHasContent=v;exports.stringHashCode=Y;exports.stringStartsWithAny=I;exports.stringToCharcodes=w;exports.stringsDifferAtIndex=K;exports.substringAfter=T;exports.substringAfterLast=z;exports.substringBefore=j;exports.substringBeforeLast=M;exports.surroundString=mt;exports.textContainsCaseInsensitive=g;exports.textEndsWithAnyCaseInsensitive=H;exports.textEndsWithCaseInsensitive=k;exports.textStartsWithAnyCaseInsensitive=$;exports.textStartsWithCaseInsensitive=F;exports.textToLines=It;exports.trimChars=wt;exports.trimCharsLeft=x;exports.trimCharsRight=y;exports.trimStringSlice=ht;exports.underscore=W;exports.upperCaseFirst=xt;exports.wrapColumns=yt;exports.wrapLine=O;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("./error.cjs"),f=require("./regexp.cjs"),T=(t,e)=>{const n=t.indexOf(e);return n<0?"":t.substring(n+e.length)},z=(t,e)=>{const n=t.lastIndexOf(e);return n<0?"":t.substring(n+e.length)},j=(t,e)=>{const n=t.indexOf(e);return n<0?"":t.substring(0,n)},M=(t,e)=>{const n=t.lastIndexOf(e);return n<0?"":t.substring(0,n)},u=t=>t.substring(0,1).toUpperCase()+t.substring(1),p=t=>t.toUpperCase(),N=(t,e=!1)=>e?f.mapRegExp(u(t),Ut,p):f.mapRegExp(u(t),Mt,p),_=t=>t.replace(Ht,`
2
+ `),U=(t,e)=>t==null&&e==null?0:t==null?-1:e==null?1:d(t.toLowerCase(),e.toLowerCase()),k=(t,e)=>t.substring(t.length-e.length).toLowerCase()===e.toLowerCase(),F=(t,e)=>t.substring(0,e.length).toLowerCase()===e.toLowerCase(),H=(t,e)=>C(t.toLowerCase(),e.map(n=>n.toLowerCase())),$=(t,e)=>I(t.toLowerCase(),e.map(n=>n.toLowerCase())),D=t=>t.trim().replace(E," "),d=(t,e)=>t<e?-1:t>e?1:0,g=(t,e)=>t.toLowerCase().includes(e.toLowerCase()),R=(t,e)=>t.split(e).length-1,P=(t,e)=>e.some(n=>g(t,n)),Z=(t,e)=>e.some(n=>t.includes(n)),q=(t,e)=>e.every(n=>g(t,n)),Q=(t,e)=>e.every(n=>t.includes(n)),G=t=>t.replace(/_/g,"-"),K=(t,e)=>{if(t===e)return-1;const n=Math.min(t.length,e.length);for(let r=0;r<n;r++)if(t.substring(r,r+1)!==e.substring(r,r+1))return r;return n},S=(t,e=20,n="…")=>{const r=t.length,s=n.length;return r>e?e<s?n.slice(s-e,e):t.slice(0,e-s)+n:t},J=(t,e=20,n="…")=>{const r=t.length,s=n.length;if(r>e){if(e<=s)return S(t,e,n);const c=Math.ceil((e-s)/2),a=Math.floor((e-s)/2);return t.slice(0,c)+n+t.slice(r-a)}else return t},C=(t,e)=>e.some(n=>t.endsWith(n)),V=(t,e)=>Array.from(t).filter(e).join(""),X=(t,e)=>w(t).filter(e).map(r=>String.fromCharCode(r)).join(""),Y=(t,e=2166136261)=>{let n=e;for(let r=0,s=t.length;r<s;r++)n^=t.charCodeAt(r),n+=(n<<1)+(n<<4)+(n<<7)+(n<<8)+(n<<24);return n>>>0},v=t=>t!=null&&t.length>0,tt=t=>W(t).split("_").join(" "),et=t=>t.length>0&&!_t.test(t),nt=t=>kt.test(t),rt=t=>!Nt.test(t),st=t=>t.toLowerCase()===t,it=t=>t.toUpperCase()===t,ot=(t,e)=>t!=null&&t!==""?t:e,ct=t=>Ft.test(t),at=t=>t==null||t==="",ut=t=>t.substring(0,1).toLowerCase()+t.substring(1),A=(t,e=1)=>{const n=Math.floor((t.length-e+1)*Math.random());return t.substring(n,n+e)},b=(t,e)=>Array.from({length:e},()=>A(t)).join(""),lt=t=>b(jt,t),gt=(t,e)=>Array.from(e).map(t),ft=(t,e)=>t.split(e).join(""),pt=(t,e)=>t.endsWith(e)?t.substring(0,t.length-e.length):t,ht=(t,e,n)=>t.substring(0,e)+t.substring(e+n),dt=(t,e)=>t.startsWith(e)?t.substring(e.length):t,St=(t,e)=>{const n=t.indexOf(e);return n<0?t:t.substring(0,n)+t.substring(n+e.length)},Ct=t=>{const e=Array.from(t);return e.reverse(),e.join("")},m=(t,e="'")=>e==="'"?t.includes("'")?t.includes('"')?"'"+t.split("'").join("\\'")+"'":'"'+t+'"':"'"+t+"'":t.includes('"')?t.includes("'")?'"'+t.split('"').join('\\"')+'"':"'"+t+"'":'"'+t+'"',L=(t,e="'")=>e+t.split(e).join("\\"+e)+e,At=(t,e="'")=>t.indexOf(`
3
+ `)>=0?L(t,"`"):m(t,e),bt=(t,e)=>{const n=t.indexOf(e);return n<0?[t]:[t.substring(0,n),t.substring(n+e.length)]},I=(t,e)=>e.some(n=>t.startsWith(n)),mt=(t,e,n=e)=>`${e}${t}${n}`,w=t=>Array.from({length:t.length},(e,n)=>t.charCodeAt(n)),Lt=(t,e)=>{const n=[];for(let r=0;r<t.length;r+=e)n.push(t.substring(r,r+e));return n},It=t=>t.split(B),wt=(t,e)=>y(x(t,e),e),x=(t,e)=>{const n=new Set(e);let r=0;for(let s=0;s<t.length&&n.has(t.charAt(s));s++)r++;return t.substring(r)},y=(t,e)=>{const n=new Set(e),r=t.length;let s=r,c;for(let a=0;a<r&&(c=r-a-1,n.has(t.charAt(c)));a++)s=c;return t.substring(0,s)},W=t=>(t=t.replace(/::/g,"/"),t=t.replace(/([A-Z]+)([A-Z][a-z])/g,"$1_$2"),t=t.replace(/([a-z\d])([A-Z])/g,"$1_$2"),t=t.replace(/-/g,"_"),t.toLowerCase()),xt=t=>t.substring(0,1).toUpperCase()+t.substring(1),yt=(t,e=78,n="",r=`
4
+ `)=>t.split(B).map(s=>O(s.replace(E," ").trim(),e,n,r)).join(r),l=(t,e)=>{if(e<0||e>=t.length)return!1;const n=t.charCodeAt(e);return n===9||n===10||n===11||n===12||n===13||n===32},Wt=t=>{if(typeof Buffer<"u")return Buffer.from(t).toString("base64");if(typeof btoa<"u")return btoa(t);throw new h.MissingImplementationError("No implementation found for base64 encoding")},Ot=t=>{if(typeof Buffer<"u")return Buffer.from(t,"base64").toString("utf8");if(typeof atob<"u")return atob(t);throw new h.MissingImplementationError("No implementation found for base64 decoding")},O=(t,e,n,r)=>{const s=[],c=t.length,a=n.length;let o=0;for(e-=a;;){if(o+e>=c-a){s.push(t.substring(o));break}let i=0;for(;!l(t,o+e-i)&&i<e;)i++;if(i===e){for(i=0;!l(t,o+e+i)&&o+e+i<c;)i++;s.push(t.substring(o,o+e+i)),o+=e+i+1}else s.push(t.substring(o,o+e-i)),o+=e-i+1}return n+s.join(r+n)},Et=(t,e,n)=>{const r=n-t.length;return r>0?e.repeat(r)+t:t},Bt=(t,e,n)=>{const r=n-t.length;return r>0?t+e.repeat(r):t},Tt=(t,e)=>{const n=t.lastIndexOf(e);return n>=0?[t.substring(0,n),t.substring(n+e.length)]:[t]},zt=(t,e)=>{const n=t.indexOf(e);return n>=0?[t.substring(0,n),t.substring(n+e.length)]:[t]},jt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",Mt=/[^a-zA-Z]([a-z])/g,Nt=/[^\t\n\r ]/,_t=/[^a-zA-Z]/,Ut=/[ \t\r\n][a-z]/g,kt=/^[a-z0-9]+$/i,Ft=/^[0-9]+$/,E=/[ \t\r\n]+/g,B=/\r\n|\n\r|\n|\r/g,Ht=/\r\n|\n\r|\r/g;exports.canonicalizeNewlines=_;exports.capitalize=u;exports.capitalizeWords=N;exports.chunkString=Lt;exports.collapseText=D;exports.compareCaseInsensitive=U;exports.compareStrings=d;exports.containsAllText=Q;exports.containsAllTextCaseInsensitive=q;exports.containsAnyText=Z;exports.containsAnyTextCaseInsensitive=P;exports.countStringOccurrences=R;exports.dasherize=G;exports.decodeBase64=Ot;exports.deleteFirstFromString=St;exports.deleteStringAfter=pt;exports.deleteStringBefore=dt;exports.deleteSubstring=ft;exports.ellipsis=S;exports.ellipsisMiddle=J;exports.encodeBase64=Wt;exports.filterCharcodes=X;exports.filterChars=V;exports.humanize=tt;exports.ifEmptyString=ot;exports.isAlpha=et;exports.isAlphaNum=nt;exports.isBreakingWhitespace=rt;exports.isDigitsOnly=ct;exports.isEmptyString=at;exports.isLowerCase=st;exports.isSpaceAt=l;exports.isUpperCase=it;exports.jsQuote=At;exports.lowerCaseFirst=ut;exports.lpad=Et;exports.mapChars=gt;exports.quote=L;exports.randomStringSequence=b;exports.randomStringSequenceBase64=lt;exports.randomSubString=A;exports.reverseString=Ct;exports.rpad=Bt;exports.smartQuote=m;exports.splitStringOnFirst=zt;exports.splitStringOnLast=Tt;exports.splitStringOnce=bt;exports.stringEndsWithAny=C;exports.stringHasContent=v;exports.stringHashCode=Y;exports.stringStartsWithAny=I;exports.stringToCharcodes=w;exports.stringsDifferAtIndex=K;exports.substringAfter=T;exports.substringAfterLast=z;exports.substringBefore=j;exports.substringBeforeLast=M;exports.surroundString=mt;exports.textContainsCaseInsensitive=g;exports.textEndsWithAnyCaseInsensitive=H;exports.textEndsWithCaseInsensitive=k;exports.textStartsWithAnyCaseInsensitive=$;exports.textStartsWithCaseInsensitive=F;exports.textToLines=It;exports.trimChars=wt;exports.trimCharsLeft=x;exports.trimCharsRight=y;exports.trimStringSlice=ht;exports.underscore=W;exports.upperCaseFirst=xt;exports.wrapColumns=yt;exports.wrapLine=O;