Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Commit 5c7f5d6

Browse files
authored
fix: align ISODate with shell COMPASS-4655 (#199)
* fix: align ISODate with shell * restore locks from master * reformat
1 parent 720edc4 commit 5c7f5d6

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/scope.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,26 @@ const SCOPE_ANY: { [x: string]: Function } = lookupMap({
103103

104104
return new bson.Timestamp(low);
105105
},
106-
ISODate: function(...args: any[]) {
107-
// casting our arguments as an empty array because we don't know
108-
// the length of our arguments, and should allow users to pass what
109-
// they want as date arguments
110-
return new Date(...(args as []));
106+
ISODate: function(input?: string): Date {
107+
if (!input) input = new Date().toISOString();
108+
const isoDateRegex = /^(?<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})?)?)?$/;
109+
const match = input.match(isoDateRegex);
110+
if (match !== null && match.groups !== undefined) {
111+
// Normalize the representation because ISO-8601 accepts e.g.
112+
// '20201002T102950Z' without : and -, but `new Date()` does not.
113+
const { Y, M, D, h, m, s, ms, tz } = match.groups;
114+
const normalized = `${Y}-${M}-${D}T${h || '00'}:${m || '00'}:${s ||
115+
'00'}.${ms || '000'}${tz || 'Z'}`;
116+
const date = new Date(normalized);
117+
// Make sur we're in the range 0000-01-01T00:00:00.000Z - 9999-12-31T23:59:59.999Z
118+
if (
119+
date.getTime() >= -62167219200000 &&
120+
date.getTime() <= 253402300799999
121+
) {
122+
return date;
123+
}
124+
}
125+
throw new Error(`${JSON.stringify(input)} is not a valid ISODate`);
111126
},
112127
});
113128

test/index.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ it('should accept a complex query', function() {
8585
Timestamp: new bson.Timestamp({ t: 100, i: 0 }),
8686
Timestamp_object: new bson.Timestamp({ t: 1, i: 2 }),
8787
Timestamp_long: new bson.Timestamp(bson.Long.fromNumber(8589934593)),
88-
ISODate: new Date('2020-01-01 12:00:00'),
88+
ISODate: new Date('2020-01-01T12:00:00.000Z'),
8989
Date: new Date('2020-01-01 12:00:00'),
9090
});
9191
});

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"compilerOptions": {
33
/* Basic Options */
44
// "incremental": true, /* Enable incremental compilation */
5-
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6-
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
5+
"target": "ES2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6+
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
77
// "lib": [], /* Specify library files to be included in the compilation. */
88
// "allowJs": true, /* Allow javascript files to be compiled. */
99
// "checkJs": true, /* Report errors in .js files. */

0 commit comments

Comments
 (0)