Skip to content

Commit 57a3234

Browse files
committed
make it less clever and brittle
1 parent a62408d commit 57a3234

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/uneval.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ const reserved =
1515
/**
1616
* Turn a value into the JavaScript that creates an equivalent value
1717
* @param {any} value
18-
* @param {(value: any, callback: (value: any) => any) => string | void} [replacer]
18+
* @param {(value: any) => string | void} [replacer]
1919
*/
2020
export function uneval(value, replacer) {
2121
const counts = new Map();
2222

2323
/** @type {string[]} */
2424
const keys = [];
2525

26-
const custom = new Set();
26+
const custom = new Map();
2727

2828
/** @param {any} thing */
2929
function walk(thing) {
@@ -40,13 +40,10 @@ export function uneval(value, replacer) {
4040
counts.set(thing, 1);
4141

4242
if (replacer) {
43-
const str = replacer(thing, (child) => {
44-
walk(child);
45-
return '<pending>';
46-
});
43+
const str = replacer(thing);
4744

4845
if (typeof str === 'string') {
49-
custom.add(thing);
46+
custom.set(thing, str);
5047
return;
5148
}
5249
}
@@ -133,8 +130,7 @@ export function uneval(value, replacer) {
133130
}
134131

135132
if (custom.has(thing)) {
136-
const str = replacer(thing, stringify);
137-
if (typeof str === 'string') return str;
133+
return custom.get(thing);
138134
}
139135

140136
const type = get_type(thing);
@@ -195,7 +191,7 @@ export function uneval(value, replacer) {
195191
params.push(name);
196192

197193
if (custom.has(thing)) {
198-
values.push(/** @type {string} */ (replacer(thing, stringify)));
194+
values.push(/** @type {string} */ (custom.get(thing)));
199195
return;
200196
}
201197

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ const fixtures = {
388388
value: [instance, instance],
389389
js: '(function(a){return [a,a]}(new Custom({answer:42})))',
390390
json: '[[1,1],["Custom",2],{"answer":3},42]',
391-
replacer: (value, uneval) => {
391+
replacer: (value) => {
392392
if (value instanceof Custom) {
393393
return `new Custom(${uneval(value.value)})`;
394394
}

0 commit comments

Comments
 (0)