scats 1.4.0-dev → 1.4.1-dev
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/abstract-map.d.ts +4 -4
- package/dist/abstract-map.js +12 -15
- package/dist/abstract-set.d.ts +2 -2
- package/dist/abstract-set.js +6 -9
- package/dist/array-iterable.d.ts +1 -1
- package/dist/array-iterable.js +22 -26
- package/dist/collection.d.ts +4 -4
- package/dist/collection.js +50 -71
- package/dist/either.d.ts +4 -4
- package/dist/either.js +31 -39
- package/dist/hashmap.d.ts +2 -2
- package/dist/hashmap.js +9 -12
- package/dist/hashset.d.ts +3 -3
- package/dist/hashset.js +6 -11
- package/dist/index.d.ts +8 -8
- package/dist/index.js +9 -13
- package/dist/mappable.js +1 -2
- package/dist/mutable/hashmap.d.ts +3 -3
- package/dist/mutable/hashmap.js +3 -8
- package/dist/mutable/hashset.d.ts +2 -2
- package/dist/mutable/hashset.js +3 -8
- package/dist/mutable.d.ts +3 -3
- package/dist/mutable.js +3 -9
- package/dist/option.d.ts +6 -6
- package/dist/option.js +34 -44
- package/dist/try.d.ts +3 -3
- package/dist/try.js +27 -37
- package/dist/util.d.ts +2 -2
- package/dist/util.js +41 -50
- package/package.json +1 -1
- package/.eslintrc.cjs +0 -44
- package/coverage/clover.xml +0 -937
- package/coverage/coverage-final.json +0 -15
- package/coverage/lcov-report/array-iterable.ts.html +0 -1709
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/collection.ts.html +0 -1475
- package/coverage/lcov-report/either.ts.html +0 -1934
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/hashmap.ts.html +0 -527
- package/coverage/lcov-report/hashset.ts.html +0 -392
- package/coverage/lcov-report/index.html +0 -126
- package/coverage/lcov-report/index.ts.html +0 -101
- package/coverage/lcov-report/option.ts.html +0 -758
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov-report/src/abstract-map.ts.html +0 -317
- package/coverage/lcov-report/src/abstract-set.ts.html +0 -200
- package/coverage/lcov-report/src/array-iterable.ts.html +0 -1751
- package/coverage/lcov-report/src/collection.ts.html +0 -1778
- package/coverage/lcov-report/src/either.ts.html +0 -1934
- package/coverage/lcov-report/src/hashmap.ts.html +0 -428
- package/coverage/lcov-report/src/hashset.ts.html +0 -482
- package/coverage/lcov-report/src/index.html +0 -276
- package/coverage/lcov-report/src/index.ts.html +0 -110
- package/coverage/lcov-report/src/mutable/hashmap.ts.html +0 -821
- package/coverage/lcov-report/src/mutable/hashset.ts.html +0 -611
- package/coverage/lcov-report/src/mutable/index.html +0 -126
- package/coverage/lcov-report/src/mutable.ts.html +0 -89
- package/coverage/lcov-report/src/option.ts.html +0 -758
- package/coverage/lcov-report/src/try.ts.html +0 -923
- package/coverage/lcov-report/src/util.ts.html +0 -518
- package/coverage/lcov-report/try.ts.html +0 -923
- package/coverage/lcov-report/util.ts.html +0 -518
- package/coverage/lcov.info +0 -2223
- package/jest.config.js +0 -32
- package/src/abstract-map.ts +0 -79
- package/src/abstract-set.ts +0 -40
- package/src/array-iterable.ts +0 -557
- package/src/collection.ts +0 -619
- package/src/either.ts +0 -618
- package/src/hashmap.ts +0 -116
- package/src/hashset.ts +0 -134
- package/src/index.ts +0 -10
- package/src/mappable.ts +0 -8
- package/src/mutable/hashmap.ts +0 -247
- package/src/mutable/hashset.ts +0 -177
- package/src/mutable.ts +0 -3
- package/src/option.ts +0 -226
- package/src/try.ts +0 -281
- package/src/util.ts +0 -146
package/src/option.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import {Collection} from './collection';
|
|
2
|
-
import {Either, Left, left, Right, right} from './either';
|
|
3
|
-
import {ArrayIterable} from './array-iterable';
|
|
4
|
-
import {HashSet} from './hashset';
|
|
5
|
-
import {HashMap} from './hashmap';
|
|
6
|
-
import {Mappable} from './mappable';
|
|
7
|
-
|
|
8
|
-
export interface OptionMatch<A, T> {
|
|
9
|
-
some: (value: A) => T;
|
|
10
|
-
none: () => T;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export abstract class Option<A> extends ArrayIterable<A, Option<A>> implements Mappable<A> {
|
|
15
|
-
|
|
16
|
-
abstract readonly get: A;
|
|
17
|
-
|
|
18
|
-
/** When a given condition is true, evaluates the `a` argument and returns
|
|
19
|
-
* Some(a). When the condition is false, `a` is not evaluated and None is
|
|
20
|
-
* returned.
|
|
21
|
-
*/
|
|
22
|
-
static when<A>(cond: boolean): (a: () => A) => Option<A> {
|
|
23
|
-
return a => {
|
|
24
|
-
if (cond) {
|
|
25
|
-
return some(a());
|
|
26
|
-
} else {
|
|
27
|
-
return none;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
static useless<A>(cond: boolean): (a: () => A) => Option<A> {
|
|
34
|
-
return Option.when(!cond);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
protected fromArray(array: A[]): Option<A> {
|
|
38
|
-
if (array.length <= 0) {
|
|
39
|
-
return none;
|
|
40
|
-
} else {
|
|
41
|
-
return some(array[0]);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
exists(p: (value: A) => boolean): boolean {
|
|
46
|
-
if (this.isEmpty) {
|
|
47
|
-
return false;
|
|
48
|
-
} else {
|
|
49
|
-
return p(this.get);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
filter(p: (value: A) => boolean): Option<A> {
|
|
54
|
-
if (this.isEmpty) {
|
|
55
|
-
return none;
|
|
56
|
-
} else {
|
|
57
|
-
return p(this.get) ? this : none;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
filterNot(p: (value: A) => boolean): Option<A> {
|
|
62
|
-
if (this.isEmpty) {
|
|
63
|
-
return none;
|
|
64
|
-
} else {
|
|
65
|
-
return p(this.get) ? none : this;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
map<B>(f: (item: A) => B): Option<B> {
|
|
70
|
-
return this.isEmpty ? none : some(f(this.get));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
flatMap<B>(p: (value: A) => Option<B>): Option<B> {
|
|
75
|
-
return this.isEmpty ? none : p(this.get);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async mapPromise<B>(f: (v: A) => Promise<B>): Promise<Option<B>> {
|
|
79
|
-
if (this.isEmpty) {
|
|
80
|
-
return Promise.resolve(none);
|
|
81
|
-
} else {
|
|
82
|
-
return option<B>(await f(this.get));
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
flatMapPromise<B>(f: (item: A) => Promise<Mappable<B>>): Promise<Mappable<B>> {
|
|
88
|
-
if (this.isEmpty) {
|
|
89
|
-
return Promise.resolve(none);
|
|
90
|
-
} else {
|
|
91
|
-
return f(this.get);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
foldValue<B>(ifEmpty: () => B): (f: (_: A) => B) => B {
|
|
97
|
-
if (this.isEmpty) {
|
|
98
|
-
return function() { return ifEmpty(); };
|
|
99
|
-
} else {
|
|
100
|
-
return (f: (_: A) => B) => { return f(this.get); };
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
forall(p: (_: A) => boolean): boolean {
|
|
105
|
-
return this.isEmpty ? true : p(this.get);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
foreach(f: (_: A) => void): void {
|
|
110
|
-
if (this.nonEmpty) {
|
|
111
|
-
f(this.get);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
getOrElse(f: () => A): A {
|
|
116
|
-
return this.isEmpty ? f() : this.get;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
getOrElseValue(other: A): A {
|
|
120
|
-
return this.isEmpty ? other : this.get;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
getOrElseThrow(error: () => Error): A {
|
|
124
|
-
if (this.isEmpty) {
|
|
125
|
-
throw error();
|
|
126
|
-
} else {
|
|
127
|
-
return this.get;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
contains<A1 extends A>(x: A1): boolean {
|
|
133
|
-
return this.isEmpty ? false : x === this.get;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
get isDefined(): boolean {
|
|
137
|
-
return !this.isEmpty;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
orElse(alternative: () => Option<A>): Option<A> {
|
|
141
|
-
return this.isEmpty ? alternative() : this;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
orElseValue(alternative: Option<A>): Option<A> {
|
|
146
|
-
return this.isEmpty ? alternative : this;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
get orNull(): A | null {
|
|
150
|
-
return this.isEmpty ? null : this.get;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
get orUndefined(): A | undefined {
|
|
154
|
-
return this.isEmpty ? undefined : this.get;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
get toCollection(): Collection<A> {
|
|
158
|
-
return this.isEmpty ? Collection.empty : Collection.of(this.get);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
toRight<X>(left: () => X): Either<X, A> {
|
|
162
|
-
return this.isEmpty ? new Left(left()) : right(this.get);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
toLeft<X>(right: () => X): Either<A, X> {
|
|
166
|
-
return this.isEmpty ? new Right(right()) : left(this.get);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
get toArray(): A[] {
|
|
170
|
-
return this.isEmpty ? [] : [this.get];
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
get toSet(): HashSet<A> {
|
|
174
|
-
return this.isEmpty ? HashSet.empty : HashSet.of(this.get);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
match<T>(matcher: OptionMatch<A, T>): T {
|
|
178
|
-
return this.isEmpty ? matcher.none() : matcher.some(this.get);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
toMap<K, V>(mapper: (item: A) => [K, V]): HashMap<K, V> {
|
|
182
|
-
return this.isEmpty ? HashMap.empty : HashMap.of(...this.map(mapper).toArray);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
export class Some<A> extends Option<A> {
|
|
189
|
-
|
|
190
|
-
constructor(private readonly value: A) {
|
|
191
|
-
super();
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
get get(): A {
|
|
195
|
-
return this.value;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
get isEmpty(): boolean {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
export class None<A> extends Option<A> {
|
|
206
|
-
|
|
207
|
-
get get(): A {
|
|
208
|
-
throw new Error('No such element.');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
get isEmpty(): boolean {
|
|
212
|
-
return true;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export function option<A>(value: A | null | undefined): Option<A> {
|
|
218
|
-
return value === null || typeof value === 'undefined' ? none : some(value);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
export function some<A>(value: A): Some<A> {
|
|
223
|
-
return new Some<A>(value);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export const none: Option<any> = new None<any>();
|
package/src/try.ts
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import {none, Option, some} from './option';
|
|
2
|
-
import {Either, left, right} from './either';
|
|
3
|
-
import {identity} from './util';
|
|
4
|
-
import {Mappable} from './mappable';
|
|
5
|
-
|
|
6
|
-
export interface TryMatch<T, R> {
|
|
7
|
-
success: (result: T) => R;
|
|
8
|
-
failure: (error: Error) => R;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export abstract class TryLike<T> implements Mappable<T>{
|
|
13
|
-
abstract readonly toOption: Option<T>;
|
|
14
|
-
abstract readonly toEither: Either<Error, T>;
|
|
15
|
-
abstract map<B>(f: (x: T) => B): TryLike<B>;
|
|
16
|
-
abstract readonly isFailure: boolean;
|
|
17
|
-
abstract readonly isSuccess: boolean;
|
|
18
|
-
abstract getOrElse(value: () => T): T;
|
|
19
|
-
abstract getOrElseValue(value: T): T;
|
|
20
|
-
abstract orElse(value: () => TryLike<T>): TryLike<T>;
|
|
21
|
-
abstract readonly get: T;
|
|
22
|
-
abstract match<R>(matcher: TryMatch<T, R>): R;
|
|
23
|
-
abstract flatMap<U>(f: (value: T) => TryLike<U>): TryLike<U>;
|
|
24
|
-
abstract filter(p: (value: T) => boolean): TryLike<T>;
|
|
25
|
-
abstract readonly failed: TryLike<Error>;
|
|
26
|
-
abstract fold<U>(fa: (e: Error) => U, fb: (result: T) => U): U;
|
|
27
|
-
// no lower bound generic in typescript: https://github.com/Microsoft/TypeScript/issues/14520
|
|
28
|
-
abstract recover(f: (e: Error) => any): TryLike<any>;
|
|
29
|
-
abstract recoverWith(f: (e: Error) => TryLike<any>): TryLike<any>;
|
|
30
|
-
abstract transform<U>(s: (value: T) => TryLike<U>, f: (e: Error) => TryLike<U>): TryLike<U>;
|
|
31
|
-
|
|
32
|
-
async mapPromise<B>(f: (v: T) => Promise<B>): Promise<TryLike<B>> {
|
|
33
|
-
return this.match({
|
|
34
|
-
success: r => Try.promise(() => f(r)),
|
|
35
|
-
failure: () => Promise.resolve(this as unknown as TryLike<B>)
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
flatMapPromise<B>(f: (item: T) => Promise<TryLike<B>>): Promise<TryLike<B>> {
|
|
40
|
-
return this.match({
|
|
41
|
-
success: r => f(r),
|
|
42
|
-
failure: () => Promise.resolve(this as unknown as TryLike<B>)
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
foreach<U>(f: (value: T) => U): void {
|
|
47
|
-
if (this.isSuccess) {
|
|
48
|
-
f(this.get);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
tapFailure(f: (e: Error) => void): TryLike<T> {
|
|
53
|
-
return this.match<TryLike<T>>({
|
|
54
|
-
success: () => this,
|
|
55
|
-
failure: e => {
|
|
56
|
-
try {
|
|
57
|
-
f(e);
|
|
58
|
-
return this;
|
|
59
|
-
} catch (ex) {
|
|
60
|
-
return failure(ex as Error);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
toEitherWithLeft<L>(f: (e: Error) => L): Either<L, T> {
|
|
67
|
-
return this.match<Either<L, T>>({
|
|
68
|
-
success: r => right(r),
|
|
69
|
-
failure: e => left(f(e))
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
export class Success<T> extends TryLike<T> {
|
|
79
|
-
|
|
80
|
-
readonly isSuccess = true;
|
|
81
|
-
readonly isFailure = false;
|
|
82
|
-
|
|
83
|
-
constructor(private readonly result: T) {
|
|
84
|
-
super();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
get toOption(): Option<T> {
|
|
88
|
-
return some(this.result);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
get toEither(): Either<Error, T> {
|
|
92
|
-
return right(this.result);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
map<B>(f: (x: T) => B): TryLike<B> {
|
|
96
|
-
return success(f(this.result));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
get get(): T {
|
|
100
|
-
return this.result;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
getOrElse(_: () => T): T {
|
|
104
|
-
return this.result;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
getOrElseValue(_: T): T {
|
|
108
|
-
return this.result;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
orElse(_: () => TryLike<T>): TryLike<T> {
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
match<R>(matcher: TryMatch<T, R>): R {
|
|
116
|
-
return matcher.success(this.result);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
flatMap<U>(f: (value: T) => TryLike<U>): TryLike<U> {
|
|
120
|
-
try {
|
|
121
|
-
return f(this.result);
|
|
122
|
-
} catch (e) {
|
|
123
|
-
return failure(e as Error);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
filter(p: (value: T) => boolean): TryLike<T> {
|
|
128
|
-
try {
|
|
129
|
-
if (p(this.result)) {
|
|
130
|
-
return this;
|
|
131
|
-
} else {
|
|
132
|
-
return failure(new Error('Predicate does not hold for ' + this.result));
|
|
133
|
-
}
|
|
134
|
-
} catch (e) {
|
|
135
|
-
return failure(e as Error);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
get failed(): TryLike<Error> {
|
|
140
|
-
return failure(new Error('Success.failed'));
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
fold<U>(fa: (e: Error) => U, fb: (result: T) => U): U {
|
|
144
|
-
try {
|
|
145
|
-
return fb(this.result);
|
|
146
|
-
} catch (e) {
|
|
147
|
-
return fa(e as Error);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
recover(_: (e: Error) => any): TryLike<any> {
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
recoverWith(_: (e: Error) => TryLike<any>): TryLike<any> {
|
|
156
|
-
return this;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
transform<U>(s: (value: T) => TryLike<U>, _: (e: Error) => TryLike<U>): TryLike<U> {
|
|
160
|
-
return this.flatMap(s);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export class Failure extends TryLike<any> {
|
|
169
|
-
|
|
170
|
-
readonly isSuccess = false;
|
|
171
|
-
readonly isFailure = true;
|
|
172
|
-
|
|
173
|
-
constructor(private readonly error: Error) {
|
|
174
|
-
super();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
get toOption(): Option<any> {
|
|
178
|
-
return none;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
get toEither(): Either<Error, any> {
|
|
182
|
-
return left(this.error);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
map<B>(_: (x: any) => B): TryLike<any> {
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
get get(): any {
|
|
190
|
-
throw this.error;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
getOrElse<T>(value: () => T): T {
|
|
194
|
-
return value();
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
getOrElseValue<T>(value: T): T {
|
|
198
|
-
return value;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
orElse<T>(value: () => TryLike<T>): TryLike<T> {
|
|
202
|
-
try {
|
|
203
|
-
return value();
|
|
204
|
-
} catch (e) {
|
|
205
|
-
return failure(e as Error);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
match<R>(matcher: TryMatch<any, R>): R {
|
|
210
|
-
return matcher.failure(this.error);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
flatMap<U>(_: (value: any) => TryLike<U>): TryLike<U> {
|
|
214
|
-
return this;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
filter(_: (value: any) => boolean): TryLike<any> {
|
|
218
|
-
return this;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
get failed(): TryLike<Error> {
|
|
222
|
-
return success(this.error);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
fold<U>(fa: (e: Error) => U, _: (result: any) => U): U {
|
|
226
|
-
return fa(this.error);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
recover(f: (e: Error) => any): TryLike<any> {
|
|
230
|
-
try {
|
|
231
|
-
return success(f(this.error));
|
|
232
|
-
} catch (ex) {
|
|
233
|
-
return failure(ex as Error);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
recoverWith(f: (e: Error) => TryLike<any>): TryLike<any> {
|
|
238
|
-
return this.transform(identity, f);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
transform<U>(s: (value: any) => TryLike<U>, f: (e: Error) => TryLike<U>): TryLike<U> {
|
|
242
|
-
try {
|
|
243
|
-
return f(this.error);
|
|
244
|
-
} catch (ex) {
|
|
245
|
-
return failure(ex as Error);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
export function Try<T>(block: () => T): TryLike<T> {
|
|
254
|
-
try {
|
|
255
|
-
return new Success(block());
|
|
256
|
-
} catch (e) {
|
|
257
|
-
return new Failure(e as Error);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
export namespace Try {
|
|
262
|
-
export function promise<T>(block: () => Promise<T>): Promise<TryLike<T>> {
|
|
263
|
-
try {
|
|
264
|
-
return block()
|
|
265
|
-
.then(res => new Success(res))
|
|
266
|
-
.catch(e => new Failure(e));
|
|
267
|
-
} catch (e) {
|
|
268
|
-
return Promise.resolve(new Failure(e as Error));
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
export function success<T>(x: T): Success<T> {
|
|
276
|
-
return new Success<T>(x);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export function failure(x: Error): Failure {
|
|
280
|
-
return new Failure(x);
|
|
281
|
-
}
|
package/src/util.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import {none, Option, some} from './option';
|
|
2
|
-
import {Mappable} from './mappable';
|
|
3
|
-
|
|
4
|
-
export function identity<T>(x: T): T {
|
|
5
|
-
return x;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export function toErrorConversion(x: unknown): Error {
|
|
10
|
-
if (x instanceof Error) {
|
|
11
|
-
return x as Error;
|
|
12
|
-
} else {
|
|
13
|
-
return new Error(`${x}`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type StepFunction<R> = (state: any) => R;
|
|
18
|
-
export type StepCondition = (state: any) => boolean;
|
|
19
|
-
export interface Filterable<T, R extends Mappable<T>> {
|
|
20
|
-
filter(p: (x: T ) => boolean): R;
|
|
21
|
-
}
|
|
22
|
-
export type MaybeWithFilter<C extends Mappable<any>, R> = C extends Filterable<any, Mappable<any>> ? StepWithFilter<R> : Step<R>;
|
|
23
|
-
export type TaskMaybeWithFilter<C extends Mappable<any>> = C extends Filterable<any, Mappable<any>> ? TaskWithFilter<C> : Task<C>;
|
|
24
|
-
|
|
25
|
-
export interface Step<R> {
|
|
26
|
-
|
|
27
|
-
readonly name: Option<string>;
|
|
28
|
-
invokeStep(state: any): R;
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type Task<C extends Mappable<any>> = Step<Promise<C>>;
|
|
33
|
-
|
|
34
|
-
export class StepWithFilter<R> implements Step<R>{
|
|
35
|
-
|
|
36
|
-
constructor(readonly name: Option<string>,
|
|
37
|
-
readonly f: StepFunction<R>,
|
|
38
|
-
readonly filter: Option<StepCondition>) {
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if(condition: (state: any) => boolean): Step<R> {
|
|
42
|
-
return new StepWithFilter(this.name, this.f, some(condition));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
invokeStep(state: Record<string, unknown>): R {
|
|
46
|
-
const result = this.f(state);
|
|
47
|
-
return this.filter.filter(() => 'filter' in result).map(filter => {
|
|
48
|
-
return (result as unknown as Filterable<any, any>).filter(x => {
|
|
49
|
-
this.name.foreach(name => state[name] = x);
|
|
50
|
-
return filter(state);
|
|
51
|
-
}) as unknown as R;
|
|
52
|
-
}).getOrElseValue(result as unknown as R);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export class TaskWithFilter<C extends Mappable<any>> implements Task<C> {
|
|
58
|
-
|
|
59
|
-
constructor(readonly name: Option<string>,
|
|
60
|
-
readonly f: StepFunction<Promise<C>>,
|
|
61
|
-
readonly filter: Option<StepCondition>) {
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if(condition: (state: any) => boolean): Step<Promise<C>> {
|
|
65
|
-
return new TaskWithFilter(this.name, this.f, some(condition));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async invokeStep(state: Record<string, unknown>): Promise<C> {
|
|
69
|
-
const result = await this.f(state);
|
|
70
|
-
return this.filter.filter(() => 'filter' in result).map(filter => {
|
|
71
|
-
return (result as unknown as Filterable<any, any>).filter(x => {
|
|
72
|
-
this.name.foreach(name => state[name] = x);
|
|
73
|
-
return filter(state);
|
|
74
|
-
}) as C;
|
|
75
|
-
}).getOrElseValue(result as unknown as C);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function step<C extends Mappable<any>>(name: string, f: StepFunction<C>): MaybeWithFilter<C, C> {
|
|
81
|
-
return new StepWithFilter<C>(some(name), f, none) as MaybeWithFilter<C, C>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function task<C extends Mappable<any>>(name: string, f: StepFunction<Promise<C>>): TaskMaybeWithFilter<C> {
|
|
85
|
-
return new TaskWithFilter<C>(some(name), f, none) as MaybeWithFilter<C, Promise<C>>;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function forComprehension<C extends Mappable<any>>(...steps: Step<C>[]): { yield: (final: (state: any) => any) => C } {
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
yield: function(final: (state: any) => any): C {
|
|
92
|
-
|
|
93
|
-
function processStep(stepIdx: number, acc: any): C {
|
|
94
|
-
const result = steps[stepIdx].invokeStep(acc);
|
|
95
|
-
|
|
96
|
-
if (stepIdx < steps.length - 1) {
|
|
97
|
-
return result.flatMap(x => {
|
|
98
|
-
steps[stepIdx].name.foreach(name => acc[name] = x);
|
|
99
|
-
return processStep(stepIdx + 1, acc);
|
|
100
|
-
}) as C;
|
|
101
|
-
} else {
|
|
102
|
-
return result.map(x => {
|
|
103
|
-
steps[stepIdx].name.foreach(name => acc[name] = x);
|
|
104
|
-
return final(acc);
|
|
105
|
-
}) as C;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return processStep(0, {});
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export namespace forComprehension {
|
|
118
|
-
|
|
119
|
-
export function promise<C extends Mappable<any>>(...steps: Task<C>[]): { yield: (final: (state: any) => any) => Promise<C> } {
|
|
120
|
-
|
|
121
|
-
return {
|
|
122
|
-
yield: function (final: (state: any) => any): Promise<C> {
|
|
123
|
-
|
|
124
|
-
async function processStep(stepIdx: number, acc: any): Promise<C> {
|
|
125
|
-
const result = await steps[stepIdx].invokeStep(acc);
|
|
126
|
-
|
|
127
|
-
if (stepIdx < steps.length - 1) {
|
|
128
|
-
return await result.flatMapPromise(x => {
|
|
129
|
-
steps[stepIdx].name.foreach(name => acc[name] = x);
|
|
130
|
-
return processStep(stepIdx + 1, acc);
|
|
131
|
-
}) as C;
|
|
132
|
-
} else {
|
|
133
|
-
return result.map(x => {
|
|
134
|
-
steps[stepIdx].name.foreach(name => acc[name] = x);
|
|
135
|
-
return final(acc);
|
|
136
|
-
}) as C;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return processStep(0, {});
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
}
|