UNPKG

advanced-ms

Version:

An advanced millisecond conversion package

101 lines (97 loc) 4.11 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { default: () => index_default }); module.exports = __toCommonJS(index_exports); // src/toMS.ts function toMS(value, option = {}) { const number = Number(value.replace(/[^-.0-9]+/g, "")), noSpaceValue = value.replace(/\s+/g, ""); const yearMs = option.isLeapYear ? 316224e5 : 31536e6; if (/\d+(?=y)/i.test(noSpaceValue)) return number * yearMs; else if (/\d+(?=ms|milliseconds?)/i.test(noSpaceValue)) return number; else if (/\d+(?=mo|months?)/i.test(noSpaceValue)) return number * (yearMs / 12); else if (/\d+(?=w)/i.test(noSpaceValue)) return number * 6048e5; else if (/\d+(?=d)/i.test(noSpaceValue)) return number * 864e5; else if (/\d+(?=h)/i.test(noSpaceValue)) return number * 36e5; else if (/\d+(?=m)/i.test(noSpaceValue)) return number * 6e4; else if (/\d+(?=s)/i.test(noSpaceValue)) return number * 1e3; throw new Error(`Something went wrong while converting value "${value}" with function toMS()`); } // src/toDuration.ts function toDuration(value, option = {}) { if (typeof value !== "number" || isNaN(value)) throw new Error("Value is not a valid number."); const { isLeapYear, returnAllUnits, compactUnits, avoidUnits, staticUnits } = option; const yearMs = isLeapYear ? 316224e5 : 31536e6; const timeUnits = [ { short: "y", long: "year", ms: yearMs }, { short: "mo", long: "month", ms: yearMs / 12 }, { short: "w", long: "week", ms: 6048e5 }, { short: "d", long: "day", ms: 864e5 }, { short: "h", long: "hour", ms: 36e5 }, { short: "m", long: "minute", ms: 6e4 }, { short: "s", long: "second", ms: 1e3 }, { short: "ms", long: "millisecond", ms: 1 } ]; const result = []; let remainingMs = Math.abs(value); for (const unit of timeUnits) { const count = Math.floor(remainingMs / unit.ms); if (returnAllUnits || count > 0) { if (staticUnits) remainingMs %= unit.ms; if (avoidUnits && avoidUnits.includes(unit.short)) continue; if (!staticUnits) remainingMs %= unit.ms; result.push(`${Math.sign(value) === -1 ? "-" : ""}${compactUnits ? `${count}${unit.short}` : `${count} ${unit.long}${count > 1 ? "s" : ""}`}`); } ; } ; return result.length > 0 ? result.join(compactUnits ? " " : ", ") : "0"; } // src/index.ts function isError(error) { return typeof error === "object" && error !== null && "message" in error; } function AdvancedMS(value, option) { try { if (typeof value === "string") { if (/^\d+$/.test(value)) return Number(value); const durations = value.match(/\s*-?\s*\d*\.?\d+\s*?(years?|yrs?|months?|mo|weeks?|days?|hours?|hrs?|minutes?|mins?|seconds?|secs?|milliseconds?|ms|[smhdwy])/gi); if (durations) return durations.reduce((a, b) => a + toMS(b, { isLeapYear: option == null ? void 0 : option.isLeapYear }), 0); throw new Error("Value is not a valid string"); } ; if (typeof value === "number") return toDuration(value, option); throw new Error("Value is not a valid string nor number"); } catch (err) { const message = isError(err) ? `${err.message}. Value = ${JSON.stringify(value)}` : "An unknown error has occured."; throw new Error(message); } ; } var index_default = AdvancedMS; //# sourceMappingURL=index.cjs.map