8000 Enhanced object literals don't have arrows (#51) · dgtlmonk/serialize-javascript@cc2a309 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc2a309

Browse files
jowenjowenokuryu
authored andcommitted
Enhanced object literals don't have arrows (yahoo#51)
1 parent d941082 commit cc2a309

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S)-' + UID + '-(\\d+)__@"', '
1212

1313
var IS_NATIVE_CODE_REGEXP = /\{\s*\[native code\]\s*\}/g;
1414
var IS_PURE_FUNCTION = /function.*?\(/;
15+
var IS_ARROW_FUNCTION = /.*?=>.*?/;
1516
var UNSAFE_CHARS_REGEXP = /[<>\/\u2028\u2029]/g;
1617

1718
var RESERVED_SYMBOLS = ['*', 'async'];
@@ -92,6 +93,11 @@ module.exports = function serialize(obj, options) {
9293
return serializedFn;
9394
}
9495

96+
// arrow functions, example: arg1 => arg1+5
97+
if(IS_ARROW_FUNCTION.test(serializedFn)) {
98+
return serializedFn;
99+
}
100+
95101
var argsStartsAt = serializedFn.indexOf('(');
96102
var def = serializedFn.substr(0, argsStartsAt)
97103
.trim()

test/unit/serialize.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,120 @@ describe('serialize( obj )', function () {
125125

126126
expect(obj.hello()).to.equal(true);
127127
});
128+
129+
it('should serialize functions that contain dates', function () {
130+
function fn(arg1) {return new Date('2016-04-28T22:02:17.156Z')};
131+
expect(serialize(fn)).to.be.a('string').equal('function fn(arg1) {return new Date(\'2016-04-28T22:02:17.156Z\')}');
132+
});
133+
134+
it('should deserialize functions that contain dates', function () {
135+
var fn; eval('fn = ' + serialize(function () { return new Date('2016-04-28T22:02:17.156Z') }));
136+
expect(fn).to.be.a('function');
137+
expect(fn().getTime()).to.equal(new Date('2016-04-28T22:02:17.156Z').getTime());
138+
});
139+
140+
it('should serialize functions that return other functions', function () {
141+
function fn() {return function(arg1) {return arg1 + 5}};
142+
expect(serialize(fn)).to.be.a('string').equal('function fn() {return function(arg1) {return arg1 + 5}}');
143+
});
144+
145+
it('should deserialize functions that return other functions', function () {
146+
var fn; eval('fn = ' + serialize(function () { return function(arg1) {return arg1 + 5} }));
147+
expect(fn).to.be.a('function');
148+
expect(fn()(7)).to.equal(12);
149+
});
150+
});
151+
152+
describe('arrow-functions', function () {
153+
it('should serialize arrow functions', function () {
154+
var fn = () => {};
155+
expect(serialize(fn)).to.be.a('string').equal('() => {}');
156+
});
157+
158+
it('should deserialize arrow functions', function () {
159+
var fn; eval('fn = ' + serialize(() => true));
160+
expect(fn).to.be.a('function');
161+
expect(fn()).to.equal(true);
162+
});
163+
164+
it('should serialize arrow functions with one argument', function () {
165+
var fn = arg1 => {}
166+
expect(serialize(fn)).to.be.a('string').equal('arg1 => {}');
167+
});
168+
169+
it('should deserialize arrow functions with one argument', function () {
170+
var fn; eval('fn = ' + serialize(arg1 => {}));
171+
expect(fn).to.be.a('function');
172+
expect(fn.length).to.equal(1);
173+
});
174+
175+
it('should serialize arrow functions with multiple arguments', function () {
176+
var fn = (arg1, arg2) => {}
177+
expect(serialize(fn)).to.equal('(arg1, arg2) => {}');
178+
});
179+
180+
it('should deserialize arrow functions with multiple arguments', function () {
181+
var fn; eval('fn = ' + serialize( (arg1, arg2) => {}));
182+
expect(fn).to.be.a('function');
183+
expect(fn.length).to.equal(2);
184+
});
185+
186+
it('should serialize arrow functions with bodies', function () {
187+
var fn = () => { return true; }
188+
expect(serialize(fn)).to.equal('() => { return true; }');
189+
});
190+
191+
it('should deserialize arrow functions with bodies', function () {
192+
var fn; eval('fn = ' + serialize( () => { return true; }));
193+
expect(fn).to.be.a('function');
194+
expect(fn()).to.equal(true);
195+
});
196+
197+
it('should serialize enhanced literal objects', function () {
198+
var obj = {
199+
foo: () => { return true; },
200+
bar: arg1 => { return true; },
201+
baz: (arg1, arg2) => { return true; }
202+
};
203+
204+
expect(serialize(obj)).to.equal('{"foo":() => { return true; },"bar":arg1 => { return true; },"baz":(arg1, arg2) => { return true; }}');
205+
});
206+
207+
it('should deserialize enhanced literal objects', function () {
208+
var obj;
209+
eval('obj = ' + serialize({ foo: () => { return true; },
210+
foo: () => { return true; },
211+
bar: arg1 => { return true; },
212+
baz: (arg1, arg2) => { return true; }
213+
}));
214+
215+
expect(obj.foo()).to.equal(true);
216+
expect(obj.bar('arg1')).to.equal(true);
217+
expect(obj.baz('arg1', 'arg1')).to.equal(true);
218+
});
219+
220+
it('should serialize arrow functions with added properties', function () {
221+
var fn = () => {};
222+
fn.property1 = 'a string'
223+
expect(serialize(fn)).to.be.a('string').equal('() => {}');
224+
});
225+
226+
it('should deserialize arrow functions with added properties', function () {
227+
var fn; eval('fn = ' + serialize( () => { this.property1 = 'a string'; return 5 }));
228+
expect(fn).to.be.a('function');
229+
expect(fn()).to.equal(5);
230+
});
231+
232+
it('should serialize arrow functions that return other functions', function () {
233+
var fn = arg1 => { return arg2 => arg1 + arg2 };
234+
expect(serialize(fn)).to.be.a('string').equal('arg1 => { return arg2 => arg1 + arg2 }');
235+
});
236+
237+
it('should deserialize arrow functions that return other functions', function () {
238+
var fn; eval('fn = ' + serialize(arg1 => { return arg2 => arg1 + arg2 } ));
239+
expect(fn).to.be.a('function');
240+
expect(fn(2)(3)).to.equal(5);
241+
});
128242
});
129243

130244
describe('regexps', function () {
@@ -196,6 +310,1 8302 2 @@ describe('serialize( obj )', function () {
196310
expect(d).to.be.a('string');
197311
expect(d).to.equal('2016-04-28T25:02:17.156Z');
198312
});
313+
314+
it('should serialize dates within objects', function () {
315+
var d = {foo: new Date('2016-04-28T22:02:17.156Z')};
316+
expect(serialize(d)).to.be.a('string').equal('{"foo":new Date("2016-04-28T22:02:17.156Z")}');
317+
expect(serialize({t: [d]})).to.be.a('string').equal('{"t":[{"foo":new Date("2016-04-28T22:02:17.156Z")}]}');
318+
});
199319
});
200320

201321
describe('maps', function () {

0 commit comments

Comments
 (0)
0