Fixing eslint issues, and still working on test cases

This commit is contained in:
Jonathan Putney
2019-11-11 15:05:02 -05:00
parent 5dae5ca0ae
commit 2f5c9804c9
19 changed files with 3667 additions and 1718 deletions

View File

@@ -1,5 +1,5 @@
// @flow
export const SECONDS_PER_SECOND = 1;
export const SECONDS_PER_SECOND = 1.0;
export const SECONDS_PER_MINUTE = 60;
export const SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
export const SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
@@ -27,11 +27,12 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) {
const dateObj = new Date(totalSeconds * 1000);
const minutes = dateObj.getUTCMinutes();
const seconds = dateObj.getSeconds();
// make sure we add any possible decimal value
const seconds = dateObj.getSeconds() + (totalSeconds % 1.0);
return hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
}
/**
@@ -50,9 +51,14 @@ export function getSecondsAsISODuration(seconds: Number) {
let remainder = seconds;
designations.forEach(([sign, current_seconds]) => {
const value = Math.floor(remainder / current_seconds);
let value = Math.floor(remainder / current_seconds);
remainder = remainder % current_seconds;
// If we have anything left in the remainder, and we're currently adding
// seconds to the duration, go ahead and add the decimal to the seconds
if (sign === 'S' && remainder > 0) {
value += remainder;
}
if (value) {
duration += `${value}${sign}`;
@@ -104,10 +110,45 @@ export function getDurationAsSeconds(duration: String, durationRegex: RegExp) {
anchor.setHours(anchor.getHours() + Number(hours || 0));
anchor.setMinutes(anchor.getMinutes() + Number(minutes || 0));
anchor.setSeconds(anchor.getSeconds() + Number(seconds || 0));
if (seconds) {
if (seconds && String(seconds).indexOf('.') > 0) {
const milliseconds = Number(Number(seconds) % 1).toFixed(6) * 1000.0;
anchor.setMilliseconds(anchor.getMilliseconds() + milliseconds);
}
return (anchor - now) / 1000.0;
return ((anchor * 1.0) - now) / 1000.0;
}
/**
* Adds together two ISO8601 Duration strings
*
* @param {string} first
* @param {string} second
* @param {RegExp} durationRegex
* @return {string}
*/
export function addTwoDurations(
first: String,
second: String,
durationRegex: RegExp) {
const firstSeconds = getDurationAsSeconds(first, durationRegex);
const secondSeconds = getDurationAsSeconds(second, durationRegex);
return getSecondsAsISODuration(firstSeconds + secondSeconds);
}
/**
* Add together two HH:MM:SS.DD strings
*
* @param {string} first
* @param {string} second
* @param {RegExp} timeRegex
* @return {string}
*/
export function addHHMMSSTimeStrings(
first: String,
second: String,
timeRegex: RegExp) {
const firstSeconds = getTimeAsSeconds(first, timeRegex);
const secondSeconds = getTimeAsSeconds(second, timeRegex);
return getSecondsAsHHMMSS(firstSeconds + secondSeconds);
}