pomeranian-durations
Version:
An immutable duration library based on the ISO-8601 format for durations.
105 lines (90 loc) • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.whenInvalidDuration = exports.whenInvalid = exports.isInvalid = exports.isValid = void 0;
var _constants = require("./constants");
var _utils = require("./_utils");
/**
* Helpers for validating ISO8601 durations.
* @name default
*/
var MATCH_NUMBER = /[+-]?\d+(\.\d+)?/.source;
var MATCH_DATE = (0, _utils.createRegexBuilder)().and("(".concat(MATCH_NUMBER, "Y)?")).and("(".concat(MATCH_NUMBER, "M)?")).and("(".concat(MATCH_NUMBER, "W)?")).and("(".concat(MATCH_NUMBER, "D)?"));
var MATCH_TIME = (0, _utils.createRegexBuilder)().maybe((0, _utils.createRegexBuilder)().and(_constants.TIME_DESIGNATOR).and("(".concat(MATCH_NUMBER, "H)?")).and("(".concat(MATCH_NUMBER, "M)?")).and("(".concat(MATCH_NUMBER, "S)?")));
var MATCH_DURATION = (0, _utils.createRegexBuilder)().startOfLine().and(_constants.DURATION_DESIGNATOR).and(MATCH_TIME).and(MATCH_DATE).and(MATCH_TIME).endOfLine();
/**
* Returns if the iso8601 duration is valid or not.
* @param isoDuration
* @returns {boolean}
* @example
* isValid('PT1S') // => true
* @example
* isValid('invalid') // => false
*/
var isValid = function isValid(isoDuration) {
return typeof isoDuration === 'string' && MATCH_DURATION.test(isoDuration.toUpperCase());
};
/**
* Returns if the iso8601 duration is invalid or not.
* @param isoDuration {string}
* @returns {boolean}
* @example
* isInvalid('invalid') // => true
* @example
* isInvalid('PT1S') // => false
*/
exports.isValid = isValid;
var isInvalid = function isInvalid(isoDuration) {
return !isValid(isoDuration);
};
/**
* Returns a default value when the given duration is invalid and duration when it is valid.
* @param value {string}
* @param isoDuration {string}
* @example
* const add10 = compose(
* add(10),
* whenInvalid(() => { throw new Error('Invalid duration') }),
* );
*
* add10('invalid') // error: 'Invalid duration'
*/
exports.isInvalid = isInvalid;
var whenInvalid = (0, _utils.curry)(function (value, isoDuration) {
if (isValid(isoDuration)) {
return isoDuration;
}
if (typeof value === 'function') {
return value(isoDuration);
}
return value;
});
/**
* Returns a given default value when the given duration matches the string 'Invalid Duration'.
* In comparison to whenInvalid the function only returns the default value when it exactly matches
* the 'Invalid Duration' string. Otherwise it just returns the value. In many cases you would prefer
* this function over `whenInvalid`.
*
* @param value {string}
* @param isoDuration {string}
* @example
* const convertToHours = compose(
* asHours,
* whenInvalidDuration(null),
* );
*
* convertToHours('PT10H') // 10
* convertToHours('Blub') // null
*/
exports.whenInvalid = whenInvalid;
var whenInvalidDuration = (0, _utils.curry)(function (value, isoDuration) {
if (isoDuration !== _constants.INVALID_DURATION) {
return isoDuration;
}
if (typeof value === 'function') {
return value(isoDuration);
}
return value;
});
exports.whenInvalidDuration = whenInvalidDuration;