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

Special-case Date in deep-object-assign to ensure Dates work. #1386

Merged
merged 3 commits into from
Oct 3, 2023
Merged
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
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
"lint-fix": "eslint --fix --ext .js,.ts .",
"prepublishOnly": "npm run build",
"test": "run-s test:unit test:types:check-dts test:types:tsc test:interop",
"test:coverage": "BABEL_ENV=test-cov nyc mocha",
"test:coverage": "cross-env BABEL_ENV=test-cov nyc mocha",
"test:interop": "node interop.js",
"test:interop:debug": "npm run test:interop -- --fail-command \"$SHELL\"",
"test:types:check-dts": "cd test && check-dts",
"test:types:tsc": "tsc --noemit --project tsconfig.check.json",
"test:unit": "BABEL_ENV=test mocha",
"test:unit": "cross-env BABEL_ENV=test mocha",
"type-check": "run-s test:types:*",
"version": "npm run contributors:update && git add package.json",
"contributors:update": "git-authors-cli",
Expand Down Expand Up @@ -100,6 +100,7 @@
"assert": "2.1.0",
"check-dts": "0.7.2",
"component-emitter": "1.3.0",
"cross-env": "^7.0.3",
"eslint": "8.50.0",
"git-authors-cli": "1.0.46",
"husky": "8.0.3",
Expand Down
8 changes: 8 additions & 0 deletions src/deep-object-assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function deepObjectAssignNonentry(...values: readonly any[]): any {
const a = values[0];
const b = values[1];

if (a instanceof Date && b instanceof Date) {
a.setTime(b.getTime());
return a;
}

for (const prop of Reflect.ownKeys(b)) {
if (!Object.prototype.propertyIsEnumerable.call(b, prop)) {
// Ignore nonenumerable props, Object.assign() would do the same.
Expand Down Expand Up @@ -108,6 +113,9 @@ function clone(a: any): any {
if (Array.isArray(a)) {
return a.map((value: any): any => clone(value));
} else if (typeof a === "object" && a !== null) {
if (a instanceof Date) {
return new Date(a.getTime());
}
return deepObjectAssignNonentry({}, a);
} else {
return a;
Expand Down
36 changes: 36 additions & 0 deletions test/deep-object-assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,40 @@ test(deepObjectAssign, (): void => {
foo: "bar",
deleteFoo: "foo",
});

const date = new Date(0);
const date2 = new Date(50000);
const date3 = new Date(150000);

// Merge single date into empty object
given(
{},
{
start: date,
}
).expect({
start: date,
});

// Merge single date into non-empty object (no overlap)
given(
{ start: date },
{
end: date2,
}
).expect({
start: date,
end: date2,
});

// Merge single date into non-empty object (overlap)
given(
{ start: date, end: date2 },
{
end: date3,
}
).expect({
start: date,
end: date3,
});
});