@zelgadis87/utils-core 5.1.5 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@zelgadis87/utils-core",
4
- "version": "5.1.5",
4
+ "version": "5.2.0",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
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();
@@ -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
- const maybeCause = error.cause ? getMessageFromError( asError( error.cause ) ) : null;
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
@@ -14,7 +14,7 @@ export function parseJson<T extends TJsonSerializable>( jsonContent: string ): T
14
14
  }
15
15
 
16
16
  export function stringifyJson( jsonSerializable: TJsonSerializable, minify = false ): string {
17
- return JSON.stringify( jsonSerializable, undefined, minify ? 2 : undefined );
17
+ return JSON.stringify( jsonSerializable, undefined, minify ? undefined : 2 );
18
18
  }
19
19
 
20
20
  export function jsonCloneDeep<A extends TJsonSerializable>( a: A ): A {
@@ -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.length === 0;
93
+ return isNullOrUndefined( source ) || isEmpty( source );
86
94
  }
87
95
 
88
96