@driveflux/result 3.0.1 → 3.0.2
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/dist/index.js +87 -80
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,88 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
export class Err {
|
|
15
|
+
/**
|
|
10
16
|
* If the result has a value returns that value. Otherwise returns the passed in value.
|
|
11
17
|
* @param val the value to replace the error with
|
|
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
|
-
|
|
18
|
+
*/ else(val) {
|
|
19
|
+
return val;
|
|
20
|
+
}
|
|
21
|
+
expect(msg) {
|
|
22
|
+
throw new Error(`${msg} - Error: ${JSON.stringify(this.val)}`);
|
|
23
|
+
}
|
|
24
|
+
unwrap() {
|
|
25
|
+
throw new Error(`Tried to unwrap Error: ${JSON.stringify(this.val)}`);
|
|
26
|
+
}
|
|
27
|
+
map(_mapper) {
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
mapErr(mapper) {
|
|
31
|
+
return new Err(mapper(this.val));
|
|
32
|
+
}
|
|
33
|
+
constructor(val){
|
|
34
|
+
_define_property(this, "val", void 0);
|
|
35
|
+
_define_property(this, "ok", void 0);
|
|
36
|
+
_define_property(this, "err", void 0);
|
|
37
|
+
this.val = val;
|
|
38
|
+
this.ok = false;
|
|
39
|
+
this.err = true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
_define_property(Err, "EMPTY", new Err(undefined));
|
|
43
|
+
export class Ok {
|
|
44
|
+
/**
|
|
37
45
|
* If the result has a value returns that value. Otherwise returns the passed in value.
|
|
38
46
|
* @param val the value to replace the error with
|
|
39
|
-
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
} else {
|
|
62
|
-
return new Err(result.val);
|
|
47
|
+
*/ else(_val) {
|
|
48
|
+
return this.val;
|
|
49
|
+
}
|
|
50
|
+
expect(_msg) {
|
|
51
|
+
return this.val;
|
|
52
|
+
}
|
|
53
|
+
unwrap() {
|
|
54
|
+
return this.val;
|
|
55
|
+
}
|
|
56
|
+
map(mapper) {
|
|
57
|
+
return new Ok(mapper(this.val));
|
|
58
|
+
}
|
|
59
|
+
mapErr(_mapper) {
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
constructor(val){
|
|
63
|
+
_define_property(this, "val", void 0);
|
|
64
|
+
_define_property(this, "ok", void 0);
|
|
65
|
+
_define_property(this, "err", void 0);
|
|
66
|
+
this.val = val;
|
|
67
|
+
this.ok = true;
|
|
68
|
+
this.err = false;
|
|
63
69
|
}
|
|
64
|
-
}
|
|
65
|
-
return new Ok(okResult);
|
|
66
70
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
_define_property(Ok, "EMPTY", new Ok(undefined));
|
|
72
|
+
export function Results(...results) {
|
|
73
|
+
const okResult = [];
|
|
74
|
+
for (let result of results){
|
|
75
|
+
if (result.ok) {
|
|
76
|
+
okResult.push(result.val);
|
|
77
|
+
} else {
|
|
78
|
+
return new Err(result.val);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return new Ok(okResult);
|
|
82
|
+
}
|
|
83
|
+
export const isResult = (response)=>{
|
|
84
|
+
return typeof response === 'object' && response && typeof response.ok !== 'undefined' && typeof response.err !== 'undefined';
|
|
79
85
|
};
|
|
80
|
-
export {
|
|
81
|
-
|
|
82
|
-
ErrorWithResult,
|
|
83
|
-
Ok,
|
|
84
|
-
Results,
|
|
85
|
-
isErrorWithResult,
|
|
86
|
-
isResult
|
|
86
|
+
export const isErrorWithResult = (response)=>{
|
|
87
|
+
return response.result && isResult(response.result);
|
|
87
88
|
};
|
|
88
|
-
|
|
89
|
+
export class ErrorWithResult extends Error {
|
|
90
|
+
constructor(result){
|
|
91
|
+
super('');
|
|
92
|
+
_define_property(this, "result", void 0);
|
|
93
|
+
this.result = result;
|
|
94
|
+
}
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@driveflux/result",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./package.json": "./package.json",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "npm run clean && npm run build:types && npm run build:js",
|
|
31
|
-
"build:js": "node
|
|
31
|
+
"build:js": "node build.js && swc src --out-dir dist",
|
|
32
32
|
"build:types": "tsc --build",
|
|
33
33
|
"clean": "del dist/**/*.js",
|
|
34
34
|
"type-check": "tsc --noEmit"
|