Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced object literals don't have arrows #51

Merged
merged 3 commits into from Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = /.+?=>.+?/;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use +? not *?? I prefer *? like the above line if you don't have any reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Yes, *? is much better. Can you make the change or do you want me to do it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the change into your branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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