@toreda/time
Version:
Simple, small footprint library for common time operations and converting between units of time.
53 lines (52 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeValueParseDate = timeValueParseDate;
const utils_1 = require("../utils");
const convert_1 = require("../convert");
/**
* Parse a timestamp value into a number expressed in `units`. Accepts:
* - finite numbers (treated as **already** a timestamp in `units`; not
* re-interpreted as ms or seconds)
* - any string accepted by `Date.parse` (ISO 8601, RFC 2822, etc.) —
* converted from ms to `units`
*
* Returns null for empty / whitespace-only strings, NaN/Infinity numeric
* input, unparseable strings, pre-epoch results (negative timestamps), and
* any result outside the safe integer range.
*
* @param value Timestamp value to parse.
* @param units Target unit. Numeric inputs are treated as already in
* this unit; date-string inputs are converted from ms.
*
* @category Time Parsing
*/
function timeValueParseDate(value, units) {
if (typeof value === 'number') {
if (!isFinite(value) || !utils_1.TimeUtils.withinSafeRange(value)) {
return null;
}
if (value < 0) {
return null;
}
return value;
}
if (typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (trimmed.length === 0) {
return null;
}
const ms = Date.parse(trimmed);
if (!isFinite(ms)) {
return null;
}
if (ms < 0) {
return null;
}
const converted = (0, convert_1.timeConvert)('ms', units, ms);
if (converted === null || !utils_1.TimeUtils.withinSafeRange(converted)) {
return null;
}
return converted;
}