mini-func.ts
5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
'use strict';
namespace AmeMiniFunc {
//region Option
interface OptionOps<T> {
isDefined(): this is Some<T>;
isEmpty(): this is None;
nonEmpty(): this is Some<T>;
get(): T;
map<R>(f: (value: T) => R): Option<R>;
flatMap<R>(f: (value: T) => Option<R>): Option<R>;
filter(f: (value: T) => boolean): Option<T>;
forEach(f: (value: T) => void): void;
orElse(alternative: () => Option<T>): Option<T>;
getOrElse(defaultValue: () => T): T;
orNull<R extends T>(): T | R | null;
toArray(): T[];
}
class Some<T> implements OptionOps<T> {
constructor(private value: T) {
}
get(): T {
return this.value;
}
isDefined(): boolean {
return true;
}
isEmpty(): boolean {
return false;
}
nonEmpty(): boolean {
return true;
}
map<R>(f: (value: T) => R): Some<R> {
return new Some(f(this.value));
}
flatMap<R>(f: (value: T) => Option<R>): Option<R> {
return f(this.value);
}
filter(f: (value: T) => boolean): Option<T> {
return f(this.value) ? this : none;
}
forEach(f: (value: T) => void): void {
f(this.value);
}
orElse(alternative: () => Option<T>): Option<T> {
return this;
}
getOrElse(alternative: () => T): T {
return this.value;
}
toArray(): T[] {
return [this.value];
}
orNull(): T | null {
return this.value;
}
}
class None implements OptionOps<never> {
map<R>(f: (value: never) => R): None {
return this;
}
get(): never {
throw new Error('Cannot get value from None');
}
isDefined(): boolean {
return false;
}
isEmpty(): boolean {
return true;
}
nonEmpty(): boolean {
return false;
}
filter(f: (value: never) => boolean): None {
return this;
}
forEach(f: (value: never) => void): void {
}
orElse<R>(alternative: () => Option<R>): Option<R> {
return alternative();
}
getOrElse<R>(alternative: () => R): R {
return alternative();
}
orNull(): null {
return null;
}
flatMap<R>(f: (value: never) => Option<R>): Option<R> {
return this;
}
toArray(): [] {
return [];
}
}
export const none = new None();
export function some<T>(value: T): Some<T> {
return new Some(value);
}
export type Option<T> = Some<T> | None;
type LiftedFunction<TArgs extends any[], R> = (
...args: { [K in keyof TArgs]: TArgs[K] extends Option<infer U> ? U : never }
) => R;
export function lift<TArgs extends Option<any>[], R>(
options: [...TArgs],
f: LiftedFunction<TArgs, R>
): Option<R> {
const areAllDefined = options.every((opt) => opt.isDefined());
if (areAllDefined) {
const unwrappedValues = options.map((opt) => opt.get()) as unknown as Parameters<LiftedFunction<TArgs, R>>;
return some(f(...unwrappedValues));
} else {
return none;
}
}
//endregion
//region Either
export abstract class Either<A, B> {
abstract isLeft(): this is Left<A, B> ;
abstract isRight(): this is Right<A, B> ;
abstract getOrElse(defaultValue: () => B): B;
map<R>(f: (value: B) => R): Either<A, R> {
if (this.isRight()) {
return new Right(f(this.value));
} else {
return (this as unknown as Either<A, R>); //Should be safe.
}
}
flatMap<R>(f: (value: B) => Either<A, R>): Either<A, R> {
if (this.isRight()) {
return f(this.value);
} else {
return (this as unknown as Either<A, R>);
}
}
toOption(): Option<B> {
if (this.isRight()) {
return some(this.value);
} else {
return none;
}
}
static left<A, B>(value: A): Left<A, B> {
return new Left(value);
}
static right<A, B>(value: B): Right<A, B> {
return new Right(value);
}
}
export class Left<A, B> extends Either<A, B> {
constructor(public readonly value: A) {
super();
}
isLeft(): this is Left<A, B> {
return true;
}
isRight(): this is Right<A, B> {
return false;
}
getOrElse(defaultValue: () => B): B {
return defaultValue();
}
}
export class Right<A, B> extends Either<A, B> {
constructor(public readonly value: B) {
super();
}
isLeft(): this is Left<A, B> {
return false;
}
isRight(): this is Right<A, B> {
return true;
}
getOrElse(defaultValue: () => B): B {
return this.value;
}
}
//endregion
//region Misc
export function sanitizeNumericString(str: string): string {
if (str === '') {
return ''
}
let sanitizedString: string = str
//Replace commas with periods.
.replace(/,/g, '.')
//Remove all non-numeric characters.
.replace(/[^0-9.-]/g, '')
//Remove all but the last period.
.replace(/\.(?=.*\.)/g, '');
//Keep a minus sign only if it's the first character. Remove all other occurrences.
const hasMinusSign = (sanitizedString.charAt(0) === '-');
sanitizedString = sanitizedString.replace(/-/g, '');
if (hasMinusSign) {
sanitizedString = '-' + sanitizedString;
}
return sanitizedString;
}
export function forEachObjectKey<T extends object>(collection: T, callback: (key: keyof T, value: T[keyof T]) => void) {
for (const k in collection) {
if (!collection.hasOwnProperty(k)) {
continue;
}
callback(k, collection[k]);
}
}
//endregion
}