@controlplane/cli
Version:
Control Plane Corporation CLI
39 lines • 1.18 kB
JavaScript
;
/**
* Parse a duration string into milliseconds.
* Supports: Xms, Xs, Xm, Xh, Xd, Xw, Xmo, Xy
* Examples: "1h", "30m", "2d", "500ms", "1h30m", "2w3d"
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDuration = parseDuration;
const units = {
ms: 1,
s: 1000,
m: 60 * 1000,
h: 60 * 60 * 1000,
d: 24 * 60 * 60 * 1000,
w: 7 * 24 * 60 * 60 * 1000,
mo: 30 * 24 * 60 * 60 * 1000,
y: 365 * 24 * 60 * 60 * 1000,
};
function parseDuration(duration) {
if (typeof duration !== 'string' || !duration.trim()) {
return null;
}
let total = 0;
// Match patterns like "1h", "30m", "2d", "500ms", "-5m", etc.
const regex = /(-?\d+(?:\.\d+)?)\s*(y|mo|w|d|h|m(?!s)|s|ms)/gi;
let match;
let hasMatch = false;
while ((match = regex.exec(duration)) !== null) {
hasMatch = true;
const value = parseFloat(match[1]);
const unit = match[2].toLowerCase();
if (units[unit] !== undefined) {
total += value * units[unit];
}
}
return hasMatch ? total : null;
}
exports.default = parseDuration;
//# sourceMappingURL=parse-duration.js.map