pax_global_header00006660000000000000000000000064131324431210014504gustar00rootroot0000000000000052 comment=ddec9a0d2eaae532fff69989dcece8cf7bc34a0e pretty-ms-3.0.0/000077500000000000000000000000001313244312100134505ustar00rootroot00000000000000pretty-ms-3.0.0/.editorconfig000066400000000000000000000002571313244312100161310ustar00rootroot00000000000000root = 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 pretty-ms-3.0.0/.gitattributes000066400000000000000000000000351313244312100163410ustar00rootroot00000000000000* text=auto *.js text eol=lf pretty-ms-3.0.0/.gitignore000066400000000000000000000000271313244312100154370ustar00rootroot00000000000000node_modules yarn.lock pretty-ms-3.0.0/.npmrc000066400000000000000000000000231313244312100145630ustar00rootroot00000000000000package-lock=false pretty-ms-3.0.0/.travis.yml000066400000000000000000000000631313244312100155600ustar00rootroot00000000000000language: node_js node_js: - '8' - '6' - '4' pretty-ms-3.0.0/index.js000066400000000000000000000022751313244312100151230ustar00rootroot00000000000000'use strict'; const parseMs = require('parse-ms'); const plur = require('plur'); module.exports = (ms, opts) => { if (!Number.isFinite(ms)) { throw new TypeError('Expected a finite number'); } opts = opts || {}; if (ms < 1000) { const msDecimalDigits = typeof opts.msDecimalDigits === 'number' ? opts.msDecimalDigits : 0; return (msDecimalDigits ? ms.toFixed(msDecimalDigits) : Math.ceil(ms)) + (opts.verbose ? ' ' + plur('millisecond', Math.ceil(ms)) : 'ms'); } const ret = []; const add = (val, long, short, valStr) => { if (val === 0) { return; } const postfix = opts.verbose ? ' ' + plur(long, val) : short; ret.push((valStr || val) + postfix); }; const parsed = parseMs(ms); add(Math.trunc(parsed.days / 365), 'year', 'y'); add(parsed.days % 365, 'day', 'd'); add(parsed.hours, 'hour', 'h'); add(parsed.minutes, 'minute', 'm'); if (opts.compact) { add(parsed.seconds, 'second', 's'); return '~' + ret[0]; } const sec = ms / 1000 % 60; const secDecimalDigits = typeof opts.secDecimalDigits === 'number' ? opts.secDecimalDigits : 1; const secStr = sec.toFixed(secDecimalDigits).replace(/\.0$/, ''); add(sec, 'second', 's', secStr); return ret.join(' '); }; pretty-ms-3.0.0/license000066400000000000000000000021251313244312100150150ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (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. pretty-ms-3.0.0/package.json000066400000000000000000000015061313244312100157400ustar00rootroot00000000000000{ "name": "pretty-ms", "version": "3.0.0", "description": "Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`", "license": "MIT", "repository": "sindresorhus/pretty-ms", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "pretty", "prettify", "human", "humanize", "humanized", "readable", "time", "ms", "milliseconds", "duration", "period", "range", "text", "string", "str", "number", "hrtime" ], "dependencies": { "parse-ms": "^1.0.0", "plur": "^2.1.2" }, "devDependencies": { "ava": "*", "xo": "*" } } pretty-ms-3.0.0/readme.md000066400000000000000000000030411313244312100152250ustar00rootroot00000000000000# pretty-ms [![Build Status](https://travis-ci.org/sindresorhus/pretty-ms.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-ms) > Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s` ## Usage ``` $ npm install pretty-ms ``` ```js const prettyMs = require('pretty-ms'); prettyMs(1337000000); //=> '15d 11h 23m 20s' prettyMs(1337); //=> '1.3s' prettyMs(133); //=> '133ms' // compact option prettyMs(1337, {compact: true}); //=> '~1s' // verbose option prettyMs(1335669000, {verbose: true}); //=> '15 days 11 hours 1 minute 9 seconds' // can be useful for time durations prettyMs(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5)) //=> '35m' ``` ## API ### prettyMs(input, [options]) #### input Type: `number` Milliseconds to humanize. #### options Type: `Object` ##### secDecimalDigits Type: `number`
Default: `1` Number of digits to appear after the seconds decimal point. ##### msDecimalDigits Type: `number`
Default: `0` Number of digits to appear after the milliseconds decimal point. Useful in combination with [`process.hrtime()`](https://nodejs.org/api/process.html#process_process_hrtime). ##### compact Type: `boolean`
Default: `false` Only show the first unit: `1h 10m` → `~1h`. ##### verbose Type: `boolean`
Default: `false` Use full-length units: `5h 1m 45s` → `5 hours 1 minute 45 seconds` ## Related - [pretty-ms-cli](https://github.com/sindresorhus/pretty-ms-cli) - CLI for this module ## License MIT © [Sindre Sorhus](https://sindresorhus.com) pretty-ms-3.0.0/test.js000066400000000000000000000065521313244312100147750ustar00rootroot00000000000000import test from 'ava'; import m from '.'; test('prettify milliseconds', t => { t.is(m(0), '0ms'); t.is(m(0.1), '1ms'); t.is(m(1), '1ms'); t.is(m(1000 + 400), '1.4s'); t.is(m((1000 * 2) + 400), '2.4s'); t.is(m(1000 * 55), '55s'); t.is(m(1000 * 67), '1m 7s'); t.is(m(1000 * 60 * 5), '5m'); t.is(m(1000 * 60 * 67), '1h 7m'); t.is(m(1000 * 60 * 60 * 12), '12h'); t.is(m(1000 * 60 * 60 * 40), '1d 16h'); t.is(m(1000 * 60 * 60 * 999), '41d 15h'); t.is(m(1000 * 60 * 60 * 24 * 465), '1y 100d'); t.is(m(1000 * 60 * 67 * 24 * 465), '1y 154d 6h'); }); test('have a compact option', t => { t.is(m(1000 + 4, {compact: true}), '~1s'); t.is(m(1000 * 60 * 60 * 999, {compact: true}), '~41d'); t.is(m(1000 * 60 * 60 * 24 * 465, {compact: true}), '~1y'); t.is(m(1000 * 60 * 67 * 24 * 465, {compact: true}), '~1y'); }); test('have a secDecimalDigits option', t => { t.is(m(33333), '33.3s'); t.is(m(33333, {secDecimalDigits: 0}), '33s'); t.is(m(33333, {secDecimalDigits: 4}), '33.3330s'); }); test('have a msDecimalDigits option', t => { t.is(m(33.333), '34ms'); t.is(m(33.333, {msDecimalDigits: 0}), '34ms'); t.is(m(33.333, {msDecimalDigits: 4}), '33.3330ms'); }); test('have a verbose option', t => { const fn = ms => m(ms, {verbose: true}); t.is(fn(0), '0 milliseconds'); t.is(fn(0.1), '1 millisecond'); t.is(fn(1), '1 millisecond'); t.is(fn(1000), '1 second'); t.is(fn(1000 + 400), '1.4 seconds'); t.is(fn((1000 * 2) + 400), '2.4 seconds'); t.is(fn(1000 * 5), '5 seconds'); t.is(fn(1000 * 55), '55 seconds'); t.is(fn(1000 * 67), '1 minute 7 seconds'); t.is(fn(1000 * 60 * 5), '5 minutes'); t.is(fn(1000 * 60 * 67), '1 hour 7 minutes'); t.is(fn(1000 * 60 * 60 * 12), '12 hours'); t.is(fn(1000 * 60 * 60 * 40), '1 day 16 hours'); t.is(fn(1000 * 60 * 60 * 999), '41 days 15 hours'); t.is(fn(1000 * 60 * 60 * 24 * 465), '1 year 100 days'); t.is(fn(1000 * 60 * 67 * 24 * 465), '1 year 154 days 6 hours'); }); test('work with verbose and compact options', t => { const fn = ms => m(ms, { verbose: true, compact: true }); t.is(fn(1000), '~1 second'); t.is(fn(1000 + 400), '~1 second'); t.is(fn((1000 * 2) + 400), '~2 seconds'); t.is(fn(1000 * 5), '~5 seconds'); t.is(fn(1000 * 55), '~55 seconds'); t.is(fn(1000 * 67), '~1 minute'); t.is(fn(1000 * 60 * 5), '~5 minutes'); t.is(fn(1000 * 60 * 67), '~1 hour'); t.is(fn(1000 * 60 * 60 * 12), '~12 hours'); t.is(fn(1000 * 60 * 60 * 40), '~1 day'); t.is(fn(1000 * 60 * 60 * 999), '~41 days'); t.is(fn(1000 * 60 * 60 * 24 * 465), '~1 year'); t.is(fn(1000 * 60 * 67 * 24 * 750), '~2 years'); }); test('work with verbose and secDecimalDigits options', t => { const fn = ms => m(ms, { verbose: true, secDecimalDigits: 4 }); t.is(fn(1000), '1.0000 second'); t.is(fn(1000 + 400), '1.4000 seconds'); t.is(fn((1000 * 2) + 400), '2.4000 seconds'); t.is(fn((1000 * 5) + 254), '5.2540 seconds'); t.is(fn(33333), '33.3330 seconds'); }); test('work with verbose and msDecimalDigits options', t => { const fn = ms => m(ms, { verbose: true, msDecimalDigits: 4 }); t.is(fn(1), '1.0000 millisecond'); t.is(fn(1 + 0.4), '1.4000 milliseconds'); t.is(fn((1 * 2) + 0.400), '2.4000 milliseconds'); t.is(fn((1 * 5) + 0.254), '5.2540 milliseconds'); t.is(fn(33.333), '33.3330 milliseconds'); }); test('throw on invalid', t => { t.throws(() => { m('foo'); }); t.throws(() => { m(NaN); }); t.throws(() => { m(Infinity); }); });