forked from yahoo/serialize-javascript
/
serialize.js
315 lines (262 loc) · 11.9 KB
/
serialize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* global describe, it, beforeEach */
'use strict';
var serialize = require('../../'),
expect = require('chai').expect;
describe('serialize( obj )', function () {
it('should be a function', function () {
expect(serialize).to.be.a('function');
});
describe('undefined', function () {
it('should serialize `undefined` to a string', function () {
expect(serialize()).to.be.a('string').equal('undefined');
expect(serialize(undefined)).to.be.a('string').equal('undefined');
});
it('should deserialize "undefined" to `undefined`', function () {
expect(eval(serialize())).to.equal(undefined);
expect(eval(serialize(undefined))).to.equal(undefined);
});
});
describe('null', function () {
it('should serialize `null` to a string', function () {
expect(serialize(null)).to.be.a('string').equal('null');
});
it('should deserialize "null" to `null`', function () {
expect(eval(serialize(null))).to.equal(null);
});
});
describe('JSON', function () {
var data;
beforeEach(function () {
data = {
str : 'string',
num : 0,
obj : {foo: 'foo'},
arr : [1, 2, 3],
bool: true,
nil : null
};
});
it('should serialize JSON to a JSON string', function () {
expect(serialize(data)).to.equal(JSON.stringify(data));
});
it('should deserialize a JSON string to a JSON object', function () {
expect(JSON.parse(serialize(data))).to.deep.equal(data);
});
it('should serialize weird whitespace characters correctly', function () {
var ws = String.fromCharCode(8232);
expect(eval(serialize(ws))).to.equal(ws);
});
});
describe('functions', function () {
it('should serialize annonymous functions', function () {
var fn = function () {};
expect(serialize(fn)).to.be.a('string').equal('function () {}');
});
it('should deserialize annonymous functions', function () {
var fn; eval('fn = ' + serialize(function () {}));
expect(fn).to.be.a('function');
});
it('should serialize named functions', function () {
function fn() {}
expect(serialize(fn)).to.be.a('string').equal('function fn() {}');
});
it('should deserialize named functions', function () {
var fn; eval('fn = ' + serialize(function fn() {}));
expect(fn).to.be.a('function');
expect(fn.name).to.equal('fn');
});
it('should serialize functions with arguments', function () {
function fn(arg1, arg2) {}
expect(serialize(fn)).to.equal('function fn(arg1, arg2) {}');
});
it('should deserialize functions with arguments', function () {
var fn; eval('fn = ' + serialize(function (arg1, arg2) {}));
expect(fn).to.be.a('function');
expect(fn.length).to.equal(2);
});
it('should serialize functions with bodies', function () {
function fn() { return true; }
expect(serialize(fn)).to.equal('function fn() { return true; }');
});
it('should deserialize functions with bodies', function () {
var fn; eval('fn = ' + serialize(function () { return true; }));
expect(fn).to.be.a('function');
expect(fn()).to.equal(true);
});
it('should throw a TypeError when serializing native built-ins', function () {
var err;
expect(Number.toString()).to.equal('function Number() { [native code] }');
try { serialize(Number); } catch (e) { err = e; }
expect(err).to.be.an.instanceOf(TypeError);
});
it('should serialize enhanced literal objects', function () {
var obj = {
foo() { return true; },
*bar() { return true; }
};
expect(serialize(obj)).to.equal('{"foo":function() { return true; },"bar":function*() { return true; }}');
});
it('should deserialize enhanced literal objects', function () {
var obj;
eval('obj = ' + serialize({ hello() { return true; } }));
expect(obj.hello()).to.equal(true);
});
});
describe('regexps', function () {
it('should serialize constructed regexps', function () {
var re = new RegExp('asdf');
expect(serialize(re)).to.be.a('string').equal('/asdf/');
});
it('should deserialize constructed regexps', function () {
var re = eval(serialize(new RegExp('asdf')));
expect(re).to.be.a('RegExp');
expect(re.source).to.equal('asdf');
});
it('should serialize literal regexps', function () {
var re = /asdf/;
expect(serialize(re)).to.be.a('string').equal('/asdf/');
});
it('should deserialize literal regexps', function () {
var re = eval(serialize(/asdf/));
expect(re).to.be.a('RegExp');
expect(re.source).to.equal('asdf');
});
it('should serialize regexps with flags', function () {
var re = /^asdf$/gi;
expect(serialize(re)).to.equal('/^asdf$/gi');
});
it('should deserialize regexps with flags', function () {
var re = eval(serialize(/^asdf$/gi));
expect(re).to.be.a('RegExp');
expect(re.global).to.equal(true);
expect(re.ignoreCase).to.equal(true);
expect(re.multiline).to.equal(false);
});
it('should serialize regexps with escaped chars', function () {
expect(serialize(/\..*/)).to.equal('/\\..*/');
expect(serialize(new RegExp('\\..*'))).to.equal('/\\..*/');
});
it('should deserialize regexps with escaped chars', function () {
var re = eval(serialize(/\..*/));
expect(re).to.be.a('RegExp');
expect(re.source).to.equal('\\..*');
re = eval(serialize(new RegExp('\\..*')));
expect(re).to.be.a('RegExp');
expect(re.source).to.equal('\\..*');
});
});
describe('dates', function () {
it('should serialize dates', function () {
var d = new Date('2016-04-28T22:02:17.156Z');
expect(serialize(d)).to.be.a('string').equal('new Date("2016-04-28T22:02:17.156Z")');
expect(serialize({t: [d]})).to.be.a('string').equal('{"t":[new Date("2016-04-28T22:02:17.156Z")]}');
});
it('should deserialize a date', function () {
var d = eval(serialize(new Date('2016-04-28T22:02:17.156Z')));
expect(d).to.be.a('Date');
expect(d.toISOString()).to.equal('2016-04-28T22:02:17.156Z');
});
it('should deserialize a string that is not a valid date', function () {
var d = eval(serialize('2016-04-28T25:02:17.156Z'));
expect(d).to.be.a('string');
expect(d).to.equal('2016-04-28T25:02:17.156Z');
});
});
describe('maps', function () {
it('should serialize maps', function () {
var regexKey = /.*/;
var m = new Map([
['a', 123],
[regexKey, 456]
]);
expect(serialize(m)).to.be.a('string').equal('new Map([["a",123],[/.*/,456]])');
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Map([["a",123],[/.*/,456]])]}');
});
it('should deserialize a map', function () {
var m = eval(serialize(new Map([
['a', 123],
[null, 456]
])));
expect(m).to.be.a('Map');
expect(m.get(null)).to.equal(456);
});
});
describe('sets', function () {
it('should serialize sets', function () {
var regex = /.*/;
var m = new Set([
'a',
123,
regex
]);
expect(serialize(m)).to.be.a('string').equal('new Set(["a",123,/.*/])');
expect(serialize({t: [m]})).to.be.a('string').equal('{"t":[new Set(["a",123,/.*/])]}');
});
it('should deserialize a set', function () {
var m = eval(serialize(new Set([
'a',
123,
null
])));
expect(m).to.be.a('Set');
expect(m.has(null)).to.equal(true);
});
});
describe('XSS', function () {
it('should encode unsafe HTML chars to Unicode', function () {
expect(serialize('</script>')).to.equal('"\\u003C\\u002Fscript\\u003E"');
expect(JSON.parse(serialize('</script>'))).to.equal('</script>');
expect(eval(serialize('</script>'))).to.equal('</script>');
});
});
describe('options', function () {
it('should accept options as the second argument', function () {
expect(serialize('foo', {})).to.equal('"foo"');
});
it('should accept a `space` option', function () {
expect(serialize([1], {space: 0})).to.equal('[1]');
expect(serialize([1], {space: ''})).to.equal('[1]');
expect(serialize([1], {space: undefined})).to.equal('[1]');
expect(serialize([1], {space: null})).to.equal('[1]');
expect(serialize([1], {space: false})).to.equal('[1]');
expect(serialize([1], {space: 1})).to.equal('[\n 1\n]');
expect(serialize([1], {space: ' '})).to.equal('[\n 1\n]');
expect(serialize([1], {space: 2})).to.equal('[\n 1\n]');
});
it('should accept a `isJSON` option', function () {
expect(serialize('foo', {isJSON: true})).to.equal('"foo"');
expect(serialize('foo', {isJSON: false})).to.equal('"foo"');
function fn() { return true; }
expect(serialize(fn)).to.equal('function fn() { return true; }');
expect(serialize(fn, {isJSON: false})).to.equal('function fn() { return true; }');
expect(serialize(fn, {isJSON: true})).to.equal('undefined');
expect(serialize([1], {isJSON: true, space: 2})).to.equal('[\n 1\n]');
});
it('should accept a `unsafe` option', function () {
expect(serialize('foo', {unsafe: true})).to.equal('"foo"');
expect(serialize('foo', {unsafe: false})).to.equal('"foo"');
function fn() { return true; }
expect(serialize(fn)).to.equal('function fn() { return true; }');
expect(serialize(fn, {unsafe: false})).to.equal('function fn() { return true; }');
expect(serialize(fn, {unsafe: undefined})).to.equal('function fn() { return true; }');
expect(serialize(fn, {unsafe: "true"})).to.equal('function fn() { return true; }');
expect(serialize(fn, {unsafe: true})).to.equal('function fn() { return true; }');
expect(serialize(["1"], {unsafe: false, space: 2})).to.equal('[\n "1"\n]');
expect(serialize(["1"], {unsafe: true, space: 2})).to.equal('[\n "1"\n]');
expect(serialize(["<"], {space: 2})).to.equal('[\n "\\u003C"\n]');
expect(serialize(["<"], {unsafe: true, space: 2})).to.equal('[\n "<"\n]');
});
});
describe('backwards-compatability', function () {
it('should accept `space` as the second argument', function () {
expect(serialize([1], 0)).to.equal('[1]');
expect(serialize([1], '')).to.equal('[1]');
expect(serialize([1], undefined)).to.equal('[1]');
expect(serialize([1], null)).to.equal('[1]');
expect(serialize([1], false)).to.equal('[1]');
expect(serialize([1], 1)).to.equal('[\n 1\n]');
expect(serialize([1], ' ')).to.equal('[\n 1\n]');
expect(serialize([1], 2)).to.equal('[\n 1\n]');
});
});
});