catch-match 0.1.0 → 0.2.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/package.json +8 -7
- package/src/main.js +43 -0
- package/src/main.js.map +1 -0
- package/src/main.ts +49 -0
- package/tsconfig.json +3 -3
- package/src/vite-env.d.ts +0 -1
package/package.json
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
{
|
2
2
|
"name": "catch-match",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.2.0",
|
4
|
+
"repository": {
|
5
|
+
"url": "https://github.com/kobzarvs/catch-match"
|
6
|
+
},
|
4
7
|
"scripts": {
|
5
|
-
"
|
6
|
-
"build": "tsc && vite build",
|
7
|
-
"preview": "vite preview"
|
8
|
+
"build": "tsc"
|
8
9
|
},
|
9
|
-
"devDependencies": {
|
10
|
-
|
11
|
-
"
|
10
|
+
"devDependencies": {},
|
11
|
+
"dependencies": {
|
12
|
+
"typescript": "^4.5.4"
|
12
13
|
}
|
13
14
|
}
|
package/src/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/src/main.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["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/src/main.ts
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
type InternalData<T> = {
|
2
|
+
value?: T;
|
3
|
+
error?: any;
|
4
|
+
context?: any;
|
5
|
+
};
|
6
|
+
|
7
|
+
export function _try<T>(body: (context: any) => T) {
|
8
|
+
let error: any;
|
9
|
+
let result: T;
|
10
|
+
let context: any = {};
|
11
|
+
const isSameInstance = (e1: any) => e1 === error || e1 === error?.constructor;
|
12
|
+
|
13
|
+
try {
|
14
|
+
result = body(context);
|
15
|
+
} catch (e) {
|
16
|
+
error = e;
|
17
|
+
}
|
18
|
+
|
19
|
+
const chain = {
|
20
|
+
catch: (err: any, handler: (context: any) => void) => {
|
21
|
+
if (!err || !handler) {
|
22
|
+
return chain;
|
23
|
+
}
|
24
|
+
if (isSameInstance(err)) {
|
25
|
+
handler && handler(context);
|
26
|
+
} else if (Array.isArray(err)) {
|
27
|
+
err.some(isSameInstance) && handler(context);
|
28
|
+
}
|
29
|
+
return chain;
|
30
|
+
},
|
31
|
+
other: (handler: (error: any, context: any) => void) => {
|
32
|
+
if (error) {
|
33
|
+
handler && handler(error, context);
|
34
|
+
}
|
35
|
+
return {
|
36
|
+
finally: chain.finally
|
37
|
+
};
|
38
|
+
},
|
39
|
+
finally: (callback: (params: InternalData<T>) => T): InternalData<T> => {
|
40
|
+
return {
|
41
|
+
value: callback ? callback({value: result, error, context}) : result,
|
42
|
+
error,
|
43
|
+
context,
|
44
|
+
};
|
45
|
+
},
|
46
|
+
};
|
47
|
+
|
48
|
+
return chain;
|
49
|
+
}
|
package/tsconfig.json
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
{
|
2
2
|
"compilerOptions": {
|
3
|
-
"target": "
|
3
|
+
"target": "es6",
|
4
4
|
"useDefineForClassFields": true,
|
5
5
|
"module": "ESNext",
|
6
|
-
"lib": ["ESNext"
|
6
|
+
"lib": ["ESNext"],
|
7
7
|
"moduleResolution": "Node",
|
8
8
|
"strict": true,
|
9
9
|
"sourceMap": true,
|
10
10
|
"resolveJsonModule": true,
|
11
11
|
"esModuleInterop": true,
|
12
|
-
"noEmit":
|
12
|
+
"noEmit": false,
|
13
13
|
"noUnusedLocals": true,
|
14
14
|
"noUnusedParameters": true,
|
15
15
|
"noImplicitReturns": true
|
package/src/vite-env.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
/// <reference types="vite/client" />
|