-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Convert
TimeInterval.js
back to CommonJS
Previously, this file was `TimeInterval.mjs` and exported things with ESM. This was originally done to make Vite happy, but however, this didn't actually work server-side. This is because the server is written in CommonJS - and to import ESM from CommonJS, one needs to use dynamic import. And, since the imports are at the top of the file, TLA (top-level await) must be active so the module is implemented (rather than Promise<Module>, which was previously occuring). And, TLA is only available in ECMAScript Modules. Migrating everything to ESM would take a lot of work, and it's not guaranteed everything will work the same. So revisit keeping `TimeInterval.js` CommonJS. This was previously not possible becaues Vite errored out. But now, Vite accepts this perfectly fine.
- Loading branch information
1 parent
78ed31e
commit 9455185
Showing
8 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
const moment = require('moment') | ||
|
||
exports.TimeInterval = class TimeInterval { | ||
constructor(startTimestamp, endTimestamp) { | ||
// utc keeps the moments from changing timezone. | ||
this.startTimestamp = startTimestamp && moment.utc(startTimestamp); | ||
this.endTimestamp = endTimestamp && moment.utc(endTimestamp); | ||
this.isBounded = (this.startTimestamp !== null) && (this.endTimestamp !== null); | ||
} | ||
|
||
toString() { | ||
if (this.isBounded) { | ||
// Using '_' as a separator character since it doesn't appear in ISO dates | ||
if (this.startTimestamp === undefined || this.endTimestamp === undefined) { | ||
throw Error('startTimestamp or endTimestamp was undefined in a bounded TimeInterval'); | ||
} | ||
return `${this.startTimestamp.format()}_${this.endTimestamp.format()}`; | ||
} | ||
return 'all'; | ||
} | ||
|
||
equals(other) { | ||
return (other instanceof TimeInterval) && this.toString() === other.toString(); | ||
} | ||
|
||
/** | ||
* Returns the duration of the time interval | ||
* @param specifier - Optional parameter, defaults to milliseconds | ||
* @returns {number} | ||
*/ | ||
duration(specifier) { | ||
if (specifier) { | ||
return this.endTimestamp.diff(this.startTimestamp, specifier); | ||
} | ||
return this.endTimestamp.diff(this.startTimestamp); | ||
} | ||
|
||
/** | ||
* Test if this time interval is contains another. | ||
* Intervals are considered to contain equal intervals. | ||
* @param other | ||
* @return {boolean} | ||
*/ | ||
contains(other) { | ||
if (!(other instanceof TimeInterval)) { | ||
throw new Error('TimeInterval objects can only be compared to other TimeInterval objects'); | ||
} | ||
/* The logic here is: | ||
* | ||
* THIS starts at -∞ OR not after OTHER | ||
* AND | ||
* THIS ends at +∞ OR not before OTHER | ||
*/ | ||
return ( | ||
((this.startTimestamp === null) || (this.startTimestamp <= other.startTimestamp)) | ||
&& | ||
((this.endTimestamp === null) || (this.endTimestamp >= other.endTimestamp)) | ||
); | ||
} | ||
|
||
/** | ||
* Returns TimeInterval.toString() so that using a time interval as an object key will | ||
* have reasonable behaviour. | ||
* @return {*} | ||
*/ | ||
valueOf() { | ||
return this.toString(); | ||
} | ||
|
||
getStartTimestamp() { | ||
return this.startTimestamp; | ||
} | ||
|
||
getEndTimestamp() { | ||
return this.endTimestamp; | ||
} | ||
|
||
/** | ||
* Creates a new unbounded time interval | ||
* @return {TimeInterval} | ||
*/ | ||
static unbounded() { | ||
return new TimeInterval(null, null); | ||
} | ||
|
||
/** | ||
* Creates a new TimeInterval from its string representation | ||
* @param {string} stringified the string representation | ||
* @return {TimeInterval} | ||
*/ | ||
static fromString(stringified) { | ||
if (stringified === 'all') { | ||
return TimeInterval.unbounded(); | ||
} | ||
// Using '_' as a separator character since it doesn't appear in ISO dates | ||
const [startTimestamp, endTimestamp] = stringified.split('_').map(timestamp => moment(timestamp)); | ||
return new TimeInterval(startTimestamp, endTimestamp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters