Skip to content

Commit

Permalink
Enhanced object literals don't have arrows (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowenjowen authored and okuryu committed Aug 20, 2019
1 parent d941082 commit cc2a309
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -12,6 +12,7 @@ var PLACE_HOLDER_REGEXP = new RegExp('"@__(F|R|D|M|S)-' + UID + '-(\\d+)__@"', '

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

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

// arrow functions, example: arg1 => arg1+5
if(IS_ARROW_FUNCTION.test(serializedFn)) {
return serializedFn;
}

var argsStartsAt = serializedFn.indexOf('(');
var def = serializedFn.substr(0, argsStartsAt)
.trim()
Expand Down
120 changes: 120 additions & 0 deletions test/unit/serialize.js
Expand Up @@ -125,6 +125,120 @@ describe('serialize( obj )', function () {

expect(obj.hello()).to.equal(true);
});

it('should serialize functions that contain dates', function () {
function fn(arg1) {return new Date('2016-04-28T22:02:17.156Z')};
expect(serialize(fn)).to.be.a('string').equal('function fn(arg1) {return new Date(\'2016-04-28T22:02:17.156Z\')}');
});

it('should deserialize functions that contain dates', function () {
var fn; eval('fn = ' + serialize(function () { return new Date('2016-04-28T22:02:17.156Z') }));
expect(fn).to.be.a('function');
expect(fn().getTime()).to.equal(new Date('2016-04-28T22:02:17.156Z').getTime());
});

it('should serialize functions that return other functions', function () {
function fn() {return function(arg1) {return arg1 + 5}};
expect(serialize(fn)).to.be.a('string').equal('function fn() {return function(arg1) {return arg1 + 5}}');
});

it('should deserialize functions that return other functions', function () {
var fn; eval('fn = ' + serialize(function () { return function(arg1) {return arg1 + 5} }));
expect(fn).to.be.a('function');
expect(fn()(7)).to.equal(12);
});
});

describe('arrow-functions', function () {
it('should serialize arrow functions', function () {
var fn = () => {};
expect(serialize(fn)).to.be.a('string').equal('() => {}');
});

it('should deserialize arrow functions', function () {
var fn; eval('fn = ' + serialize(() => true));
expect(fn).to.be.a('function');
expect(fn()).to.equal(true);
});

it('should serialize arrow functions with one argument', function () {
var fn = arg1 => {}
expect(serialize(fn)).to.be.a('string').equal('arg1 => {}');
});

it('should deserialize arrow functions with one argument', function () {
var fn; eval('fn = ' + serialize(arg1 => {}));
expect(fn).to.be.a('function');
expect(fn.length).to.equal(1);
});

it('should serialize arrow functions with multiple arguments', function () {
var fn = (arg1, arg2) => {}
expect(serialize(fn)).to.equal('(arg1, arg2) => {}');
});

it('should deserialize arrow functions with multiple arguments', function () {
var fn; eval('fn = ' + serialize( (arg1, arg2) => {}));
expect(fn).to.be.a('function');
expect(fn.length).to.equal(2);
});

it('should serialize arrow functions with bodies', function () {
var fn = () => { return true; }
expect(serialize(fn)).to.equal('() => { return true; }');
});

it('should deserialize arrow functions with bodies', function () {
var fn; eval('fn = ' + serialize( () => { return true; }));
expect(fn).to.be.a('function');
expect(fn()).to.equal(true);
});

it('should serialize enhanced literal objects', function () {
var obj = {
foo: () => { return true; },
bar: arg1 => { return true; },
baz: (arg1, arg2) => { return true; }
};

expect(serialize(obj)).to.equal('{"foo":() => { return true; },"bar":arg1 => { return true; },"baz":(arg1, arg2) => { return true; }}');
});

it('should deserialize enhanced literal objects', function () {
var obj;
eval('obj = ' + serialize({ foo: () => { return true; },
foo: () => { return true; },
bar: arg1 => { return true; },
baz: (arg1, arg2) => { return true; }
}));

expect(obj.foo()).to.equal(true);
expect(obj.bar('arg1')).to.equal(true);
expect(obj.baz('arg1', 'arg1')).to.equal(true);
});

it('should serialize arrow functions with added properties', function () {
var fn = () => {};
fn.property1 = 'a string'
expect(serialize(fn)).to.be.a('string').equal('() => {}');
});

it('should deserialize arrow functions with added properties', function () {
var fn; eval('fn = ' + serialize( () => { this.property1 = 'a string'; return 5 }));
expect(fn).to.be.a('function');
expect(fn()).to.equal(5);
});

it('should serialize arrow functions that return other functions', function () {
var fn = arg1 => { return arg2 => arg1 + arg2 };
expect(serialize(fn)).to.be.a('string').equal('arg1 => { return arg2 => arg1 + arg2 }');
});

it('should deserialize arrow functions that return other functions', function () {
var fn; eval('fn = ' + serialize(arg1 => { return arg2 => arg1 + arg2 } ));
expect(fn).to.be.a('function');
expect(fn(2)(3)).to.equal(5);
});
});

describe('regexps', function () {
Expand Down Expand Up @@ -196,6 +310,12 @@ describe('serialize( obj )', function () {
expect(d).to.be.a('string');
expect(d).to.equal('2016-04-28T25:02:17.156Z');
});

it('should serialize dates within objects', function () {
var d = {foo: new Date('2016-04-28T22:02:17.156Z')};
expect(serialize(d)).to.be.a('string').equal('{"foo":new Date("2016-04-28T22:02:17.156Z")}');
expect(serialize({t: [d]})).to.be.a('string').equal('{"t":[{"foo":new Date("2016-04-28T22:02:17.156Z")}]}');
});
});

describe('maps', function () {
Expand Down

0 comments on commit cc2a309

Please sign in to comment.