catch-match 0.2.2 → 1.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/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/main.d.ts +13 -0
- package/dist/main.js +43 -0
- package/dist/main.js.map +1 -0
- package/package.json +3 -1
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Valeriy Kobzar
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
@@ -1 +1,145 @@
|
|
1
1
|
# catch-match
|
2
|
+
|
3
|
+
## Motivation
|
4
|
+
|
5
|
+
### java
|
6
|
+
```java
|
7
|
+
try {
|
8
|
+
...
|
9
|
+
} catch (ExceptionClass1 e) {
|
10
|
+
....
|
11
|
+
} catch (ExceptionClass2, ExceptionClass3 e) {
|
12
|
+
....
|
13
|
+
} catch (any) {
|
14
|
+
...
|
15
|
+
} finally {
|
16
|
+
....
|
17
|
+
}
|
18
|
+
```
|
19
|
+
|
20
|
+
### javascript
|
21
|
+
```javascript
|
22
|
+
try {
|
23
|
+
error; // error intruction
|
24
|
+
} catch (err) {
|
25
|
+
switch(err.constructor) {
|
26
|
+
case ReferenceError:
|
27
|
+
case SyntaxError:
|
28
|
+
console.error(`${err.constructor.name}: ${err.message}`);
|
29
|
+
break;
|
30
|
+
default:
|
31
|
+
console.error('other error:', err);
|
32
|
+
}
|
33
|
+
} finally {
|
34
|
+
console.log('final')
|
35
|
+
}
|
36
|
+
|
37
|
+
//> ReferenceError: error is not defined
|
38
|
+
//> final
|
39
|
+
```
|
40
|
+
|
41
|
+
## Getting started
|
42
|
+
|
43
|
+
```shell
|
44
|
+
yarn add catch-match
|
45
|
+
|
46
|
+
or
|
47
|
+
|
48
|
+
npm install catch-match
|
49
|
+
```
|
50
|
+
|
51
|
+
```javascript
|
52
|
+
import { _try } from 'catch-match';
|
53
|
+
```
|
54
|
+
|
55
|
+
## Example 1
|
56
|
+
```javascript
|
57
|
+
const result = _try(context => {
|
58
|
+
context.tmp = 'any context data';
|
59
|
+
console.log('start', context);
|
60
|
+
return [1, 2, 3, 4, 5]; // value
|
61
|
+
}).catch(SyntaxError, (context) => {
|
62
|
+
// noop
|
63
|
+
}).catch([TypeError, ReferenceError], (context) => {
|
64
|
+
// noop
|
65
|
+
}).other((error, context) => {
|
66
|
+
// noop
|
67
|
+
}).finally(({value, context, error}) => {
|
68
|
+
// value: [1, 2, 3, 4, 5]
|
69
|
+
// context: {tmp: 'any context data'}
|
70
|
+
// error: undefined
|
71
|
+
if (!error) {
|
72
|
+
return value.reverse();
|
73
|
+
}
|
74
|
+
});
|
75
|
+
|
76
|
+
console.log(result);
|
77
|
+
// {
|
78
|
+
// value: [5, 4, 3, 2, 1]
|
79
|
+
// context: {tmp: 'any context data'}
|
80
|
+
// error: undefined
|
81
|
+
// }
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
## Example 2
|
86
|
+
```javascript
|
87
|
+
const result = _try(context => {
|
88
|
+
context.tmp = 'any context data';
|
89
|
+
console.log('start', context);
|
90
|
+
// ...
|
91
|
+
throw ReferenceError;
|
92
|
+
}).catch(SyntaxError, (context) => {
|
93
|
+
// noop
|
94
|
+
}).catch([TypeError, ReferenceError], (context) => {
|
95
|
+
// context: {tmp: 'any context data'}
|
96
|
+
}).other((error, context) => {
|
97
|
+
// noop
|
98
|
+
}).finally(({value, context, error}) => {
|
99
|
+
// value: undefined
|
100
|
+
// context: {tmp: 'any context data'}
|
101
|
+
// error: ReferenceError
|
102
|
+
if (!error) {
|
103
|
+
return value.reverse();
|
104
|
+
}
|
105
|
+
});
|
106
|
+
|
107
|
+
console.log(result);
|
108
|
+
// {
|
109
|
+
// value: undefined
|
110
|
+
// context: {tmp: 'any context data'}
|
111
|
+
// error: ReferenceError
|
112
|
+
// }
|
113
|
+
|
114
|
+
```
|
115
|
+
|
116
|
+
## Example 3
|
117
|
+
```javascript
|
118
|
+
const result = _try(context => {
|
119
|
+
context.tmp = 'any context data';
|
120
|
+
console.log('start', context);
|
121
|
+
// ...
|
122
|
+
throw SyntaxError;
|
123
|
+
}).catch([TypeError, ReferenceError], (context) => {
|
124
|
+
// noop
|
125
|
+
}).other((error, context) => {
|
126
|
+
// error: SyntaxError
|
127
|
+
// context: {tmp: 'any context data'}
|
128
|
+
context.unexpectedError = true;
|
129
|
+
}).finally(({value, context, error}) => {
|
130
|
+
// value: undefined
|
131
|
+
// context: {tmp: 'any context data', unexpectedError: true}
|
132
|
+
// error: SyntaxError
|
133
|
+
if (!error) {
|
134
|
+
return value.reverse();
|
135
|
+
}
|
136
|
+
});
|
137
|
+
|
138
|
+
console.log(result);
|
139
|
+
// {
|
140
|
+
// value: undefined
|
141
|
+
// context: {tmp: 'any context data', unexpectedError: true}
|
142
|
+
// error: SyntaxError
|
143
|
+
// }
|
144
|
+
|
145
|
+
```
|
package/dist/main.d.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
declare type InternalData<T> = {
|
2
|
+
value?: T;
|
3
|
+
error?: any;
|
4
|
+
context?: any;
|
5
|
+
};
|
6
|
+
export declare function _try<T>(body: (context: any) => T): {
|
7
|
+
catch: (err: any, handler: (context: any) => void) => any;
|
8
|
+
other: (handler: (error: any, context: any) => void) => {
|
9
|
+
finally: (callback: (params: InternalData<T>) => T) => InternalData<T>;
|
10
|
+
};
|
11
|
+
finally: (callback: (params: InternalData<T>) => T) => InternalData<T>;
|
12
|
+
};
|
13
|
+
export {};
|
package/dist/main.js
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
export function _try(body) {
|
2
|
+
let error;
|
3
|
+
let result;
|
4
|
+
let context = {};
|
5
|
+
const isSameInstance = (e1) => e1 === error || e1 === (error === null || error === void 0 ? void 0 : error.constructor);
|
6
|
+
try {
|
7
|
+
result = body(context);
|
8
|
+
}
|
9
|
+
catch (e) {
|
10
|
+
error = e;
|
11
|
+
}
|
12
|
+
const chain = {
|
13
|
+
catch: (err, handler) => {
|
14
|
+
if (!err || !handler) {
|
15
|
+
return chain;
|
16
|
+
}
|
17
|
+
if (isSameInstance(err)) {
|
18
|
+
handler && handler(context);
|
19
|
+
}
|
20
|
+
else if (Array.isArray(err)) {
|
21
|
+
err.some(isSameInstance) && handler(context);
|
22
|
+
}
|
23
|
+
return chain;
|
24
|
+
},
|
25
|
+
other: (handler) => {
|
26
|
+
if (error) {
|
27
|
+
handler && handler(error, context);
|
28
|
+
}
|
29
|
+
return {
|
30
|
+
finally: chain.finally
|
31
|
+
};
|
32
|
+
},
|
33
|
+
finally: (callback) => {
|
34
|
+
return {
|
35
|
+
value: callback ? callback({ value: result, error, context }) : result,
|
36
|
+
error,
|
37
|
+
context,
|
38
|
+
};
|
39
|
+
},
|
40
|
+
};
|
41
|
+
return chain;
|
42
|
+
}
|
43
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,IAAI,CAAI,IAAyB;IAC/C,IAAI,KAAU,CAAC;IACf,IAAI,MAAS,CAAC;IACd,IAAI,OAAO,GAAQ,EAAE,CAAC;IACtB,MAAM,cAAc,GAAG,CAAC,EAAO,EAAE,EAAE,CAAC,EAAE,KAAK,KAAK,IAAI,EAAE,MAAK,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,WAAW,CAAA,CAAC;IAE9E,IAAI;QACF,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;KACxB;IAAC,OAAO,CAAC,EAAE;QACV,KAAK,GAAG,CAAC,CAAC;KACX;IAED,MAAM,KAAK,GAAG;QACZ,KAAK,EAAE,CAAC,GAAQ,EAAE,OAA+B,EAAE,EAAE;YACnD,IAAI,CAAC,GAAG,IAAI,CAAC,OAAO,EAAE;gBACpB,OAAO,KAAK,CAAC;aACd;YACD,IAAI,cAAc,CAAC,GAAG,CAAC,EAAE;gBACvB,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;aAC7B;iBAAM,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;gBAC7B,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;aAC9C;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QACD,KAAK,EAAE,CAAC,OAA2C,EAAE,EAAE;YACrD,IAAI,KAAK,EAAE;gBACT,OAAO,IAAI,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;aACpC;YACD,OAAO;gBACL,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC;QACJ,CAAC;QACD,OAAO,EAAE,CAAC,QAAwC,EAAmB,EAAE;YACrE,OAAO;gBACL,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAC,CAAC,CAAC,CAAC,CAAC,MAAM;gBACpE,KAAK;gBACL,OAAO;aACR,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED