Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serialization of URL #200 #201

Merged
merged 2 commits into from
Sep 14, 2022
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/mongodb": "^3.6.12",
"@types/node": "^18.7.18",
"benchmark": "^2.1.4",
"decimal.js": "^10.3.1",
"eslint-plugin-es5": "^1.5.0",
Expand Down
16 changes: 16 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,22 @@ describe('stringify & parse', () => {
expect(value.pet.woof()).toEqual('Rover');
},
},
'works with URL': {
input: {
a: new URL('https://example.com/'),
b: new URL('https://github.com/blitz-js/superjson'),
},
output: {
a: 'https://example.com/',
b: 'https://github.com/blitz-js/superjson',
},
outputAnnotations: {
values: {
a: ['URL'],
b: ['URL'],
},
},
},
};

function deepFreeze(object: any, alreadySeenObjects = new Set()) {
Expand Down
4 changes: 4 additions & 0 deletions src/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isUndefined,
isPlainObject,
isTypedArray,
isURL,
} from './is';

test('Basic true tests', () => {
Expand All @@ -31,6 +32,7 @@ test('Basic true tests', () => {
expect(isDate(new Date())).toBe(true);
expect(isSymbol(Symbol())).toBe(true);
expect(isTypedArray(new Uint8Array())).toBe(true);
expect(isURL(new URL('https://example.com'))).toBe(true);
});

test('Basic false tests', () => {
Expand All @@ -48,6 +50,8 @@ test('Basic false tests', () => {
expect(isSymbol(NaN)).toBe(false);

expect(isTypedArray([])).toBe(false);

expect(isURL('https://example.com')).toBe(false);
});

test('Primitive tests', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,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;
15 changes: 14 additions & 1 deletion src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isError,
isTypedArray,
TypedArrayConstructor,
isURL,
} from './is';
import { ClassRegistry } from './class-registry';
import { SymbolRegistry } from './symbol-registry';
Expand All @@ -21,7 +22,12 @@ import { findArr } from './util';

export type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint';

type LeafTypeAnnotation = PrimitiveTypeAnnotation | 'regexp' | 'Date' | 'Error';
type LeafTypeAnnotation =
| PrimitiveTypeAnnotation
| 'regexp'
| 'Date'
| 'Error'
| 'URL';

type TypedArrayAnnotation = ['typed-array', string];
type ClassTypeAnnotation = ['class', string];
Expand Down Expand Up @@ -159,6 +165,13 @@ const simpleRules = [
},
Number
),

simpleTransformation(
isURL,
'URL',
v => v.toString(),
v => new URL(v)
),
];

function compositeTransformation<I, O, A extends CompositeTypeAnnotation>(
Expand Down
Loading