Skip to content

Commit 189703d

Browse files
authored
fix(shell-api): pass non-string arguments of ISODate through to Date MONGOSH-1859 (#2145)
1 parent 3e9a558 commit 189703d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

packages/shell-api/src/shell-bson.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ describe('Shell BSON', function () {
381381
}
382382
expect.fail('expected error');
383383
});
384+
it('passes through non-string args to `new Date()`', function () {
385+
expect(shellBson.ISODate()).to.be.an.instanceOf(Date);
386+
expect(shellBson.ISODate(0).getTime()).to.equal(0);
387+
expect(shellBson.ISODate(null).getTime()).to.equal(0);
388+
expect(shellBson.ISODate(1234).getTime()).to.equal(1234);
389+
expect(shellBson.ISODate(shellBson.ISODate(1234)).getTime()).to.equal(
390+
1234
391+
);
392+
});
384393
});
385394
describe('BinData', function () {
386395
it('expects strings as base 64', function () {

packages/shell-api/src/shell-bson.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,11 @@ export default function constructShellBson(
261261
},
262262
{ prototype: bson.Long.prototype }
263263
),
264-
ISODate: function ISODate(input?: string): Date {
265-
if (!input) input = new Date().toISOString();
264+
ISODate: function ISODate(
265+
input?: string | number | Date | undefined
266+
): Date {
267+
if (input === undefined) return new Date();
268+
if (typeof input !== 'string') return new Date(input);
266269
const isoDateRegex =
267270
/^(?<Y>\d{4})-?(?<M>\d{2})-?(?<D>\d{2})([T ](?<h>\d{2})(:?(?<m>\d{2})(:?((?<s>\d{2})(\.(?<ms>\d+))?))?)?(?<tz>Z|([+-])(\d{2}):?(\d{2})?)?)?$/;
268271
const match = isoDateRegex.exec(input);

0 commit comments

Comments
 (0)