aang 2.0.0-alpha.21 → 2.0.0-alpha.23
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/lib/option.d.ts +4 -0
- package/lib/option.js +26 -0
- package/package.json +1 -1
package/lib/option.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ declare abstract class OptionTrait implements Semigroup<Option<never>>, TotalOrd
|
|
|
17
17
|
or<A>(this: Option<A>, that: Option<A>): Option<A>;
|
|
18
18
|
flatMap<A, B>(this: Option<A>, arrow: (value: A) => Option<B>): Option<B>;
|
|
19
19
|
flatten<A>(this: Option<Option<A>>): Option<A>;
|
|
20
|
+
flatMapUntil<A, B>(this: Option<A>, arrow: (value: A) => Option<Result<A, B>>): Option<B>;
|
|
20
21
|
filter<A, B extends A>(this: Option<A>, predicate: (value: A) => value is B): Option<B>;
|
|
21
22
|
filter<A>(this: Option<A>, predicate: (value: A) => boolean): Option<A>;
|
|
22
23
|
isSomeAnd<A, B extends A>(this: Option<A>, predicate: (value: A) => value is B): this is Option<B>;
|
|
@@ -42,6 +43,8 @@ declare abstract class OptionTrait implements Semigroup<Option<never>>, TotalOrd
|
|
|
42
43
|
min<A extends TotalOrder<A>>(this: Option<A>, that: Option<A>): Option<A>;
|
|
43
44
|
clamp<A extends TotalOrder<A>>(this: Option<A>, lower: Option<A>, upper: Option<A>): Option<A>;
|
|
44
45
|
[Symbol.iterator]<A>(this: Option<A>): Generator<A, void, undefined>;
|
|
46
|
+
effectMap<A, B>(this: Option<A>, morphism: (value: A) => B): Generator<Option<A>, B, A>;
|
|
47
|
+
effect<A>(this: Option<A>): Generator<Option<A>, A, A>;
|
|
45
48
|
}
|
|
46
49
|
export declare class Some<out A> extends OptionTrait {
|
|
47
50
|
readonly value: A;
|
|
@@ -59,4 +62,5 @@ export declare const fromNullable: <A>(value: A) => Option<NonNullable<A>>;
|
|
|
59
62
|
export declare const fromFalsy: <A>(value: A) => Option<NonNullable<A>>;
|
|
60
63
|
export declare function fromValue<A, B extends A>(value: A, predicate: (value: A) => value is B): Option<B>;
|
|
61
64
|
export declare function fromValue<A>(value: A, predicate: (value: A) => boolean): Option<A>;
|
|
65
|
+
export declare const fromGenerator: <A>(getGenerator: () => Generator<Option<unknown>, A, unknown>) => Option<A>;
|
|
62
66
|
export {};
|
package/lib/option.js
CHANGED
|
@@ -32,6 +32,12 @@ class OptionTrait {
|
|
|
32
32
|
flatten() {
|
|
33
33
|
return this.isSome ? this.value : None.instance;
|
|
34
34
|
}
|
|
35
|
+
flatMapUntil(arrow) {
|
|
36
|
+
let result = this.flatMap(arrow).transpose();
|
|
37
|
+
while (result.isFail)
|
|
38
|
+
result = arrow(result.value).transpose();
|
|
39
|
+
return result.value;
|
|
40
|
+
}
|
|
35
41
|
filter(predicate) {
|
|
36
42
|
return this.isSome && predicate(this.value) ? this : None.instance;
|
|
37
43
|
}
|
|
@@ -125,6 +131,14 @@ class OptionTrait {
|
|
|
125
131
|
if (this.isSome)
|
|
126
132
|
yield this.value;
|
|
127
133
|
}
|
|
134
|
+
*effectMap(morphism) {
|
|
135
|
+
const value = yield this;
|
|
136
|
+
return morphism(value);
|
|
137
|
+
}
|
|
138
|
+
*effect() {
|
|
139
|
+
const value = yield this;
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
128
142
|
}
|
|
129
143
|
export class Some extends OptionTrait {
|
|
130
144
|
value;
|
|
@@ -148,3 +162,15 @@ export const fromFalsy = (value) => value ? new Some(value) : None.instance;
|
|
|
148
162
|
export function fromValue(value, predicate) {
|
|
149
163
|
return predicate(value) ? new Some(value) : None.instance;
|
|
150
164
|
}
|
|
165
|
+
// TODO: <A, B>(getGenerator: () => Generator<Option<A>, B, A>) => Option<B>
|
|
166
|
+
export const fromGenerator = (getGenerator) => {
|
|
167
|
+
const generator = getGenerator();
|
|
168
|
+
let result = generator.next();
|
|
169
|
+
while (!result.done) {
|
|
170
|
+
const option = result.value;
|
|
171
|
+
if (option.isNone)
|
|
172
|
+
return None.instance;
|
|
173
|
+
result = generator.next(option.value);
|
|
174
|
+
}
|
|
175
|
+
return new Some(result.value);
|
|
176
|
+
};
|