@othree.io/optional 1.3.0 → 1.7.0
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/optional.d.ts +8 -4
- package/lib/optional.d.ts.map +1 -1
- package/lib/optional.js +55 -19
- package/lib/optional.js.map +1 -1
- package/package.json +1 -1
package/lib/optional.d.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
export declare function Try<T
|
|
1
|
+
export declare function Try<T>(thunk: () => Optional<T> | T): Optional<T>;
|
|
2
|
+
export declare function TryAsync<T>(thunk: () => Promise<Optional<T>> | Promise<T>): Promise<Optional<T>>;
|
|
2
3
|
export interface Maybe<T, E extends Error> {
|
|
3
4
|
readonly get: () => T;
|
|
4
5
|
readonly orElse: (value: T) => T;
|
|
5
6
|
readonly orThrow: (error: E) => T;
|
|
6
|
-
readonly map: <A>(callback: (value: T) => A |
|
|
7
|
+
readonly map: <A>(callback: (value: T) => Optional<A> | A) => Optional<A>;
|
|
8
|
+
readonly mapAsync: <A>(callback: (value: T) => Promise<Optional<A>> | Promise<A>) => Promise<Optional<A>>;
|
|
7
9
|
readonly getError: () => any;
|
|
10
|
+
readonly catch: <A>(callback: (error: E) => Optional<A> | A) => Optional<A> | Optional<T>;
|
|
8
11
|
readonly isEmpty: boolean;
|
|
9
12
|
readonly isPresent: boolean;
|
|
10
13
|
}
|
|
11
|
-
export declare type Optional<T> = Maybe<T
|
|
14
|
+
export declare type Optional<T> = Maybe<T, Error>;
|
|
15
|
+
export declare function Empty<T>(error?: Error): Optional<T>;
|
|
12
16
|
export declare class EmptyOptionalError extends Error {
|
|
13
17
|
static ERROR: string;
|
|
14
18
|
constructor();
|
|
15
19
|
}
|
|
16
|
-
export declare function Optional<T>(value: T, error?: Error
|
|
20
|
+
export declare function Optional<T>(value: Optional<T> | T, error?: Error): Optional<T>;
|
|
17
21
|
//# sourceMappingURL=optional.d.ts.map
|
package/lib/optional.d.ts.map
CHANGED
|
@@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Q,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;IACzF,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAA;CAC9B;AAED,oBAAY,QAAQ,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;AAEzC,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,CAEnD;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IACzC,MAAM,CAAC,KAAK,SAAuB;;CAOtC;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,CAkE9E"}
|
package/lib/optional.js
CHANGED
|
@@ -9,27 +9,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Optional = exports.EmptyOptionalError = exports.Try = void 0;
|
|
13
|
-
|
|
12
|
+
exports.Optional = exports.EmptyOptionalError = exports.Empty = exports.TryAsync = exports.Try = void 0;
|
|
13
|
+
function isOptional(optional) {
|
|
14
|
+
return Boolean(optional &&
|
|
15
|
+
typeof optional.get === 'function' &&
|
|
16
|
+
typeof optional.orElse === 'function' &&
|
|
17
|
+
typeof optional.orThrow === 'function' &&
|
|
18
|
+
typeof optional.map === 'function' &&
|
|
19
|
+
typeof optional.mapAsync === 'function' &&
|
|
20
|
+
typeof optional.getError === 'function' &&
|
|
21
|
+
typeof optional.isEmpty === 'boolean' &&
|
|
22
|
+
typeof optional.isPresent === 'boolean');
|
|
23
|
+
}
|
|
14
24
|
function Try(thunk) {
|
|
15
25
|
try {
|
|
16
26
|
const value = thunk();
|
|
17
|
-
if (
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
return Optional(value);
|
|
27
|
+
if (isOptional(value)) {
|
|
28
|
+
return value;
|
|
22
29
|
}
|
|
30
|
+
return Optional(value);
|
|
23
31
|
}
|
|
24
32
|
catch (error) {
|
|
25
33
|
return Optional(undefined, error);
|
|
26
34
|
}
|
|
27
35
|
}
|
|
28
36
|
exports.Try = Try;
|
|
29
|
-
function
|
|
37
|
+
function TryAsync(thunk) {
|
|
30
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
39
|
try {
|
|
32
|
-
const value = yield
|
|
40
|
+
const value = yield thunk();
|
|
41
|
+
if (isOptional(value)) {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
33
44
|
return Optional(value);
|
|
34
45
|
}
|
|
35
46
|
catch (error) {
|
|
@@ -38,6 +49,11 @@ function TryPromise(promise) {
|
|
|
38
49
|
}
|
|
39
50
|
});
|
|
40
51
|
}
|
|
52
|
+
exports.TryAsync = TryAsync;
|
|
53
|
+
function Empty(error) {
|
|
54
|
+
return Optional(undefined, error);
|
|
55
|
+
}
|
|
56
|
+
exports.Empty = Empty;
|
|
41
57
|
class EmptyOptionalError extends Error {
|
|
42
58
|
constructor() {
|
|
43
59
|
super('Optional is empty');
|
|
@@ -46,41 +62,61 @@ class EmptyOptionalError extends Error {
|
|
|
46
62
|
}
|
|
47
63
|
exports.EmptyOptionalError = EmptyOptionalError;
|
|
48
64
|
EmptyOptionalError.ERROR = 'EmptyOptionalError';
|
|
49
|
-
function Optional(value, error
|
|
50
|
-
const
|
|
65
|
+
function Optional(value, error) {
|
|
66
|
+
const getValue = (value) => {
|
|
67
|
+
if (isOptional(value)) {
|
|
68
|
+
return value.isPresent ? value.get() : undefined;
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
};
|
|
72
|
+
const theValue = getValue(value);
|
|
73
|
+
const theError = isOptional(value) && !error ? value.getError() : error;
|
|
74
|
+
const isEmpty = typeof theValue === 'undefined' || theValue === null;
|
|
51
75
|
const isPresent = !isEmpty;
|
|
52
76
|
return Object.freeze({
|
|
53
77
|
get: () => {
|
|
54
78
|
if (isEmpty) {
|
|
55
|
-
if (!
|
|
79
|
+
if (!theError) {
|
|
56
80
|
throw new EmptyOptionalError();
|
|
57
81
|
}
|
|
58
82
|
else {
|
|
59
|
-
throw
|
|
83
|
+
throw theError;
|
|
60
84
|
}
|
|
61
85
|
}
|
|
62
|
-
return
|
|
86
|
+
return theValue;
|
|
63
87
|
},
|
|
64
88
|
orElse: (elseValue) => {
|
|
65
89
|
if (isEmpty) {
|
|
66
90
|
return elseValue;
|
|
67
91
|
}
|
|
68
|
-
return
|
|
92
|
+
return theValue;
|
|
69
93
|
},
|
|
70
94
|
orThrow: (error) => {
|
|
71
95
|
if (isEmpty) {
|
|
72
96
|
throw error;
|
|
73
97
|
}
|
|
74
|
-
return
|
|
98
|
+
return theValue;
|
|
75
99
|
},
|
|
76
100
|
map: (callback) => {
|
|
77
101
|
if (isPresent) {
|
|
78
|
-
return Try(() => callback(
|
|
102
|
+
return Try(() => callback(theValue));
|
|
79
103
|
}
|
|
80
|
-
return Optional(undefined,
|
|
104
|
+
return Optional(undefined, theError);
|
|
105
|
+
},
|
|
106
|
+
mapAsync: (callback) => {
|
|
107
|
+
if (isPresent) {
|
|
108
|
+
return TryAsync(() => callback(theValue));
|
|
109
|
+
}
|
|
110
|
+
return Promise.resolve(Optional(undefined, theError));
|
|
81
111
|
},
|
|
82
112
|
getError: () => {
|
|
83
|
-
return
|
|
113
|
+
return theError;
|
|
114
|
+
},
|
|
115
|
+
catch: (callback) => {
|
|
116
|
+
if (isEmpty && theError) {
|
|
117
|
+
return Try(() => callback(theError));
|
|
118
|
+
}
|
|
119
|
+
return Optional(theValue, theError);
|
|
84
120
|
},
|
|
85
121
|
isEmpty: isEmpty,
|
|
86
122
|
isPresent: isPresent
|
package/lib/optional.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optional.js","sourceRoot":"","sources":["../src/optional.ts"],"names":[],"mappings":";;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"optional.js","sourceRoot":"","sources":["../src/optional.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,SAAS,UAAU,CAAC,QAAa;IAC7B,OAAO,OAAO,CAAC,QAAQ;QACnB,OAAO,QAAQ,CAAC,GAAG,KAAK,UAAU;QAClC,OAAO,QAAQ,CAAC,MAAM,KAAK,UAAU;QACrC,OAAO,QAAQ,CAAC,OAAO,KAAK,UAAU;QACtC,OAAO,QAAQ,CAAC,GAAG,KAAK,UAAU;QAClC,OAAO,QAAQ,CAAC,QAAQ,KAAK,UAAU;QACvC,OAAO,QAAQ,CAAC,QAAQ,KAAK,UAAU;QACvC,OAAO,QAAQ,CAAC,OAAO,KAAK,SAAS;QACrC,OAAO,QAAQ,CAAC,SAAS,KAAK,SAAS,CAAC,CAAA;AAChD,CAAC;AAED,SAAgB,GAAG,CAAI,KAA4B;IAC/C,IAAI;QACA,MAAM,KAAK,GAAG,KAAK,EAAE,CAAA;QACrB,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE;YACnB,OAAO,KAAoB,CAAA;SAC9B;QACD,OAAO,QAAQ,CAAC,KAAU,CAAC,CAAA;KAC9B;IAAC,OAAO,KAAK,EAAE;QACZ,OAAO,QAAQ,CAAC,SAAU,EAAE,KAAK,CAAC,CAAA;KACrC;AACL,CAAC;AAVD,kBAUC;AAED,SAAsB,QAAQ,CAAI,KAA8C;;QAC5E,IAAI;YACA,MAAM,KAAK,GAAG,MAAM,KAAK,EAAE,CAAA;YAC3B,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE;gBACnB,OAAO,KAAoB,CAAA;aAC9B;YACD,OAAO,QAAQ,CAAC,KAAU,CAAC,CAA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