@zelgadis87/utils-core 5.1.4 → 5.1.5

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.4",
4
+ "version": "5.1.5",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
@@ -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?: TTransformer<Error, Error> ): [ R, undefined ] | [ undefined, Error ] {
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, ( errMapFn ?? identity )( asError( e ) ) ] as const;
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
- return fn().then( ( value: R ) => [ value, void 0 ] as const, ( e: unknown ) => [ void 0, ( errMapFn ?? identity )( asError( e ) ) ] as const );
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/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( () => JSON.parse( jsonContent ) as T );
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 ? 2 : undefined );
10
18
  }
11
19
 
12
20
  export function jsonCloneDeep<A extends TJsonSerializable>( a: A ): A {