UNPKG

pomeranian-durations

Version:

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

112 lines (93 loc) 4.28 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.ceilYears = exports.ceilMonths = exports.ceilWeeks = exports.ceilDays = exports.ceilHours = exports.ceilMinutes = exports.ceilSeconds = exports.ceil = void 0; var _utils = require("./_utils"); var _add = _interopRequireWildcard(require("./add")); var _floor = _interopRequireWildcard(require("./floor")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * Helpers to ceil an ISO8601 duration to a particular granularity. * @name default */ var buildCeilFn = function buildCeilFn(unit) { return function (isoString) { var floorFn = _floor["floor".concat(unit)]; var addFn = _add["add".concat(unit)]; return floorFn(addFn(1, isoString)); }; }; /** * Ceils a duration to a given granularity. * @param granularity {string} - ISO8601 duration * @param isoString {string} - ISO8601 duration * @example * ceil('PT2s', 'PT5S') // => 'PT6S' * @example * ceil('PT3s', 'PT5S') // => 'PT6S' * @example * ceil('PT4s', 'PT5S') // => 'PT8S' */ var ceil = (0, _utils.curry)(function (granularity, isoString) { return _floor.floor(granularity, _add.add(granularity, isoString)); }); /** * Ceil a given ISO duration to the next second. * @param isoDuration {string} - ISO8601 duration * @example * ceilSeconds('PT1.1S') // => 'PT2S' */ exports.ceil = ceil; var ceilSeconds = buildCeilFn('Seconds'); /** * Ceil a given ISO duration to the next minute. * @param isoDuration {string} - ISO8601 duration * @example * ceilMinutes('PT1.1M') // => 'PT2M' */ exports.ceilSeconds = ceilSeconds; var ceilMinutes = buildCeilFn('Minutes'); /** * Ceil a given ISO duration to the next hour. * @param isoDuration {string} - ISO8601 duration * @example * ceilHours('PT1.1H') // => 'PT2H' */ exports.ceilMinutes = ceilMinutes; var ceilHours = buildCeilFn('Hours'); /** * Ceil a given ISO duration to the next day. * @param isoDuration {string} - ISO8601 duration * @example * ceilDays('P1.1D') // => 'P2D' */ exports.ceilHours = ceilHours; var ceilDays = buildCeilFn('Days'); /** * Ceil a given ISO duration to the next week. * @param isoDuration {string} - ISO8601 duration * @example * ceilWeeks('P1.1W') // => 'P2W' */ exports.ceilDays = ceilDays; var ceilWeeks = buildCeilFn('Weeks'); /** * Ceil a given ISO duration to the next month. * @param isoDuration {string} - ISO8601 duration * @example * ceilMonths('P1.1M') // => 'P2M' */ exports.ceilWeeks = ceilWeeks; var ceilMonths = buildCeilFn('Months'); /** * Ceil a given ISO duration to the next year. * @param isoDuration {string} - ISO8601 duration * @example * ceilYears('P1.1Y') // => 'P2Y' */ exports.ceilMonths = ceilMonths; var ceilYears = buildCeilFn('Years'); exports.ceilYears = ceilYears;