Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,9 @@ test('doesnt iterate to keys that dont exist', () => {
test('deserialize in place', () => {
const serialized = SuperJSON.serialize({ a: new Date() });
const deserializedCopy = SuperJSON.deserialize(serialized);
const deserializedInPlace = SuperJSON.deserialize(serialized, { inPlace: true });
const deserializedInPlace = SuperJSON.deserialize(serialized, {
inPlace: true,
});
expect(deserializedInPlace).toBe(serialized.json);
expect(deserializedCopy).not.toBe(serialized.json);
expect(deserializedCopy).toEqual(deserializedInPlace);
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Class, JSONValue, SuperJSONResult, SuperJSONValue } from './types.js';
import { ClassRegistry, RegisterOptions } from './class-registry.js';
import { Registry } from './registry.js';
Expand Down Expand Up @@ -60,10 +60,13 @@
return res;
}

deserialize<T = unknown>(payload: SuperJSONResult, options?: { inPlace?: boolean }): T {
deserialize<T = unknown>(
payload: SuperJSONResult,
options?: { inPlace?: boolean }
): T {
const { json, meta } = payload;

let result: T = options?.inPlace ? json : copy(json) as any;
let result: T = options?.inPlace ? json : (copy(json) as any);

if (meta?.values) {
result = applyValueAnnotations(result, meta.values, meta.v ?? 0, this);
Expand Down
11 changes: 11 additions & 0 deletions src/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,14 @@ test('Regression: null-prototype object', () => {
expect(isPlainObject(Object.create(null))).toBe(true);
expect(isPrimitive(Object.create(null))).toBe(false);
});

test('isURL without URL global', () => {
const originalURL = globalThis.URL;
// @ts-ignore
delete globalThis.URL;

expect(() => isURL('https://example.com')).not.toThrow();
expect(isURL('https://example.com')).toBe(false);

globalThis.URL = originalURL;
});
3 changes: 2 additions & 1 deletion src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ export type TypedArray = InstanceType<TypedArrayConstructor>;
export const isTypedArray = (payload: any): payload is TypedArray =>
ArrayBuffer.isView(payload) && !(payload instanceof DataView);

export const isURL = (payload: any): payload is URL => payload instanceof URL;
export const isURL = (payload: any): payload is URL =>
typeof URL !== 'undefined' && payload instanceof URL;
12 changes: 6 additions & 6 deletions src/pathstringifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ describe('parsePath', () => {
expect(parsePath(input, false)).toEqual(expectedOutput);
});

it.each([
'test\\a.b',
'foo.bar.baz\\',
])('parsePath(%p) is rejected', (input) => {
expect(() => parsePath(input, false)).toThrowError();
});
it.each(['test\\a.b', 'foo.bar.baz\\'])(
'parsePath(%p) is rejected',
input => {
expect(() => parsePath(input, false)).toThrowError();
}
);
});

describe('escapeKey', () => {
Expand Down
Loading