Skip to content

Fix bug with Date Apply #10

New issue

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

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

Already on GitHub? # to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/apply.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict';

function isDate(value) {
return Object.prototype.toString.call(value) === '[object Date]';
}

module.exports = function apply(target, patch) {
if (patch === null || typeof patch !== 'object' || Array.isArray(patch)) {
if (patch === null || typeof patch !== 'object' || Array.isArray(patch) || isDate(patch)) {
return patch;
}
if (target === null || typeof target !== 'object' || Array.isArray(target)) {
// if we are here, patch is a non-null object. target should be a non-null object too.
if (target === null || typeof target !== 'object' || Array.isArray(target) || isDate(target)) {
target = {};
}
var keys = Object.keys(patch);
Expand Down
28 changes: 28 additions & 0 deletions test/lib/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,32 @@ describe('apply', function() {
{a: {bb: {}}}
);
});

it('should replace an object with a date', function() {
assert.deepEqual(
apply({a: 'foo'}, new Date('2020-05-08')),
new Date('2020-05-08')
);
});

it('should add a date attribute', function() {
assert.deepEqual(
apply({a: 'foo'}, {b: new Date('2020-05-20')}),
{a: 'foo', b: new Date('2020-05-20')}
);
});

it('should replace a date attribute', function() {
assert.deepEqual(
apply({a: new Date('2020-05-08')}, {a: new Date('2020-05-20')}),
{a: new Date('2020-05-20')}
);
});

it('should delete a date attribute', function() {
assert.deepEqual(
apply({a: new Date('2020-05-20')}, {a: null}),
{}
);
});
});