Skip to content

feat: allow function type reducers and revivers #85

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
30 changes: 16 additions & 14 deletions src/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ export function stringify(value, reducers) {

/** @param {any} thing */
function flatten(thing) {
if (typeof thing === 'function') {
throw new DevalueError(`Cannot stringify a function`, keys);
if (indexes.has(thing)) return indexes.get(thing);

for (const { key, fn } of custom) {
const value = fn(thing);
if (value) {
const index = p++;
indexes.set(thing, index);
stringified[index] = `["${key}",${flatten(value)}]`;
return index;
}
}

if (indexes.has(thing)) return indexes.get(thing);
if (typeof thing === 'function') {
throw new DevalueError(`Cannot stringify a function`, keys);
}

if (thing === undefined) return UNDEFINED;
if (Number.isNaN(thing)) return NAN;
Expand All @@ -59,14 +69,6 @@ export function stringify(value, reducers) {
const index = p++;
indexes.set(thing, index);

for (const { key, fn } of custom) {
const value = fn(thing);
if (value) {
stringified[index] = `["${key}",${flatten(value)}]`;
return index;
}
}

let str = '';

if (is_primitive(thing)) {
Expand Down Expand Up @@ -157,16 +159,16 @@ export function stringify(value, reducers) {
str = '["' + type + '","' + base64 + '"]';
break;
}

case "ArrayBuffer": {
/** @type {ArrayBuffer} */
const arraybuffer = thing;
const base64 = encode64(arraybuffer);

str = `["ArrayBuffer","${base64}"]`;
break;
}

default:
if (!is_plain_object(thing)) {
throw new DevalueError(
Expand Down
26 changes: 25 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,36 @@ const fixtures = {
assert.equal(obj1.value.answer, 42);
}
}
])(new Custom({ answer: 42 }))
])(new Custom({ answer: 42 })),

customFunction: ((func) => [
{
name: 'Custom function type',
value: func,
json: '[["function",1],"func"]',
reducers: {
function: (x) => x === func && 'func'
},
revivers: {
function: (x) => {
assert.is(x, 'func');
return func;
}
},
validate: (f) => {
assert.is(f, func);
}
}
])(() => 42)
};

for (const [name, tests] of Object.entries(fixtures)) {
const test = uvu.suite(`uneval: ${name}`);
for (const t of tests) {
if (!t.js) {
continue;
}

test(t.name, () => {
const actual = uneval(t.value, t.replacer);
const expected = t.js;
Expand Down