Skip to content
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
11 changes: 1 addition & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ jobs:
run: npm run build

- name: Run benchmark
run: node benchmark.js | tee output.txt
run: node benchmark.js
env:
NODE_ENV: production

- name: Store benchmark result
uses: rhysd/github-action-benchmark@v1
with:
tool: 'benchmarkjs'
output-file-path: output.txt
comment-on-alert: true
save-data-file: ${{ github.ref == 'refs/heads/main' }}
github-token: ${{ secrets.GITHUB_TOKEN }}
11 changes: 8 additions & 3 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ const instances = {
const suite = new Benchmark.Suite('serialize & deserialize');

for (const [key, instance] of Object.entries(instances)) {
suite.add(key, () => {
SuperJSON.deserialize(SuperJSON.serialize(instance));
});
suite.add(
key,
async deferred => {
await SuperJSON.deserialize(await SuperJSON.serialize(instance));
deferred.resolve();
},
{ defer: true }
);
}

suite.on('cycle', event => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"scripts": {
"build": "tsc",
"check": "tsc --noEmit --project ./tsconfig.check.json",
"test": "vitest run",
"prepack": "npm run build"
},
Expand All @@ -24,7 +25,7 @@
"singleQuote": true,
"trailingComma": "es5"
},
"name": "superjson",
"name": "@eridu-tech/superjson",
"author": {
"name": "Simon Knott",
"email": "info@simonknott.de",
Expand Down
14 changes: 7 additions & 7 deletions src/accessDeep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import { setDeep } from './accessDeep.js';

import { describe, it, expect } from 'vitest';

describe('setDeep', () => {
it('correctly sets values in maps', () => {
describe('setDeep', () => {
it('correctly sets values in maps', async () => {
const obj = {
a: new Map([[new Set(['NaN']), [[1, 'undefined']]]]),
};

setDeep(obj, ['a', 0, 0, 0], Number);
setDeep(obj, ['a', 0, 1], entries => new Map(entries));
setDeep(obj, ['a', 0, 1, 0, 1], () => undefined);
await setDeep(obj, ['a', 0, 0, 0], async v => Number(v));
await setDeep(obj, ['a', 0, 1], async entries => new Map(entries));
await setDeep(obj, ['a', 0, 1, 0, 1], async () => undefined);

expect(obj).toEqual({
a: new Map([[new Set([NaN]), new Map([[1, undefined]])]]),
});
});

it('correctly sets values in sets', () => {
it('correctly sets values in sets', async () => {
const obj = {
a: new Set([10, new Set(['NaN'])]),
};

setDeep(obj, ['a', 1, 0], Number);
await setDeep(obj, ['a', 1, 0], async v => Number(v));

expect(obj).toEqual({
a: new Set([10, new Set([NaN])]),
Expand Down
18 changes: 9 additions & 9 deletions src/accessDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export const getDeep = (object: object, path: (string | number)[]): object => {
return object;
};

export const setDeep = (
export const setDeep = async (
object: any,
path: (string | number)[],
mapper: (v: any) => any
): any => {
mapper: (v: any) => Promise<any>
): Promise<any> => {
validatePath(path);

if (path.length === 0) {
return mapper(object);
return await mapper(object);
}

let parent = object;
Expand Down Expand Up @@ -100,14 +100,14 @@ export const setDeep = (
const lastKey = path[path.length - 1];

if (isArray(parent)) {
parent[+lastKey] = mapper(parent[+lastKey]);
parent[+lastKey] = await mapper(parent[+lastKey]);
} else if (isPlainObject(parent)) {
parent[lastKey] = mapper(parent[lastKey]);
parent[lastKey] = await mapper(parent[lastKey]);
}

if (isSet(parent)) {
const oldValue = getNthKey(parent, +lastKey);
const newValue = mapper(oldValue);
const newValue = await mapper(oldValue);
if (oldValue !== newValue) {
parent.delete(oldValue);
parent.add(newValue);
Expand All @@ -121,7 +121,7 @@ export const setDeep = (
const type = +lastKey === 0 ? 'key' : 'value';
switch (type) {
case 'key': {
const newKey = mapper(keyToRow);
const newKey = await mapper(keyToRow);
parent.set(newKey, parent.get(keyToRow));

if (newKey !== keyToRow) {
Expand All @@ -131,7 +131,7 @@ export const setDeep = (
}

case 'value': {
parent.set(keyToRow, mapper(parent.get(keyToRow)));
parent.set(keyToRow, await mapper(parent.get(keyToRow)));
break;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/custom-transformer-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { find } from './util.js';

export interface CustomTransfomer<I, O extends JSONValue> {
name: string;
isApplicable: (v: any) => v is I;
serialize: (v: I) => O;
deserialize: (v: O) => I;
isApplicable: (v: any) => boolean | Promise<boolean>;
serialize: (v: I) => O | Promise<O>;
deserialize: (v: O) => I | Promise<I>;
}

export class CustomTransformerRegistry {
Expand All @@ -15,13 +15,13 @@ export class CustomTransformerRegistry {
this.transfomers[transformer.name] = transformer;
}

findApplicable<T>(v: T) {
return find(this.transfomers, transformer =>
findApplicable<T>(v: T): Promise<CustomTransfomer<T, JSONValue> | undefined> {
return find(this.transfomers, async transformer =>
transformer.isApplicable(v)
) as CustomTransfomer<T, JSONValue> | undefined;
);
}

findByName(name: string) {
findByName(name: string): CustomTransfomer<any, any> {
return this.transfomers[name];
}
}
Loading
Loading