pomeranian-durations
Version:
An immutable duration library based on the ISO-8601 format for durations.
194 lines (171 loc) • 6.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addToDate = exports.addYears = exports.addMonths = exports.addWeeks = exports.addDays = exports.addHours = exports.addMinutes = exports.addSeconds = exports.addMilliseconds = exports.addMicroseconds = exports.sum = exports.add = void 0;
var _utils = require("./_utils");
var _transformations = require("./transformations");
var _validate = require("./validate");
var _constants = require("./constants");
/**
* Helpers to add to a duration.
* @name default
*/
/**
* Adds two iso durations
* @param firstIsoString {string} - a string to be added
* @param secondIsoString {string} - a string to be added
* @example
* add('PT3S', 'PT1S') // => 'PT4S'
*/
var add = (0, _utils.curry)(function (firstIsoString, secondIsoString) {
if ((0, _validate.isInvalid)(firstIsoString) || (0, _validate.isInvalid)(secondIsoString)) {
return _constants.INVALID_DURATION;
}
var firstFragments = (0, _transformations.toFragments)(firstIsoString);
var secondFragments = (0, _transformations.toFragments)(secondIsoString);
var updatedFragments = Object.keys(firstFragments).reduce(function (acc, unit) {
acc[unit] = firstFragments[unit] + secondFragments[unit]; // eslint-disable-line no-param-reassign
return acc;
}, {});
return (0, _transformations.toIso)(updatedFragments);
});
/**
* Sums an array of durations
* @param durations {array} - a string to be added
* @example
* sum(['PT1M', 'PT2M', 'PT3M']) // => 'PT6M'
*/
exports.add = add;
var sum = function sum(durations) {
return durations.reduce(function (result, current) {
return add(result, current);
});
};
/**
* Adds the given amount of microseconds to the given duration.
* @param amount {number} - number of microseconds to add
* @param isoString {string} - a string to be added
* @example
* addMicroseconds(1, 'PT1S') // => 'PT1.000001S'
*/
exports.sum = sum;
var addMicroseconds = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "PT".concat(amount / Math.pow(10, 6), "S"));
});
/**
* Adds the given amount of milliseconds to the given duration.
* @param amount {number} - number of milliseconds to add
* @param isoString {string} - a string to be added
* @example
* addMilliseconds(1, 'PT1S') // => 'PT1.001S'
*/
exports.addMicroseconds = addMicroseconds;
var addMilliseconds = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "PT".concat(amount / Math.pow(10, 3), "S"));
});
/**
* Adds the given amount of seconds to the given duration.
* @param amount {number} - number of seconds to add
* @param isoString {string} - a string to be added
* @example
* addSeconds(1, 'PT1S') // => 'PT2S'
*/
exports.addMilliseconds = addMilliseconds;
var addSeconds = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "PT".concat(amount, "S"));
});
/**
* Adds the given amount of minutes to the given duration.
* @param amount {number} - number of minutes to add
* @param isoString {string} - a string to be added
* @example
* addMinutes(1, 'PT1M') // => 'PT2M'
*/
exports.addSeconds = addSeconds;
var addMinutes = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "PT".concat(amount, "M"));
});
/**
* Adds the given amount of hours to the given duration.
* @param amount {number} - number of hours to add
* @param isoString {string} - a string to be added
* @example
* addHours(1, 'PT1M') // => 'PT1H1M'
*/
exports.addMinutes = addMinutes;
var addHours = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "PT".concat(amount, "H"));
});
/**
* Adds the given amount of hours to the given duration.
* @param amount {number} - number of days to add
* @param isoString {string} - a string to be added
* @example
* addDays(1, 'P1D') // => 'P2D'
*/
exports.addHours = addHours;
var addDays = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "P".concat(amount, "D"));
});
/**
* Adds the given amount of hours to the given duration.
* @param amount {number} - number of weeks to add
* @param isoString {string} - a string to be added
* @example
* addWeeks(1, 'P1W') // => 'P2W'
*/
exports.addDays = addDays;
var addWeeks = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "P".concat(amount, "W"));
});
/**
* Adds the given amount of hours to the given duration.
* @param amount {number} - number of months to add
* @param isoString {string} - a string to be added
* @example
* addMonths(1, 'P1M') // => 'P2M'
*/
exports.addWeeks = addWeeks;
var addMonths = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "P".concat(amount, "M"));
});
/**
* Adds the given amount of hours to the given duration.
* @param amount {number} - number of years to add
* @param isoString {string} - a string to be added
* @example
* addYears(1, 'P1Y') // => 'P2Y'
*/
exports.addMonths = addMonths;
var addYears = (0, _utils.curry)(function (amount, isoString) {
return add(isoString, "P".concat(amount, "Y"));
});
/**
* Adds the given iso duration to a date.
* @param amount {string} - iso duration to be added
* @param date {Date} - a date to be added to
* @example
* addToDate('PT1S', new Date('2000-01-01T00:00:00Z')) // => new Date('2000-01-01T00:00:01Z')
* @example
* addToDate('PT1H', new Date('2000-01-01T00:00:00Z')) // => new Date('2000-01-01T01:00:00Z')
* @example
* addToDate('P1M', new Date('2000-01-01T00:00:00Z')) // => new Date('2000-02-01T00:00:00Z')
*/
exports.addYears = addYears;
var addToDate = (0, _utils.curry)(function (amount, date) {
if ((0, _validate.isInvalid)(amount)) {
return _constants.INVALID_DURATION;
}
var dateCopy = new Date(date);
var fragments = (0, _transformations.toFragments)(amount);
dateCopy.setSeconds(dateCopy.getSeconds() + fragments.seconds);
dateCopy.setMinutes(dateCopy.getMinutes() + fragments.minutes);
dateCopy.setHours(dateCopy.getHours() + fragments.hours);
dateCopy.setDate(dateCopy.getDate() + fragments.days);
dateCopy.setDate(dateCopy.getDate() + fragments.weeks * 7);
dateCopy.setMonth(dateCopy.getMonth() + fragments.months);
dateCopy.setFullYear(dateCopy.getFullYear() + fragments.years);
return dateCopy;
});
exports.addToDate = addToDate;