Skip to content

Commit d7fa0aa

Browse files
committed
fix
1 parent 0d9d7e4 commit d7fa0aa

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/parse.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ export function parse(serialized) {
4545
case 'Set':
4646
const set = new Set();
4747
hydrated[index] = set;
48-
for (const n of value[1]) set.add(hydrate(n));
48+
for (let i = 1; i < value.length; i += 1) {
49+
set.add(hydrate(value[i]));
50+
}
4951
break;
5052

5153
case 'Map':
5254
const map = new Map();
5355
hydrated[index] = map;
54-
for (let i = 0; i < value[1].length; i += 2) {
56+
for (let i = 1; i < value.length; i += 2) {
5557
map.set(hydrate(value[i]), hydrate(value[i + 1]));
5658
}
5759
break;

src/stringify.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export function stringify(value) {
3232

3333
/** @param {any} thing */
3434
function flatten(thing) {
35+
if (typeof thing === 'function') {
36+
throw new DevalueError(`Cannot stringify a function`, keys);
37+
}
38+
3539
if (map.has(thing)) return map.get(thing);
3640

3741
if (thing === undefined) return UNDEFINED;
@@ -43,33 +47,31 @@ export function stringify(value) {
4347
const index = p++;
4448
map.set(thing, index);
4549

46-
if (typeof thing === 'function') {
47-
throw new DevalueError(`Cannot stringify a function`, keys);
48-
}
50+
let str = '';
4951

5052
if (is_primitive(thing)) {
51-
stringified[index] = stringify_primitive(thing);
53+
str = stringify_primitive(thing);
5254
} else {
5355
const type = get_type(thing);
5456

5557
switch (type) {
5658
case 'Number':
5759
case 'String':
5860
case 'Boolean':
59-
stringified[index] = `["Object",${stringify_primitive(thing)}]`;
61+
str = `["Object",${stringify_primitive(thing)}]`;
6062
break;
6163

6264
case 'BigInt':
63-
stringified[index] = `["BigInt",${thing}]`;
65+
str = `["BigInt",${thing}]`;
6466
break;
6567

6668
case 'Date':
67-
stringified[index] = `["Date","${thing.toISOString()}"]`;
69+
str = `["Date","${thing.toISOString()}"]`;
6870
break;
6971

7072
case 'RegExp':
7173
const { source, flags } = thing;
72-
stringified[index] = flags
74+
str = flags
7375
? `["RegExp",${stringify_string(source)},"${flags}"]`
7476
: `["RegExp",${stringify_string(source)}]`;
7577
break;
@@ -88,33 +90,31 @@ export function stringify(value) {
8890
}
8991
}
9092

91-
stringified[index] = `[${flattened_array.join(',')}]`;
93+
str = `[${flattened_array.join(',')}]`;
9294

9395
break;
9496

9597
case 'Set':
96-
/** @type {number[]} */
97-
const flattened_set = [];
98+
str = '["Set"';
9899

99100
for (const value of thing) {
100-
flattened_set.push(flatten(value));
101+
str += `,${flatten(value)}`;
101102
}
102103

103-
stringified[index] = `["Set",[${flattened_set.join(',')}]]`;
104+
str += ']';
104105
break;
105106

106107
case 'Map':
107-
/** @type {number[]} */
108-
const flattened_map = [];
108+
str = '["Map"';
109109

110110
for (const [key, value] of thing) {
111111
keys.push(
112112
`.get(${is_primitive(key) ? stringify_primitive(key) : '...'})`
113113
);
114-
flattened_map.push(flatten(key), flatten(value));
114+
str += `,${flatten(key)},${flatten(value)}`;
115115
}
116116

117-
stringified[index] = `["Map",[${flattened_map.join(',')}]]`;
117+
str += ']';
118118
break;
119119

120120
default:
@@ -133,15 +133,15 @@ export function stringify(value) {
133133
}
134134

135135
if (Object.getPrototypeOf(thing) === null) {
136-
let str = '["null"';
136+
str = '["null"';
137137
for (const key in thing) {
138138
keys.push(`.${key}`);
139139
str += `,${stringify_string(key)},${flatten(thing[key])}`;
140140
keys.pop();
141141
}
142-
stringified[index] = str + ']';
142+
str += ']';
143143
} else {
144-
let str = '{';
144+
str = '{';
145145
let started = false;
146146
for (const key in thing) {
147147
if (started) str += ',';
@@ -150,11 +150,12 @@ export function stringify(value) {
150150
str += `${stringify_string(key)}:${flatten(thing[key])}`;
151151
keys.pop();
152152
}
153-
stringified[index] = str + '}';
153+
str += '}';
154154
}
155155
}
156156
}
157157

158+
stringified[index] = str;
158159
return index;
159160
}
160161

test/test.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ const fixtures = {
129129
name: 'Set',
130130
value: new Set([1, 2, 3]),
131131
js: 'new Set([1,2,3])',
132-
json: '[["Set",[1,2,3]],1,2,3]'
132+
json: '[["Set",1,2,3],1,2,3]'
133133
},
134134
{
135135
name: 'Map',
136136
value: new Map([['a', 'b']]),
137137
js: 'new Map([["a","b"]])',
138-
json: '[["Map",[1,2]],"a","b"]'
138+
json: '[["Map",1,2],"a","b"]'
139139
},
140140
{
141141
name: 'BigInt',
@@ -215,7 +215,10 @@ const fixtures = {
215215
name: 'Map (cyclical)',
216216
value: map,
217217
js: '(function(a){a.set("self", a);return a}(new Map))',
218-
json: '[["Map",[1,0]],"self"]'
218+
json: '[["Map",1,0],"self"]',
219+
validate: (value) => {
220+
assert.is(value.get('self'), value);
221+
}
219222
};
220223
})(new Map()),
221224

@@ -226,9 +229,9 @@ const fixtures = {
226229
name: 'Set (cyclical)',
227230
value: set,
228231
js: '(function(a){a.add(a).add(42);return a}(new Set))',
229-
json: '[["Set",[0,1]],42]',
232+
json: '[["Set",0,1],42]',
230233
validate: (value) => {
231-
assert.equal(value.size, 2);
234+
assert.is(value.size, 2);
232235
assert.ok(value.has(42));
233236
assert.ok(value.has(value));
234237
}
@@ -243,8 +246,8 @@ const fixtures = {
243246
js: '(function(a){a[0]=a;return a}(Array(1)))',
244247
json: '[[0]]',
245248
validate: (value) => {
246-
assert.equal(value.length, 1);
247-
assert.equal(value[0], value);
249+
assert.is(value.length, 1);
250+
assert.is(value[0], value);
248251
}
249252
};
250253
})([]),
@@ -257,7 +260,7 @@ const fixtures = {
257260
js: '(function(a){a.self=a;return a}({}))',
258261
json: '[{"self":0}]',
259262
validate: (value) => {
260-
assert.equal(value.self, value);
263+
assert.is(value.self, value);
261264
}
262265
};
263266
})({}),
@@ -270,7 +273,7 @@ const fixtures = {
270273
js: '(function(a){a.self=a;return a}(Object.create(null)))',
271274
json: '[["null","self",0]]',
272275
validate: (value) => {
273-
assert.equal(Object.getPrototypeOf(value), null);
276+
assert.is(Object.getPrototypeOf(value), null);
274277
assert.is(value.self, value);
275278
}
276279
};
@@ -285,8 +288,8 @@ const fixtures = {
285288
js: '(function(a,b){a.second=b;b.first=a;return [a,b]}({},{}))',
286289
json: '[[1,2],{"second":2},{"first":1}]',
287290
validate: (value) => {
288-
assert.equal(value[0].second, value[1]);
289-
assert.equal(value[1].first, value[0]);
291+
assert.is(value[0].second, value[1]);
292+
assert.is(value[1].first, value[0]);
290293
}
291294
};
292295
})({}, {})

0 commit comments

Comments
 (0)