UNPKG

pomeranian-durations

Version:

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

63 lines (52 loc) 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.inHours = exports.inMinutes = exports.inSeconds = void 0; var _conversions = require("./conversions"); var _from = require("./from"); var _constants = require("./constants"); var _validate = require("./validate"); /** * Helpers to convert an ISO8601 duration to a different unit. * Date components (years, months, weeks, days) can't be * converted to other unites without date and timezone information. * Because of that converting date components into other units isn't * supported right now. To do precise arithmetic operations it is * recommended to avoid years, months, weeks and days completely * when using durations. For more information have a look at * http://www.ostyn.com/standards/scorm/samples/ISOTimeForSCORM.htm * @name default */ var buildInFunction = function buildInFunction(fromFn, asFn) { return function (isoDuration) { if ((0, _validate.isInvalid)(isoDuration)) { return _constants.INVALID_DURATION; } return fromFn(asFn(isoDuration)); }; }; /** * Converts all time parts of an ISO8601 duration to seconds and returns the ISO8601 duration string. * @param inSeconds {string} - isoDuration * @example * inSeconds('PT1M') // => 'PT60S' */ var inSeconds = buildInFunction(_from.fromSeconds, _conversions.asSeconds); /** * Converts all time parts of an ISO8601 duration to minutes and returns the ISO8601 duration string. * @param inSeconds {string} - isoDuration * @example * inMinutes('PT1H') // => 'PT60M' */ exports.inSeconds = inSeconds; var inMinutes = buildInFunction(_from.fromMinutes, _conversions.asMinutes); /** * Converts all time parts of an ISO8601 duration to hours and returns the ISO8601 duration string. * @param inSeconds {string} - isoDuration * @example * inHours('PT60M') // => 'PT1H' */ exports.inMinutes = inMinutes; var inHours = buildInFunction(_from.fromHours, _conversions.asHours); exports.inHours = inHours;