UNPKG

pomeranian-durations

Version:

An immutable duration library based on the ISO-8601 format for durations.

126 lines (113 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.eq = exports.lte = exports.lt = exports.gt = exports.gte = void 0; var _conversions = require("./conversions"); var _validate = require("./validate"); /** * Helpers to compare 2 iso durations with each other. Only time parts can be compared * as otherwise the comparison might be wrong. When any of the functions is partially * applied the arguments are automatically swapped so one can write the following: * ````javascript * const isStillBigger = pipe( * add('PT10S'), * gte('PT1M'), * ) * isStillBigger('PT50S') // => true * isStillBigger('PT49S') // => false * ```` * @name default */ var buildCompareFn = function buildCompareFn(compareFn) { return function resolver() { var first = arguments.length <= 0 ? undefined : arguments[0]; var second = arguments.length <= 1 ? undefined : arguments[1]; if (arguments.length !== 2) { return function (_second) { return resolver(_second, first); }; } if ((0, _validate.isInvalid)(first) || (0, _validate.isInvalid)(second)) { return false; } var firstAsSeconds = (0, _conversions.asMicroseconds)(first); var secondAsSeconds = (0, _conversions.asMicroseconds)(second); return compareFn(firstAsSeconds, secondAsSeconds); }; }; /** * Returns true when first value is bigger or equal than second value. When partially applied the * arguments are flipped. * @param firstIsoDuration {string} - ISO8601 duration * @param secondIsoDuration {string} - ISO8601 duration * @example * gte('PT2S', 'PT2S') // => true * @example * gte('PT3S', 'PT2S') // => true * @example * gte('PT3S')('PT2S') // => false */ var gte = buildCompareFn(function (first, second) { return first >= second; }); /** * Returns true when first value is bigger than second value. When partially applied the * arguments are flipped. * @param firstIsoDuration {string} - ISO8601 duration * @param secondIsoDuration {string} - ISO8601 duration * @example * gt('PT2S', 'PT2S') // => false * @example * gt('PT3S', 'PT2S') // => true * @example * gt('PT3S')('PT2S') // => false */ exports.gte = gte; var gt = buildCompareFn(function (first, second) { return first > second; }); /** * Returns true when first value is smaller than second value. When partially applied the * arguments are flipped to allow easy piping. * @param firstIsoDuration {string} - ISO8601 duration * @param secondIsoDuration {string} - ISO8601 duration * @example * lt('PT2S', 'PT2S') // => false * @example * lt('PT3S', 'PT2S') // => false * @example * lt('PT3S')('PT2S') // => true */ exports.gt = gt; var lt = buildCompareFn(function (first, second) { return first < second; }); /** * Returns true when first value is smaller or equal than second value. When partially applied the * arguments are flipped to allow easy piping. * @param firstIsoDuration {string} - ISO8601 duration * @param secondIsoDuration {string} - ISO8601 duration * @example * lte('PT2S', 'PT2S') // => true * @example * lte('PT3S', 'PT2S') // => false * @example * lte('PT3S')('PT2S') // => true */ exports.lt = lt; var lte = buildCompareFn(function (first, second) { return first <= second; }); /** * Returns true when both values are the same. * @param firstIsoDuration {string} - ISO8601 duration * @param secondIsoDuration {string} - ISO8601 duration * @example * eq('PT2S', 'PT2S') // => true */ exports.lte = lte; var eq = buildCompareFn(function (first, second) { return first === second; }); exports.eq = eq;