Skip to content

Commit

Permalink
fix(hadron-document): do not throw when rendering invalid dates COMPA…
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Mar 25, 2024
1 parent 5b5b85a commit 84fd461
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
12 changes: 10 additions & 2 deletions packages/compass-crud/src/components/change-view/bson-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export function stringifyBSON(value: any) {
return value.inspect();
}
if (value?.toISOString) {
return value.toISOString();
try {
return value.toISOString();
} catch {
return String(value);
}
}
return EJSON.stringify(value);
}
Expand All @@ -29,7 +33,11 @@ export function unBSON(value: any | any[]): any | any[] {
return value.toString();
} else if (Object.prototype.toString.call(value) === '[object Date]') {
// make sure dates are consistently strings when diffing
return value.toISOString();
try {
return value.toISOString();
} catch {
return String(value);
}
} else {
return value;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/compass-import-export/src/csv/csv-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ describe('stringifyCSVValue', function () {
expect(
stringifyCSVValue(new Date('2019-02-08T10:21:49.176Z'), options)
).to.equal('2019-02-08T10:21:49.176Z');
expect(stringifyCSVValue(new Date('NaN'), options)).to.equal(
'Invalid Date'
);
});

it('stringifies number', function () {
Expand Down
6 changes: 5 additions & 1 deletion packages/compass-import-export/src/csv/csv-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ export function stringifyCSVValue(
}

if (Object.prototype.toString.call(value) === '[object Date]') {
return value.toISOString();
try {
return value.toISOString();
} catch {
return String(value);
}
}

// When parsing with relaxed: false we won't see numbers here, but it is
Expand Down
11 changes: 8 additions & 3 deletions packages/hadron-document/src/editor/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ export default class DateEditor extends StandardEditor {
}

_formattedValue(): string {
return new Date(this.element.currentValue as any)
.toISOString()
.replace('Z', '+00:00');
// BSON Date are uint64_t ms, JS Date only supports float64 ms,
// so some valid BSON Dates are not representable in JS.
const date = new Date(this.element.currentValue as any);
try {
return date.toISOString().replace('Z', '+00:00');
} catch {
return String(date);
}
}
}
8 changes: 7 additions & 1 deletion packages/hadron-document/test/editor/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('DateEditor', function () {
describe('#start', function () {
const formattedDateString = '2017-01-01T00:00:00.000+00:00';
const date = new Date(formattedDateString);
const element = new Element('date', date, false);
const element = new Element('date', date);

context('when the current type is valid (not yet edited)', function () {
const dateEditor = new DateEditor(element);
Expand All @@ -22,6 +22,12 @@ describe('DateEditor', function () {
it('sets the current value as valid', function () {
expect(element.isCurrentTypeValid()).to.equal(true);
});

it('handles invalid JS `Date` values well', function () {
const element = new Element('date', new Date(NaN));
new DateEditor(element).start();
expect(element.currentValue).to.equal('Invalid Date');
});
});

context(
Expand Down

0 comments on commit 84fd461

Please sign in to comment.