@douglasneuroinformatics/libjs 2.0.1 → 2.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/dist/exception.d.ts +10 -4
- package/dist/exception.d.ts.map +1 -1
- package/dist/exception.js +39 -1
- package/dist/string.d.ts +1 -0
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +3 -0
- package/package.json +5 -1
package/dist/exception.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Result, ResultAsync } from 'neverthrow';
|
|
2
2
|
import type { IsNever, RequiredKeysOf } from 'type-fest';
|
|
3
3
|
import type { SingleKeyMap, ToAbstractConstructor } from './types.js';
|
|
4
4
|
type ExceptionName = `${string}Exception`;
|
|
@@ -16,8 +16,10 @@ type ExceptionConstructorArgs<TParams extends ExceptionParams, TOptions extends
|
|
|
16
16
|
message: ((details: any) => string) | string;
|
|
17
17
|
} ? [TOptions] : [message: string, options: TOptions];
|
|
18
18
|
type ExceptionStatic<TParams extends ExceptionParams, TOptions extends ExceptionOptions> = {
|
|
19
|
-
/** return an instance of the exception wrapped in a neverthrow
|
|
20
|
-
|
|
19
|
+
/** return an instance of the exception wrapped in a neverthrow Result */
|
|
20
|
+
asAsyncErr(...args: ExceptionConstructorArgs<TParams, TOptions>): ResultAsync<never, BaseException<TParams, TOptions>>;
|
|
21
|
+
/** return an instance of the exception wrapped in a neverthrow Result */
|
|
22
|
+
asErr(...args: ExceptionConstructorArgs<TParams, TOptions>): Result<never, BaseException<TParams, TOptions>>;
|
|
21
23
|
/** inference-only property that will be undefined at runtime */
|
|
22
24
|
Instance: BaseException<TParams, TOptions>;
|
|
23
25
|
};
|
|
@@ -28,7 +30,11 @@ declare abstract class BaseException<TParams extends ExceptionParams, TOptions e
|
|
|
28
30
|
details: TOptions['details'];
|
|
29
31
|
abstract name: TParams['name'];
|
|
30
32
|
constructor(message?: string, options?: TOptions);
|
|
31
|
-
|
|
33
|
+
toAsyncErr(): ResultAsync<never, this>;
|
|
34
|
+
toErr(): Result<never, this>;
|
|
35
|
+
toString(): string;
|
|
36
|
+
private extractCauses;
|
|
37
|
+
private formatError;
|
|
32
38
|
}
|
|
33
39
|
type CoreExceptionConstructor = ToAbstractConstructor<ExceptionConstructor<ExceptionParams, ExceptionOptions>>;
|
|
34
40
|
declare class ExceptionBuilder<TParams extends ExceptionParams | undefined, TOptions extends ExceptionOptions, TStaticMethods extends {
|
package/dist/exception.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exception.d.ts","sourceRoot":"","sources":["../src/exception.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"exception.d.ts","sourceRoot":"","sources":["../src/exception.ts"],"names":[],"mappings":"AAKA,OAAO,EAAiB,MAAM,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEhE,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAKzD,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEtE,KAAK,aAAa,GAAG,GAAG,MAAM,WAAW,CAAC;AAE1C,KAAK,gBAAgB,GAAG;IACtB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE;QACR,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;CACH,CAAC;AAEF,KAAK,eAAe,CAAC,QAAQ,GAAG,GAAG,IAAI;IACrC,OAAO,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,QAAQ,KAAK,MAAM,CAAC,GAAG,MAAM,CAAC;IACnD,IAAI,EAAE,aAAa,CAAC;CACrB,CAAC;AAEF,KAAK,wBAAwB,CAAC,OAAO,SAAS,eAAe,EAAE,QAAQ,SAAS,gBAAgB,IAC9F,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,SAAS,IAAI,GAC1C,CAAC,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ack';
|
|
4
|
+
import extractStack from 'extract-stack';
|
|
5
|
+
import { err, errAsync, Result, ResultAsync } from 'neverthrow';
|
|
6
|
+
import stringifyObject from 'stringify-object';
|
|
4
7
|
import { objectify } from './object.js';
|
|
8
|
+
import { indentLines } from './string.js';
|
|
5
9
|
class BaseException extends Error {
|
|
6
10
|
cause;
|
|
7
11
|
details;
|
|
@@ -10,9 +14,40 @@ class BaseException extends Error {
|
|
|
10
14
|
this.cause = options?.cause;
|
|
11
15
|
this.details = options?.details;
|
|
12
16
|
}
|
|
17
|
+
toAsyncErr() {
|
|
18
|
+
return errAsync(this);
|
|
19
|
+
}
|
|
13
20
|
toErr() {
|
|
14
21
|
return err(this);
|
|
15
22
|
}
|
|
23
|
+
toString() {
|
|
24
|
+
const result = [];
|
|
25
|
+
this.extractCauses(this).forEach((error) => {
|
|
26
|
+
result.push(this.formatError(error));
|
|
27
|
+
result.push('\nThe above exception was the cause of the following exception:\n');
|
|
28
|
+
});
|
|
29
|
+
result.push(this.formatError(this));
|
|
30
|
+
return result.join('\n');
|
|
31
|
+
}
|
|
32
|
+
extractCauses(error) {
|
|
33
|
+
const errors = [];
|
|
34
|
+
let cause = error.cause;
|
|
35
|
+
while (cause instanceof Error) {
|
|
36
|
+
errors.push(cause);
|
|
37
|
+
cause = cause.cause;
|
|
38
|
+
}
|
|
39
|
+
return errors.toReversed();
|
|
40
|
+
}
|
|
41
|
+
formatError(error) {
|
|
42
|
+
const result = [`${error.name}: ${error.message}`];
|
|
43
|
+
extractStack.lines(cleanStack(error.stack, { pretty: true })).forEach((line) => {
|
|
44
|
+
result.push(` at ${line}`);
|
|
45
|
+
});
|
|
46
|
+
if (error.details) {
|
|
47
|
+
result.push(indentLines(`details: ${stringifyObject(error.details, { indent: ' ' })}`, 4));
|
|
48
|
+
}
|
|
49
|
+
return result.join('\n');
|
|
50
|
+
}
|
|
16
51
|
}
|
|
17
52
|
class ExceptionBuilder {
|
|
18
53
|
base = BaseException;
|
|
@@ -38,6 +73,9 @@ class ExceptionBuilder {
|
|
|
38
73
|
}
|
|
39
74
|
super(message, options);
|
|
40
75
|
}
|
|
76
|
+
static asAsyncErr(...args) {
|
|
77
|
+
return new this(...args).toAsyncErr();
|
|
78
|
+
}
|
|
41
79
|
static asErr(...args) {
|
|
42
80
|
return new this(...args).toErr();
|
|
43
81
|
}
|
package/dist/string.d.ts
CHANGED
|
@@ -6,4 +6,5 @@ export declare function capitalize<T extends string>(s: T): Capitalize<T>;
|
|
|
6
6
|
export declare function toLowerCase<T extends string>(s: T): Lowercase<T>;
|
|
7
7
|
export declare function toUpperCase<T extends string>(s: T): Uppercase<T>;
|
|
8
8
|
export declare function format(s: string, ...args: Exclude<Primitive, symbol>[]): string;
|
|
9
|
+
export declare function indentLines(s: string, count: number): string;
|
|
9
10
|
//# sourceMappingURL=string.d.ts.map
|
package/dist/string.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../src/string.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEjE,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAErE;AAED,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAIrE;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAEpE;AAED,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAEhE;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC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AAG,MAAM,CAE5D"}
|
package/dist/string.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@douglasneuroinformatics/libjs",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"packageManager": "pnpm@10.3.0",
|
|
6
6
|
"description": "A collection of utility functions and types for Node.js and the browser",
|
|
7
7
|
"author": "Joshua Unrau",
|
|
@@ -34,6 +34,9 @@
|
|
|
34
34
|
"zod": "^3.22.6"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"clean-stack": "^5.2.0",
|
|
38
|
+
"extract-stack": "^3.0.0",
|
|
39
|
+
"stringify-object": "^5.0.0",
|
|
37
40
|
"type-fest": "^4.34.1"
|
|
38
41
|
},
|
|
39
42
|
"devDependencies": {
|
|
@@ -42,6 +45,7 @@
|
|
|
42
45
|
"@douglasneuroinformatics/semantic-release": "^0.2.1",
|
|
43
46
|
"@douglasneuroinformatics/tsconfig": "^1.0.2",
|
|
44
47
|
"@types/node": "22.x",
|
|
48
|
+
"@types/stringify-object": "^4.0.5",
|
|
45
49
|
"@vitest/coverage-v8": "^3.0.5",
|
|
46
50
|
"concurrently": "^9.1.2",
|
|
47
51
|
"eslint": "^9.20.1",
|