{"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/toMS.ts","../src/toDuration.ts"],"sourcesContent":["import {\n    ToMSOption,\n    toMS\n} from './toMS';\nimport {\n    ToDurationOption,\n    toDuration\n} from './toDuration';\n\nfunction isError(error: unknown): error is Error {\n    return typeof error === 'object' && error !== null && 'message' in error;\n};\n\n/**\n * Converts a duration string (e.g., \"2h 30m\") into milliseconds.\n * @param {string} value The string time value\n * @param option - An optional object containing various configuration options:\n *   - `isLeapYear` (optional) - A boolean to specify whether to use a leap year in calculations (default is false).\n * @returns The total duration in milliseconds as a number.\n *\n * @example\n * AdvancedMS(\"1 day 1 hour 1 minute 1 second\"); // 90061000\n * AdvancedMS(\"1y 2mo\", { isLeapYear: false }) // 36792000000\n * AdvancedMS(\"1y 2mo\", { isLeapYear: true }); // 36892800000\n */\nfunction AdvancedMS(value: string, option?: ToMSOption): number;\n\n/**\n * Converts a number of milliseconds into a human-readable duration string.\n *\n * @param value - The number of milliseconds to convert.\n * @param option - Optional configuration:\n *   - `isLeapYear`: If true, months and years will account for leap years.\n *   - `returnAllUnits`: Include all time units, even if zero.\n *   - `compactUnits`: Display in compact format (e.g., \"2h 30m\" instead of \"2 hours, 30 minutes\").\n *   - `skipUnits`: Array of unit short names to exclude from the output (e.g., ['h', 'm']).\n *   - `lockUnits`: Used with `skipUnits`. If true, prevents leftover milliseconds from being carried to smaller units.\n * @returns A human-readable string representing the duration.\n *\n * @example\n * AdvancedMS(90061000); // \"1 day, 1 hour, 1 minute, 1 second\"\n * AdvancedMS(90061000, { compactUnits: true }); // \"1d 1h 1m 1s\"\n * AdvancedMS(90061000, { skipUnits: ['h', 'm'] }); // \"1 day, 3661 seconds\"\n * AdvancedMS(90061000, { skipUnits: ['h', 'm'], lockUnits: true }); // \"1 day, 1 second\"\n */\nfunction AdvancedMS(value: number, option?: ToDurationOption): string;\n\n/**\n * Implementation signature for AdvancedMS.\n * \n * This function handles both string-to-milliseconds parsing and number-to-duration formatting.\n * Hidden from IntelliSense; use the overloads above for documentation.\n */\nfunction AdvancedMS(value: string | number, option?: ToDurationOption): string | number {\n    try {\n        if(typeof value === 'string') {\n            if(/^\\d+$/.test(value)) return Number(value);\n\n            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);\n            if(durations) return durations.reduce((a, b) => a + toMS(b, { isLeapYear: option?.isLeapYear }), 0);\n\n            throw new Error('Value is not a valid string');\n        };\n        if(typeof value === 'number') return toDuration(value, option);\n\n        throw new Error('Value is not a valid string nor number');\n    } catch(err) {  \n        const message = isError(err)\n        ? `${err.message}. Value = ${JSON.stringify(value)}`\n        : 'An unknown error has occured.';\n\n        throw new Error(message);\n    };\n};\n\nexport default AdvancedMS;","/**\r\n * Gets leap year in milliseconds\r\n * @param {boolean=} isLeapYear \r\n * @returns {number}\r\n */\r\nexport const getYearMs = (isLeapYear?: boolean): number => isLeapYear ? 31622400000 : 31536000000;","import { getYearMs } from \"./constants\";\r\n\r\n/**\r\n * Options for controlling how `AdvancedMS` converts to milliseconds.\r\n */\r\nexport interface ToMSOption {\r\n    /**\r\n     * Treats the year as a leap year (366 days) instead of a normal year (365 days).\r\n     * This only matters when converting large values (months or years).\r\n     *\r\n     * - Default: `false`\r\n     *\r\n     * @example\r\n     * AdvancedMS(\"1y 2mo\", { isLeapYear: false }) // 36792000000\r\n     * AdvancedMS(\"1y 2mo\", { isLeapYear: true }); // 36892800000\r\n     */\r\n    isLeapYear?: boolean;\r\n}\r\n\r\n/**\r\n * Convert Durations to milliseconds\r\n * @param {string} value The string time value\r\n * @param option - An optional object containing various configuration options:\r\n *   - `isLeapYear` (optional) - A boolean to specify whether to use a leap year in calculations (default is false).\r\n * @returns {number}\r\n * \r\n* @throws {Error} If the input is NOT a valid string.\r\n */\r\nexport function toMS(\r\n    value: string,\r\n    option: ToMSOption = {}\r\n): number {\r\n    const {\r\n        isLeapYear\r\n    } = option;\r\n\r\n    const number = Number(value.replace(/[^-.0-9]+/g, '')), noSpaceValue = value.replace(/\\s+/g, '');\r\n\r\n    const yearMs = getYearMs(isLeapYear);\r\n\r\n    if(/\\d+(?=y)/i.test(noSpaceValue)) return number * yearMs;\r\n    else if(/\\d+(?=ms|milliseconds?)/i.test(noSpaceValue)) return number;\r\n    else if(/\\d+(?=mo|months?)/i.test(noSpaceValue)) return number * (yearMs / 12);\r\n    else if(/\\d+(?=w)/i.test(noSpaceValue)) return number * 604800000;\r\n    else if(/\\d+(?=d)/i.test(noSpaceValue)) return number * 86400000;\r\n    else if(/\\d+(?=h)/i.test(noSpaceValue)) return number * 3600000;\r\n    else if(/\\d+(?=m)/i.test(noSpaceValue)) return number * 60000;\r\n    else if(/\\d+(?=s)/i.test(noSpaceValue)) return number * 1000;\r\n\r\n    throw new Error(`Something went wrong while converting value \"${value}\" with function toMS()`);\r\n};","import { getYearMs } from \"./constants\";\r\n\r\nexport type CompactUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'mo' | 'y';\r\n\r\n/**\r\n * Options for controlling how `AdvancedMS` converts to human-readble time.\r\n *\r\n * Each option changes the way milliseconds are converted into human-readable units.\r\n * Examples are provided to illustrate how different settings affect the output.\r\n */\r\ntype BaseOptions = {\r\n    /**\r\n     * Treats the year as a leap year (366 days) instead of a normal year (365 days).\r\n     * This only matters when converting large values (months or years).\r\n     *\r\n     * - Default: `false`\r\n     *\r\n     * @example\r\n     * // Normal year (365 days)\r\n     * AdvancedMS(31536000000, { isLeapYear: false }); // \"1 year\"\r\n     *\r\n     * // Leap year (366 days)\r\n     * AdvancedMS(31622400000, { isLeapYear: true }); // \"1 year\"\r\n     */\r\n    isLeapYear?: boolean;\r\n    \r\n    /**\r\n     * Forces the output to always list all time units, even if they are zero.\r\n     * Normally, zero-valued units are omitted for brevity.\r\n     *\r\n     * - Default: `false`\r\n     *\r\n     * @example\r\n     * AdvancedMS(90061000, { returnAllUnits: false }); // \"1 day, 1 hour, 1 minute, 1 second\"\r\n     * \r\n     * AdvancedMS(90061000, { returnAllUnits: true }); // \"0 year, 0 month, 0 week, 1 day, 1 hour, 1 minute, 1 second, 0 millisecond\"\r\n     */\r\n    returnAllUnits?: boolean;\r\n    \r\n    /**\r\n     * Chooses between \"compact\" and \"expanded\" output styles.\r\n     *\r\n     * - Compact: short labels (e.g., \"2h 30m\")\r\n     * - Expanded: full words (e.g., \"2 hours, 30 minutes\")\r\n     *\r\n     * - Default: `false` (expanded)\r\n     *\r\n     * @example\r\n     * AdvancedMS(90061000, { compactUnits: false }); // \"1 day, 1 hour, 1 minute, 1 second\"\r\n     * \r\n     * AdvancedMS(90061000, { compactUnits: true }); // \"1d 1h 1m 1s\"\r\n     */\r\n    compactUnits?: boolean;\r\n};\r\n\r\ntype SkipUnitsOptions = BaseOptions & {\r\n    /**\r\n     * Excludes specific units from the output. Any skipped unit will either:\r\n     *   - Roll its value down into smaller units (default behavior)\r\n     *   - Or be ignored entirely if `lockUnits` is also true\r\n     *\r\n     * Use the unit short codes: `'y'`, `'mo'`, `'w'`, `'d'`, `'h'`, `'m'`, `'s'`, `'ms'`.\r\n     *\r\n     * @example\r\n     * // Default version\r\n     * AdvancedMS(90061000, { skipUnits: [] }); // \"1 day, 1 hour, 1 minute, 1 second\"\r\n     * \r\n     * // Exclude hours and minutes\r\n     * AdvancedMS(90061000, { skipUnits: ['h', 'm'] }); // \"1 day, 3661 seconds\"\r\n     */\r\n    skipUnits: Array<CompactUnit>;\r\n    \r\n    /**\r\n     * Works only when `skipUnits` is set. Decides how skipped units behave:\r\n     *\r\n     * - `false` (default): The value of the skipped unit is passed down to smaller units.\r\n     *   Example: skipping hours turns \"1 hour 30 minutes\" into \"90 minutes\".\r\n     *\r\n     * - `true`: Skipped units are ignored entirely, and their values are discarded.\r\n     *   Example: skipping hours turns \"1 hour 30 minutes\" into just \"30 minutes\".\r\n     *\r\n     * @example\r\n     * // Normal behavior\r\n     * AdvancedMS(90061000, { skipUnits: [], lockUnits: false }); // \"1 day, 1 hour, 1 minute, 1 second\"\r\n     * \r\n     * // Roll over skipped unit (default)\r\n     * AdvancedMS(90061000, { skipUnits: ['h', 'm'], lockUnits: false }); // \"1 day, 3661 seconds\"\r\n     * \r\n     * // Ignore skipped unit completely\r\n     * AdvancedMS(90061000, { skipUnits: ['h', 'm'], lockUnits: true }); // \"1 day, 1 second\"\r\n     */\r\n    lockUnits?: boolean;\r\n}\r\n\r\n/**\r\n * Options when skipUnits is NOT specified\r\n */\r\ntype NoSkipUnitsOptions = BaseOptions & {\r\n    skipUnits?: undefined;\r\n    lockUnits?: undefined;\r\n};\r\n\r\n/**\r\n * Full options type\r\n */\r\nexport type ToDurationOption = SkipUnitsOptions | NoSkipUnitsOptions;\r\n\r\n/**\r\n * Converts a given number of milliseconds into a human-readable duration format.\r\n * \r\n * @param value - The number of milliseconds to convert.\r\n * @param option - An optional object containing various configuration options:\r\n *   - `isLeapYear` (optional) - A boolean to specify whether to use a leap year in calculations (default is false).\r\n *   - `returnAllUnits` (optional) - A boolean to specify whether to include all units, even those with a value of 0 (default is false).\r\n *   - `compactUnits` (optional) - A boolean to specify whether to return the units in a compact format (e.g., `2h 30m` instead of `2 hours, 30 minutes`) (default is false).\r\n *   - `skipUnits` (optional) - An array of time formats to exclude from the result (e.g., `['y', 'mo']`).\r\n *   - `lockUnits` (optional) - A boolean to specify whether to use static units. Can only be used with option `skipUnits`. If true, it will return formatted time depending on what units were avoided. (default is false).\r\n * @returns A string representing the duration formatted according to the options provided.\r\n * \r\n * @throws {Error} If the input is NOT a valid number.\r\n */\r\nexport function toDuration(\r\n    value: number,\r\n    option: ToDurationOption = {}\r\n): string {\r\n    const {\r\n        isLeapYear,\r\n        returnAllUnits,\r\n        compactUnits,\r\n        skipUnits,\r\n        lockUnits\r\n    } = option;\r\n\r\n    const yearMs = getYearMs(isLeapYear);\r\n    const timeUnits: Array<any> = [\r\n        { short: 'y', long: 'year', ms: yearMs },\r\n        { short: 'mo', long: 'month', ms: yearMs / 12 },\r\n        { short: 'w', long: 'week', ms: 604800000 },\r\n        { short: 'd', long: 'day', ms: 86400000 },\r\n        { short: 'h', long: 'hour', ms: 3600000 },\r\n        { short: 'm', long: 'minute', ms: 60000 },\r\n        { short: 's', long: 'second', ms: 1000 },\r\n        { short: 'ms', long: 'millisecond', ms: 1 }\r\n    ];\r\n    \r\n    const result = [];\r\n    let remainingMs = Math.abs(value);\r\n    \r\n    for(const unit of timeUnits) {\r\n        const count = Math.floor(remainingMs / unit.ms);\r\n\r\n        if(skipUnits?.includes(unit.short)) {\r\n            if(!lockUnits) continue;\r\n            else {\r\n                remainingMs %= unit.ms; \r\n                continue;\r\n            };\r\n        };\r\n\r\n        if(count > 0 || returnAllUnits) {\r\n            result.push(\r\n                `${Math.sign(value) === -1 ? '-' : ''}${compactUnits ? `${count}${unit.short}` : `${count} ${unit.long}${count > 1 ? 's' : ''}`}`\r\n            );\r\n            remainingMs %= unit.ms;\r\n        };\r\n    };\r\n    \r\n    return result.length > 0 ? result.join(compactUnits ? ' ' : ', ') : '0';\r\n};"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,YAAY,CAAC,eAAiC,aAAa,WAAc;;;ACuB/E,SAAS,KACZ,OACA,SAAqB,CAAC,GAChB;AACN,QAAM;AAAA,IACF;AAAA,EACJ,IAAI;AAEJ,QAAM,SAAS,OAAO,MAAM,QAAQ,cAAc,EAAE,CAAC,GAAG,eAAe,MAAM,QAAQ,QAAQ,EAAE;AAE/F,QAAM,SAAS,UAAU,UAAU;AAEnC,MAAG,YAAY,KAAK,YAAY,EAAG,QAAO,SAAS;AAAA,WAC3C,2BAA2B,KAAK,YAAY,EAAG,QAAO;AAAA,WACtD,qBAAqB,KAAK,YAAY,EAAG,QAAO,UAAU,SAAS;AAAA,WACnE,YAAY,KAAK,YAAY,EAAG,QAAO,SAAS;AAAA,WAChD,YAAY,KAAK,YAAY,E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