aang 2.0.0-alpha.3 → 2.0.0-alpha.30
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/bool.d.ts +30 -0
- package/lib/bool.js +69 -0
- package/lib/datetime.d.ts +19 -0
- package/lib/datetime.js +57 -0
- package/lib/double.d.ts +19 -0
- package/lib/double.js +49 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +12 -0
- package/lib/integer.d.ts +30 -0
- package/lib/integer.js +69 -0
- package/lib/miscellaneous.d.ts +5 -0
- package/lib/miscellaneous.js +6 -0
- package/lib/option.d.ts +58 -10
- package/lib/option.js +167 -13
- package/lib/order.d.ts +18 -0
- package/lib/order.js +1 -0
- package/lib/ordering.d.ts +7 -0
- package/lib/ordering.js +6 -0
- package/lib/pair.d.ts +106 -0
- package/lib/pair.js +322 -0
- package/lib/result.d.ts +125 -0
- package/lib/result.js +439 -0
- package/lib/semigroup.d.ts +3 -0
- package/lib/semigroup.js +1 -0
- package/lib/task.d.ts +36 -0
- package/lib/task.js +282 -0
- package/lib/text.d.ts +21 -0
- package/lib/text.js +50 -0
- package/package.json +1 -1
- package/lib/errors.d.ts +0 -4
- package/lib/errors.js +0 -7
package/lib/bool.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Option } from "./option.js";
|
|
2
|
+
import type { TotalOrder } from "./order.js";
|
|
3
|
+
import type { Ordering } from "./ordering.js";
|
|
4
|
+
import type { Semigroup } from "./semigroup.js";
|
|
5
|
+
export declare class Bool implements TotalOrder<Bool> {
|
|
6
|
+
readonly value: boolean;
|
|
7
|
+
constructor(value: boolean);
|
|
8
|
+
static of(value: boolean): Bool;
|
|
9
|
+
toString(this: Bool): string;
|
|
10
|
+
isSame(this: Bool, that: Bool): boolean;
|
|
11
|
+
isNotSame(this: Bool, that: Bool): boolean;
|
|
12
|
+
isLess(this: Bool, that: Bool): boolean;
|
|
13
|
+
isNotLess(this: Bool, that: Bool): boolean;
|
|
14
|
+
isMore(this: Bool, that: Bool): boolean;
|
|
15
|
+
isNotMore(this: Bool, that: Bool): boolean;
|
|
16
|
+
compare(this: Bool, that: Bool): Option<Ordering>;
|
|
17
|
+
max(this: Bool, that: Bool): Bool;
|
|
18
|
+
min(this: Bool, that: Bool): Bool;
|
|
19
|
+
clamp(this: Bool, lower: Bool, upper: Bool): Bool;
|
|
20
|
+
}
|
|
21
|
+
export declare class Any extends Bool implements Semigroup<Any> {
|
|
22
|
+
static of(value: boolean): Any;
|
|
23
|
+
toString(this: Any): string;
|
|
24
|
+
append(this: Any, that: Any): Any;
|
|
25
|
+
}
|
|
26
|
+
export declare class All extends Bool implements Semigroup<All> {
|
|
27
|
+
static of(value: boolean): All;
|
|
28
|
+
toString(this: All): string;
|
|
29
|
+
append(this: All, that: All): All;
|
|
30
|
+
}
|
package/lib/bool.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Some } from "./option.js";
|
|
2
|
+
export class Bool {
|
|
3
|
+
value;
|
|
4
|
+
constructor(value) {
|
|
5
|
+
this.value = value;
|
|
6
|
+
}
|
|
7
|
+
static of(value) {
|
|
8
|
+
return new Bool(value);
|
|
9
|
+
}
|
|
10
|
+
toString() {
|
|
11
|
+
return `Bool(${this.value})`;
|
|
12
|
+
}
|
|
13
|
+
isSame(that) {
|
|
14
|
+
return this.value === that.value;
|
|
15
|
+
}
|
|
16
|
+
isNotSame(that) {
|
|
17
|
+
return this.value !== that.value;
|
|
18
|
+
}
|
|
19
|
+
isLess(that) {
|
|
20
|
+
return this.value < that.value;
|
|
21
|
+
}
|
|
22
|
+
isNotLess(that) {
|
|
23
|
+
return this.value >= that.value;
|
|
24
|
+
}
|
|
25
|
+
isMore(that) {
|
|
26
|
+
return this.value > that.value;
|
|
27
|
+
}
|
|
28
|
+
isNotMore(that) {
|
|
29
|
+
return this.value <= that.value;
|
|
30
|
+
}
|
|
31
|
+
compare(that) {
|
|
32
|
+
if (this.value < that.value)
|
|
33
|
+
return new Some("<");
|
|
34
|
+
if (this.value > that.value)
|
|
35
|
+
return new Some(">");
|
|
36
|
+
return new Some("=");
|
|
37
|
+
}
|
|
38
|
+
max(that) {
|
|
39
|
+
return this.value >= that.value ? this : that;
|
|
40
|
+
}
|
|
41
|
+
min(that) {
|
|
42
|
+
return this.value <= that.value ? this : that;
|
|
43
|
+
}
|
|
44
|
+
clamp(lower, upper) {
|
|
45
|
+
return this.max(lower).min(upper);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class Any extends Bool {
|
|
49
|
+
static of(value) {
|
|
50
|
+
return new Any(value);
|
|
51
|
+
}
|
|
52
|
+
toString() {
|
|
53
|
+
return `Any(${this.value})`;
|
|
54
|
+
}
|
|
55
|
+
append(that) {
|
|
56
|
+
return new Any(this.value || that.value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class All extends Bool {
|
|
60
|
+
static of(value) {
|
|
61
|
+
return new All(value);
|
|
62
|
+
}
|
|
63
|
+
toString() {
|
|
64
|
+
return `All(${this.value})`;
|
|
65
|
+
}
|
|
66
|
+
append(that) {
|
|
67
|
+
return new All(this.value && that.value);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Option } from "./option.js";
|
|
2
|
+
import type { TotalOrder } from "./order.js";
|
|
3
|
+
import type { Ordering } from "./ordering.js";
|
|
4
|
+
export declare class DateTime implements TotalOrder<DateTime> {
|
|
5
|
+
readonly value: Date;
|
|
6
|
+
constructor(value: Date);
|
|
7
|
+
static of(value: Date): DateTime;
|
|
8
|
+
toString(this: DateTime): string;
|
|
9
|
+
isSame(this: DateTime, that: DateTime): boolean;
|
|
10
|
+
isNotSame(this: DateTime, that: DateTime): boolean;
|
|
11
|
+
isLess(this: DateTime, that: DateTime): boolean;
|
|
12
|
+
isNotLess(this: DateTime, that: DateTime): boolean;
|
|
13
|
+
isMore(this: DateTime, that: DateTime): boolean;
|
|
14
|
+
isNotMore(this: DateTime, that: DateTime): boolean;
|
|
15
|
+
compare(this: DateTime, that: DateTime): Option<Ordering>;
|
|
16
|
+
max(this: DateTime, that: DateTime): DateTime;
|
|
17
|
+
min(this: DateTime, that: DateTime): DateTime;
|
|
18
|
+
clamp(this: DateTime, lower: DateTime, upper: DateTime): DateTime;
|
|
19
|
+
}
|
package/lib/datetime.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { None, Some } from "./option.js";
|
|
2
|
+
export class DateTime {
|
|
3
|
+
value;
|
|
4
|
+
constructor(value) {
|
|
5
|
+
this.value = value;
|
|
6
|
+
}
|
|
7
|
+
static of(value) {
|
|
8
|
+
return new DateTime(value);
|
|
9
|
+
}
|
|
10
|
+
toString() {
|
|
11
|
+
return `DateTime(${this.value.getTime()})`;
|
|
12
|
+
}
|
|
13
|
+
isSame(that) {
|
|
14
|
+
return Object.is(this.value.getTime(), that.value.getTime());
|
|
15
|
+
}
|
|
16
|
+
isNotSame(that) {
|
|
17
|
+
return !this.isSame(that);
|
|
18
|
+
}
|
|
19
|
+
isLess(that) {
|
|
20
|
+
return this.value < that.value;
|
|
21
|
+
}
|
|
22
|
+
isNotLess(that) {
|
|
23
|
+
return this.value > that.value || this.isSame(that);
|
|
24
|
+
}
|
|
25
|
+
isMore(that) {
|
|
26
|
+
return this.value > that.value;
|
|
27
|
+
}
|
|
28
|
+
isNotMore(that) {
|
|
29
|
+
return this.value < that.value || this.isSame(that);
|
|
30
|
+
}
|
|
31
|
+
compare(that) {
|
|
32
|
+
if (this.isSame(that))
|
|
33
|
+
return new Some("=");
|
|
34
|
+
if (this.value < that.value)
|
|
35
|
+
return new Some("<");
|
|
36
|
+
if (this.value > that.value)
|
|
37
|
+
return new Some(">");
|
|
38
|
+
return None.instance;
|
|
39
|
+
}
|
|
40
|
+
max(that) {
|
|
41
|
+
if (Number.isNaN(this.value.getTime()))
|
|
42
|
+
return this;
|
|
43
|
+
if (Number.isNaN(that.value.getTime()))
|
|
44
|
+
return that;
|
|
45
|
+
return this.value >= that.value ? this : that;
|
|
46
|
+
}
|
|
47
|
+
min(that) {
|
|
48
|
+
if (Number.isNaN(this.value.getTime()))
|
|
49
|
+
return this;
|
|
50
|
+
if (Number.isNaN(that.value.getTime()))
|
|
51
|
+
return that;
|
|
52
|
+
return this.value <= that.value ? this : that;
|
|
53
|
+
}
|
|
54
|
+
clamp(lower, upper) {
|
|
55
|
+
return this.max(lower).min(upper);
|
|
56
|
+
}
|
|
57
|
+
}
|
package/lib/double.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Option } from "./option.js";
|
|
2
|
+
import type { TotalOrder } from "./order.js";
|
|
3
|
+
import type { Ordering } from "./ordering.js";
|
|
4
|
+
export declare class Double implements TotalOrder<Double> {
|
|
5
|
+
readonly value: number;
|
|
6
|
+
constructor(value: number);
|
|
7
|
+
static of(value: number): Double;
|
|
8
|
+
toString(this: Double): string;
|
|
9
|
+
isSame(this: Double, that: Double): boolean;
|
|
10
|
+
isNotSame(this: Double, that: Double): boolean;
|
|
11
|
+
isLess(this: Double, that: Double): boolean;
|
|
12
|
+
isNotLess(this: Double, that: Double): boolean;
|
|
13
|
+
isMore(this: Double, that: Double): boolean;
|
|
14
|
+
isNotMore(this: Double, that: Double): boolean;
|
|
15
|
+
compare(this: Double, that: Double): Option<Ordering>;
|
|
16
|
+
max(this: Double, that: Double): Double;
|
|
17
|
+
min(this: Double, that: Double): Double;
|
|
18
|
+
clamp(this: Double, lower: Double, upper: Double): Double;
|
|
19
|
+
}
|
package/lib/double.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { None, Some } from "./option.js";
|
|
2
|
+
export class Double {
|
|
3
|
+
value;
|
|
4
|
+
constructor(value) {
|
|
5
|
+
this.value = value;
|
|
6
|
+
}
|
|
7
|
+
static of(value) {
|
|
8
|
+
return new Double(value);
|
|
9
|
+
}
|
|
10
|
+
toString() {
|
|
11
|
+
return `Double(${this.value})`;
|
|
12
|
+
}
|
|
13
|
+
isSame(that) {
|
|
14
|
+
return Object.is(this.value, that.value);
|
|
15
|
+
}
|
|
16
|
+
isNotSame(that) {
|
|
17
|
+
return !Object.is(this.value, that.value);
|
|
18
|
+
}
|
|
19
|
+
isLess(that) {
|
|
20
|
+
return this.value < that.value;
|
|
21
|
+
}
|
|
22
|
+
isNotLess(that) {
|
|
23
|
+
return this.value > that.value || Object.is(this.value, that.value);
|
|
24
|
+
}
|
|
25
|
+
isMore(that) {
|
|
26
|
+
return this.value > that.value;
|
|
27
|
+
}
|
|
28
|
+
isNotMore(that) {
|
|
29
|
+
return this.value < that.value || Object.is(this.value, that.value);
|
|
30
|
+
}
|
|
31
|
+
compare(that) {
|
|
32
|
+
if (Object.is(this.value, that.value))
|
|
33
|
+
return new Some("=");
|
|
34
|
+
if (this.value < that.value)
|
|
35
|
+
return new Some("<");
|
|
36
|
+
if (this.value > that.value)
|
|
37
|
+
return new Some(">");
|
|
38
|
+
return None.instance;
|
|
39
|
+
}
|
|
40
|
+
max(that) {
|
|
41
|
+
return new Double(Math.max(this.value, that.value));
|
|
42
|
+
}
|
|
43
|
+
min(that) {
|
|
44
|
+
return new Double(Math.min(this.value, that.value));
|
|
45
|
+
}
|
|
46
|
+
clamp(lower, upper) {
|
|
47
|
+
return this.max(lower).min(upper);
|
|
48
|
+
}
|
|
49
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
|
+
export * from "./bool.js";
|
|
2
|
+
export * from "./datetime.js";
|
|
3
|
+
export * from "./double.js";
|
|
1
4
|
export * from "./exceptions.js";
|
|
5
|
+
export * from "./integer.js";
|
|
6
|
+
export * from "./miscellaneous.js";
|
|
2
7
|
export * from "./option.js";
|
|
8
|
+
export * from "./order.js";
|
|
9
|
+
export * from "./ordering.js";
|
|
10
|
+
export * from "./pair.js";
|
|
11
|
+
export * from "./result.js";
|
|
12
|
+
export * from "./semigroup.js";
|
|
13
|
+
export * from "./task.js";
|
|
14
|
+
export * from "./text.js";
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
|
+
export * from "./bool.js";
|
|
2
|
+
export * from "./datetime.js";
|
|
3
|
+
export * from "./double.js";
|
|
1
4
|
export * from "./exceptions.js";
|
|
5
|
+
export * from "./integer.js";
|
|
6
|
+
export * from "./miscellaneous.js";
|
|
2
7
|
export * from "./option.js";
|
|
8
|
+
export * from "./order.js";
|
|
9
|
+
export * from "./ordering.js";
|
|
10
|
+
export * from "./pair.js";
|
|
11
|
+
export * from "./result.js";
|
|
12
|
+
export * from "./semigroup.js";
|
|
13
|
+
export * from "./task.js";
|
|
14
|
+
export * from "./text.js";
|
package/lib/integer.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Option } from "./option.js";
|
|
2
|
+
import type { TotalOrder } from "./order.js";
|
|
3
|
+
import type { Ordering } from "./ordering.js";
|
|
4
|
+
import type { Semigroup } from "./semigroup.js";
|
|
5
|
+
export declare class Integer implements TotalOrder<Integer> {
|
|
6
|
+
readonly value: bigint;
|
|
7
|
+
constructor(value: bigint);
|
|
8
|
+
static of(value: bigint): Integer;
|
|
9
|
+
toString(this: Integer): string;
|
|
10
|
+
isSame(this: Integer, that: Integer): boolean;
|
|
11
|
+
isNotSame(this: Integer, that: Integer): boolean;
|
|
12
|
+
isLess(this: Integer, that: Integer): boolean;
|
|
13
|
+
isNotLess(this: Integer, that: Integer): boolean;
|
|
14
|
+
isMore(this: Integer, that: Integer): boolean;
|
|
15
|
+
isNotMore(this: Integer, that: Integer): boolean;
|
|
16
|
+
compare(this: Integer, that: Integer): Option<Ordering>;
|
|
17
|
+
max(this: Integer, that: Integer): Integer;
|
|
18
|
+
min(this: Integer, that: Integer): Integer;
|
|
19
|
+
clamp(this: Integer, lower: Integer, upper: Integer): Integer;
|
|
20
|
+
}
|
|
21
|
+
export declare class Sum extends Integer implements Semigroup<Sum> {
|
|
22
|
+
static of(value: bigint): Sum;
|
|
23
|
+
toString(this: Sum): string;
|
|
24
|
+
append(this: Sum, that: Sum): Sum;
|
|
25
|
+
}
|
|
26
|
+
export declare class Product extends Integer implements Semigroup<Product> {
|
|
27
|
+
static of(value: bigint): Product;
|
|
28
|
+
toString(this: Product): string;
|
|
29
|
+
append(this: Product, that: Product): Product;
|
|
30
|
+
}
|
package/lib/integer.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Some } from "./option.js";
|
|
2
|
+
export class Integer {
|
|
3
|
+
value;
|
|
4
|
+
constructor(value) {
|
|
5
|
+
this.value = value;
|
|
6
|
+
}
|
|
7
|
+
static of(value) {
|
|
8
|
+
return new Integer(value);
|
|
9
|
+
}
|
|
10
|
+
toString() {
|
|
11
|
+
return `Integer(${this.value})`;
|
|
12
|
+
}
|
|
13
|
+
isSame(that) {
|
|
14
|
+
return this.value === that.value;
|
|
15
|
+
}
|
|
16
|
+
isNotSame(that) {
|
|
17
|
+
return this.value !== that.value;
|
|
18
|
+
}
|
|
19
|
+
isLess(that) {
|
|
20
|
+
return this.value < that.value;
|
|
21
|
+
}
|
|
22
|
+
isNotLess(that) {
|
|
23
|
+
return this.value >= that.value;
|
|
24
|
+
}
|
|
25
|
+
isMore(that) {
|
|
26
|
+
return this.value > that.value;
|
|
27
|
+
}
|
|
28
|
+
isNotMore(that) {
|
|
29
|
+
return this.value <= that.value;
|
|
30
|
+
}
|
|
31
|
+
compare(that) {
|
|
32
|
+
if (this.value < that.value)
|
|
33
|
+
return new Some("<");
|
|
34
|
+
if (this.value > that.value)
|
|
35
|
+
return new Some(">");
|
|
36
|
+
return new Some("=");
|
|
37
|
+
}
|
|
38
|
+
max(that) {
|
|
39
|
+
return this.value >= that.value ? this : that;
|
|
40
|
+
}
|
|
41
|
+
min(that) {
|
|
42
|
+
return this.value <= that.value ? this : that;
|
|
43
|
+
}
|
|
44
|
+
clamp(lower, upper) {
|
|
45
|
+
return this.max(lower).min(upper);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class Sum extends Integer {
|
|
49
|
+
static of(value) {
|
|
50
|
+
return new Sum(value);
|
|
51
|
+
}
|
|
52
|
+
toString() {
|
|
53
|
+
return `Sum(${this.value})`;
|
|
54
|
+
}
|
|
55
|
+
append(that) {
|
|
56
|
+
return new Sum(this.value + that.value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class Product extends Integer {
|
|
60
|
+
static of(value) {
|
|
61
|
+
return new Product(value);
|
|
62
|
+
}
|
|
63
|
+
toString() {
|
|
64
|
+
return `Product(${this.value})`;
|
|
65
|
+
}
|
|
66
|
+
append(that) {
|
|
67
|
+
return new Product(this.value * that.value);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Pair } from "./pair.js";
|
|
2
|
+
export declare const id: <A>(value: A) => A;
|
|
3
|
+
export declare const uncurry2: <A, B, C>(morphism: (a: A, b: B) => C) => ({ fst: a, snd: b }: Pair<A, B>) => C;
|
|
4
|
+
export declare const uncurry3: <A, B, C, D>(morphism: (a: A, b: B, c: C) => D) => ({ fst: { fst: a, snd: b }, snd: c }: Pair<Pair<A, B>, C>) => D;
|
|
5
|
+
export declare const uncurry4: <A, B, C, D, E>(morphism: (a: A, b: B, c: C, d: D) => E) => ({ fst: { fst: { fst: a, snd: b }, snd: c }, snd: d }: Pair<Pair<Pair<A, B>, C>, D>) => E;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const id = (value) => value;
|
|
2
|
+
export const uncurry2 = (morphism) => ({ fst: a, snd: b }) => morphism(a, b);
|
|
3
|
+
export const uncurry3 = (morphism) => ({ fst: { fst: a, snd: b }, snd: c }) => morphism(a, b, c);
|
|
4
|
+
export const uncurry4 =
|
|
5
|
+
// prettier-ignore
|
|
6
|
+
(morphism) => ({ fst: { fst: { fst: a, snd: b }, snd: c }, snd: d }) => morphism(a, b, c, d);
|
package/lib/option.d.ts
CHANGED
|
@@ -1,23 +1,71 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { PartialOrder, Setoid, TotalOrder } from "./order.js";
|
|
2
|
+
import type { Ordering } from "./ordering.js";
|
|
3
|
+
import { Pair } from "./pair.js";
|
|
4
|
+
import type { Result } from "./result.js";
|
|
5
|
+
import type { Semigroup } from "./semigroup.js";
|
|
6
|
+
import { Task } from "./task.js";
|
|
2
7
|
export type Option<A> = Some<A> | None;
|
|
3
|
-
declare abstract class
|
|
8
|
+
declare abstract class OptionTrait {
|
|
4
9
|
abstract readonly isSome: boolean;
|
|
5
10
|
abstract readonly isNone: boolean;
|
|
11
|
+
toString<A>(this: Option<A>): string;
|
|
12
|
+
fold<A, B>(this: Option<A>, morphism: (value: A) => B, defaultValue: B): B;
|
|
6
13
|
map<A, B>(this: Option<A>, morphism: (value: A) => B): Option<B>;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
replace<A, B>(this: Option<A>, value: B): Option<B>;
|
|
15
|
+
and<A, B>(this: Option<A>, that: Option<B>): Option<Pair<A, B>>;
|
|
16
|
+
andThen<A, B>(this: Option<A>, that: Option<B>): Option<B>;
|
|
17
|
+
andWhen<A, B>(this: Option<A>, that: Option<B>): Option<A>;
|
|
18
|
+
or<A>(this: Option<A>, that: Option<A>): Option<A>;
|
|
19
|
+
flatMap<A, B>(this: Option<A>, arrow: (value: A) => Option<B>): Option<B>;
|
|
20
|
+
flatten<A>(this: Option<Option<A>>): Option<A>;
|
|
21
|
+
flatMapUntil<A, B>(this: Option<A>, arrow: (value: A) => Option<Result<B, A>>): Option<B>;
|
|
22
|
+
filter<A, B extends A>(this: Option<A>, predicate: (value: A) => value is B): Option<B>;
|
|
23
|
+
filter<A>(this: Option<A>, predicate: (value: A) => boolean): Option<A>;
|
|
24
|
+
isSomeAnd<A, B extends A>(this: Option<A>, predicate: (value: A) => value is B): this is Some<B>;
|
|
25
|
+
isSomeAnd<A>(this: Option<A>, predicate: (value: A) => boolean): this is Some<A>;
|
|
26
|
+
isNoneOr<A, B extends A>(this: Option<A>, predicate: (value: A) => value is B): this is Option<B>;
|
|
27
|
+
isNoneOr<A>(this: Option<A>, predicate: (value: A) => boolean): boolean;
|
|
28
|
+
unzipWith<A, B, C>(this: Option<A>, unzip: (value: A) => Pair<B, C>): Pair<Option<B>, Option<C>>;
|
|
29
|
+
unzip<A, B>(this: Option<Pair<A, B>>): Pair<Option<A>, Option<B>>;
|
|
30
|
+
transposeMapOkay<A, B, E>(this: Option<A>, transpose: (value: A) => Result<B, E>): Result<Option<B>, E>;
|
|
31
|
+
transposeMapFail<A, E, F>(this: Option<E>, transpose: (value: E) => Result<A, F>): Result<A, Option<F>>;
|
|
32
|
+
transposeOkay<A, E>(this: Option<Result<A, E>>): Result<Option<A>, E>;
|
|
33
|
+
transposeFail<A, E>(this: Option<Result<A, E>>): Result<A, Option<E>>;
|
|
34
|
+
exchangeMapOkay<A, B, E>(this: Option<A>, exchange: (value: A) => Task<B, E>): Task<Option<B>, E>;
|
|
35
|
+
exchangeMapFail<A, E, F>(this: Option<E>, exchange: (value: E) => Task<A, F>): Task<A, Option<F>>;
|
|
36
|
+
exchangeOkay<A, E>(this: Option<Task<A, E>>): Task<Option<A>, E>;
|
|
37
|
+
exchangeFail<A, E>(this: Option<Task<A, E>>): Task<A, Option<E>>;
|
|
38
|
+
extractSome<A>(this: Option<A>, defaultValue: A): A;
|
|
39
|
+
extractMapSome<A>(this: Option<A>, getDefaultValue: () => A): A;
|
|
40
|
+
toResultOkay<A, E>(this: Option<A>, defaultValue: E): Result<A, E>;
|
|
41
|
+
toResultFail<A, E>(this: Option<E>, defaultValue: A): Result<A, E>;
|
|
42
|
+
append<A extends Semigroup<A>>(this: Option<A>, that: Option<A>): Option<A>;
|
|
43
|
+
isSame<A extends Setoid<A>>(this: Option<A>, that: Option<A>): boolean;
|
|
44
|
+
isNotSame<A extends Setoid<A>>(this: Option<A>, that: Option<A>): boolean;
|
|
45
|
+
isLess<A extends PartialOrder<A>>(this: Option<A>, that: Option<A>): boolean;
|
|
46
|
+
isNotLess<A extends PartialOrder<A>>(this: Option<A>, that: Option<A>): boolean;
|
|
47
|
+
isMore<A extends PartialOrder<A>>(this: Option<A>, that: Option<A>): boolean;
|
|
48
|
+
isNotMore<A extends PartialOrder<A>>(this: Option<A>, that: Option<A>): boolean;
|
|
49
|
+
compare<A extends PartialOrder<A>>(this: Option<A>, that: Option<A>): Option<Ordering>;
|
|
50
|
+
max<A extends TotalOrder<A>>(this: Option<A>, that: Option<A>): Option<A>;
|
|
51
|
+
min<A extends TotalOrder<A>>(this: Option<A>, that: Option<A>): Option<A>;
|
|
52
|
+
clamp<A extends TotalOrder<A>>(this: Option<A>, lower: Option<A>, upper: Option<A>): Option<A>;
|
|
53
|
+
values<A>(this: Option<A>): Generator<A, void, undefined>;
|
|
10
54
|
}
|
|
11
|
-
declare class Some<out A> extends
|
|
55
|
+
export declare class Some<out A> extends OptionTrait {
|
|
12
56
|
readonly value: A;
|
|
13
57
|
readonly isSome = true;
|
|
14
58
|
readonly isNone = false;
|
|
15
59
|
constructor(value: A);
|
|
60
|
+
static of<A>(value: A): Some<A>;
|
|
61
|
+
static fromValid<A, B extends A>(value: A, validate: (value: A) => value is B): Option<B>;
|
|
62
|
+
static fromValid<A>(value: A, validate: (value: A) => boolean): Option<A>;
|
|
16
63
|
}
|
|
17
|
-
declare class None extends
|
|
64
|
+
export declare class None extends OptionTrait {
|
|
18
65
|
readonly isSome = false;
|
|
19
66
|
readonly isNone = true;
|
|
67
|
+
static readonly instance: None;
|
|
68
|
+
static fromNullish<A>(value: A): Option<NonNullable<A>>;
|
|
69
|
+
static fromFalsy<A>(value: A): Option<NonNullable<A>>;
|
|
20
70
|
}
|
|
21
|
-
export
|
|
22
|
-
export declare const none: None;
|
|
23
|
-
export type { Some, None };
|
|
71
|
+
export {};
|
package/lib/option.js
CHANGED
|
@@ -1,22 +1,165 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Pair } from "./pair.js";
|
|
2
|
+
import { Fail, Okay } from "./result.js";
|
|
3
|
+
import { Task } from "./task.js";
|
|
4
|
+
class OptionTrait {
|
|
5
|
+
toString() {
|
|
6
|
+
return this.isSome ? `Some(${String(this.value)})` : "None";
|
|
7
|
+
}
|
|
8
|
+
fold(morphism, defaultValue) {
|
|
9
|
+
return this.isSome ? morphism(this.value) : defaultValue;
|
|
10
|
+
}
|
|
4
11
|
map(morphism) {
|
|
5
|
-
return this.isSome ? new Some(morphism(this.value)) :
|
|
12
|
+
return this.isSome ? new Some(morphism(this.value)) : None.instance;
|
|
13
|
+
}
|
|
14
|
+
replace(value) {
|
|
15
|
+
return this.isSome ? new Some(value) : None.instance;
|
|
16
|
+
}
|
|
17
|
+
and(that) {
|
|
18
|
+
return this.isSome && that.isSome
|
|
19
|
+
? new Some(new Pair(this.value, that.value))
|
|
20
|
+
: None.instance;
|
|
21
|
+
}
|
|
22
|
+
andThen(that) {
|
|
23
|
+
return this.isSome ? that : None.instance;
|
|
24
|
+
}
|
|
25
|
+
andWhen(that) {
|
|
26
|
+
return this.isSome && that.isSome ? this : None.instance;
|
|
27
|
+
}
|
|
28
|
+
or(that) {
|
|
29
|
+
return this.isSome ? this : that;
|
|
30
|
+
}
|
|
31
|
+
flatMap(arrow) {
|
|
32
|
+
return this.isSome ? arrow(this.value) : None.instance;
|
|
33
|
+
}
|
|
34
|
+
flatten() {
|
|
35
|
+
return this.isSome ? this.value : None.instance;
|
|
36
|
+
}
|
|
37
|
+
flatMapUntil(arrow) {
|
|
38
|
+
let result = this.flatMap(arrow).transposeOkay();
|
|
39
|
+
while (result.isFail)
|
|
40
|
+
result = arrow(result.value).transposeOkay();
|
|
41
|
+
return result.value;
|
|
42
|
+
}
|
|
43
|
+
filter(predicate) {
|
|
44
|
+
return this.isSome && predicate(this.value) ? this : None.instance;
|
|
45
|
+
}
|
|
46
|
+
isSomeAnd(predicate) {
|
|
47
|
+
return this.isSome && predicate(this.value);
|
|
48
|
+
}
|
|
49
|
+
isNoneOr(predicate) {
|
|
50
|
+
return this.isNone || predicate(this.value);
|
|
51
|
+
}
|
|
52
|
+
unzipWith(unzip) {
|
|
53
|
+
return this.isNone
|
|
54
|
+
? Pair.from(None.instance)
|
|
55
|
+
: unzip(this.value).map(Some.of, Some.of);
|
|
56
|
+
}
|
|
57
|
+
unzip() {
|
|
58
|
+
return this.isNone
|
|
59
|
+
? Pair.from(None.instance)
|
|
60
|
+
: this.value.map(Some.of, Some.of);
|
|
61
|
+
}
|
|
62
|
+
transposeMapOkay(transpose) {
|
|
63
|
+
return this.isNone
|
|
64
|
+
? new Okay(None.instance)
|
|
65
|
+
: transpose(this.value).mapOkay(Some.of);
|
|
66
|
+
}
|
|
67
|
+
transposeMapFail(transpose) {
|
|
68
|
+
return this.isNone
|
|
69
|
+
? new Fail(None.instance)
|
|
70
|
+
: transpose(this.value).mapFail(Some.of);
|
|
71
|
+
}
|
|
72
|
+
transposeOkay() {
|
|
73
|
+
return this.isNone ? new Okay(None.instance) : this.value.mapOkay(Some.of);
|
|
74
|
+
}
|
|
75
|
+
transposeFail() {
|
|
76
|
+
return this.isNone ? new Fail(None.instance) : this.value.mapFail(Some.of);
|
|
77
|
+
}
|
|
78
|
+
exchangeMapOkay(exchange) {
|
|
79
|
+
return this.isNone
|
|
80
|
+
? Task.okay(None.instance)
|
|
81
|
+
: exchange(this.value).mapOkay(Some.of);
|
|
6
82
|
}
|
|
7
|
-
|
|
83
|
+
exchangeMapFail(exchange) {
|
|
84
|
+
return this.isNone
|
|
85
|
+
? Task.fail(None.instance)
|
|
86
|
+
: exchange(this.value).mapFail(Some.of);
|
|
87
|
+
}
|
|
88
|
+
exchangeOkay() {
|
|
89
|
+
return this.isNone ? Task.okay(None.instance) : this.value.mapOkay(Some.of);
|
|
90
|
+
}
|
|
91
|
+
exchangeFail() {
|
|
92
|
+
return this.isNone ? Task.fail(None.instance) : this.value.mapFail(Some.of);
|
|
93
|
+
}
|
|
94
|
+
extractSome(defaultValue) {
|
|
8
95
|
return this.isSome ? this.value : defaultValue;
|
|
9
96
|
}
|
|
10
|
-
|
|
97
|
+
extractMapSome(getDefaultValue) {
|
|
11
98
|
return this.isSome ? this.value : getDefaultValue();
|
|
12
99
|
}
|
|
13
|
-
|
|
100
|
+
toResultOkay(defaultValue) {
|
|
101
|
+
return this.isSome ? new Okay(this.value) : new Fail(defaultValue);
|
|
102
|
+
}
|
|
103
|
+
toResultFail(defaultValue) {
|
|
104
|
+
return this.isSome ? new Fail(this.value) : new Okay(defaultValue);
|
|
105
|
+
}
|
|
106
|
+
append(that) {
|
|
107
|
+
if (this.isNone)
|
|
108
|
+
return that;
|
|
109
|
+
if (that.isNone)
|
|
110
|
+
return this;
|
|
111
|
+
return new Some(this.value.append(that.value));
|
|
112
|
+
}
|
|
113
|
+
isSame(that) {
|
|
114
|
+
return this.isSome
|
|
115
|
+
? that.isSome && this.value.isSame(that.value)
|
|
116
|
+
: that.isNone;
|
|
117
|
+
}
|
|
118
|
+
isNotSame(that) {
|
|
119
|
+
return this.isSome
|
|
120
|
+
? that.isNone || this.value.isNotSame(that.value)
|
|
121
|
+
: that.isSome;
|
|
122
|
+
}
|
|
123
|
+
isLess(that) {
|
|
124
|
+
return that.isSome && (this.isNone || this.value.isLess(that.value));
|
|
125
|
+
}
|
|
126
|
+
isNotLess(that) {
|
|
127
|
+
return that.isNone || (this.isSome && this.value.isNotLess(that.value));
|
|
128
|
+
}
|
|
129
|
+
isMore(that) {
|
|
130
|
+
return this.isSome && (that.isNone || this.value.isMore(that.value));
|
|
131
|
+
}
|
|
132
|
+
isNotMore(that) {
|
|
133
|
+
return this.isNone || (that.isSome && this.value.isNotMore(that.value));
|
|
134
|
+
}
|
|
135
|
+
compare(that) {
|
|
136
|
+
if (this.isNone)
|
|
137
|
+
return new Some(that.isSome ? "<" : "=");
|
|
138
|
+
if (that.isNone)
|
|
139
|
+
return new Some(">");
|
|
140
|
+
return this.value.compare(that.value);
|
|
141
|
+
}
|
|
142
|
+
max(that) {
|
|
143
|
+
if (this.isNone)
|
|
144
|
+
return that;
|
|
145
|
+
if (that.isNone)
|
|
146
|
+
return this;
|
|
147
|
+
return new Some(this.value.max(that.value));
|
|
148
|
+
}
|
|
149
|
+
min(that) {
|
|
150
|
+
if (this.isNone || that.isNone)
|
|
151
|
+
return None.instance;
|
|
152
|
+
return new Some(this.value.min(that.value));
|
|
153
|
+
}
|
|
154
|
+
clamp(lower, upper) {
|
|
155
|
+
return this.max(lower).min(upper);
|
|
156
|
+
}
|
|
157
|
+
*values() {
|
|
14
158
|
if (this.isSome)
|
|
15
|
-
|
|
16
|
-
throw error instanceof Exception ? error : new UnsafeExtractError(error);
|
|
159
|
+
yield this.value;
|
|
17
160
|
}
|
|
18
161
|
}
|
|
19
|
-
class Some extends
|
|
162
|
+
export class Some extends OptionTrait {
|
|
20
163
|
value;
|
|
21
164
|
isSome = true;
|
|
22
165
|
isNone = false;
|
|
@@ -24,10 +167,21 @@ class Some extends OptionMethods {
|
|
|
24
167
|
super();
|
|
25
168
|
this.value = value;
|
|
26
169
|
}
|
|
170
|
+
static of(value) {
|
|
171
|
+
return new Some(value);
|
|
172
|
+
}
|
|
173
|
+
static fromValid(value, validate) {
|
|
174
|
+
return validate(value) ? new Some(value) : None.instance;
|
|
175
|
+
}
|
|
27
176
|
}
|
|
28
|
-
class None extends
|
|
177
|
+
export class None extends OptionTrait {
|
|
29
178
|
isSome = false;
|
|
30
179
|
isNone = true;
|
|
180
|
+
static instance = new None();
|
|
181
|
+
static fromNullish(value) {
|
|
182
|
+
return value == null ? None.instance : new Some(value);
|
|
183
|
+
}
|
|
184
|
+
static fromFalsy(value) {
|
|
185
|
+
return value ? new Some(value) : None.instance;
|
|
186
|
+
}
|
|
31
187
|
}
|
|
32
|
-
export const some = (value) => new Some(value);
|
|
33
|
-
export const none = new None();
|