pax_global_header00006660000000000000000000000064145465306200014517gustar00rootroot0000000000000052 comment=265f81cac937a9720a04bb754b6e739d8c6a5130 parse-ms-4.0.0/000077500000000000000000000000001454653062000132475ustar00rootroot00000000000000parse-ms-4.0.0/.editorconfig000066400000000000000000000002571454653062000157300ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 parse-ms-4.0.0/.gitattributes000066400000000000000000000000231454653062000161350ustar00rootroot00000000000000* text=auto eol=lf parse-ms-4.0.0/.github/000077500000000000000000000000001454653062000146075ustar00rootroot00000000000000parse-ms-4.0.0/.github/security.md000066400000000000000000000002631454653062000170010ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. parse-ms-4.0.0/.github/workflows/000077500000000000000000000000001454653062000166445ustar00rootroot00000000000000parse-ms-4.0.0/.github/workflows/main.yml000066400000000000000000000006451454653062000203200ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 20 - 18 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test parse-ms-4.0.0/.gitignore000066400000000000000000000000271454653062000152360ustar00rootroot00000000000000node_modules yarn.lock parse-ms-4.0.0/.npmrc000066400000000000000000000000231454653062000143620ustar00rootroot00000000000000package-lock=false parse-ms-4.0.0/index.d.ts000066400000000000000000000010431454653062000151460ustar00rootroot00000000000000export type TimeComponents = { days: T; hours: T; minutes: T; seconds: T; milliseconds: T; microseconds: T; nanoseconds: T; }; /** Parse milliseconds into an object. @example ``` import parseMilliseconds from 'parse-ms'; parseMilliseconds(1337000001); // { // days: 15, // hours: 11, // minutes: 23, // seconds: 20, // milliseconds: 1, // microseconds: 0, // nanoseconds: 0 // } ``` */ export default function parseMilliseconds(milliseconds: T): TimeComponents; parse-ms-4.0.0/index.js000066400000000000000000000022071454653062000147150ustar00rootroot00000000000000const toZeroIfInfinity = value => Number.isFinite(value) ? value : 0; function parseNumber(milliseconds) { return { days: Math.trunc(milliseconds / 86_400_000), hours: Math.trunc(milliseconds / 3_600_000 % 24), minutes: Math.trunc(milliseconds / 60_000 % 60), seconds: Math.trunc(milliseconds / 1000 % 60), milliseconds: Math.trunc(milliseconds % 1000), microseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1000) % 1000), nanoseconds: Math.trunc(toZeroIfInfinity(milliseconds * 1e6) % 1000), }; } function parseBigint(milliseconds) { return { days: milliseconds / 86_400_000n, hours: milliseconds / 3_600_000n % 24n, minutes: milliseconds / 60_000n % 60n, seconds: milliseconds / 1000n % 60n, milliseconds: milliseconds % 1000n, microseconds: 0n, nanoseconds: 0n, }; } export default function parseMilliseconds(milliseconds) { switch (typeof milliseconds) { case 'number': { if (Number.isFinite(milliseconds)) { return parseNumber(milliseconds); } break; } case 'bigint': { return parseBigint(milliseconds); } // No default } throw new TypeError('Expected a finite number or bigint'); } parse-ms-4.0.0/index.test-d.ts000066400000000000000000000013721454653062000161300ustar00rootroot00000000000000import {expectType} from 'tsd'; import parseMilliseconds, {type TimeComponents} from './index.js'; const result1: TimeComponents = parseMilliseconds(3000); expectType(result1.days); expectType(result1.hours); expectType(result1.minutes); expectType(result1.seconds); expectType(result1.milliseconds); expectType(result1.microseconds); expectType(result1.nanoseconds); const result2: TimeComponents = parseMilliseconds(3000n); expectType(result2.days); expectType(result2.hours); expectType(result2.minutes); expectType(result2.seconds); expectType(result2.milliseconds); expectType(result2.microseconds); expectType(result2.nanoseconds); parse-ms-4.0.0/license000066400000000000000000000021351454653062000146150ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. parse-ms-4.0.0/package.json000066400000000000000000000014611454653062000155370ustar00rootroot00000000000000{ "name": "parse-ms", "version": "4.0.0", "description": "Parse milliseconds into an object", "license": "MIT", "repository": "sindresorhus/parse-ms", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": { "types": "./index.d.ts", "default": "./index.js" }, "sideEffects": false, "engines": { "node": ">=18" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "browser", "parse", "time", "ms", "milliseconds", "microseconds", "nanoseconds", "duration", "period", "range", "interval" ], "devDependencies": { "ava": "^6.0.1", "tsd": "^0.30.3", "xo": "^0.56.0" } } parse-ms-4.0.0/readme.md000066400000000000000000000012371454653062000150310ustar00rootroot00000000000000# parse-ms > Parse milliseconds into an object ## Install ```sh npm install parse-ms ``` ## Usage ```js import parseMilliseconds from 'parse-ms'; parseMilliseconds(1337000001); /* { days: 15, hours: 11, minutes: 23, seconds: 20, milliseconds: 1, microseconds: 0, nanoseconds: 0 } */ parseMilliseconds(1337000001n); /* { days: 15n, hours: 11n, minutes: 23n, seconds: 20n, milliseconds: 1n, microseconds: 0n, nanoseconds: 0n } */ ``` ## Related - [to-milliseconds](https://github.com/sindresorhus/to-milliseconds) - The inverse of this module - [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string parse-ms-4.0.0/test.js000066400000000000000000000073371454653062000145760ustar00rootroot00000000000000import test from 'ava'; import parseMilliseconds from './index.js'; const toBigInt = milliseconds => { if (typeof milliseconds !== 'number') { return; } try { return BigInt(milliseconds); } catch {} }; /** @template {T extend number | bigint} @param {import('ava').Assertions} t @param {T} milliseconds @param {import('./index.js').TimeComponents} expected */ function runTest(t, milliseconds, expected) { t.deepEqual(parseMilliseconds(milliseconds), expected); const bigint = toBigInt(milliseconds); if (typeof bigint !== 'bigint') { return; } t.deepEqual( parseMilliseconds(bigint), Object.fromEntries( Object.entries(expected).map(([unit, value]) => [unit, BigInt(value)]), ), ); } test('parse milliseconds into an object', t => { runTest(t, 1000 + 400, { days: 0, hours: 0, minutes: 0, seconds: 1, milliseconds: 400, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 55, { days: 0, hours: 0, minutes: 0, seconds: 55, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 67, { days: 0, hours: 0, minutes: 1, seconds: 7, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 60 * 5, { days: 0, hours: 0, minutes: 5, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 60 * 67, { days: 0, hours: 1, minutes: 7, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 60 * 60 * 12, { days: 0, hours: 12, minutes: 0, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 60 * 60 * 40, { days: 1, hours: 16, minutes: 0, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, 1000 * 60 * 60 * 999, { days: 41, hours: 15, minutes: 0, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); runTest(t, (1000 * 60) + 500 + 0.345_678, { days: 0, hours: 0, minutes: 1, seconds: 0, milliseconds: 500, microseconds: 345, nanoseconds: 678, }); runTest(t, 0.000_543, { days: 0, hours: 0, minutes: 0, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 543, }); runTest( t, 0n // 1ms + 1n // 2s + (2n * 1000n) // 3m + (3n * 1000n * 60n) // 4h + (4n * 1000n * 60n * 60n) // Days + (BigInt(Number.MAX_VALUE) * 1000n * 60n * 60n * 24n), { days: BigInt(Number.MAX_VALUE), hours: 4n, minutes: 3n, seconds: 2n, milliseconds: 1n, microseconds: 0n, nanoseconds: 0n, }, ); t.deepEqual(parseMilliseconds(Number.MAX_VALUE), { days: 2.080_663_350_535_087_5e+300, hours: 8, minutes: 8, seconds: 48, milliseconds: 368, microseconds: 0, nanoseconds: 0, }); t.deepEqual(parseMilliseconds(Number.MIN_VALUE), { days: 0, hours: 0, minutes: 0, seconds: 0, milliseconds: 0, microseconds: 0, nanoseconds: 0, }); }); test('handle negative millisecond values', t => { const units = [ 'days', 'hours', 'minutes', 'seconds', 'milliseconds', ]; const times = [ 0.0005, 0.3, ...[ 100 + 400, 1000 * 55, 1000 * 67, 1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12, 1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999, ].flatMap(number => [number, toBigInt(number)]), BigInt('0x' + 'F'.repeat(1e6)), ]; for (const milliseconds of times) { const positive = parseMilliseconds(milliseconds); const negative = parseMilliseconds(-milliseconds); for (const unit of units) { t.is(negative[unit], -positive[unit]); } } }); test('errors', t => { t.throws(() => { parseMilliseconds('string'); }); t.throws(() => { parseMilliseconds(Number.NaN); }); t.throws(() => { parseMilliseconds(Number.POSITIVE_INFINITY); }); });