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

fix: align ISODate with shell COMPASS-4655 #199

Merged
merged 3 commits into from
Jun 27, 2024
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
25 changes: 20 additions & 5 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,26 @@ const SCOPE_ANY: { [x: string]: Function } = lookupMap({

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

Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ it('should accept a complex query', function() {
Timestamp: new bson.Timestamp({ t: 100, i: 0 }),
Timestamp_object: new bson.Timestamp({ t: 1, i: 2 }),
Timestamp_long: new bson.Timestamp(bson.Long.fromNumber(8589934593)),
ISODate: new Date('2020-01-01 12:00:00'),
ISODate: new Date('2020-01-01T12:00:00.000Z'),
Date: new Date('2020-01-01 12:00:00'),
});
});
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"target": "ES2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be safe at this point? helps with regex groups

"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down
Loading