@terrygonguet/utils 0.11.0 → 0.12.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.
package/dist/result.js DELETED
@@ -1,153 +0,0 @@
1
- // src/result.ts
2
- var Result = class _Result {
3
- value;
4
- error;
5
- constructor(value, error) {
6
- this.value = value;
7
- this.error = error;
8
- }
9
- asTuple() {
10
- return [this.error, this.value];
11
- }
12
- asObject() {
13
- return { value: this.value, error: this.error };
14
- }
15
- andThen(f) {
16
- if (this.error) return this;
17
- try {
18
- const newValue = f(this.value);
19
- if (newValue instanceof Promise) return new AsyncResult(newValue);
20
- else return new _Result(newValue, null);
21
- } catch (error) {
22
- return new _Result(null, RawError.wrap(error));
23
- }
24
- }
25
- match(onValue, onError) {
26
- if (this.error) return onError?.(this.error);
27
- else return onError ? onValue(this.value) : void onValue(this.value);
28
- }
29
- recover(f) {
30
- if (!this.error) return this;
31
- try {
32
- const newValue = f(this.error);
33
- if (newValue instanceof Promise) return new AsyncResult(newValue);
34
- else return new _Result(newValue, null);
35
- } catch (error) {
36
- return new _Result(null, RawError.wrap(error));
37
- }
38
- }
39
- unwrap() {
40
- if (!this.error) return this.value;
41
- else throw new Error("Tried to unwrap a failed Result");
42
- }
43
- unwrapErr() {
44
- if (this.error) return this.error;
45
- else throw new Error("Tried to unwrapErr a successful Result");
46
- }
47
- };
48
- var AsyncResult = class _AsyncResult {
49
- promise;
50
- constructor(promise) {
51
- this.promise = promise;
52
- }
53
- asTuple() {
54
- return this.then((result) => result.asTuple());
55
- }
56
- asObject() {
57
- return this.then((result) => result.asObject());
58
- }
59
- then(cb) {
60
- return this.promise.then(
61
- (value) => cb(new Result(value, null)),
62
- (error) => cb(new Result(null, RawError.wrap(error)))
63
- );
64
- }
65
- andThen(f) {
66
- return new _AsyncResult(this.promise.then((value) => f(value)));
67
- }
68
- match(onValue, onError) {
69
- return this.promise.then(
70
- (value) => onError ? onValue(value) : void onValue(value),
71
- (error) => onError?.(error)
72
- );
73
- }
74
- recover(f) {
75
- return new _AsyncResult(
76
- this.promise.then(
77
- (value) => value,
78
- (error) => f(error)
79
- )
80
- );
81
- }
82
- unwrap() {
83
- return this.then((result) => result.unwrap());
84
- }
85
- unwrapErr() {
86
- return this.then((result) => result.unwrapErr());
87
- }
88
- };
89
- var RawError = class _RawError extends Error {
90
- name = "RawError";
91
- value;
92
- constructor(value) {
93
- super();
94
- this.value = value;
95
- }
96
- static wrap(error) {
97
- if (error instanceof Error) return error;
98
- else return new _RawError(error);
99
- }
100
- };
101
- function safe(promiseOrFunction, ...args) {
102
- if (promiseOrFunction instanceof Promise) return new AsyncResult(promiseOrFunction);
103
- try {
104
- const value = promiseOrFunction(...args);
105
- if (value instanceof Promise) return new AsyncResult(value);
106
- else return new Result(value, null);
107
- } catch (error) {
108
- return new Result(null, RawError.wrap(error));
109
- }
110
- }
111
- var SafeFunctionValidationError = class extends Error {
112
- name = "SafeFunctionValidationError";
113
- issues;
114
- constructor(message, issues, options) {
115
- super(message, options);
116
- this.issues = issues;
117
- }
118
- };
119
- function makeSafeFn(f, options) {
120
- return function(...args) {
121
- if (options?.paramsSchema) {
122
- const parsed = options.paramsSchema["~standard"].validate(args);
123
- if ("then" in parsed)
124
- return new Result(
125
- null,
126
- new TypeError("[makeSafeFn] Parameters check failed: Async schemas are not supported")
127
- );
128
- else if (parsed.issues)
129
- return new Result(
130
- null,
131
- new SafeFunctionValidationError("[makeSafeFn] Parameters check failed", parsed.issues)
132
- );
133
- else args = parsed.value;
134
- }
135
- if (options?.returnSchema)
136
- return safe(f, ...args).andThen((returned) => {
137
- const parsed = options.returnSchema["~standard"].validate(returned);
138
- if ("then" in parsed)
139
- throw new TypeError("[makeSafeFn] Return check failed: Async schemas are not supported");
140
- else if (parsed.issues)
141
- throw new SafeFunctionValidationError("[makeSafeFn] Return check failed", parsed.issues);
142
- else return parsed.value;
143
- });
144
- else return safe(f, ...args);
145
- };
146
- }
147
- export {
148
- AsyncResult,
149
- Result,
150
- SafeFunctionValidationError,
151
- makeSafeFn,
152
- safe
153
- };
package/dist/tableau.d.ts DELETED
@@ -1,63 +0,0 @@
1
- type Predicate<T> = (value: T, index: number, tableau: Tableau<T>) => boolean;
2
- type AssertPredicate<T, U extends T> = (value: T, index: number, tableau: Tableau<T>) => value is U;
3
- type Reducer<T, U> = (previousValue: U, currentValue: T, index: number, tableau: Tableau<T>) => U;
4
- type Mapper<T, U> = (value: T, index: number, tableau: Tableau<T>) => U;
5
- type Comparer<T> = (a: T, b: T) => number;
6
- type FlatTableau<Arr, Depth extends number> = {
7
- done: Arr;
8
- recur: Arr extends Tableau<infer InnerArr> ? FlatTableau<InnerArr, [
9
- -1,
10
- 0,
11
- 1,
12
- 2,
13
- 3,
14
- 4,
15
- 5,
16
- 6,
17
- 7,
18
- 8,
19
- 9,
20
- 10,
21
- 11,
22
- 12,
23
- 13,
24
- 14,
25
- 15,
26
- 16,
27
- 17,
28
- 18,
29
- 19,
30
- 20
31
- ][Depth]> : Arr;
32
- }[Depth extends -1 ? "done" : "recur"];
33
- declare class Tableau<T> extends Array<T> {
34
- constructor(length?: number);
35
- i(idx: number): T | undefined;
36
- ientries(): IterableIterator<[number, T]>;
37
- ievery<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): this is Tableau<U>;
38
- ievery(predicate: Predicate<T>, thisArg?: any): boolean;
39
- ifilter<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): Tableau<U>;
40
- ifilter(predicate: Predicate<T>, thisArg?: any): Tableau<T>;
41
- ifind<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): U | undefined;
42
- ifind(predicate: Predicate<T>, thisArg?: any): T | undefined;
43
- ifindIndex(predicate: Predicate<T>, thisArg?: any): number | undefined;
44
- ifindLast<U extends T>(predicate: AssertPredicate<T, U>, thisArg?: any): U | undefined;
45
- ifindLast(predicate: Predicate<T>, thisArg?: any): T | undefined;
46
- ifindLastIndex(predicate: Predicate<T>, thisArg?: any): number | undefined;
47
- iflat<Depth extends number = 1>(depth?: Depth): FlatTableau<T, Depth>;
48
- iflatMap<U>(callback: Mapper<T, U | Tableau<U>>, thisArg?: any): Tableau<U>;
49
- iindexOf(searchElement: T, fromIndex?: number): number | undefined;
50
- ikeys(): IterableIterator<number>;
51
- ilastIndexOf(searchElement: T, fromIndex?: number): number | undefined;
52
- imap<U>(callbackFn: Mapper<T, U>, thisArg?: any): Tableau<U>;
53
- ireduce<U>(callbackFn: Reducer<T, U>, initialValue: U): U;
54
- ireduceRight<U>(callbackFn: Reducer<T, U>, initialValue: U): U;
55
- isome(predicate: Predicate<T>, thisArg?: any): boolean;
56
- isort(compareFn: Comparer<T>): Tableau<T>;
57
- isplice(start: number, deleteCount?: number, ...items: T[]): Tableau<T>;
58
- iwith(index: number, value: T): Tableau<T>;
59
- static from<T>(source: Iterable<T> | ArrayLike<T>): Tableau<T>;
60
- static of<T>(...elements: T[]): Tableau<T>;
61
- }
62
-
63
- export { Tableau };
package/dist/tableau.js DELETED
@@ -1,125 +0,0 @@
1
- // src/tableau.ts
2
- var Tableau = class _Tableau extends Array {
3
- constructor(length) {
4
- length == void 0 ? super() : super(length);
5
- }
6
- i(idx) {
7
- if (idx == 0) throw new RangeError("Cannot access index 0 in a Tableau");
8
- return this.at(idx - 1);
9
- }
10
- *ientries() {
11
- for (let i = 0; i < this.length; i++) {
12
- yield [i + 1, this[i]];
13
- }
14
- }
15
- ievery(predicate, thisArg) {
16
- return this.every((value, i) => predicate(value, i + 1, this), thisArg);
17
- }
18
- ifilter(predicate, thisArg) {
19
- return this.filter((value, i) => predicate(value, i + 1, this), thisArg);
20
- }
21
- ifind(predicate, thisArg) {
22
- return this.find((value, i) => predicate(value, i + 1, this), thisArg);
23
- }
24
- ifindIndex(predicate, thisArg) {
25
- const idx = this.findIndex(
26
- (value, i) => predicate(value, i + 1, this),
27
- thisArg
28
- );
29
- return idx == -1 ? void 0 : idx + 1;
30
- }
31
- ifindLast(predicate, thisArg) {
32
- return this.findLast(
33
- (value, i) => predicate(value, i + 1, this),
34
- thisArg
35
- );
36
- }
37
- ifindLastIndex(predicate, thisArg) {
38
- const idx = this.findLastIndex(
39
- (value, i) => predicate(value, i + 1, this),
40
- thisArg
41
- );
42
- return idx == -1 ? void 0 : idx + 1;
43
- }
44
- iflat(depth) {
45
- return this.flat(depth);
46
- }
47
- iflatMap(callback, thisArg) {
48
- return this.flatMap(
49
- (value, i) => callback(value, i + 1, this),
50
- thisArg
51
- );
52
- }
53
- iindexOf(searchElement, fromIndex = 1) {
54
- if (fromIndex == 0)
55
- throw new RangeError("Cannot access index 0 in a Tableau");
56
- const idx = this.indexOf(searchElement, fromIndex - 1);
57
- return idx == -1 ? void 0 : idx + 1;
58
- }
59
- *ikeys() {
60
- for (const key of this.keys()) {
61
- yield key + 1;
62
- }
63
- }
64
- ilastIndexOf(searchElement, fromIndex = this.length) {
65
- if (fromIndex == 0)
66
- throw new RangeError("Cannot access index 0 in a Tableau");
67
- const idx = this.lastIndexOf(searchElement, fromIndex - 1);
68
- return idx == -1 ? void 0 : idx + 1;
69
- }
70
- imap(callbackFn, thisArg) {
71
- return this.map(
72
- (value, i) => callbackFn(value, i + 1, this),
73
- thisArg
74
- );
75
- }
76
- ireduce(callbackFn, initialValue) {
77
- return this.reduce(
78
- (acc, cur, i) => callbackFn(acc, cur, i + 1, this),
79
- initialValue
80
- );
81
- }
82
- ireduceRight(callbackFn, initialValue) {
83
- return this.reduceRight(
84
- (acc, cur, i) => callbackFn(acc, cur, i + 1, this),
85
- initialValue
86
- );
87
- }
88
- isome(predicate, thisArg) {
89
- return this.some((value, i) => predicate(value, i + 1, this), thisArg);
90
- }
91
- isort(compareFn) {
92
- return this.toSorted(compareFn);
93
- }
94
- isplice(start, deleteCount, ...items) {
95
- if (start == 0)
96
- throw new RangeError("Cannot access index 0 in a Tableau");
97
- return this.toSpliced(start - 1, deleteCount, ...items);
98
- }
99
- iwith(index, value) {
100
- if (index == 0)
101
- throw new RangeError("Cannot access index 0 in a Tableau");
102
- return this.with(index - 1, value);
103
- }
104
- static from(source) {
105
- if ("length" in source) {
106
- const litanie = new _Tableau(source.length);
107
- for (let i = 0; i < source.length; i++) {
108
- litanie[i] = source[i];
109
- }
110
- return litanie;
111
- } else {
112
- const litanie = new _Tableau();
113
- for (const element of source) {
114
- litanie.push(element);
115
- }
116
- return litanie;
117
- }
118
- }
119
- static of(...elements) {
120
- return _Tableau.from(elements);
121
- }
122
- };
123
- export {
124
- Tableau
125
- };