diff --git a/src/index.js b/src/index.js index 9edc014..9873a37 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,16 @@ const timePattern = `T(${numbers}H)?(${numbers}M)?(${numbers}S)?` const iso8601 = `P(?:${weekPattern}|${datePattern}(?:${timePattern})?)` const objMap = ['weeks', 'years', 'months', 'days', 'hours', 'minutes', 'seconds'] +const defaultDuration = Object.freeze({ + years: 0, + months: 0, + weeks: 0, + days: 0, + hours: 0, + minutes: 0, + seconds: 0 +}) + /** * The ISO8601 regex for matching / testing durations */ @@ -41,6 +51,8 @@ export const parse = durationString => { * @return {Date} - The resulting end Date */ export const end = (duration, startDate) => { + duration = Object.assign({}, defaultDuration, duration) + // Create two equal timestamps, add duration to 'then' and return time difference const timestamp = (startDate ? startDate.getTime() : Date.now()) const then = new Date(timestamp) @@ -66,6 +78,8 @@ export const end = (duration, startDate) => { * @return {Number} */ export const toSeconds = (duration, startDate) => { + duration = Object.assign({}, defaultDuration, duration) + const timestamp = (startDate ? startDate.getTime() : Date.now()) const now = new Date(timestamp) const then = end(duration, now) diff --git a/test/iso8601-tests.js b/test/iso8601-tests.js index ef8b049..d51dd1b 100644 --- a/test/iso8601-tests.js +++ b/test/iso8601-tests.js @@ -122,3 +122,11 @@ test('expose vulnerable time calculation in toSeconds', t => { t.is(sec, 0) }) }) + +test('optional arguments for time calculation in toSeconds', t => { + const sec = toSeconds({ + minutes: 3 + }) + + t.is(sec, 180) +})