-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better json codec and avoid reverse if not needed
- Loading branch information
1 parent
21fb394
commit 849c23f
Showing
10 changed files
with
171 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// JSON.stringify and JSON.parse with URL, Map and Uint8Array type support. | ||
|
||
/** | ||
* Json replacer with URL, Map, Set, BitInt, RegExp and Uint8Array type support. | ||
* | ||
* @param {string} k | ||
* @param {any} v | ||
*/ | ||
export const replacer = (k, v) => { | ||
if (v instanceof URL) { | ||
return { $url: v.toString() } | ||
} else if (v instanceof Map) { | ||
return { $map: [...v.entries()] } | ||
} else if (v instanceof Uint8Array) { | ||
return { $bytes: [...v.values()] } | ||
} else if (v instanceof ArrayBuffer) { | ||
return { $bytes: [...new Uint8Array(v).values()] } | ||
} else if (v?.type === 'Buffer' && Array.isArray(v.data)) { | ||
return { $bytes: v.data } | ||
} else if (typeof v === 'bigint') { | ||
return { $bigint: v.toString() } | ||
} else if (v instanceof Set) { | ||
return { $set: [...v.values()] } | ||
} else if (v instanceof RegExp) { | ||
return { $regex: [v.source, v.flags] } | ||
} | ||
return v | ||
} | ||
|
||
// const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ | ||
/** | ||
* Json reviver with URL, Map, Set, BitInt, RegExp and Uint8Array type support. | ||
* | ||
* @param {string} k - key | ||
* @param {any} v - value | ||
*/ | ||
export const reviver = (k, v) => { | ||
if (!v) return v | ||
if (v.$url) return new URL(v.$url) | ||
if (v.$map) return new Map(v.$map) | ||
if (v.$bytes) return new Uint8Array(v.$bytes) | ||
if (v.$bigint) return BigInt(v.$bigint) | ||
// if (typeof v === 'string' && isoDateRegex.test(v)) return new Date(v) | ||
if (v.$set) return new Set(v.$set) | ||
if (v.$regex) return new RegExp(v.$regex[0], v.$regex[1]) | ||
return v | ||
} | ||
|
||
/** | ||
* @param {any} value | ||
* @param {number|string} [space] | ||
*/ | ||
export const stringify = (value, space) => | ||
JSON.stringify(value, replacer, space) | ||
|
||
/** @param {string} value */ | ||
export const parse = (value) => JSON.parse(value, reviver) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,3 @@ const kv = new KV({ | |
}) | ||
|
||
baseTests(kv, suite('Memory')) | ||
|
||
// const test = suite('Misc') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { assert, suite } from 'playwright-test/taps' | ||
import { parse, stringify } from '../src/json.js' | ||
|
||
const { test } = suite('json') | ||
|
||
test('should support data types', async () => { | ||
let v | ||
assert.deepEqual(parse(stringify({ a: 1 })), { a: 1 }) | ||
assert.deepEqual(parse(stringify([1n])), [1n]) | ||
assert.deepEqual(parse(stringify({ a: { b: 1n } })), { a: { b: 1n } }) | ||
|
||
v = new Map([['a', { b: 1n }]]) | ||
assert.deepEqual(parse(stringify(v)), v) | ||
|
||
// v = new Date() | ||
// assert.deepEqual(parse(stringify(v)), v) | ||
|
||
v = new Set(['a', 1, new Set(['c', 'd'])]) | ||
assert.deepEqual(parse(stringify(v)), v) | ||
|
||
v = /^[\s\w!,?àáâãçèéêíïñóôõöú-]+$/ | ||
assert.deepEqual(parse(stringify(v)), v) | ||
}) |