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

Commit

Permalink
fix: align ISODate with shell
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasimir committed Jun 25, 2024
1 parent 720edc4 commit 564db8a
Show file tree
Hide file tree
Showing 5 changed files with 1,357 additions and 1,273 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

23 changes: 18 additions & 5 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,24 @@ 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'. */
"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

0 comments on commit 564db8a

Please sign in to comment.