Skip to content

Commit e75ac0f

Browse files
committed
fixes
1 parent 46ee86e commit e75ac0f

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/parse.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ export function parse(serialized) {
4545
case 'Set':
4646
const set = new Set();
4747
values[i] = set;
48-
for (let j = 1; j < value.length; j += 1) {
49-
set.add(get_value(value[j]));
48+
for (const n of value[1]) set.add(get_value(n));
49+
break;
50+
51+
case 'Map':
52+
const map = new Map();
53+
values[i] = map;
54+
for (let i = 0; i < value[1].length; i += 2) {
55+
map.set(get_value(value[i]), get_value(value[i + 1]));
5056
}
5157
break;
5258

@@ -90,6 +96,5 @@ export function parse(serialized) {
9096
}
9197
}
9298

93-
console.log(values);
9499
return values[0];
95100
}

src/stringify.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,25 @@ export function stringify(value) {
132132
);
133133
}
134134

135-
/** @type {string[]} */
136-
const flattened_object = [];
137-
138-
for (const key in thing) {
139-
keys.push(`.${key}`);
140-
flattened_object.push(
141-
`${stringify_string(key)}:${flatten(thing[key])}`
142-
);
143-
keys.pop();
144-
}
145-
146-
stringified[index] = `{${flattened_object.join(',')}}`;
147-
148135
if (Object.getPrototypeOf(thing) === null) {
149-
stringified[index] = `["null",${stringified[index]}]`;
136+
let str = '["null"';
137+
for (const key in thing) {
138+
keys.push(`.${key}`);
139+
str += `,${stringify_string(key)},${flatten(thing[key])}`;
140+
keys.pop();
141+
}
142+
stringified[index] = str + ']';
143+
} else {
144+
let str = '{';
145+
let started = false;
146+
for (const key in thing) {
147+
if (started) str += ',';
148+
started = true;
149+
keys.push(`.${key}`);
150+
str += `${stringify_string(key)}:${flatten(thing[key])}`;
151+
keys.pop();
152+
}
153+
stringified[index] = str + '}';
150154
}
151155
}
152156
}

test/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as util from 'util';
12
import * as vm from 'vm';
23
import * as assert from 'uvu/assert';
34
import * as uvu from 'uvu';
@@ -256,7 +257,7 @@ const fixtures = {
256257
name: 'Object with null prototype (cyclical)',
257258
value: obj,
258259
js: '(function(a){a.self=a;return a}(Object.create(null)))',
259-
json: '[["null",{"self":0}]]'
260+
json: '[["null","self",0]]'
260261
};
261262
})(Object.create(null)),
262263

@@ -307,7 +308,7 @@ const fixtures = {
307308
name: 'Object without prototype',
308309
value: Object.create(null),
309310
js: 'Object.create(null)',
310-
json: '[["null",{}]]'
311+
json: '[["null"]]'
311312
},
312313
{
313314
name: 'cross-realm POJO',

0 commit comments

Comments
 (0)