Comparing version 1.12.4 to 1.13.0
@@ -1,2 +0,2 @@ | ||
import { SuperJSONResult, SuperJSONValue, Class, JSONValue } from './types'; | ||
import { Class, JSONValue, SuperJSONResult, SuperJSONValue } from './types'; | ||
import { ClassRegistry, RegisterOptions } from './class-registry'; | ||
@@ -6,2 +6,12 @@ import { Registry } from './registry'; | ||
export default class SuperJSON { | ||
/** | ||
* If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
private readonly dedupe; | ||
/** | ||
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
constructor({ dedupe, }?: { | ||
dedupe?: boolean; | ||
}); | ||
serialize(object: SuperJSONValue): SuperJSONResult; | ||
@@ -8,0 +18,0 @@ deserialize<T = unknown>(payload: SuperJSONResult): T; |
@@ -36,6 +36,10 @@ var __assign = (this && this.__assign) || function () { | ||
import { CustomTransformerRegistry, } from './custom-transformer-registry'; | ||
import { walker, applyReferentialEqualityAnnotations, applyValueAnnotations, generateReferentialEqualityAnnotations, } from './plainer'; | ||
import { applyReferentialEqualityAnnotations, applyValueAnnotations, generateReferentialEqualityAnnotations, walker, } from './plainer'; | ||
import { copy } from 'copy-anything'; | ||
var SuperJSON = /** @class */ (function () { | ||
function SuperJSON() { | ||
/** | ||
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
function SuperJSON(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.dedupe, dedupe = _c === void 0 ? false : _c; | ||
this.classRegistry = new ClassRegistry(); | ||
@@ -45,6 +49,7 @@ this.symbolRegistry = new Registry(function (s) { var _a; return (_a = s.description) !== null && _a !== void 0 ? _a : ''; }); | ||
this.allowedErrorProps = []; | ||
this.dedupe = dedupe; | ||
} | ||
SuperJSON.prototype.serialize = function (object) { | ||
var identities = new Map(); | ||
var output = walker(object, identities, this); | ||
var output = walker(object, identities, this, this.dedupe); | ||
var res = { | ||
@@ -51,0 +56,0 @@ json: output.transformedValue |
@@ -15,3 +15,3 @@ import { TypeAnnotation } from './transformer'; | ||
export declare function generateReferentialEqualityAnnotations(identitites: Map<any, any[][]>): ReferentialEqualityAnnotations | undefined; | ||
export declare const walker: (object: any, identities: Map<any, any[][]>, superJson: SuperJSON, path?: any[], objectsInThisPath?: any[]) => Result; | ||
export declare const walker: (object: any, identities: Map<any, any[][]>, superJson: SuperJSON, dedupe: boolean, path?: any[], objectsInThisPath?: any[], seenObjects?: Map<unknown, Result>) => Result; | ||
export {}; |
@@ -119,24 +119,37 @@ var __read = (this && this.__read) || function (o, n) { | ||
} | ||
export var walker = function (object, identities, superJson, path, objectsInThisPath) { | ||
export var walker = function (object, identities, superJson, dedupe, path, objectsInThisPath, seenObjects) { | ||
var _a; | ||
if (path === void 0) { path = []; } | ||
if (objectsInThisPath === void 0) { objectsInThisPath = []; } | ||
if (!isPrimitive(object)) { | ||
if (seenObjects === void 0) { seenObjects = new Map(); } | ||
var primitive = isPrimitive(object); | ||
if (!primitive) { | ||
addIdentity(object, path, identities); | ||
var seen = seenObjects.get(object); | ||
if (seen) { | ||
// short-circuit result if we've seen this object before | ||
return dedupe | ||
? { | ||
transformedValue: null | ||
} | ||
: seen; | ||
} | ||
} | ||
if (!isDeep(object, superJson)) { | ||
var transformed_1 = transformValue(object, superJson); | ||
if (transformed_1) { | ||
return { | ||
var result_1 = transformed_1 | ||
? { | ||
transformedValue: transformed_1.value, | ||
annotations: [transformed_1.type] | ||
}; | ||
} | ||
else { | ||
return { | ||
} | ||
: { | ||
transformedValue: object | ||
}; | ||
if (!primitive) { | ||
seenObjects.set(object, result_1); | ||
} | ||
return result_1; | ||
} | ||
if (includes(objectsInThisPath, object)) { | ||
// prevent circular references | ||
return { | ||
@@ -148,9 +161,6 @@ transformedValue: null | ||
var transformed = (_a = transformationResult === null || transformationResult === void 0 ? void 0 : transformationResult.value) !== null && _a !== void 0 ? _a : object; | ||
if (!isPrimitive(object)) { | ||
objectsInThisPath = __spreadArray(__spreadArray([], __read(objectsInThisPath)), [object]); | ||
} | ||
var transformedValue = isArray(transformed) ? [] : {}; | ||
var innerAnnotations = {}; | ||
forEach(transformed, function (value, index) { | ||
var recursiveResult = walker(value, identities, superJson, __spreadArray(__spreadArray([], __read(path)), [index]), objectsInThisPath); | ||
var recursiveResult = walker(value, identities, superJson, dedupe, __spreadArray(__spreadArray([], __read(path)), [index]), __spreadArray(__spreadArray([], __read(objectsInThisPath)), [object]), seenObjects); | ||
transformedValue[index] = recursiveResult.transformedValue; | ||
@@ -166,4 +176,4 @@ if (isArray(recursiveResult.annotations)) { | ||
}); | ||
if (isEmptyObject(innerAnnotations)) { | ||
return { | ||
var result = isEmptyObject(innerAnnotations) | ||
? { | ||
transformedValue: transformedValue, | ||
@@ -173,6 +183,4 @@ annotations: !!transformationResult | ||
: undefined | ||
}; | ||
} | ||
else { | ||
return { | ||
} | ||
: { | ||
transformedValue: transformedValue, | ||
@@ -183,4 +191,7 @@ annotations: !!transformationResult | ||
}; | ||
if (!primitive) { | ||
seenObjects.set(object, result); | ||
} | ||
return result; | ||
}; | ||
//# sourceMappingURL=plainer.js.map |
@@ -1,2 +0,2 @@ | ||
import { SuperJSONResult, SuperJSONValue, Class, JSONValue } from './types'; | ||
import { Class, JSONValue, SuperJSONResult, SuperJSONValue } from './types'; | ||
import { ClassRegistry, RegisterOptions } from './class-registry'; | ||
@@ -6,2 +6,12 @@ import { Registry } from './registry'; | ||
export default class SuperJSON { | ||
/** | ||
* If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
private readonly dedupe; | ||
/** | ||
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
constructor({ dedupe, }?: { | ||
dedupe?: boolean; | ||
}); | ||
serialize(object: SuperJSONValue): SuperJSONResult; | ||
@@ -8,0 +18,0 @@ deserialize<T = unknown>(payload: SuperJSONResult): T; |
@@ -42,3 +42,7 @@ "use strict"; | ||
var SuperJSON = /** @class */ (function () { | ||
function SuperJSON() { | ||
/** | ||
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
function SuperJSON(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.dedupe, dedupe = _c === void 0 ? false : _c; | ||
this.classRegistry = new class_registry_1.ClassRegistry(); | ||
@@ -48,6 +52,7 @@ this.symbolRegistry = new registry_1.Registry(function (s) { var _a; return (_a = s.description) !== null && _a !== void 0 ? _a : ''; }); | ||
this.allowedErrorProps = []; | ||
this.dedupe = dedupe; | ||
} | ||
SuperJSON.prototype.serialize = function (object) { | ||
var identities = new Map(); | ||
var output = plainer_1.walker(object, identities, this); | ||
var output = plainer_1.walker(object, identities, this, this.dedupe); | ||
var res = { | ||
@@ -54,0 +59,0 @@ json: output.transformedValue |
@@ -15,3 +15,3 @@ import { TypeAnnotation } from './transformer'; | ||
export declare function generateReferentialEqualityAnnotations(identitites: Map<any, any[][]>): ReferentialEqualityAnnotations | undefined; | ||
export declare const walker: (object: any, identities: Map<any, any[][]>, superJson: SuperJSON, path?: any[], objectsInThisPath?: any[]) => Result; | ||
export declare const walker: (object: any, identities: Map<any, any[][]>, superJson: SuperJSON, dedupe: boolean, path?: any[], objectsInThisPath?: any[], seenObjects?: Map<unknown, Result>) => Result; | ||
export {}; |
@@ -125,24 +125,37 @@ "use strict"; | ||
exports.generateReferentialEqualityAnnotations = generateReferentialEqualityAnnotations; | ||
var walker = function (object, identities, superJson, path, objectsInThisPath) { | ||
var walker = function (object, identities, superJson, dedupe, path, objectsInThisPath, seenObjects) { | ||
var _a; | ||
if (path === void 0) { path = []; } | ||
if (objectsInThisPath === void 0) { objectsInThisPath = []; } | ||
if (!is_1.isPrimitive(object)) { | ||
if (seenObjects === void 0) { seenObjects = new Map(); } | ||
var primitive = is_1.isPrimitive(object); | ||
if (!primitive) { | ||
addIdentity(object, path, identities); | ||
var seen = seenObjects.get(object); | ||
if (seen) { | ||
// short-circuit result if we've seen this object before | ||
return dedupe | ||
? { | ||
transformedValue: null | ||
} | ||
: seen; | ||
} | ||
} | ||
if (!isDeep(object, superJson)) { | ||
var transformed_1 = transformer_1.transformValue(object, superJson); | ||
if (transformed_1) { | ||
return { | ||
var result_1 = transformed_1 | ||
? { | ||
transformedValue: transformed_1.value, | ||
annotations: [transformed_1.type] | ||
}; | ||
} | ||
else { | ||
return { | ||
} | ||
: { | ||
transformedValue: object | ||
}; | ||
if (!primitive) { | ||
seenObjects.set(object, result_1); | ||
} | ||
return result_1; | ||
} | ||
if (util_1.includes(objectsInThisPath, object)) { | ||
// prevent circular references | ||
return { | ||
@@ -154,9 +167,6 @@ transformedValue: null | ||
var transformed = (_a = transformationResult === null || transformationResult === void 0 ? void 0 : transformationResult.value) !== null && _a !== void 0 ? _a : object; | ||
if (!is_1.isPrimitive(object)) { | ||
objectsInThisPath = __spreadArray(__spreadArray([], __read(objectsInThisPath)), [object]); | ||
} | ||
var transformedValue = is_1.isArray(transformed) ? [] : {}; | ||
var innerAnnotations = {}; | ||
util_1.forEach(transformed, function (value, index) { | ||
var recursiveResult = exports.walker(value, identities, superJson, __spreadArray(__spreadArray([], __read(path)), [index]), objectsInThisPath); | ||
var recursiveResult = exports.walker(value, identities, superJson, dedupe, __spreadArray(__spreadArray([], __read(path)), [index]), __spreadArray(__spreadArray([], __read(objectsInThisPath)), [object]), seenObjects); | ||
transformedValue[index] = recursiveResult.transformedValue; | ||
@@ -172,4 +182,4 @@ if (is_1.isArray(recursiveResult.annotations)) { | ||
}); | ||
if (is_1.isEmptyObject(innerAnnotations)) { | ||
return { | ||
var result = is_1.isEmptyObject(innerAnnotations) | ||
? { | ||
transformedValue: transformedValue, | ||
@@ -179,6 +189,4 @@ annotations: !!transformationResult | ||
: undefined | ||
}; | ||
} | ||
else { | ||
return { | ||
} | ||
: { | ||
transformedValue: transformedValue, | ||
@@ -189,5 +197,8 @@ annotations: !!transformationResult | ||
}; | ||
if (!primitive) { | ||
seenObjects.set(object, result); | ||
} | ||
return result; | ||
}; | ||
exports.walker = walker; | ||
//# sourceMappingURL=plainer.js.map |
{ | ||
"version": "1.12.4", | ||
"version": "1.13.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -1129,1 +1129,68 @@ /* eslint-disable es5/no-for-of */ | ||
}); | ||
test('regression #245: superjson referential equalities only use the top-most parent node', () => { | ||
type Node = { | ||
children: Node[]; | ||
}; | ||
const root: Node = { | ||
children: [], | ||
}; | ||
const input = { | ||
a: root, | ||
b: root, | ||
}; | ||
const res = SuperJSON.serialize(input); | ||
expect(res.meta?.referentialEqualities).toHaveProperty(['a']); | ||
// saying that a.children is equal to b.children is redundant since its already know that a === b | ||
expect(res.meta?.referentialEqualities).not.toHaveProperty(['a.children']); | ||
expect(res.meta).toMatchInlineSnapshot(` | ||
Object { | ||
"referentialEqualities": Object { | ||
"a": Array [ | ||
"b", | ||
], | ||
}, | ||
} | ||
`); | ||
const parsed = SuperJSON.deserialize(res); | ||
expect(parsed).toEqual(input); | ||
}); | ||
test('dedupe=true', () => { | ||
const instance = new SuperJSON({ | ||
dedupe: true, | ||
}); | ||
type Node = { | ||
children: Node[]; | ||
}; | ||
const root: Node = { | ||
children: [], | ||
}; | ||
const input = { | ||
a: root, | ||
b: root, | ||
}; | ||
const output = instance.serialize(input); | ||
const json = output.json as any; | ||
expect(json.a); | ||
// This has already been seen and should be deduped | ||
expect(json.b).toBeNull(); | ||
expect(json).toMatchInlineSnapshot(` | ||
Object { | ||
"a": Object { | ||
"children": Array [], | ||
}, | ||
"b": null, | ||
} | ||
`); | ||
expect(instance.deserialize(output)).toEqual(input); | ||
}); |
@@ -1,2 +0,2 @@ | ||
import { SuperJSONResult, SuperJSONValue, Class, JSONValue } from './types'; | ||
import { Class, JSONValue, SuperJSONResult, SuperJSONValue } from './types'; | ||
import { ClassRegistry, RegisterOptions } from './class-registry'; | ||
@@ -9,6 +9,6 @@ import { Registry } from './registry'; | ||
import { | ||
walker, | ||
applyReferentialEqualityAnnotations, | ||
applyValueAnnotations, | ||
generateReferentialEqualityAnnotations, | ||
walker, | ||
} from './plainer'; | ||
@@ -18,5 +18,21 @@ import { copy } from 'copy-anything'; | ||
export default class SuperJSON { | ||
/** | ||
* If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
private readonly dedupe: boolean; | ||
/** | ||
* @param dedupeReferentialEqualities If true, SuperJSON will make sure only one instance of referentially equal objects are serialized and the rest are replaced with `null`. | ||
*/ | ||
constructor({ | ||
dedupe = false, | ||
}: { | ||
dedupe?: boolean; | ||
} = {}) { | ||
this.dedupe = dedupe; | ||
} | ||
serialize(object: SuperJSONValue): SuperJSONResult { | ||
const identities = new Map<any, any[][]>(); | ||
const output = walker(object, identities, this); | ||
const output = walker(object, identities, this, this.dedupe); | ||
const res: SuperJSONResult = { | ||
@@ -23,0 +39,0 @@ json: output.transformedValue, |
@@ -12,3 +12,4 @@ import SuperJSON from '.'; | ||
new Map(), | ||
new SuperJSON() | ||
new SuperJSON(), | ||
false | ||
) | ||
@@ -15,0 +16,0 @@ ).toEqual({ |
@@ -157,7 +157,21 @@ import { | ||
superJson: SuperJSON, | ||
dedupe: boolean, | ||
path: any[] = [], | ||
objectsInThisPath: any[] = [] | ||
objectsInThisPath: any[] = [], | ||
seenObjects = new Map<unknown, Result>() | ||
): Result => { | ||
if (!isPrimitive(object)) { | ||
const primitive = isPrimitive(object); | ||
if (!primitive) { | ||
addIdentity(object, path, identities); | ||
const seen = seenObjects.get(object); | ||
if (seen) { | ||
// short-circuit result if we've seen this object before | ||
return dedupe | ||
? { | ||
transformedValue: null, | ||
} | ||
: seen; | ||
} | ||
} | ||
@@ -167,15 +181,19 @@ | ||
const transformed = transformValue(object, superJson); | ||
if (transformed) { | ||
return { | ||
transformedValue: transformed.value, | ||
annotations: [transformed.type], | ||
}; | ||
} else { | ||
return { | ||
transformedValue: object, | ||
}; | ||
const result: Result = transformed | ||
? { | ||
transformedValue: transformed.value, | ||
annotations: [transformed.type], | ||
} | ||
: { | ||
transformedValue: object, | ||
}; | ||
if (!primitive) { | ||
seenObjects.set(object, result); | ||
} | ||
return result; | ||
} | ||
if (includes(objectsInThisPath, object)) { | ||
// prevent circular references | ||
return { | ||
@@ -189,6 +207,2 @@ transformedValue: null, | ||
if (!isPrimitive(object)) { | ||
objectsInThisPath = [...objectsInThisPath, object]; | ||
} | ||
const transformedValue: any = isArray(transformed) ? [] : {}; | ||
@@ -202,4 +216,6 @@ const innerAnnotations: Record<string, Tree<TypeAnnotation>> = {}; | ||
superJson, | ||
dedupe, | ||
[...path, index], | ||
objectsInThisPath | ||
[...objectsInThisPath, object], | ||
seenObjects | ||
); | ||
@@ -218,17 +234,20 @@ | ||
if (isEmptyObject(innerAnnotations)) { | ||
return { | ||
transformedValue, | ||
annotations: !!transformationResult | ||
? [transformationResult.type] | ||
: undefined, | ||
}; | ||
} else { | ||
return { | ||
transformedValue, | ||
annotations: !!transformationResult | ||
? [transformationResult.type, innerAnnotations] | ||
: innerAnnotations, | ||
}; | ||
const result: Result = isEmptyObject(innerAnnotations) | ||
? { | ||
transformedValue, | ||
annotations: !!transformationResult | ||
? [transformationResult.type] | ||
: undefined, | ||
} | ||
: { | ||
transformedValue, | ||
annotations: !!transformationResult | ||
? [transformationResult.type, innerAnnotations] | ||
: innerAnnotations, | ||
}; | ||
if (!primitive) { | ||
seenObjects.set(object, result); | ||
} | ||
return result; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
239486
4670