UNPKG

pomeranian-durations

Version:

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

110 lines (83 loc) 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromPostgresVerbose = exports.fromPostgres = void 0; var _utils = require("./_utils"); var _transformations = require("./transformations"); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var findExtendedUnit = function findExtendedUnit(stringComponent, units) { var findUnitWithValue = new RegExp(units.map(function (unit) { return "[+,-]?[0-9] ?".concat(unit, " "); }).join('|')); var findUnits = new RegExp(units.join('|').toLowerCase()); var matchedUnit = "".concat(stringComponent, " ").toLowerCase().match(findUnitWithValue); if (!matchedUnit) { return 0; } return parseFloat(matchedUnit[0].replace(findUnits, '')); }; var buildFloatParser = (0, _utils.curry)(function (sign, value) { return parseFloat("".concat(sign).concat(value.replace(sign, ''))); }); var parsePositive = buildFloatParser('+'); var parseNegative = buildFloatParser('-'); var findTimeUnits = function findTimeUnits(stringComponent) { var matchTimeUnits = new RegExp('[+-]?[0-9]+:[0-9]+:[0-9]+'); var matchedUnit = stringComponent.toLowerCase().match(matchTimeUnits); if (!matchedUnit) { return 0; } var parse = matchedUnit[0][0] === '-' ? parseNegative : parsePositive; var _matchedUnit$0$split$ = matchedUnit[0].split(':').map(parse), _matchedUnit$0$split$2 = _slicedToArray(_matchedUnit$0$split$, 3), hours = _matchedUnit$0$split$2[0], minutes = _matchedUnit$0$split$2[1], seconds = _matchedUnit$0$split$2[2]; return { hours: hours, minutes: minutes, seconds: seconds }; }; /** * Converts a postgres duration to an ISO8601 duration * @param postgresString {string} - a postgres interval string * @returns {string} - an ISO8601 duration string * @example * fromPostgres('1 mons 01:02:03') // => 'P1MT1H2M3S' */ var fromPostgres = function fromPostgres(postgresString) { return (0, _transformations.toIso)(_objectSpread({ years: findExtendedUnit(postgresString, 'years year y'.split(' ')), months: findExtendedUnit(postgresString, 'months month mons mon'.split(' ')), days: findExtendedUnit(postgresString, 'days day d'.split(' ')) }, findTimeUnits(postgresString))); }; /** * Converts a postgres verbose duration to an interval string * @param postgresString {string} - a postgres interval verbose string * @returns {string} - an ISO8601 duration string * @example * fromPostgresVerbose('1 mons 3 secs 1 day') // => 'P1M1DT3S' */ exports.fromPostgres = fromPostgres; var fromPostgresVerbose = function fromPostgresVerbose(postgresString) { return (0, _transformations.toIso)({ years: findExtendedUnit(postgresString, 'years year y'.split(' ')), months: findExtendedUnit(postgresString, 'months month mons mon'.split(' ')), days: findExtendedUnit(postgresString, 'days day d'.split(' ')), hours: findExtendedUnit(postgresString, 'hours hour h'.split(' ')), minutes: findExtendedUnit(postgresString, 'minutes minute mins min m'.split(' ')), seconds: findExtendedUnit(postgresString, 'seconds second secs sec s'.split(' ')) }); }; exports.fromPostgresVerbose = fromPostgresVerbose;