@augment-vir/common
Advanced tools
Comparing version 25.0.0 to 26.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.executeAndReturnError = exports.wrapInTry = exports.ensureErrorAndPrependMessage = exports.ensureError = exports.extractErrorMessage = exports.combineErrorMessages = exports.combineErrors = void 0; | ||
exports.ensureErrorAndPrependMessage = exports.ensureError = exports.extractErrorMessage = exports.combineErrorMessages = exports.combineErrors = void 0; | ||
const boolean_1 = require("./boolean"); | ||
const typed_has_property_1 = require("./object/typed-has-property"); | ||
const promise_1 = require("./promise/promise"); | ||
function combineErrors(errors) { | ||
@@ -55,54 +54,1 @@ if (!errors || errors.length === 0) { | ||
exports.ensureErrorAndPrependMessage = ensureErrorAndPrependMessage; | ||
function wrapInTry(callback, inputs) { | ||
try { | ||
const returnValue = callback(); | ||
if (returnValue instanceof Promise) { | ||
return returnValue.catch((error) => { | ||
if (inputs.catchCallback) { | ||
return inputs.catchCallback(error); | ||
} | ||
else { | ||
return inputs.fallbackValue; | ||
} | ||
}); | ||
} | ||
else { | ||
return returnValue; | ||
} | ||
} | ||
catch (error) { | ||
if (inputs.catchCallback) { | ||
return inputs.catchCallback(error); | ||
} | ||
else { | ||
return inputs.fallbackValue; | ||
} | ||
} | ||
} | ||
exports.wrapInTry = wrapInTry; | ||
function executeAndReturnError(callback) { | ||
let caughtError; | ||
try { | ||
const result = callback(); | ||
if ((0, promise_1.isPromiseLike)(result)) { | ||
return new Promise(async (resolve) => { | ||
try { | ||
const output = await result; | ||
return resolve(output); | ||
} | ||
catch (error) { | ||
caughtError = ensureError(error); | ||
} | ||
return resolve(caughtError); | ||
}); | ||
} | ||
else { | ||
return result; | ||
} | ||
} | ||
catch (error) { | ||
caughtError = ensureError(error); | ||
} | ||
return caughtError; | ||
} | ||
exports.executeAndReturnError = executeAndReturnError; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.areJsonEqual = exports.stringifyJson = exports.parseJson = void 0; | ||
exports.stringifyJson = exports.parseJson = void 0; | ||
const run_time_assertions_1 = require("run-time-assertions"); | ||
const error_1 = require("./error"); | ||
const matches_object_shape_1 = require("./object/matches-object-shape"); | ||
const object_1 = require("./object/object"); | ||
const wrap_in_try_1 = require("./wrap-in-try"); | ||
function parseJson({ jsonString, errorHandler, shapeMatcher, }) { | ||
@@ -31,69 +30,11 @@ try { | ||
exports.parseJson = parseJson; | ||
function stringifyJson({ source, whitespace, errorHandler, }) { | ||
try { | ||
const stringifiedJson = JSON.stringify(source, undefined, whitespace); | ||
return stringifiedJson; | ||
function stringifyJson(jsonValue, { whitespace, ...tryOptions } = {}) { | ||
const result = (0, wrap_in_try_1.wrapInTry)(() => JSON.stringify(jsonValue, undefined, whitespace), tryOptions); | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
catch (error) { | ||
if (errorHandler) { | ||
return errorHandler(error); | ||
} | ||
else { | ||
throw error; | ||
} | ||
else { | ||
return result; | ||
} | ||
} | ||
exports.stringifyJson = stringifyJson; | ||
const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify'; | ||
function baseAreJsonEqual(a, b, ignoreStringifyErrors) { | ||
return (stringifyJson({ | ||
source: a, | ||
errorHandler(error) { | ||
if (ignoreStringifyErrors) { | ||
return ''; | ||
} | ||
else { | ||
throw error; | ||
} | ||
}, | ||
}) === | ||
stringifyJson({ | ||
source: b, | ||
errorHandler(error) { | ||
if (ignoreStringifyErrors) { | ||
return ''; | ||
} | ||
else { | ||
throw error; | ||
} | ||
}, | ||
})); | ||
} | ||
function areJsonEqual(a, b, options = {}) { | ||
try { | ||
if (a === b) { | ||
return true; | ||
} | ||
if ((0, object_1.isObject)(a) && (0, object_1.isObject)(b)) { | ||
const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort(), !!options?.ignoreNonSerializableProperties); | ||
if (!areKeysEqual) { | ||
return false; | ||
} | ||
return Object.keys(a).every((keyName) => { | ||
return areJsonEqual(a[keyName], b[keyName]); | ||
}); | ||
} | ||
else { | ||
return baseAreJsonEqual(a, b, !!options?.ignoreNonSerializableProperties); | ||
} | ||
} | ||
catch (caught) { | ||
const error = (0, error_1.ensureError)(caught); | ||
if (error.message.startsWith(areJsonEqualFailureMessage)) { | ||
throw error; | ||
} | ||
error.message = `${areJsonEqualFailureMessage}: ${error.message}`; | ||
throw error; | ||
} | ||
} | ||
exports.areJsonEqual = areJsonEqual; |
@@ -58,1 +58,2 @@ "use strict"; | ||
__exportStar(require("./augments/type"), exports); | ||
__exportStar(require("./augments/wrap-in-try"), exports); |
import { isTruthy } from './boolean'; | ||
import { typedHasProperty } from './object/typed-has-property'; | ||
import { isPromiseLike } from './promise/promise'; | ||
export function combineErrors(errors) { | ||
@@ -47,52 +46,1 @@ if (!errors || errors.length === 0) { | ||
} | ||
export function wrapInTry(callback, inputs) { | ||
try { | ||
const returnValue = callback(); | ||
if (returnValue instanceof Promise) { | ||
return returnValue.catch((error) => { | ||
if (inputs.catchCallback) { | ||
return inputs.catchCallback(error); | ||
} | ||
else { | ||
return inputs.fallbackValue; | ||
} | ||
}); | ||
} | ||
else { | ||
return returnValue; | ||
} | ||
} | ||
catch (error) { | ||
if (inputs.catchCallback) { | ||
return inputs.catchCallback(error); | ||
} | ||
else { | ||
return inputs.fallbackValue; | ||
} | ||
} | ||
} | ||
export function executeAndReturnError(callback) { | ||
let caughtError; | ||
try { | ||
const result = callback(); | ||
if (isPromiseLike(result)) { | ||
return new Promise(async (resolve) => { | ||
try { | ||
const output = await result; | ||
return resolve(output); | ||
} | ||
catch (error) { | ||
caughtError = ensureError(error); | ||
} | ||
return resolve(caughtError); | ||
}); | ||
} | ||
else { | ||
return result; | ||
} | ||
} | ||
catch (error) { | ||
caughtError = ensureError(error); | ||
} | ||
return caughtError; | ||
} |
import { assertRunTimeType, getRunTimeType, isRunTimeType } from 'run-time-assertions'; | ||
import { ensureError } from './error'; | ||
import { assertMatchesObjectShape } from './object/matches-object-shape'; | ||
import { isObject } from './object/object'; | ||
import { wrapInTry } from './wrap-in-try'; | ||
export function parseJson({ jsonString, errorHandler, shapeMatcher, }) { | ||
@@ -27,67 +26,10 @@ try { | ||
} | ||
export function stringifyJson({ source, whitespace, errorHandler, }) { | ||
try { | ||
const stringifiedJson = JSON.stringify(source, undefined, whitespace); | ||
return stringifiedJson; | ||
export function stringifyJson(jsonValue, { whitespace, ...tryOptions } = {}) { | ||
const result = wrapInTry(() => JSON.stringify(jsonValue, undefined, whitespace), tryOptions); | ||
if (result instanceof Error) { | ||
throw result; | ||
} | ||
catch (error) { | ||
if (errorHandler) { | ||
return errorHandler(error); | ||
} | ||
else { | ||
throw error; | ||
} | ||
else { | ||
return result; | ||
} | ||
} | ||
const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify'; | ||
function baseAreJsonEqual(a, b, ignoreStringifyErrors) { | ||
return (stringifyJson({ | ||
source: a, | ||
errorHandler(error) { | ||
if (ignoreStringifyErrors) { | ||
return ''; | ||
} | ||
else { | ||
throw error; | ||
} | ||
}, | ||
}) === | ||
stringifyJson({ | ||
source: b, | ||
errorHandler(error) { | ||
if (ignoreStringifyErrors) { | ||
return ''; | ||
} | ||
else { | ||
throw error; | ||
} | ||
}, | ||
})); | ||
} | ||
export function areJsonEqual(a, b, options = {}) { | ||
try { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (isObject(a) && isObject(b)) { | ||
const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort(), !!options?.ignoreNonSerializableProperties); | ||
if (!areKeysEqual) { | ||
return false; | ||
} | ||
return Object.keys(a).every((keyName) => { | ||
return areJsonEqual(a[keyName], b[keyName]); | ||
}); | ||
} | ||
else { | ||
return baseAreJsonEqual(a, b, !!options?.ignoreNonSerializableProperties); | ||
} | ||
} | ||
catch (caught) { | ||
const error = ensureError(caught); | ||
if (error.message.startsWith(areJsonEqualFailureMessage)) { | ||
throw error; | ||
} | ||
error.message = `${areJsonEqualFailureMessage}: ${error.message}`; | ||
throw error; | ||
} | ||
} |
@@ -42,1 +42,2 @@ export * from './augments/ansi'; | ||
export * from './augments/type'; | ||
export * from './augments/wrap-in-try'; |
@@ -1,3 +0,1 @@ | ||
import { RequireExactlyOne } from 'type-fest'; | ||
import { NoInputsFunction } from './function'; | ||
import { AtLeastTuple } from './tuple'; | ||
@@ -12,8 +10,1 @@ export declare function combineErrors(errors: AtLeastTuple<Error, 1>): Error; | ||
export declare function ensureErrorAndPrependMessage(maybeError: unknown, prependMessage: string): Error; | ||
export type TryWrapConfig<FallbackReturn> = RequireExactlyOne<{ | ||
fallbackValue: FallbackReturn; | ||
catchCallback: (error: unknown) => FallbackReturn; | ||
}>; | ||
export declare function wrapInTry<CallbackReturn, FallbackReturn>(callback: () => CallbackReturn, inputs: TryWrapConfig<FallbackReturn>): FallbackReturn | CallbackReturn; | ||
export declare function executeAndReturnError<CallbackGeneric extends NoInputsFunction<PromiseLike<any>>>(callback: CallbackGeneric): Promise<Error | Awaited<ReturnType<CallbackGeneric>>>; | ||
export declare function executeAndReturnError<CallbackGeneric extends NoInputsFunction>(callback: CallbackGeneric): Error | ReturnType<CallbackGeneric>; |
import { JsonCompatibleValue } from './json-compatible'; | ||
import { WrapInTryOptions } from './wrap-in-try'; | ||
export declare function parseJson<ParsedJsonGeneric>({ jsonString, errorHandler, shapeMatcher, }: { | ||
@@ -7,9 +8,4 @@ jsonString: string; | ||
}): ParsedJsonGeneric; | ||
export declare function stringifyJson({ source, whitespace, errorHandler, }: { | ||
source: unknown; | ||
export declare function stringifyJson(jsonValue: JsonCompatibleValue, { whitespace, ...tryOptions }?: { | ||
whitespace?: number; | ||
errorHandler?: (error: unknown) => string | never; | ||
}): string; | ||
export declare function areJsonEqual(a: Readonly<JsonCompatibleValue | undefined>, b: Readonly<JsonCompatibleValue | undefined>, options?: Partial<{ | ||
ignoreNonSerializableProperties: boolean | undefined; | ||
}>): boolean; | ||
} & WrapInTryOptions<string>): string; |
@@ -42,1 +42,2 @@ export * from './augments/ansi'; | ||
export * from './augments/type'; | ||
export * from './augments/wrap-in-try'; |
{ | ||
"name": "@augment-vir/common", | ||
"version": "25.0.0", | ||
"version": "26.0.0", | ||
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common", | ||
@@ -5,0 +5,0 @@ "bugs": { |
131
177092
4221