pomeranian-durations
Version:
An immutable duration library based on the ISO-8601 format for durations.
165 lines (144 loc) • 5.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.subtractFromDate = exports.subtractYears = exports.subtractMonths = exports.subtractWeeks = exports.subtractDays = exports.subtractHours = exports.subtractMinutes = exports.subtractSeconds = exports.subtractMicroseconds = exports.subtractMilliseconds = exports.subtract = void 0;
var _utils = require("./_utils");
var _add = require("./add");
var _math = require("./math");
var _transformations = require("./transformations");
var _validate = require("./validate");
var _constants = require("./constants");
/**
* Helpers to subtract from a duration.
* @name default
*/
/**
* Subtracts the given iso duration from the given duration.
* @param firstDuration {string} - a duration to be subtracted
* @param secondDuration {string} - a duration to be subtracted
* @example
* subtract('PT2M', 'PT1M') // => 'PT1M'
*/
var subtract = (0, _utils.curry)(function (firstDuration, secondDurations) {
var secondFragments = (0, _transformations.toFragments)(secondDurations);
var negativeFragments = Object.keys(secondFragments).reduce(function (acc, unit) {
acc[unit] = (0, _utils.negate)(secondFragments[unit]); // eslint-disable-line no-param-reassign
return acc;
}, {});
return (0, _add.add)(firstDuration, (0, _transformations.toIso)(negativeFragments));
});
/**
* Subtracts the given amount of milliseconds from the given duration.
* @param amount {number} - number of milliseconds to be subtracted
* @param isoString {string} - a duration to be subtracted
* @example
* subtractMilliseconds(1, 'PT2M') // => 'PT2M-0.001S'
*/
exports.subtract = subtract;
var subtractMilliseconds = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addMilliseconds)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of microseconds to microseconds from the duration.
* @param amount {number} - number of microseconds to be subtracted
* @param isoString {string} - a duration to be subtracted
* @example
* subtractMicroseconds(1, 'PT2M') // => 'PT2M-0.000001S'
*/
exports.subtractMilliseconds = subtractMilliseconds;
var subtractMicroseconds = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addMicroseconds)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of microseconds seconds from the given duration.
* @param amount {number} - number of microseconds toseconds subtract
* @param isoString {string} - a duration to be subtracted
* @example
* subtractSeconds(1, 'PT2S') // => 'PT1S'
*/
exports.subtractMicroseconds = subtractMicroseconds;
var subtractSeconds = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addSeconds)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of microseconds minutes frome given duration.
* @param amount {number} - number of microseconds tominutes subtract
* @param isoString {string} - a duration to be subtracted
* @example
* subtractMinutes(1, 'PT2M') // => 'PT1M'
*/
exports.subtractSeconds = subtractSeconds;
var subtractMinutes = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addMinutes)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of hours from the given duration.
* @param amount {number} - number of hours to subtract
* @param isoString {string} - a duration to be subtracted
* @example
* subtractHours(1, 'PT2H') // => 'PT1H'
*/
exports.subtractMinutes = subtractMinutes;
var subtractHours = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addHours)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of days from the given duration.
* @param amount {number} - number ofdays microseconds to subtract
* @param isoString {string} - a duration to be subtracted
* @example
* subtractDays(1, 'P2D') // => 'P1D'
*/
exports.subtractHours = subtractHours;
var subtractDays = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addDays)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of weeks from the given duration.
* @param amount {number} - number of weeks to subtract
* @param isoString {string} - a string to be added
* @example
* subtractWeeks(1, 'P2W') // => 'P1W'
*/
exports.subtractDays = subtractDays;
var subtractWeeks = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addWeeks)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts the given amount of microseconds months frome given duration.
* @param amount {number} - number of microseconds tmonths subtract
* @param isoString {string} - a string to be added
* @example
* subtractMonths(1, 'P2M') // => 'P1M'
*/
exports.subtractWeeks = subtractWeeks;
var subtractMonths = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addMonths)((0, _utils.negate)(amount), isoString);
});
/**
* 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
* subtractYears(1, 'P2Y') // => 'P1Y'
*/
exports.subtractMonths = subtractMonths;
var subtractYears = (0, _utils.curry)(function (amount, isoString) {
return (0, _add.addYears)((0, _utils.negate)(amount), isoString);
});
/**
* Subtracts an iso duration from a js date.
* @param amount {string} - iso duration to be subtracted
* @param date {Date} - a date to be subtracted from
* @example
* subtractFromDate('PT1S', new Date('2000-01-01T00:00:00Z')) // => new Date('1999-12-31T23:59:59Z')
*/
exports.subtractYears = subtractYears;
var subtractFromDate = (0, _utils.curry)(function (amount, date) {
if ((0, _validate.isInvalid)(amount)) {
return _constants.INVALID_DURATION;
}
return (0, _add.addToDate)((0, _math.invert)(amount), date);
});
exports.subtractFromDate = subtractFromDate;