@zelgadis87/utils-core 5.1.4 → 5.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/Logger.d.ts +2 -1
- package/dist/Optional.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/errors/withTryCatch.d.ts +1 -1
- package/dist/utils/errors.d.ts +2 -0
- package/dist/utils/json.d.ts +2 -0
- package/dist/utils/strings.d.ts +2 -0
- package/esbuild/index.cjs +58 -15
- package/esbuild/index.cjs.map +2 -2
- package/esbuild/index.mjs +52 -15
- package/esbuild/index.mjs.map +2 -2
- package/package.json +1 -1
- package/src/Logger.ts +2 -1
- package/src/Optional.ts +6 -0
- package/src/utils/errors/withTryCatch.ts +8 -4
- package/src/utils/errors.ts +15 -6
- package/src/utils/json.ts +9 -1
- package/src/utils/strings.ts +10 -2
package/package.json
CHANGED
package/src/Logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import TimeInstant from "./time/TimeInstant";
|
|
2
2
|
import { noop, padRight } from "./utils/_index.js";
|
|
3
3
|
|
|
4
|
-
const LEVELS = [ "log", "debug", "info", "warn", "error" ] as const;
|
|
4
|
+
const LEVELS = [ "trace", "log", "debug", "info", "warn", "error" ] as const;
|
|
5
5
|
type TLogLevel = keyof Console & typeof LEVELS[ number ]
|
|
6
6
|
|
|
7
7
|
const timestamp = () => TimeInstant.now().format( 'HH:mm:ss' );
|
|
@@ -27,6 +27,7 @@ export class Logger {
|
|
|
27
27
|
this.minLevel = LEVELS.indexOf( ( args?.minLevel ?? "DEBUG" ).toLowerCase() as TLogLevel );
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
public get trace() { return this.logWrap( 'trace' ); }
|
|
30
31
|
public get log() { return this.logWrap( 'log' ); }
|
|
31
32
|
public get debug() { return this.logWrap( 'debug' ); }
|
|
32
33
|
public get info() { return this.logWrap( 'info' ); }
|
package/src/Optional.ts
CHANGED
|
@@ -60,6 +60,12 @@ export class Optional<T> implements TOptional<T> {
|
|
|
60
60
|
if ( this.isPresent() )
|
|
61
61
|
callback( this.get() );
|
|
62
62
|
}
|
|
63
|
+
public ifPresentThenClear( callback: TConsumer<T> ) {
|
|
64
|
+
if ( this.isPresent() ) {
|
|
65
|
+
callback( this.get() );
|
|
66
|
+
this.clear();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
63
69
|
public apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ) {
|
|
64
70
|
if ( this.isEmpty() ) {
|
|
65
71
|
callbackIfEmpty();
|
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
import { asError } from "../errors.js";
|
|
3
3
|
import { identity, type TFunction, type TTransformer } from "../functions.js";
|
|
4
4
|
|
|
5
|
-
export function withTryCatch<R>( fn: TFunction<void, R>, errMapFn
|
|
5
|
+
export function withTryCatch<R>( fn: TFunction<void, R>, errMapFn: TTransformer<Error, Error> = identity ): [ R, undefined ] | [ undefined, Error ] {
|
|
6
6
|
try {
|
|
7
7
|
return [ fn(), void 0 ] as const;
|
|
8
8
|
} catch ( e: unknown ) {
|
|
9
|
-
return [ void 0,
|
|
9
|
+
return [ void 0, errMapFn( asError( e ) ) ] as const;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export async function withTryCatchAsync<R>( fn: TFunction<void, Promise<R>>, errMapFn: TTransformer<Error, Error> ): Promise<[ R, undefined ] | [ undefined, Error ]> {
|
|
14
|
-
|
|
13
|
+
export async function withTryCatchAsync<R>( fn: TFunction<void, Promise<R>>, errMapFn: TTransformer<Error, Error> = identity ): Promise<[ R, undefined ] | [ undefined, Error ]> {
|
|
14
|
+
try {
|
|
15
|
+
return [ await fn(), void 0 ] as const;
|
|
16
|
+
} catch ( e: unknown ) {
|
|
17
|
+
return [ void 0, errMapFn( asError( e ) ) ] as const;
|
|
18
|
+
}
|
|
15
19
|
}
|
package/src/utils/errors.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isNullOrUndefined } from './nulls.js';
|
|
1
2
|
|
|
2
3
|
export * from './errors/withTryCatch.js';
|
|
3
4
|
|
|
@@ -18,15 +19,23 @@ export function isError( e: unknown ): e is Error {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
export function getMessageFromError( error: Error ): string {
|
|
21
|
-
|
|
22
|
-
const cause = maybeCause ? `\ncaused by: ${maybeCause}` : '';
|
|
23
|
-
return `${error.name}: ${error.message}${cause}`;
|
|
22
|
+
return `${error.name}: ${error.message}${getCauseMessageFromError( error.cause )}`;
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
export function getStackFromError( error: Error ): string {
|
|
27
|
-
const maybeCause = error.cause ? getStackFromError( asError( error.cause ) ) : null;
|
|
28
|
-
const cause = maybeCause ? `\ncaused by: ${maybeCause}` : '';
|
|
29
26
|
const stack = error.stack && error.stack.includes( error.message ) ? error.stack : error.message + ' ' + ( error.stack ?? '' );
|
|
30
|
-
return `${error.name}: ${stack}${cause}`;
|
|
27
|
+
return `${error.name}: ${stack}${getCauseStackFromError( error.cause )}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getCauseMessageFromError( cause: unknown ) {
|
|
31
|
+
if ( isNullOrUndefined( cause ) )
|
|
32
|
+
return '';
|
|
33
|
+
return `\ncaused by: ${getMessageFromError( asError( cause ) )}`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getCauseStackFromError( cause: unknown ) {
|
|
37
|
+
if ( isNullOrUndefined( cause ) )
|
|
38
|
+
return '';
|
|
39
|
+
return `\ncaused by: ${getStackFromError( asError( cause ) )}`
|
|
31
40
|
}
|
|
32
41
|
|
package/src/utils/json.ts
CHANGED
|
@@ -6,7 +6,15 @@ export type TJsonObject = { [ key: string ]: TJsonSerializable }
|
|
|
6
6
|
export type TJsonSerializable = TJsonPrimitive | TJsonArray | TJsonObject;
|
|
7
7
|
|
|
8
8
|
export function tryToParseJson<T extends TJsonSerializable>( jsonContent: string ): [ T, undefined ] | [ undefined, Error ] {
|
|
9
|
-
return withTryCatch( () =>
|
|
9
|
+
return withTryCatch( () => parseJson<T>( jsonContent ) );
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function parseJson<T extends TJsonSerializable>( jsonContent: string ): T {
|
|
13
|
+
return JSON.parse( jsonContent ) as T;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function stringifyJson( jsonSerializable: TJsonSerializable, minify = false ): string {
|
|
17
|
+
return JSON.stringify( jsonSerializable, undefined, minify ? undefined : 2 );
|
|
10
18
|
}
|
|
11
19
|
|
|
12
20
|
export function jsonCloneDeep<A extends TJsonSerializable>( a: A ): A {
|
package/src/utils/strings.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { range } from "./arrays.js";
|
|
2
|
-
import { ensureDefined, isNullOrUndefined, TMaybe } from "./nulls.js";
|
|
2
|
+
import { ensureDefined, isDefined, isNullOrUndefined, TMaybe } from "./nulls.js";
|
|
3
3
|
|
|
4
4
|
export * from './strings/StringParts.js';
|
|
5
5
|
|
|
@@ -81,8 +81,16 @@ export function pluralize( n: number, singular: string, plural?: string ): strin
|
|
|
81
81
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
export function isString( source: unknown ): source is string {
|
|
85
|
+
return isDefined( source ) && typeof source === "string";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function isEmpty( source: string ): boolean {
|
|
89
|
+
return source.trim().length === 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
84
92
|
export function isNullOrUndefinedOrEmpty( source: TMaybe<string> ): boolean {
|
|
85
|
-
return isNullOrUndefined( source ) || source
|
|
93
|
+
return isNullOrUndefined( source ) || isEmpty( source );
|
|
86
94
|
}
|
|
87
95
|
|
|
88
96
|
|