@augment-vir/common 25.0.0 → 26.0.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.
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.executeAndReturnError = exports.wrapInTry = exports.ensureErrorAndPrependMessage = exports.ensureError = exports.extractErrorMessage = exports.combineErrorMessages = exports.combineErrors = void 0;
3
+ exports.ensureErrorAndPrependMessage = exports.ensureError = exports.extractErrorMessage = exports.combineErrorMessages = exports.combineErrors = void 0;
4
4
  const boolean_1 = require("./boolean");
5
5
  const typed_has_property_1 = require("./object/typed-has-property");
6
- const promise_1 = require("./promise/promise");
7
6
  function combineErrors(errors) {
8
7
  if (!errors || errors.length === 0) {
9
8
  return undefined;
@@ -52,56 +51,3 @@ function ensureErrorAndPrependMessage(maybeError, prependMessage) {
52
51
  return error;
53
52
  }
54
53
  exports.ensureErrorAndPrependMessage = ensureErrorAndPrependMessage;
55
- function wrapInTry(callback, inputs) {
56
- try {
57
- const returnValue = callback();
58
- if (returnValue instanceof Promise) {
59
- return returnValue.catch((error) => {
60
- if (inputs.catchCallback) {
61
- return inputs.catchCallback(error);
62
- }
63
- else {
64
- return inputs.fallbackValue;
65
- }
66
- });
67
- }
68
- else {
69
- return returnValue;
70
- }
71
- }
72
- catch (error) {
73
- if (inputs.catchCallback) {
74
- return inputs.catchCallback(error);
75
- }
76
- else {
77
- return inputs.fallbackValue;
78
- }
79
- }
80
- }
81
- exports.wrapInTry = wrapInTry;
82
- function executeAndReturnError(callback) {
83
- let caughtError;
84
- try {
85
- const result = callback();
86
- if ((0, promise_1.isPromiseLike)(result)) {
87
- return new Promise(async (resolve) => {
88
- try {
89
- const output = await result;
90
- return resolve(output);
91
- }
92
- catch (error) {
93
- caughtError = ensureError(error);
94
- }
95
- return resolve(caughtError);
96
- });
97
- }
98
- else {
99
- return result;
100
- }
101
- }
102
- catch (error) {
103
- caughtError = ensureError(error);
104
- }
105
- return caughtError;
106
- }
107
- exports.executeAndReturnError = executeAndReturnError;
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.areJsonEqual = exports.stringifyJson = exports.parseJson = void 0;
3
+ exports.stringifyJson = exports.parseJson = void 0;
4
4
  const run_time_assertions_1 = require("run-time-assertions");
5
- const error_1 = require("./error");
6
5
  const matches_object_shape_1 = require("./object/matches-object-shape");
7
- const object_1 = require("./object/object");
6
+ const wrap_in_try_1 = require("./wrap-in-try");
8
7
  function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
9
8
  try {
10
9
  const parsedJson = JSON.parse(jsonString);
@@ -28,71 +27,13 @@ function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
28
27
  }
29
28
  }
30
29
  exports.parseJson = parseJson;
31
- function stringifyJson({ source, whitespace, errorHandler, }) {
32
- try {
33
- const stringifiedJson = JSON.stringify(source, undefined, whitespace);
34
- return stringifiedJson;
30
+ function stringifyJson(jsonValue, { whitespace, ...tryOptions } = {}) {
31
+ const result = (0, wrap_in_try_1.wrapInTry)(() => JSON.stringify(jsonValue, undefined, whitespace), tryOptions);
32
+ if (result instanceof Error) {
33
+ throw result;
35
34
  }
36
- catch (error) {
37
- if (errorHandler) {
38
- return errorHandler(error);
39
- }
40
- else {
41
- throw error;
42
- }
35
+ else {
36
+ return result;
43
37
  }
44
38
  }
45
39
  exports.stringifyJson = stringifyJson;
46
- const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify';
47
- function baseAreJsonEqual(a, b, ignoreStringifyErrors) {
48
- return (stringifyJson({
49
- source: a,
50
- errorHandler(error) {
51
- if (ignoreStringifyErrors) {
52
- return '';
53
- }
54
- else {
55
- throw error;
56
- }
57
- },
58
- }) ===
59
- stringifyJson({
60
- source: b,
61
- errorHandler(error) {
62
- if (ignoreStringifyErrors) {
63
- return '';
64
- }
65
- else {
66
- throw error;
67
- }
68
- },
69
- }));
70
- }
71
- function areJsonEqual(a, b, options = {}) {
72
- try {
73
- if (a === b) {
74
- return true;
75
- }
76
- if ((0, object_1.isObject)(a) && (0, object_1.isObject)(b)) {
77
- const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort(), !!options?.ignoreNonSerializableProperties);
78
- if (!areKeysEqual) {
79
- return false;
80
- }
81
- return Object.keys(a).every((keyName) => {
82
- return areJsonEqual(a[keyName], b[keyName]);
83
- });
84
- }
85
- else {
86
- return baseAreJsonEqual(a, b, !!options?.ignoreNonSerializableProperties);
87
- }
88
- }
89
- catch (caught) {
90
- const error = (0, error_1.ensureError)(caught);
91
- if (error.message.startsWith(areJsonEqualFailureMessage)) {
92
- throw error;
93
- }
94
- error.message = `${areJsonEqualFailureMessage}: ${error.message}`;
95
- throw error;
96
- }
97
- }
98
- exports.areJsonEqual = areJsonEqual;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapInTry = void 0;
4
+ const run_time_assertions_1 = require("run-time-assertions");
5
+ const error_1 = require("./error");
6
+ function wrapInTry(callback, options = {}) {
7
+ try {
8
+ const value = callback();
9
+ if (value instanceof Promise) {
10
+ return value.catch((error) => {
11
+ if (options.handleError) {
12
+ return options.handleError(error);
13
+ }
14
+ else if ((0, run_time_assertions_1.hasProperty)(options, 'fallbackValue')) {
15
+ return options.fallbackValue;
16
+ }
17
+ else {
18
+ return (0, error_1.ensureError)(error);
19
+ }
20
+ });
21
+ }
22
+ else {
23
+ return value;
24
+ }
25
+ }
26
+ catch (error) {
27
+ if (options.handleError) {
28
+ return options.handleError(error);
29
+ }
30
+ else if ((0, run_time_assertions_1.hasProperty)(options, 'fallbackValue')) {
31
+ return options.fallbackValue;
32
+ }
33
+ else {
34
+ return (0, error_1.ensureError)(error);
35
+ }
36
+ }
37
+ }
38
+ exports.wrapInTry = wrapInTry;
package/dist/cjs/index.js CHANGED
@@ -55,3 +55,4 @@ __exportStar(require("./augments/time"), exports);
55
55
  __exportStar(require("./augments/truncate-number"), exports);
56
56
  __exportStar(require("./augments/tuple"), exports);
57
57
  __exportStar(require("./augments/type"), exports);
58
+ __exportStar(require("./augments/wrap-in-try"), exports);
@@ -1,6 +1,5 @@
1
1
  import { isTruthy } from './boolean';
2
2
  import { typedHasProperty } from './object/typed-has-property';
3
- import { isPromiseLike } from './promise/promise';
4
3
  export function combineErrors(errors) {
5
4
  if (!errors || errors.length === 0) {
6
5
  return undefined;
@@ -44,54 +43,3 @@ export function ensureErrorAndPrependMessage(maybeError, prependMessage) {
44
43
  error.message = `${prependMessage}: ${error.message}`;
45
44
  return error;
46
45
  }
47
- export function wrapInTry(callback, inputs) {
48
- try {
49
- const returnValue = callback();
50
- if (returnValue instanceof Promise) {
51
- return returnValue.catch((error) => {
52
- if (inputs.catchCallback) {
53
- return inputs.catchCallback(error);
54
- }
55
- else {
56
- return inputs.fallbackValue;
57
- }
58
- });
59
- }
60
- else {
61
- return returnValue;
62
- }
63
- }
64
- catch (error) {
65
- if (inputs.catchCallback) {
66
- return inputs.catchCallback(error);
67
- }
68
- else {
69
- return inputs.fallbackValue;
70
- }
71
- }
72
- }
73
- export function executeAndReturnError(callback) {
74
- let caughtError;
75
- try {
76
- const result = callback();
77
- if (isPromiseLike(result)) {
78
- return new Promise(async (resolve) => {
79
- try {
80
- const output = await result;
81
- return resolve(output);
82
- }
83
- catch (error) {
84
- caughtError = ensureError(error);
85
- }
86
- return resolve(caughtError);
87
- });
88
- }
89
- else {
90
- return result;
91
- }
92
- }
93
- catch (error) {
94
- caughtError = ensureError(error);
95
- }
96
- return caughtError;
97
- }
@@ -1,7 +1,6 @@
1
1
  import { assertRunTimeType, getRunTimeType, isRunTimeType } from 'run-time-assertions';
2
- import { ensureError } from './error';
3
2
  import { assertMatchesObjectShape } from './object/matches-object-shape';
4
- import { isObject } from './object/object';
3
+ import { wrapInTry } from './wrap-in-try';
5
4
  export function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
6
5
  try {
7
6
  const parsedJson = JSON.parse(jsonString);
@@ -24,69 +23,12 @@ export function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
24
23
  }
25
24
  }
26
25
  }
27
- export function stringifyJson({ source, whitespace, errorHandler, }) {
28
- try {
29
- const stringifiedJson = JSON.stringify(source, undefined, whitespace);
30
- return stringifiedJson;
31
- }
32
- catch (error) {
33
- if (errorHandler) {
34
- return errorHandler(error);
35
- }
36
- else {
37
- throw error;
38
- }
26
+ export function stringifyJson(jsonValue, { whitespace, ...tryOptions } = {}) {
27
+ const result = wrapInTry(() => JSON.stringify(jsonValue, undefined, whitespace), tryOptions);
28
+ if (result instanceof Error) {
29
+ throw result;
39
30
  }
40
- }
41
- const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify';
42
- function baseAreJsonEqual(a, b, ignoreStringifyErrors) {
43
- return (stringifyJson({
44
- source: a,
45
- errorHandler(error) {
46
- if (ignoreStringifyErrors) {
47
- return '';
48
- }
49
- else {
50
- throw error;
51
- }
52
- },
53
- }) ===
54
- stringifyJson({
55
- source: b,
56
- errorHandler(error) {
57
- if (ignoreStringifyErrors) {
58
- return '';
59
- }
60
- else {
61
- throw error;
62
- }
63
- },
64
- }));
65
- }
66
- export function areJsonEqual(a, b, options = {}) {
67
- try {
68
- if (a === b) {
69
- return true;
70
- }
71
- if (isObject(a) && isObject(b)) {
72
- const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort(), !!options?.ignoreNonSerializableProperties);
73
- if (!areKeysEqual) {
74
- return false;
75
- }
76
- return Object.keys(a).every((keyName) => {
77
- return areJsonEqual(a[keyName], b[keyName]);
78
- });
79
- }
80
- else {
81
- return baseAreJsonEqual(a, b, !!options?.ignoreNonSerializableProperties);
82
- }
83
- }
84
- catch (caught) {
85
- const error = ensureError(caught);
86
- if (error.message.startsWith(areJsonEqualFailureMessage)) {
87
- throw error;
88
- }
89
- error.message = `${areJsonEqualFailureMessage}: ${error.message}`;
90
- throw error;
31
+ else {
32
+ return result;
91
33
  }
92
34
  }
@@ -0,0 +1,34 @@
1
+ import { hasProperty } from 'run-time-assertions';
2
+ import { ensureError } from './error';
3
+ export function wrapInTry(callback, options = {}) {
4
+ try {
5
+ const value = callback();
6
+ if (value instanceof Promise) {
7
+ return value.catch((error) => {
8
+ if (options.handleError) {
9
+ return options.handleError(error);
10
+ }
11
+ else if (hasProperty(options, 'fallbackValue')) {
12
+ return options.fallbackValue;
13
+ }
14
+ else {
15
+ return ensureError(error);
16
+ }
17
+ });
18
+ }
19
+ else {
20
+ return value;
21
+ }
22
+ }
23
+ catch (error) {
24
+ if (options.handleError) {
25
+ return options.handleError(error);
26
+ }
27
+ else if (hasProperty(options, 'fallbackValue')) {
28
+ return options.fallbackValue;
29
+ }
30
+ else {
31
+ return ensureError(error);
32
+ }
33
+ }
34
+ }
package/dist/esm/index.js CHANGED
@@ -39,3 +39,4 @@ export * from './augments/time';
39
39
  export * from './augments/truncate-number';
40
40
  export * from './augments/tuple';
41
41
  export * from './augments/type';
42
+ export * from './augments/wrap-in-try';
@@ -1,5 +1,3 @@
1
- import { RequireExactlyOne } from 'type-fest';
2
- import { NoInputsFunction } from './function';
3
1
  import { AtLeastTuple } from './tuple';
4
2
  export declare function combineErrors(errors: AtLeastTuple<Error, 1>): Error;
5
3
  export declare function combineErrors(errors: ReadonlyArray<never>): undefined;
@@ -9,10 +7,3 @@ export declare function combineErrorMessages(errors?: ReadonlyArray<Error | stri
9
7
  export declare function extractErrorMessage(maybeError: unknown): string;
10
8
  export declare function ensureError(maybeError: unknown): Error;
11
9
  export declare function ensureErrorAndPrependMessage(maybeError: unknown, prependMessage: string): Error;
12
- export type TryWrapConfig<FallbackReturn> = RequireExactlyOne<{
13
- fallbackValue: FallbackReturn;
14
- catchCallback: (error: unknown) => FallbackReturn;
15
- }>;
16
- export declare function wrapInTry<CallbackReturn, FallbackReturn>(callback: () => CallbackReturn, inputs: TryWrapConfig<FallbackReturn>): FallbackReturn | CallbackReturn;
17
- export declare function executeAndReturnError<CallbackGeneric extends NoInputsFunction<PromiseLike<any>>>(callback: CallbackGeneric): Promise<Error | Awaited<ReturnType<CallbackGeneric>>>;
18
- export declare function executeAndReturnError<CallbackGeneric extends NoInputsFunction>(callback: CallbackGeneric): Error | ReturnType<CallbackGeneric>;
@@ -1,14 +1,10 @@
1
1
  import { JsonCompatibleValue } from './json-compatible';
2
+ import { WrapInTryOptions } from './wrap-in-try';
2
3
  export declare function parseJson<ParsedJsonGeneric>({ jsonString, errorHandler, shapeMatcher, }: {
3
4
  jsonString: string;
4
5
  errorHandler?: (error: unknown) => never | ParsedJsonGeneric;
5
6
  shapeMatcher?: ParsedJsonGeneric;
6
7
  }): ParsedJsonGeneric;
7
- export declare function stringifyJson({ source, whitespace, errorHandler, }: {
8
- source: unknown;
8
+ export declare function stringifyJson(jsonValue: JsonCompatibleValue, { whitespace, ...tryOptions }?: {
9
9
  whitespace?: number;
10
- errorHandler?: (error: unknown) => string | never;
11
- }): string;
12
- export declare function areJsonEqual(a: Readonly<JsonCompatibleValue | undefined>, b: Readonly<JsonCompatibleValue | undefined>, options?: Partial<{
13
- ignoreNonSerializableProperties: boolean | undefined;
14
- }>): boolean;
10
+ } & WrapInTryOptions<string>): string;
@@ -0,0 +1,40 @@
1
+ import { NoInputsFunction } from './function';
2
+ import { PartialAndUndefined } from './object/object';
3
+ export type WrapInTryOptions<FallbackValue> = PartialAndUndefined<{
4
+ /**
5
+ * A callback that is passed the error. The output of this callback is returned by `wrapInTry`.
6
+ * This takes precedence over the other two options.
7
+ */
8
+ handleError: (error: unknown) => FallbackValue;
9
+ /**
10
+ * Fallback to this value if the callback passed to `wrapInTry` throws an error. Takes
11
+ * precedence over `returnError`.
12
+ */
13
+ fallbackValue: FallbackValue;
14
+ }>;
15
+ export declare function wrapInTry<Value extends Promise<any>>(callback: NoInputsFunction<Value>, options?: undefined | {
16
+ handleError?: undefined;
17
+ fallbackValue?: never;
18
+ }): Promise<Error | Awaited<Value>>;
19
+ export declare function wrapInTry<Value>(callback: NoInputsFunction<Value>, options?: undefined | {
20
+ handleError?: undefined;
21
+ fallbackValue?: never;
22
+ }): Error | Value;
23
+ export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: {
24
+ handleError: (error: unknown) => FallbackValue;
25
+ fallbackValue?: FallbackValue;
26
+ }): Promise<Awaited<FallbackValue> | Awaited<Value>>;
27
+ export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: {
28
+ handleError: (error: unknown) => FallbackValue;
29
+ fallbackValue?: FallbackValue;
30
+ }): FallbackValue | Value;
31
+ export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: {
32
+ handleError?: ((error: unknown) => FallbackValue) | undefined;
33
+ fallbackValue: FallbackValue;
34
+ }): Promise<Awaited<FallbackValue> | Awaited<Value>>;
35
+ export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: {
36
+ handleError?: ((error: unknown) => FallbackValue) | undefined;
37
+ fallbackValue: FallbackValue;
38
+ }): FallbackValue | Value;
39
+ export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options?: WrapInTryOptions<FallbackValue> | undefined): Promise<FallbackValue | Value | Error>;
40
+ export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options?: WrapInTryOptions<FallbackValue> | undefined): FallbackValue | Value | Error;
@@ -39,3 +39,4 @@ export * from './augments/time';
39
39
  export * from './augments/truncate-number';
40
40
  export * from './augments/tuple';
41
41
  export * from './augments/type';
42
+ export * from './augments/wrap-in-try';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "25.0.0",
3
+ "version": "26.0.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"