@tempots/std 0.9.6 → 0.10.3
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/README.md +1 -3
- package/array.cjs +1 -0
- package/array.d.ts +49 -0
- package/array.js +223 -0
- package/bigint.cjs +1 -0
- package/bigint.d.ts +18 -0
- package/bigint.js +96 -0
- package/boolean.cjs +1 -0
- package/boolean.d.ts +23 -0
- package/boolean.js +44 -0
- package/domain.cjs +1 -0
- package/domain.d.ts +22 -0
- package/domain.js +1 -0
- package/equal.cjs +1 -0
- package/equal.d.ts +3 -0
- package/equal.js +68 -0
- package/function.cjs +1 -0
- package/function.d.ts +20 -0
- package/function.js +29 -0
- package/index.cjs +1 -0
- package/index.d.ts +0 -0
- package/index.js +1 -0
- package/maybe.cjs +1 -0
- package/maybe.d.ts +9 -0
- package/maybe.js +9 -0
- package/number.cjs +1 -0
- package/number.d.ts +101 -0
- package/number.js +103 -0
- package/object.cjs +1 -0
- package/object.d.ts +8 -0
- package/object.js +30 -0
- package/package.json +55 -25
- package/regexp.cjs +1 -0
- package/regexp.d.ts +10 -0
- package/regexp.js +14 -0
- package/string.cjs +3 -0
- package/string.d.ts +312 -0
- package/string.js +396 -0
- package/src/arrays.ts +0 -296
- package/src/async-result.ts +0 -103
- package/src/bigint.ts +0 -111
- package/src/booleans.ts +0 -73
- package/src/colors/cmyk.ts +0 -84
- package/src/colors/convert.ts +0 -1093
- package/src/colors/hsl.ts +0 -73
- package/src/colors/hsla.ts +0 -45
- package/src/colors/hsluv.ts +0 -73
- package/src/colors/hsv.ts +0 -75
- package/src/colors/lab.ts +0 -69
- package/src/colors/lch.ts +0 -53
- package/src/colors/luv.ts +0 -56
- package/src/colors/rgb.ts +0 -55
- package/src/colors/rgba.ts +0 -53
- package/src/colors/srgb.ts +0 -72
- package/src/colors/xyz.ts +0 -52
- package/src/edit.ts +0 -29
- package/src/equals.ts +0 -116
- package/src/functions.ts +0 -108
- package/src/json.ts +0 -52
- package/src/match.ts +0 -88
- package/src/maybe.ts +0 -32
- package/src/memoize.ts +0 -9
- package/src/newtype.ts +0 -59
- package/src/numbers.ts +0 -222
- package/src/objects.ts +0 -47
- package/src/ord.ts +0 -79
- package/src/reg-exps.ts +0 -48
- package/src/result.ts +0 -140
- package/src/strings.ts +0 -768
- package/src/types/assert.ts +0 -96
- package/src/types/differentiate.ts +0 -89
- package/src/types/functions.ts +0 -114
- package/src/types/generic.ts +0 -42
- package/src/types/objects.ts +0 -212
- package/src/types/tuples.ts +0 -244
- package/src/types/utility.ts +0 -3
- package/src/uuid.ts +0 -61
- package/src/validation.ts +0 -69
- package/test/arrays.spec.ts +0 -410
- package/test/colors.spec.ts +0 -406
- package/test/commmon.ts +0 -9
- package/test/equals.spec.ts +0 -165
- package/test/functions.spec.ts +0 -9
- package/test/index.d.ts +0 -20
- package/test/objects.spec.ts +0 -22
- package/test/reg-exps.spec.ts +0 -33
- package/test/strings.spec.ts +0 -333
- package/test/uuid.spec.ts +0 -35
- package/tsconfig.json +0 -19
package/src/numbers.ts
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { lpad } from './strings'
|
|
2
|
-
|
|
3
|
-
export const TOLERANCE = 10e-5
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
Constant value employed to see if two `number` values are very close.
|
|
7
|
-
**/
|
|
8
|
-
export const EPSILON = 1e-9
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
Returns the angular distance between 2 angles.
|
|
12
|
-
**/
|
|
13
|
-
export function angleDifference (a: number, b: number, turn = 360.0): number {
|
|
14
|
-
let r = (b - a) % turn
|
|
15
|
-
if (r < 0) r += turn
|
|
16
|
-
if (r > turn / 2) r -= turn
|
|
17
|
-
return r
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
Rounds a number up to the specified number of decimals.
|
|
22
|
-
**/
|
|
23
|
-
export function ceilTo (v: number, decimals: number): number {
|
|
24
|
-
const p = Math.pow(10, decimals)
|
|
25
|
-
return Math.ceil(v * p) / p
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
`clamp` restricts a value within the specified range.
|
|
30
|
-
```ts
|
|
31
|
-
log(clamp(1.3, 0, 1)) // prints 1
|
|
32
|
-
log(clamp(0.8, 0, 1)) // prints 0.8
|
|
33
|
-
log(clamp(-0.5, 0, 1)) // prints 0.0
|
|
34
|
-
```
|
|
35
|
-
**/
|
|
36
|
-
export function clamp (value: number, min: number, max: number): number {
|
|
37
|
-
return Math.min(Math.max(value, min), max)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function clampInt (value: number, min: number, max: number): number {
|
|
41
|
-
return Math.trunc(clamp(value, min, max))
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
Like clamp but you only pass one argument (`max`) that is used as the upper limit
|
|
46
|
-
and the opposite (additive inverse or `-max`) as the lower limit.
|
|
47
|
-
**/
|
|
48
|
-
export function clampSym (v: number, max: number): number {
|
|
49
|
-
return clamp(v, -max, max)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
It returns the comparison value (an integer number) between two `float` values.
|
|
54
|
-
**/
|
|
55
|
-
export function compare (a: number, b: number): number {
|
|
56
|
-
return a < b ? -1 : a > b ? 1 : 0
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
Rounds a number down to the specified number of decimals.
|
|
61
|
-
**/
|
|
62
|
-
export function floorTo (v: number, decimals: number): number {
|
|
63
|
-
const p = Math.pow(10, decimals)
|
|
64
|
-
return Math.floor(v * p) / p
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
`normalize` clamps the passwed value between 0 and 1.
|
|
69
|
-
**/
|
|
70
|
-
export function normalize (value: number): number {
|
|
71
|
-
return clamp(value, 0, 1)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function toHex (num: number, length = 0): string {
|
|
75
|
-
return lpad(num.toString(16), '0', length)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
`interpolate` returns a value between `a` and `b` for any value of `t` (normally between 0 and 1).
|
|
80
|
-
**/
|
|
81
|
-
export function interpolate (a: number, b: number, t: number): number {
|
|
82
|
-
return (b - a) * t + a
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
Interpolates values in a polar coordinate system looking for the narrowest delta angle.
|
|
87
|
-
It can be either clock-wise or counter-clock-wise.
|
|
88
|
-
**/
|
|
89
|
-
export function interpolateAngle (
|
|
90
|
-
a: number,
|
|
91
|
-
b: number,
|
|
92
|
-
t: number,
|
|
93
|
-
turn = 360.0
|
|
94
|
-
): number {
|
|
95
|
-
return wrapCircular(interpolate(a, a + angleDifference(a, b, turn), t), turn)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
Interpolates values in a polar coordinate system looking for the wideset delta angle.
|
|
100
|
-
It can be either clock-wise or counter-clock-wise.
|
|
101
|
-
**/
|
|
102
|
-
export function interpolateAngleWidest (
|
|
103
|
-
a: number,
|
|
104
|
-
b: number,
|
|
105
|
-
t: number,
|
|
106
|
-
turn = 360
|
|
107
|
-
): number {
|
|
108
|
-
return wrapCircular(interpolateAngle(a, b, t, turn) - turn / 2, turn)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
Interpolates values in a polar coordinate system always in clock-wise direction.
|
|
113
|
-
**/
|
|
114
|
-
export function interpolateAngleCW (
|
|
115
|
-
a: number,
|
|
116
|
-
b: number,
|
|
117
|
-
t: number,
|
|
118
|
-
turn = 360
|
|
119
|
-
): number {
|
|
120
|
-
a = wrapCircular(a, turn)
|
|
121
|
-
b = wrapCircular(b, turn)
|
|
122
|
-
if (b < a) b += turn
|
|
123
|
-
return wrapCircular(interpolate(a, b, t), turn)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
Interpolates values in a polar coordinate system always in counter-clock-wise direction.
|
|
128
|
-
**/
|
|
129
|
-
export function interpolateAngleCCW (
|
|
130
|
-
a: number,
|
|
131
|
-
b: number,
|
|
132
|
-
t: number,
|
|
133
|
-
turn = 360
|
|
134
|
-
): number {
|
|
135
|
-
a = wrapCircular(a, turn)
|
|
136
|
-
b = wrapCircular(b, turn)
|
|
137
|
-
if (b > a) b -= turn
|
|
138
|
-
return wrapCircular(interpolate(a, b, t), turn)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
number numbers can sometime introduce tiny errors even for simple operations.
|
|
143
|
-
`nearEquals` compares two floats using a tiny tollerance (last optional
|
|
144
|
-
argument). By default it is defined as `EPSILON`.
|
|
145
|
-
**/
|
|
146
|
-
export function nearEquals (a: number, b: number, tollerance = EPSILON): boolean {
|
|
147
|
-
if (isFinite(a)) {
|
|
148
|
-
if (!isFinite(b)) return false
|
|
149
|
-
return Math.abs(a - b) <= tollerance
|
|
150
|
-
}
|
|
151
|
-
if (isNaN(a)) return isNaN(b)
|
|
152
|
-
if (isNaN(b)) return false
|
|
153
|
-
if (!isFinite(b)) return (a > 0) === (b > 0)
|
|
154
|
-
// a is Infinity and b is finite
|
|
155
|
-
return false
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
number numbers can sometime introduce tiny errors even for simple operations.
|
|
160
|
-
`nearEqualAngles` compares two angles (default is 360deg) using a tiny
|
|
161
|
-
tollerance (last optional argument). By default the tollerance is defined as
|
|
162
|
-
`EPSILON`.
|
|
163
|
-
**/
|
|
164
|
-
export function nearEqualAngles (
|
|
165
|
-
a: number,
|
|
166
|
-
b: number,
|
|
167
|
-
turn = 360.0,
|
|
168
|
-
tollerance = EPSILON
|
|
169
|
-
): boolean {
|
|
170
|
-
return Math.abs(angleDifference(a, b, turn)) <= tollerance
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
`nearZero` finds if the passed number is zero or very close to it. By default
|
|
175
|
-
`EPSILON` is used as the tollerance value.
|
|
176
|
-
**/
|
|
177
|
-
export function nearZero (n: number, tollerance = EPSILON): boolean {
|
|
178
|
-
return Math.abs(n) <= tollerance
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
Computes the nth root (`index`) of `base`.
|
|
183
|
-
**/
|
|
184
|
-
export function root (base: number, index: number): number {
|
|
185
|
-
return Math.pow(base, 1 / index)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
Rounds a number to the specified number of decimals.
|
|
190
|
-
**/
|
|
191
|
-
export function roundTo (f: number, decimals: number): number {
|
|
192
|
-
const p = Math.pow(10, decimals)
|
|
193
|
-
return Math.fround(f * p) / p
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
`sign` returns `-1` if `value` is a negative number, `1` otherwise.
|
|
198
|
-
*/
|
|
199
|
-
export function sign<T extends number> (value: T): number {
|
|
200
|
-
return value < 0 ? -1 : 1
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
Passed two boundaries values (`min`, `max`), `wrap` ensures that the passed value `v` will
|
|
205
|
-
be included in the boundaries. If the value exceeds `max`, the value is reduced by `min`
|
|
206
|
-
repeatedely until it falls within the range. Similar and inverted treatment is performed if
|
|
207
|
-
the value is below `min`.
|
|
208
|
-
**/
|
|
209
|
-
export function wrap (v: number, min: number, max: number): number {
|
|
210
|
-
const range = max - min + 1
|
|
211
|
-
if (v < min) v += range * ((min - v) / range + 1)
|
|
212
|
-
return min + ((v - min) % range)
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
Similar to `wrap`, it works for numbers between 0 and `max`.
|
|
217
|
-
**/
|
|
218
|
-
export function wrapCircular (v: number, max: number): number {
|
|
219
|
-
v = v % max
|
|
220
|
-
if (v < 0) v += max
|
|
221
|
-
return v
|
|
222
|
-
}
|
package/src/objects.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { type TupleToUnion } from './types/tuples'
|
|
2
|
-
import { type Merge } from './types/objects'
|
|
3
|
-
import { type AnyKey } from './types/utility'
|
|
4
|
-
|
|
5
|
-
export function keys<T extends object> (obj: T): Array<keyof T> {
|
|
6
|
-
return Object.keys(obj) as Array<keyof T>
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function sameKeys<T extends object> (a: T, b: T): boolean {
|
|
10
|
-
const ak = keys(a)
|
|
11
|
-
const bk = keys(b)
|
|
12
|
-
if (ak.length !== bk.length) return false
|
|
13
|
-
for (const k of ak) {
|
|
14
|
-
if (!(k in b)) return false
|
|
15
|
-
}
|
|
16
|
-
return true
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isObject (obj: unknown): obj is Record<AnyKey, unknown> {
|
|
20
|
-
return Object.prototype.toString.call(obj) === '[object Object]'
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function removeFields<T extends object, F extends Array<keyof T>> (
|
|
24
|
-
ob: T,
|
|
25
|
-
...fields: F
|
|
26
|
-
): Omit<T, TupleToUnion<F>> {
|
|
27
|
-
const ks = keys(ob)
|
|
28
|
-
return ks.reduce((acc: Record<AnyKey, unknown>, key) => {
|
|
29
|
-
if (!fields.includes(key)) acc[key] = ob[key]
|
|
30
|
-
return acc
|
|
31
|
-
}, {}) as Omit<T, TupleToUnion<F>>
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function merge<
|
|
35
|
-
A extends Record<AnyKey, unknown>,
|
|
36
|
-
B extends Record<AnyKey, unknown>
|
|
37
|
-
> (a: A, b: B): Merge<A, B> {
|
|
38
|
-
return Object.assign({}, a, b) as Merge<A, B>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function isEmpty (obj: object): boolean {
|
|
42
|
-
return (
|
|
43
|
-
obj != null &&
|
|
44
|
-
Object.keys(obj).length === 0 &&
|
|
45
|
-
Object.getPrototypeOf(obj) === Object.prototype
|
|
46
|
-
)
|
|
47
|
-
}
|
package/src/ord.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software
|
|
8
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
-
See the License for the specific language governing permissions and
|
|
11
|
-
limitations under the License.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export enum Ordering {
|
|
15
|
-
LT = -1,
|
|
16
|
-
EQ = 0,
|
|
17
|
-
GT = 1
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type CompareOrd<T> = (a: T, b: T) => Ordering
|
|
21
|
-
export type Compare<T> = (a: T, b: T) => number
|
|
22
|
-
|
|
23
|
-
export class Ord<T> {
|
|
24
|
-
static fromNumberComparison<T>(compare: (a: T, b: T) => number): Ord<T> {
|
|
25
|
-
return new Ord<T>(fromNumberComparison(compare))
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
constructor (readonly compare: CompareOrd<T>) {}
|
|
29
|
-
|
|
30
|
-
max (a: T, b: T): T {
|
|
31
|
-
switch (this.compare(a, b)) {
|
|
32
|
-
case Ordering.LT:
|
|
33
|
-
case Ordering.EQ:
|
|
34
|
-
return a
|
|
35
|
-
case Ordering.GT:
|
|
36
|
-
return b
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
min (a: T, b: T): T {
|
|
41
|
-
switch (this.compare(a, b)) {
|
|
42
|
-
case Ordering.GT:
|
|
43
|
-
case Ordering.EQ:
|
|
44
|
-
return a
|
|
45
|
-
case Ordering.LT:
|
|
46
|
-
return b
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
equals (a: T, b: T): boolean {
|
|
51
|
-
switch (this.compare(a, b)) {
|
|
52
|
-
case Ordering.EQ:
|
|
53
|
-
return true
|
|
54
|
-
default:
|
|
55
|
-
return false
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
contramap<B>(f: (b: B) => T): Ord<B> {
|
|
60
|
-
return new Ord((b0: B, b1: B) => this.compare(f(b0), f(b1)))
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
inverse (): Ord<T> {
|
|
64
|
-
return new Ord((a0: T, a1: T) => this.compare(a1, a0))
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
numberComparison (a0: T, a1: T): number {
|
|
68
|
-
return this.compare(a0, a1)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function fromNumberComparison<A> (f: (a: A, b: A) => number) {
|
|
73
|
-
return function compare (a: A, b: A) {
|
|
74
|
-
const r = f(a, b)
|
|
75
|
-
if (r < 0) return Ordering.LT
|
|
76
|
-
else if (r === 0) return Ordering.EQ
|
|
77
|
-
else return Ordering.GT
|
|
78
|
-
}
|
|
79
|
-
}
|
package/src/reg-exps.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License")
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software
|
|
8
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
-
See the License for the specific language governing permissions and
|
|
11
|
-
limitations under the License.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Utility module to manipulate `RegExp` instances.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Map the function `f` on each occurance matched by the pattern.
|
|
20
|
-
* @param f
|
|
21
|
-
* @param pattern
|
|
22
|
-
* @param subject
|
|
23
|
-
*/
|
|
24
|
-
export function map (
|
|
25
|
-
subject: string,
|
|
26
|
-
pattern: RegExp,
|
|
27
|
-
f: (...s: string[]) => string
|
|
28
|
-
): string {
|
|
29
|
-
const buff = [] as string[]
|
|
30
|
-
let pos = 0
|
|
31
|
-
let result: RegExpExecArray | null
|
|
32
|
-
if (pattern.global) {
|
|
33
|
-
pattern.lastIndex = 0
|
|
34
|
-
while ((result = pattern.exec(subject)) !== null) {
|
|
35
|
-
buff.push(subject.substring(pos, result.index))
|
|
36
|
-
buff.push(f(...result))
|
|
37
|
-
pos = result.index + result[0].length
|
|
38
|
-
}
|
|
39
|
-
} else {
|
|
40
|
-
while ((result = pattern.exec(subject.substring(pos))) !== null) {
|
|
41
|
-
buff.push(subject.substring(pos, pos + result.index))
|
|
42
|
-
buff.push(f(...result))
|
|
43
|
-
pos += result.index + result[0].length
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
buff.push(subject.substring(pos))
|
|
47
|
-
return buff.join('')
|
|
48
|
-
}
|
package/src/result.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { type AsyncResult } from './async-result'
|
|
2
|
-
|
|
3
|
-
export interface Success<V> {
|
|
4
|
-
type: 'success'
|
|
5
|
-
value: V
|
|
6
|
-
}
|
|
7
|
-
export interface Failure<E> {
|
|
8
|
-
type: 'failure'
|
|
9
|
-
error: E
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type Result<V, E> = Success<V> | Failure<E>
|
|
13
|
-
|
|
14
|
-
export type PromiseResult<V, E> = PromiseLike<Result<V, E>>
|
|
15
|
-
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
17
|
-
export const Result = {
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
success<V>(value: V): Result<V, any> {
|
|
20
|
-
return { type: 'success', value }
|
|
21
|
-
},
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
-
failure<E>(error: E): Result<any, E> {
|
|
24
|
-
return { type: 'failure', error }
|
|
25
|
-
},
|
|
26
|
-
cmap:
|
|
27
|
-
<V1, V2, E>(f: (value: V1) => V2) =>
|
|
28
|
-
(r: Result<V1, E>): Result<V2, E> => {
|
|
29
|
-
if (r.type === 'success') {
|
|
30
|
-
return Result.success(f(r.value))
|
|
31
|
-
} else {
|
|
32
|
-
return r
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
map: <V1, V2, E>(r: Result<V1, E>, f: (value: V1) => V2): Result<V2, E> => {
|
|
36
|
-
if (r.type === 'success') {
|
|
37
|
-
return Result.success(f(r.value))
|
|
38
|
-
} else {
|
|
39
|
-
return r
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
cflatMap:
|
|
43
|
-
<V1, V2, E>(f: (value: V1) => Result<V2, E>) =>
|
|
44
|
-
(r: Result<V1, E>): Result<V2, E> => {
|
|
45
|
-
if (r.type === 'success') {
|
|
46
|
-
return f(r.value)
|
|
47
|
-
} else {
|
|
48
|
-
return r
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
flatMap: <V1, V2, E>(
|
|
52
|
-
r: Result<V1, E>,
|
|
53
|
-
f: (value: V1) => Result<V2, E>
|
|
54
|
-
): Result<V2, E> => {
|
|
55
|
-
if (r.type === 'success') {
|
|
56
|
-
return f(r.value)
|
|
57
|
-
} else {
|
|
58
|
-
return r
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
toAsync<V, E>(r: Result<V, E>): AsyncResult<V, E> {
|
|
62
|
-
return r
|
|
63
|
-
},
|
|
64
|
-
isSuccess<V, E>(r: Result<V, E>): r is Success<V> {
|
|
65
|
-
return r.type === 'success'
|
|
66
|
-
},
|
|
67
|
-
isFailure<V, E>(r: Result<V, E>): r is Failure<E> {
|
|
68
|
-
return r.type === 'failure'
|
|
69
|
-
},
|
|
70
|
-
getOrElse<V, E>(r: Result<V, E>, alt: V): V {
|
|
71
|
-
return Result.isSuccess(r) ? r.value : alt
|
|
72
|
-
},
|
|
73
|
-
getOrElseLazy<V, E>(r: Result<V, E>, altf: () => V): V {
|
|
74
|
-
return Result.isSuccess(r) ? r.value : altf()
|
|
75
|
-
},
|
|
76
|
-
getOrNull<V, E>(r: Result<V, E>): V | null {
|
|
77
|
-
return Result.isSuccess(r) ? r.value : null
|
|
78
|
-
},
|
|
79
|
-
getOrUndefined<V, E>(r: Result<V, E>): V | undefined {
|
|
80
|
-
return Result.isSuccess(r) ? r.value : undefined
|
|
81
|
-
},
|
|
82
|
-
cmatch:
|
|
83
|
-
<V1, V2, E>(success: (value: V1) => V2, failure: (error: E) => V2) =>
|
|
84
|
-
(r: Result<V1, E>): V2 => {
|
|
85
|
-
if (Result.isSuccess(r)) {
|
|
86
|
-
return success(r.value)
|
|
87
|
-
} else {
|
|
88
|
-
return failure(r.error)
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
match: <V1, V2, E>(
|
|
92
|
-
r: Result<V1, E>,
|
|
93
|
-
success: (value: V1) => V2,
|
|
94
|
-
failure: (error: E) => V2
|
|
95
|
-
): V2 => {
|
|
96
|
-
if (Result.isSuccess(r)) {
|
|
97
|
-
return success(r.value)
|
|
98
|
-
} else {
|
|
99
|
-
return failure(r.error)
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
whenSuccess:
|
|
103
|
-
<V, E>(apply: (v: V) => void) =>
|
|
104
|
-
(r: Result<V, E>): Result<V, E> => {
|
|
105
|
-
if (Result.isSuccess(r)) {
|
|
106
|
-
apply(r.value)
|
|
107
|
-
}
|
|
108
|
-
return r
|
|
109
|
-
},
|
|
110
|
-
whenFailure:
|
|
111
|
-
<V, E>(apply: (e: E) => void) =>
|
|
112
|
-
(r: Result<V, E>): Result<V, E> => {
|
|
113
|
-
if (Result.isFailure(r)) {
|
|
114
|
-
apply(r.error)
|
|
115
|
-
}
|
|
116
|
-
return r
|
|
117
|
-
},
|
|
118
|
-
combine: <V, E>(
|
|
119
|
-
r1: Result<V, E>,
|
|
120
|
-
r2: Result<V, E>,
|
|
121
|
-
combineV: (v1: V, v2: V) => V,
|
|
122
|
-
combineE: (e1: E, e2: E) => E
|
|
123
|
-
): Result<V, E> =>
|
|
124
|
-
Result.match<V, Result<V, E>, E>(
|
|
125
|
-
r1,
|
|
126
|
-
(v1: V) =>
|
|
127
|
-
Result.match<V, Result<V, E>, E>(
|
|
128
|
-
r2,
|
|
129
|
-
(v2: V) => Result.success<V>(combineV(v1, v2)),
|
|
130
|
-
(e2: E) => Result.failure<E>(e2)
|
|
131
|
-
),
|
|
132
|
-
(e1: E) =>
|
|
133
|
-
Result.match<V, Result<V, E>, E>(
|
|
134
|
-
r2,
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
136
|
-
(_: V) => Result.failure<E>(e1),
|
|
137
|
-
(e2: E) => Result.failure<E>(combineE(e1, e2))
|
|
138
|
-
)
|
|
139
|
-
)
|
|
140
|
-
}
|