@guillaume-docquier/tools-ts 1.4.1 → 2.1.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/dist/Assert.d.ts +6 -0
- package/dist/Assert.js +14 -0
- package/dist/Assert.js.map +1 -1
- package/dist/Result.d.ts +13 -4
- package/dist/Result.js +12 -6
- package/dist/Result.js.map +1 -1
- package/package.json +3 -2
package/dist/Assert.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { EnumType, ConstructorType } from "./utility-types.js";
|
|
2
|
+
import { Result, Success } from "./Result.js";
|
|
2
3
|
/**
|
|
3
4
|
* From Wikipedia
|
|
4
5
|
* In computer programming, [...] an assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution.
|
|
@@ -127,4 +128,9 @@ export declare class Assert {
|
|
|
127
128
|
* ```
|
|
128
129
|
*/
|
|
129
130
|
static isExhausted(this: void, value: never): asserts value is never;
|
|
131
|
+
/**
|
|
132
|
+
* Asserts that the Result is a success.
|
|
133
|
+
* This is mostly useful in tests, as real code should handle Failures.
|
|
134
|
+
*/
|
|
135
|
+
static isSuccess<T>(this: void, result: Result<T, unknown>, paramName?: string): asserts result is Success<T>;
|
|
130
136
|
}
|
package/dist/Assert.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AssertionError } from "./errors/AssertionError.js";
|
|
2
2
|
import { TypeGuard } from "./TypeGuard.js";
|
|
3
|
+
import { Result } from "./Result.js";
|
|
3
4
|
/**
|
|
4
5
|
* From Wikipedia
|
|
5
6
|
* In computer programming, [...] an assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution.
|
|
@@ -175,6 +176,19 @@ export class Assert {
|
|
|
175
176
|
received: value,
|
|
176
177
|
}, Assert.isExhausted);
|
|
177
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Asserts that the Result is a success.
|
|
181
|
+
* This is mostly useful in tests, as real code should handle Failures.
|
|
182
|
+
*/
|
|
183
|
+
static isSuccess(result, paramName) {
|
|
184
|
+
if (Result.isFailure(result)) {
|
|
185
|
+
throw new AssertionError("Expected result to be Success", {
|
|
186
|
+
paramName,
|
|
187
|
+
expected: "a Success",
|
|
188
|
+
received: `a Failure: ${formatPrimitiveValue(result.error)}`,
|
|
189
|
+
}, Assert.isSuccess);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
178
192
|
}
|
|
179
193
|
/**
|
|
180
194
|
* `quoteStrings` will put strings inside single quote for nicer output when you know the string will be nested inside a double quoted string
|
package/dist/Assert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Assert.js","sourceRoot":"","sources":["../src/Assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAE3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"Assert.js","sourceRoot":"","sources":["../src/Assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAE3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAW,MAAM,aAAa,CAAA;AAE7C;;;;;;;;;;;;GAYG;AAEH,MAAM,OAAO,MAAM;IACjB;;;;;;;;;;;;;OAaG;IACI,MAAM,CAAC,YAAY,CAExB,aAAqC,EACrC,eAAwB,EACxB,SAAkB;QAElB,IAAI,CAAC,CAAC,eAAe,YAAY,aAAa,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,cAAc,CACtB,wCAAwC,EACxC;gBACE,SAAS;gBACT,QAAQ,EAAE,aAAa,CAAC,IAAI;gBAC5B,QAAQ,EAAE,eAAe,EAAE,WAAW,CAAC,IAAI,IAAI,OAAO,eAAe;aACtE,EACD,MAAM,CAAC,YAAY,CACpB,CAAA;QACH,CAAC;IACH,CAAC;IAED;;;;;;;;;;;;;O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SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK;YACjE,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK;gBACnC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;AAC7B,CAAC"}
|
package/dist/Result.d.ts
CHANGED
|
@@ -48,16 +48,25 @@ export declare const Result: {
|
|
|
48
48
|
* If the promise rejects, you will get a {@link Result.Failure}.
|
|
49
49
|
* If the promise rejects with a fatal error, the error will be rethrown.
|
|
50
50
|
*
|
|
51
|
-
* @param
|
|
51
|
+
* @param promise The promise.
|
|
52
52
|
*/
|
|
53
|
-
declare function tryCatch<T>(
|
|
53
|
+
declare function tryCatch<T>(promise: Promise<T>): Promise<Result<T, Error>>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a Result from the result of a function that returns a promise.
|
|
56
|
+
* If the returned promise resolves, you will get a {@link Result.Success}.
|
|
57
|
+
* If the returned promise rejects, you will get a {@link Result.Failure}.
|
|
58
|
+
* If the returned promise rejects with a fatal error, the error will be rethrown.
|
|
59
|
+
*
|
|
60
|
+
* @param asyncFn The asynchronous callback to execute.
|
|
61
|
+
*/
|
|
62
|
+
declare function tryCatch<T>(asyncFn: () => Promise<T>): Promise<Result<T, Error>>;
|
|
54
63
|
/**
|
|
55
64
|
* Creates a Result from the result of a function that could throw.
|
|
56
65
|
* If the function does not throw, you will get a {@link Result.Success}.
|
|
57
66
|
* If the function throws, you will get a {@link Result.Failure}.
|
|
58
67
|
* If the function throws a fatal error, the error will be rethrown.
|
|
59
68
|
*
|
|
60
|
-
* @param
|
|
69
|
+
* @param syncFn The synchronous callback to execute.
|
|
61
70
|
*/
|
|
62
|
-
declare function tryCatch<T>(
|
|
71
|
+
declare function tryCatch<T>(syncFn: () => T): Result<T, Error>;
|
|
63
72
|
export {};
|
package/dist/Result.js
CHANGED
|
@@ -35,15 +35,15 @@ export const Result = {
|
|
|
35
35
|
// You can't overload while defining object properties, which is why the function implementation is not inlined
|
|
36
36
|
tryCatch,
|
|
37
37
|
};
|
|
38
|
-
function tryCatch(
|
|
38
|
+
function tryCatch(op) {
|
|
39
|
+
if (op instanceof Promise) {
|
|
40
|
+
return tryCatchPromise(op);
|
|
41
|
+
}
|
|
39
42
|
try {
|
|
40
|
-
const result =
|
|
43
|
+
const result = op();
|
|
41
44
|
// Async handling
|
|
42
45
|
if (result instanceof Promise) {
|
|
43
|
-
return result
|
|
44
|
-
Rethrow.ifFatal(error);
|
|
45
|
-
return Result.Failure(error);
|
|
46
|
-
});
|
|
46
|
+
return tryCatchPromise(result);
|
|
47
47
|
}
|
|
48
48
|
// Sync handling
|
|
49
49
|
return Result.Success(result);
|
|
@@ -53,4 +53,10 @@ function tryCatch(fn) {
|
|
|
53
53
|
return Result.Failure(error);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
+
function tryCatchPromise(promise) {
|
|
57
|
+
return promise.then(Result.Success).catch((error) => {
|
|
58
|
+
Rethrow.ifFatal(error);
|
|
59
|
+
return Result.Failure(error);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
56
62
|
//# sourceMappingURL=Result.js.map
|
package/dist/Result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAkB7C;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,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AAO,EAAE,CAAC;YAC9B,OAAO,eAAe,CAAC,MAAM,CAAC,CAAA;QAChC,CAAC;QAED,gBAAgB;QAChB,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAI,OAAmB;IAC7C,OAAO,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAClD,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guillaume-docquier/tools-ts",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "My personal collection of typescript tools",
|
|
5
5
|
"homepage": "https://github.com/Guillaume-Docquier/tools-ts",
|
|
6
6
|
"repository": {
|
|
@@ -21,11 +21,12 @@
|
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "rm -rf dist && tsc --project tsconfig.build.json",
|
|
24
|
+
"checks": "pnpm lint:fix && pnpm format:fix && pnpm typecheck && pnpm test --run",
|
|
24
25
|
"format": "prettier --check . && pnpm format:package-json --check",
|
|
25
26
|
"format:fix": "prettier --write --list-different . && pnpm format:package-json",
|
|
26
27
|
"format:package-json": "sort-package-json package.json",
|
|
27
28
|
"lint": "eslint . --max-warnings 0",
|
|
28
|
-
"lint:fix": "pnpm lint --
|
|
29
|
+
"lint:fix": "pnpm lint --fix",
|
|
29
30
|
"prepack": "pnpm build",
|
|
30
31
|
"prepare": "husky",
|
|
31
32
|
"prepublishOnly": "pnpm i",
|