@augment-vir/common 15.1.0 → 15.2.1
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/cjs/augments/json.js +71 -1
- package/dist/cjs/augments/object/object.js +1 -34
- package/dist/esm/augments/json.js +68 -0
- package/dist/esm/augments/object/object.js +0 -32
- package/dist/types/augments/json.d.ts +9 -0
- package/dist/types/augments/object/object.d.ts +0 -2
- package/package.json +1 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseJson = void 0;
|
|
3
|
+
exports.areJsonEqual = exports.stringifyJson = exports.parseJson = void 0;
|
|
4
|
+
const error_1 = require("./error");
|
|
4
5
|
const matches_object_shape_1 = require("./object/matches-object-shape");
|
|
6
|
+
const object_1 = require("./object/object");
|
|
5
7
|
const runtime_type_of_1 = require("./runtime-type-of");
|
|
6
8
|
function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
|
|
7
9
|
try {
|
|
@@ -26,3 +28,71 @@ function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
|
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
exports.parseJson = parseJson;
|
|
31
|
+
function stringifyJson({ source, whitespace, errorHandler, }) {
|
|
32
|
+
try {
|
|
33
|
+
const stringifiedJson = JSON.stringify(source, undefined, whitespace);
|
|
34
|
+
return stringifiedJson;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (errorHandler) {
|
|
38
|
+
return errorHandler(error);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
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;
|
|
@@ -1,43 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.copyThroughJson = exports.
|
|
4
|
-
const error_1 = require("../error");
|
|
3
|
+
exports.copyThroughJson = exports.isObject = void 0;
|
|
5
4
|
function isObject(input) {
|
|
6
5
|
return !!input && typeof input === 'object';
|
|
7
6
|
}
|
|
8
7
|
exports.isObject = isObject;
|
|
9
|
-
const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify';
|
|
10
|
-
function baseAreJsonEqual(a, b) {
|
|
11
|
-
return JSON.stringify(a) === JSON.stringify(b);
|
|
12
|
-
}
|
|
13
|
-
function areJsonEqual(a, b) {
|
|
14
|
-
try {
|
|
15
|
-
if (a === b) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
if (isObject(a) && isObject(b)) {
|
|
19
|
-
const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort());
|
|
20
|
-
if (!areKeysEqual) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
return Object.keys(a).every((keyName) => {
|
|
24
|
-
return areJsonEqual(a[keyName], b[keyName]);
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return baseAreJsonEqual(a, b);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
catch (caught) {
|
|
32
|
-
const error = (0, error_1.ensureError)(caught);
|
|
33
|
-
if (error.message.startsWith(areJsonEqualFailureMessage)) {
|
|
34
|
-
throw error;
|
|
35
|
-
}
|
|
36
|
-
error.message = `${areJsonEqualFailureMessage}: ${error.message}`;
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.areJsonEqual = areJsonEqual;
|
|
41
8
|
/** The input here must be serializable otherwise JSON parsing errors will be thrown */
|
|
42
9
|
function copyThroughJson(input) {
|
|
43
10
|
try {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { ensureError } from './error';
|
|
1
2
|
import { assertMatchesObjectShape } from './object/matches-object-shape';
|
|
3
|
+
import { isObject } from './object/object';
|
|
2
4
|
import { assertRuntimeTypeOf, getRuntimeTypeOf, isRuntimeTypeOf } from './runtime-type-of';
|
|
3
5
|
export function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
|
|
4
6
|
try {
|
|
@@ -22,3 +24,69 @@ export function parseJson({ jsonString, errorHandler, shapeMatcher, }) {
|
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
}
|
|
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
|
+
}
|
|
39
|
+
}
|
|
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;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -1,38 +1,6 @@
|
|
|
1
|
-
import { ensureError } from '../error';
|
|
2
1
|
export function isObject(input) {
|
|
3
2
|
return !!input && typeof input === 'object';
|
|
4
3
|
}
|
|
5
|
-
const areJsonEqualFailureMessage = 'Failed to compare objects using JSON.stringify';
|
|
6
|
-
function baseAreJsonEqual(a, b) {
|
|
7
|
-
return JSON.stringify(a) === JSON.stringify(b);
|
|
8
|
-
}
|
|
9
|
-
export function areJsonEqual(a, b) {
|
|
10
|
-
try {
|
|
11
|
-
if (a === b) {
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
if (isObject(a) && isObject(b)) {
|
|
15
|
-
const areKeysEqual = baseAreJsonEqual(Object.keys(a).sort(), Object.keys(b).sort());
|
|
16
|
-
if (!areKeysEqual) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
return Object.keys(a).every((keyName) => {
|
|
20
|
-
return areJsonEqual(a[keyName], b[keyName]);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
return baseAreJsonEqual(a, b);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
catch (caught) {
|
|
28
|
-
const error = ensureError(caught);
|
|
29
|
-
if (error.message.startsWith(areJsonEqualFailureMessage)) {
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
error.message = `${areJsonEqualFailureMessage}: ${error.message}`;
|
|
33
|
-
throw error;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
4
|
/** The input here must be serializable otherwise JSON parsing errors will be thrown */
|
|
37
5
|
export function copyThroughJson(input) {
|
|
38
6
|
try {
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
import { JsonCompatibleValue } from './json-compatible';
|
|
1
2
|
export declare function parseJson<ParsedJsonGeneric>({ jsonString, errorHandler, shapeMatcher, }: {
|
|
2
3
|
jsonString: string;
|
|
3
4
|
errorHandler?: (error: unknown) => never | ParsedJsonGeneric;
|
|
4
5
|
shapeMatcher?: ParsedJsonGeneric;
|
|
5
6
|
}): ParsedJsonGeneric;
|
|
7
|
+
export declare function stringifyJson({ source, whitespace, errorHandler, }: {
|
|
8
|
+
source: unknown;
|
|
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;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { JsonCompatibleValue } from '../json-compatible';
|
|
2
1
|
export type PartialAndNullable<T extends object> = {
|
|
3
2
|
[Prop in keyof T]?: T[Prop] | null | undefined;
|
|
4
3
|
};
|
|
@@ -6,7 +5,6 @@ export type PartialAndUndefined<T extends object> = {
|
|
|
6
5
|
[Prop in keyof T]?: T[Prop] | undefined;
|
|
7
6
|
};
|
|
8
7
|
export declare function isObject(input: any): input is NonNullable<object>;
|
|
9
|
-
export declare function areJsonEqual(a: Readonly<JsonCompatibleValue | undefined>, b: Readonly<JsonCompatibleValue | undefined>): boolean;
|
|
10
8
|
/** The input here must be serializable otherwise JSON parsing errors will be thrown */
|
|
11
9
|
export declare function copyThroughJson<T>(input: T): T;
|
|
12
10
|
export type PropertyValueType<T> = T[keyof T];
|