pax_global_header00006660000000000000000000000064133217067250014520gustar00rootroot0000000000000052 comment=c4b3f79c08adb9a16f6a93152920dd1a63c18f96 pretty-time-1.1.0/000077500000000000000000000000001332170672500140025ustar00rootroot00000000000000pretty-time-1.1.0/.editorconfig000066400000000000000000000004411332170672500164560ustar00rootroot00000000000000# http://editorconfig.org/ root = true [*] charset = utf-8 end_of_line = lf indent_size = 2 indent_style = space insert_final_newline = true trim_trailing_whitespace = true [{**/{actual,fixtures,expected,templates}/**,*.md}] trim_trailing_whitespace = false insert_final_newline = false pretty-time-1.1.0/.eslintrc.json000066400000000000000000000073251332170672500166050ustar00rootroot00000000000000{ "extends": [ "eslint:recommended" ], "env": { "browser": false, "es6": true, "node": true, "mocha": true }, "parserOptions":{ "ecmaVersion": 9, "sourceType": "module", "ecmaFeatures": { "modules": true, "experimentalObjectRestSpread": true } }, "globals": { "document": false, "navigator": false, "window": false }, "rules": { "accessor-pairs": 2, "arrow-spacing": [2, { "before": true, "after": true }], "block-spacing": [2, "always"], "brace-style": [2, "1tbs", { "allowSingleLine": true }], "comma-dangle": [2, "never"], "comma-spacing": [2, { "before": false, "after": true }], "comma-style": [2, "last"], "constructor-super": 2, "curly": [2, "multi-line"], "dot-location": [2, "property"], "eol-last": 2, "eqeqeq": [2, "allow-null"], "generator-star-spacing": [2, { "before": true, "after": true }], "handle-callback-err": [2, "^(err|error)$" ], "indent": [2, 2, { "SwitchCase": 1 }], "key-spacing": [2, { "beforeColon": false, "afterColon": true }], "keyword-spacing": [2, { "before": true, "after": true }], "new-cap": [2, { "newIsCap": true, "capIsNew": false }], "new-parens": 2, "no-array-constructor": 2, "no-caller": 2, "no-class-assign": 2, "no-cond-assign": 2, "no-const-assign": 2, "no-control-regex": 2, "no-debugger": 2, "no-delete-var": 2, "no-dupe-args": 2, "no-dupe-class-members": 2, "no-dupe-keys": 2, "no-duplicate-case": 2, "no-empty-character-class": 2, "no-eval": 2, "no-ex-assign": 2, "no-extend-native": 2, "no-extra-bind": 2, "no-extra-boolean-cast": 2, "no-extra-parens": [2, "functions"], "no-fallthrough": 2, "no-floating-decimal": 2, "no-func-assign": 2, "no-implied-eval": 2, "no-inner-declarations": [2, "functions"], "no-invalid-regexp": 2, "no-irregular-whitespace": 2, "no-iterator": 2, "no-label-var": 2, "no-labels": 2, "no-lone-blocks": 2, "no-mixed-spaces-and-tabs": 2, "no-multi-spaces": 2, "no-multi-str": 2, "no-multiple-empty-lines": [2, { "max": 1 }], "no-native-reassign": 0, "no-negated-in-lhs": 2, "no-new": 2, "no-new-func": 2, "no-new-object": 2, "no-new-require": 2, "no-new-wrappers": 2, "no-obj-calls": 2, "no-octal": 2, "no-octal-escape": 2, "no-proto": 0, "no-redeclare": 2, "no-regex-spaces": 2, "no-return-assign": 2, "no-self-compare": 2, "no-sequences": 2, "no-shadow-restricted-names": 2, "no-spaced-func": 2, "no-sparse-arrays": 2, "no-this-before-super": 2, "no-throw-literal": 2, "no-trailing-spaces": 0, "no-undef": 2, "no-undef-init": 2, "no-unexpected-multiline": 2, "no-unneeded-ternary": [2, { "defaultAssignment": false }], "no-unreachable": 2, "no-unused-vars": [2, { "vars": "all", "args": "none" }], "no-useless-call": 0, "no-with": 2, "one-var": [0, { "initialized": "never" }], "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], "padded-blocks": [0, "never"], "quotes": [2, "single", "avoid-escape"], "radix": 2, "semi": [2, "always"], "semi-spacing": [2, { "before": false, "after": true }], "space-before-blocks": [2, "always"], "space-before-function-paren": [2, "never"], "space-in-parens": [2, "never"], "space-infix-ops": 2, "space-unary-ops": [2, { "words": true, "nonwords": false }], "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], "use-isnan": 2, "valid-typeof": 2, "wrap-iife": [2, "any"], "yoda": [2, "never"] } } pretty-time-1.1.0/.gitattributes000066400000000000000000000002001332170672500166650ustar00rootroot00000000000000# Enforce Unix newlines * text eol=lf # binaries *.ai binary *.psd binary *.jpg binary *.gif binary *.png binary *.jpeg binary pretty-time-1.1.0/.gitignore000066400000000000000000000004561332170672500157770ustar00rootroot00000000000000# always ignore files *.DS_Store .idea .vscode *.sublime-* # test related, or directories generated by tests test/actual actual coverage .nyc* # npm node_modules npm-debug.log # yarn yarn.lock yarn-error.log # misc _gh_pages _draft _drafts bower_components vendor temp tmp TODO.md package-lock.jsonpretty-time-1.1.0/.travis.yml000066400000000000000000000001771332170672500161200ustar00rootroot00000000000000sudo: false os: - linux - osx language: node_js node_js: - node - '10' - '9' - '8' - '7' - '6' - '5' - '4' pretty-time-1.1.0/.verb.md000066400000000000000000000027411332170672500153440ustar00rootroot00000000000000## Usage ```js var pretty = require('{%= name %}'); var start = process.hrtime(); var time = process.hrtime(start); console.log(pretty(time)); //=> 3μs ``` ## API By default, when no time increment is given as the second argument, the closest timescale is used (e.g. _most granular without being less than zero_). **Examples:** ```js pretty([1200708, 795428088]); //=> '2w' pretty([800708, 795428088]); //=> '1w' pretty([400708, 795428088]); //=> '5d' pretty([70708, 795428088]); //=> '20h' pretty([12708, 795428088]); //=> '4h' pretty([3708, 795428088]); //=> '1h' pretty([208, 795428088]); //=> '3m' pretty([20, 795428088]); //=> '21s' pretty([0, 795428088]); //=> '795ms' pretty([0, 000428088]); //=> '428μs' pretty([0, 000000088]); //=> '88ns' pretty([0, 000000018]); //=> '18ns' ``` ### Minimum time increment _(All of the following examples use `[6740, 795428088]` as the hrtime array.)_ This value is passed as the second argument and determines how granular to make the time. **Examples** ```js pretty(time, 'h'); //=> '2h' pretty(time, 'm'); //=> '1h 52m' pretty(time, 's'); //=> '1h 52m 21s' ``` **Valid time increments** Any of the following may be used: - `ns` | `nano` | `nanosecond` | `nanoseconds` - `μs` | `micro` | `microsecond` | `microseconds` - `ms` | `milli` | `millisecond` | `milliseconds` - `s` | `sec` | `second` | `seconds` - `m` | `min` | `minute` | `minutes` - `h` | `hr` | `hour` | `hours` - `d` | `day` | `days` - `w` | `wk` | `week` | `weeks` pretty-time-1.1.0/LICENSE000066400000000000000000000021031332170672500150030ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-present, Jon Schlinkert. 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-time-1.1.0/README.md000066400000000000000000000131541332170672500152650ustar00rootroot00000000000000# pretty-time [![NPM version](https://img.shields.io/npm/v/pretty-time.svg?style=flat)](https://www.npmjs.com/package/pretty-time) [![NPM monthly downloads](https://img.shields.io/npm/dm/pretty-time.svg?style=flat)](https://npmjs.org/package/pretty-time) [![NPM total downloads](https://img.shields.io/npm/dt/pretty-time.svg?style=flat)](https://npmjs.org/package/pretty-time) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/pretty-time.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/pretty-time) > Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save pretty-time ``` ## Usage ```js var pretty = require('pretty-time'); var start = process.hrtime(); var time = process.hrtime(start); console.log(pretty(time)); //=> 3μs ``` ## API By default, when no time increment is given as the second argument, the closest timescale is used (e.g. _most granular without being less than zero_). **Examples:** ```js pretty([1200708, 795428088]); //=> '2w' pretty([800708, 795428088]); //=> '1w' pretty([400708, 795428088]); //=> '5d' pretty([70708, 795428088]); //=> '20h' pretty([12708, 795428088]); //=> '4h' pretty([3708, 795428088]); //=> '1h' pretty([208, 795428088]); //=> '3m' pretty([20, 795428088]); //=> '21s' pretty([0, 795428088]); //=> '795ms' pretty([0, 000428088]); //=> '428μs' pretty([0, 000000088]); //=> '88ns' pretty([0, 000000018]); //=> '18ns' ``` ### Minimum time increment _(All of the following examples use `[6740, 795428088]` as the hrtime array.)_ This value is passed as the second argument and determines how granular to make the time. **Examples** ```js pretty(time, 'h'); //=> '2h' pretty(time, 'm'); //=> '1h 52m' pretty(time, 's'); //=> '1h 52m 21s' ``` **Valid time increments** Any of the following may be used: * `ns` | `nano` | `nanosecond` | `nanoseconds` * `μs` | `micro` | `microsecond` | `microseconds` * `ms` | `milli` | `millisecond` | `milliseconds` * `s` | `sec` | `second` | `seconds` * `m` | `min` | `minute` | `minutes` * `h` | `hr` | `hour` | `hours` * `d` | `day` | `days` * `w` | `wk` | `week` | `weeks` ## About
Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
Running Tests Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: ```sh $ npm install && npm test ```
Building docs _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ```
### Related projects You might also be interested in these projects: * [o-clock](https://www.npmjs.com/package/o-clock): Simple javascript utility for displaying the time in 12-hour clock format. | [homepage](https://github.com/jonschlinkert/o-clock "Simple javascript utility for displaying the time in 12-hour clock format.") * [seconds](https://www.npmjs.com/package/seconds): Get the number of seconds for a minute, hour, day and week. | [homepage](https://github.com/jonschlinkert/seconds "Get the number of seconds for a minute, hour, day and week.") * [time-stamp](https://www.npmjs.com/package/time-stamp): Get a formatted timestamp. | [homepage](https://github.com/jonschlinkert/time-stamp "Get a formatted timestamp.") * [timescale](https://www.npmjs.com/package/timescale): Convert from one time scale to another. Nanosecond is the most atomic unit, week is… [more](https://github.com/jonschlinkert/timescale) | [homepage](https://github.com/jonschlinkert/timescale "Convert from one time scale to another. Nanosecond is the most atomic unit, week is the largest unit.") * [week](https://www.npmjs.com/package/week): Get the current week number. | [homepage](https://github.com/datetime/week "Get the current week number.") * [weekday](https://www.npmjs.com/package/weekday): Get the name and number of the current weekday. Or get the name of the… [more](https://github.com/datetime/weekday) | [homepage](https://github.com/datetime/weekday "Get the name and number of the current weekday. Or get the name of the weekday for a given number.") * [year](https://www.npmjs.com/package/year): Simple utility to get the current year with 2 or 4 digits. | [homepage](https://github.com/jonschlinkert/year "Simple utility to get the current year with 2 or 4 digits.") ### Contributors | **Commits** | **Contributor** | | --- | --- | | 14 | [jonschlinkert](https://github.com/jonschlinkert) | | 5 | [doowb](https://github.com/doowb) | ### Author **Jon Schlinkert** * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) * [GitHub Profile](https://github.com/jonschlinkert) * [Twitter Profile](https://twitter.com/jonschlinkert) ### License Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 12, 2018._pretty-time-1.1.0/examples.js000066400000000000000000000004521332170672500161570ustar00rootroot00000000000000 const pretty = require('./'); const start = process.hrtime(); const time = process.hrtime(start); console.log(pretty(time)); //=> 3μs const t = [6740, 795428088]; console.log(pretty(t, 'h')); //=> '2h' console.log(pretty(t, 'm')); //=> '1h 52m' console.log(pretty(t, 's')); //=> '1h 52m 21s' pretty-time-1.1.0/index.js000066400000000000000000000023721332170672500154530ustar00rootroot00000000000000/*! * pretty-time * * Copyright (c) 2015-2018, present, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; const utils = require('./utils'); module.exports = (time, smallest, digits) => { const isNumber = /^[0-9]+$/.test(time); if (!isNumber && !Array.isArray(time)) { throw new TypeError('expected an array or number in nanoseconds'); } if (Array.isArray(time) && time.length !== 2) { throw new TypeError('expected an array from process.hrtime()'); } if (/^[0-9]+$/.test(smallest)) { digits = smallest; smallest = null; } let num = isNumber ? time : utils.nano(time); let res = ''; let prev; for (const uom of Object.keys(utils.scale)) { const step = utils.scale[uom]; let inc = num / step; if (smallest && utils.isSmallest(uom, smallest)) { inc = utils.round(inc, digits); if (prev && (inc === (prev / step))) --inc; res += inc + uom; return res.trim(); } if (inc < 1) continue; if (!smallest) { inc = utils.round(inc, digits); res += inc + uom; return res; } prev = step; inc = Math.floor(inc); num -= (inc * step); res += inc + uom + ' '; } return res.trim(); }; pretty-time-1.1.0/package.json000066400000000000000000000023771332170672500163010ustar00rootroot00000000000000{ "name": "pretty-time", "description": "Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds.", "version": "1.1.0", "homepage": "https://github.com/jonschlinkert/pretty-time", "author": "Jon Schlinkert (https://github.com/jonschlinkert)", "repository": "jonschlinkert/pretty-time", "bugs": { "url": "https://github.com/jonschlinkert/pretty-time/issues" }, "license": "MIT", "files": [ "index.js", "utils.js" ], "main": "index.js", "engines": { "node": ">=4" }, "scripts": { "test": "mocha" }, "devDependencies": { "gulp-format-md": "^1.0.0", "mocha": "^3.5.3" }, "keywords": [ "convert", "date", "format", "formatting", "hour", "hrtime", "micro", "milli", "minute", "nano", "nanosecond", "pretty", "second", "time", "week" ], "verb": { "toc": false, "layout": "default", "tasks": [ "readme" ], "plugins": [ "gulp-format-md" ], "related": { "list": [ "o-clock", "seconds", "time-stamp", "timescale", "week", "weekday", "year" ] }, "lint": { "reflinks": true } } } pretty-time-1.1.0/test.js000066400000000000000000000260461332170672500153270ustar00rootroot00000000000000'use strict'; require('mocha'); const assert = require('assert'); const pretty = require('.'); const { nano, regex } = require('./utils'); describe('pretty', function() { it('should throw an error when invalid args are passed:', function() { assert.throws(() => pretty(), /expected an array or number in nanoseconds/); }); it('should throw an error when invalid arrays are passed:', function() { assert.throws(() => pretty([0,1,2]), /expected an array from process\.hrtime\(\)/); }); it('should support hrtime:', function() { const start = process.hrtime(); const time = process.hrtime(start); assert(typeof pretty(time) === 'string'); }); it('should support nanoseconds:', function() { const start = process.hrtime(); const time = process.hrtime(start); assert(typeof pretty(nano(time)) === 'string'); }); it('should support time increment as second arg:', function() { const time = [1200708, 795428088]; assert.equal(pretty(time, 'w'), '2w'); assert.equal(pretty(time, 'd'), '1w 6d'); assert.equal(pretty(time, 'h'), '1w 6d 22h'); assert.equal(pretty(time, 'm'), '1w 6d 21h 32m'); assert.equal(pretty(time, 's'), '1w 6d 21h 31m 49s'); assert.equal(pretty(time, 'ms'), '1w 6d 21h 31m 48s 795ms'); assert.equal(pretty(time, 'μs'), '1w 6d 21h 31m 48s 795ms 428μs'); assert.equal(pretty(time, 'ns'), '1w 6d 21h 31m 48s 795ms 428μs 88ns'); assert.equal(pretty([0, 795428088], 'ms'), '795ms'); assert.equal(pretty([0, 428088], 'ms'), '0ms'); assert.equal(pretty([0, 428088], 'ns'), '428μs 88ns'); assert.equal(pretty([0, 528088], 'ms'), '1ms'); assert.equal(pretty([0, 428088], 'ms', 1), '0.4ms'); assert.equal(pretty([0, 628088], 'ms', 1), '0.6ms'); assert.equal(pretty([0, 428088], 'ms', 2), '0.43ms'); assert.equal(pretty([0, 795946488], 'ms', 2), '795.95ms'); assert.equal(pretty([0, 795946488], 'μs'), '795ms 946μs'); assert.equal(pretty([0, 795428088], 'μs'), '795ms 428μs'); assert.equal(pretty(nano(time), 'w'), '2w'); assert.equal(pretty(nano(time), 'd'), '1w 6d'); assert.equal(pretty(nano(time), 'h'), '1w 6d 22h'); assert.equal(pretty(nano(time), 'm'), '1w 6d 21h 32m'); assert.equal(pretty(nano(time), 's'), '1w 6d 21h 31m 49s'); assert.equal(pretty(nano(time), 'ms'), '1w 6d 21h 31m 48s 795ms'); assert.equal(pretty(nano(time), 'μs'), '1w 6d 21h 31m 48s 795ms 428μs'); assert.equal(pretty(nano(time), 'ns'), '1w 6d 21h 31m 48s 795ms 428μs 88ns'); assert.equal(pretty(nano([0, 795428088]), 'ms'), '795ms'); assert.equal(pretty(nano([0, 428088]), 'ms'), '0ms'); assert.equal(pretty(nano([0, 428088]), 'ns'), '428μs 88ns'); assert.equal(pretty(nano([0, 528088]), 'ms'), '1ms'); assert.equal(pretty(nano([0, 428088]), 'ms', 1), '0.4ms'); assert.equal(pretty(nano([0, 628088]), 'ms', 1), '0.6ms'); assert.equal(pretty(nano([0, 428088]), 'ms', 2), '0.43ms'); assert.equal(pretty(nano([0, 795946488]), 'ms', 2), '795.95ms'); assert.equal(pretty(nano([0, 795946488]), 'μs'), '795ms 946μs'); assert.equal(pretty(nano([0, 795428088]), 'μs'), '795ms 428μs'); }); it('should round to the closest increment', function() { assert.equal(pretty([1200708, 795428088]), '2w'); assert.equal(pretty([800708, 795428088]), '1w'); assert.equal(pretty([400708, 795428088]), '5d'); assert.equal(pretty([70708, 795428088]), '20h'); assert.equal(pretty([12708, 795428088]), '4h'); assert.equal(pretty([3708, 795428088]), '1h'); assert.equal(pretty([208, 795428088]), '3m'); assert.equal(pretty([20, 795428088]), '21s'); assert.equal(pretty([0, 795428088]), '795ms'); assert.equal(pretty([0, 428088]), '428μs'); assert.equal(pretty([0, 88]), '88ns'); assert.equal(pretty([0, 18]), '18ns'); assert.equal(pretty(nano([1200708, 795428088])), '2w'); assert.equal(pretty(nano([800708, 795428088])), '1w'); assert.equal(pretty(nano([400708, 795428088])), '5d'); assert.equal(pretty(nano([70708, 795428088])), '20h'); assert.equal(pretty(nano([12708, 795428088])), '4h'); assert.equal(pretty(nano([3708, 795428088])), '1h'); assert.equal(pretty(nano([208, 795428088])), '3m'); assert.equal(pretty(nano([20, 795428088])), '21s'); assert.equal(pretty(nano([0, 795428088])), '795ms'); assert.equal(pretty(nano([0, 428088])), '428μs'); assert.equal(pretty(nano([0, 88])), '88ns'); assert.equal(pretty(nano([0, 18])), '18ns'); }); it('should work when numbers are strings', function() { assert.equal(pretty(['1200708', '795428088']), '2w'); assert.equal(pretty(['800708', '795428088']), '1w'); assert.equal(pretty(['400708', '795428088']), '5d'); assert.equal(pretty(['70708', '795428088']), '20h'); assert.equal(pretty(['3708', '795428088']), '1h'); assert.equal(pretty(['208', '795428088']), '3m'); assert.equal(pretty(['20', '795428088']), '21s'); assert.equal(pretty(['0', '795428088']), '795ms'); assert.equal(pretty(['0', '000428088']), '428μs'); assert.equal(pretty(['0', '000000088']), '88ns'); assert.equal(pretty(['0', '000000018']), '18ns'); assert.equal(pretty(['0', '000000001']), '1ns'); assert.equal(pretty(''+nano(['1200708', '795428088'])), '2w'); assert.equal(pretty(''+nano(['800708', '795428088'])), '1w'); assert.equal(pretty(''+nano(['400708', '795428088'])), '5d'); assert.equal(pretty(''+nano(['70708', '795428088'])), '20h'); assert.equal(pretty(''+nano(['3708', '795428088'])), '1h'); assert.equal(pretty(''+nano(['208', '795428088'])), '3m'); assert.equal(pretty(''+nano(['20', '795428088'])), '21s'); assert.equal(pretty(''+nano(['0', '795428088'])), '795ms'); assert.equal(pretty(''+nano(['0', '000428088'])), '428μs'); assert.equal(pretty(''+nano(['0', '000000088'])), '88ns'); assert.equal(pretty(''+nano(['0', '000000018'])), '18ns'); assert.equal(pretty(''+nano(['0', '000000001'])), '1ns'); }); it('should round the given number of digits', function() { assert.equal(pretty([1200708, 795428088], 2), '1.99w'); assert.equal(pretty([800708, 795428088], 2), '1.32w'); assert.equal(pretty([400708, 795428088], 2), '4.64d'); assert.equal(pretty([70708, 795428088], 2), '19.64h'); assert.equal(pretty([3708, 795428088], 2), '1.03h'); assert.equal(pretty([208, 795428088], 2), '3.48m'); assert.equal(pretty([20, 795428088], 2), '20.80s'); assert.equal(pretty([0, 795428088], 2), '795.43ms'); assert.equal(pretty([0, 428088], 2), '428.09μs'); assert.equal(pretty(nano([1200708, 795428088]), 2), '1.99w'); assert.equal(pretty(nano([800708, 795428088]), 2), '1.32w'); assert.equal(pretty(nano([400708, 795428088]), 2), '4.64d'); assert.equal(pretty(nano([70708, 795428088]), 2), '19.64h'); assert.equal(pretty(nano([3708, 795428088]), 2), '1.03h'); assert.equal(pretty(nano([208, 795428088]), 2), '3.48m'); assert.equal(pretty(nano([20, 795428088]), 2), '20.80s'); assert.equal(pretty(nano([0, 795428088]), 2), '795.43ms'); assert.equal(pretty(nano([0, 428088]), 2), '428.09μs'); }); it('should support rounding as the third arg:', function() { const time = [1281708, 795428088]; assert.equal(pretty(time, 'w'), '2w'); assert.equal(pretty(time, 'w', 1), '2.1w'); assert.equal(pretty(time, 'w', 2), '2.12w'); assert.equal(pretty(time, 'd'), '2w 1d'); assert.equal(pretty(time, 'd', 1), '2w 0.8d'); assert.equal(pretty(time, 'd', 2), '2w 0.83d'); assert.equal(pretty(time, 'h'), '2w 20h'); assert.equal(pretty(time, 'h', 1), '2w 20.0h'); assert.equal(pretty(time, 'h', 2), '2w 20.03h'); assert.equal(pretty(time, 'm'), '2w 20h 2m'); assert.equal(pretty(time, 's'), '2w 20h 1m 49s'); assert.equal(pretty(time, 's', 1), '2w 20h 1m 48.8s'); assert.equal(pretty(time, 's', 2), '2w 20h 1m 48.80s'); assert.equal(pretty(time, 'ms'), '2w 20h 1m 48s 795ms'); assert.equal(pretty(time, 'ms', 2), '2w 20h 1m 48s 795.43ms'); assert.equal(pretty(time, 'μs'), '2w 20h 1m 48s 795ms 428μs'); assert.equal(pretty(time, 'μs', 1), '2w 20h 1m 48s 795ms 428.1μs'); assert.equal(pretty(time, 'μs', 2), '2w 20h 1m 48s 795ms 428.09μs'); assert.equal(pretty(time, 'ns'), '2w 20h 1m 48s 795ms 428μs 88ns'); assert.equal(pretty(time, 'ns', 2), '2w 20h 1m 48s 795ms 428μs 88.00ns'); assert.equal(pretty(nano(time), 'w'), '2w'); assert.equal(pretty(nano(time), 'w', 1), '2.1w'); assert.equal(pretty(nano(time), 'w', 2), '2.12w'); assert.equal(pretty(nano(time), 'd'), '2w 1d'); assert.equal(pretty(nano(time), 'd', 1), '2w 0.8d'); assert.equal(pretty(nano(time), 'd', 2), '2w 0.83d'); assert.equal(pretty(nano(time), 'h'), '2w 20h'); assert.equal(pretty(nano(time), 'h', 1), '2w 20.0h'); assert.equal(pretty(nano(time), 'h', 2), '2w 20.03h'); assert.equal(pretty(nano(time), 'm'), '2w 20h 2m'); assert.equal(pretty(nano(time), 's'), '2w 20h 1m 49s'); assert.equal(pretty(nano(time), 's', 1), '2w 20h 1m 48.8s'); assert.equal(pretty(nano(time), 's', 2), '2w 20h 1m 48.80s'); assert.equal(pretty(nano(time), 'ms'), '2w 20h 1m 48s 795ms'); assert.equal(pretty(nano(time), 'ms', 2), '2w 20h 1m 48s 795.43ms'); assert.equal(pretty(nano(time), 'μs'), '2w 20h 1m 48s 795ms 428μs'); assert.equal(pretty(nano(time), 'μs', 1), '2w 20h 1m 48s 795ms 428.1μs'); assert.equal(pretty(nano(time), 'μs', 2), '2w 20h 1m 48s 795ms 428.09μs'); assert.equal(pretty(nano(time), 'ns'), '2w 20h 1m 48s 795ms 428μs 88ns'); assert.equal(pretty(nano(time), 'ns', 2), '2w 20h 1m 48s 795ms 428μs 88.00ns'); }); }); describe('regex', function() { it('should match nanoseconds:', function() { assert(regex.ns.test('nanoseconds')); assert(regex.ns.test('nanosecond')); assert(regex.ns.test('nano')); assert(regex.ns.test('ns')); assert(regex.ns.test('n')); }); it('should match microseconds:', function() { assert(regex.μs.test('microseconds')); assert(regex.μs.test('microsecond')); assert(regex.μs.test('micro')); assert(regex.μs.test('μs')); }); it('should match milliseconds:', function() { assert(regex.ms.test('milliseconds')); assert(regex.ms.test('millisecond')); assert(regex.ms.test('milli')); assert(regex.ms.test('ms')); assert(!regex.ms.test('mil')); assert(!regex.ms.test('ml')); }); it('should match seconds:', function() { assert(regex.s.test('seconds')); assert(regex.s.test('second')); assert(regex.s.test('sec')); assert(regex.s.test('s')); assert(!regex.s.test('ns')); assert(!regex.s.test('ss')); assert(!regex.s.test('ms')); }); it('should match hours:', function() { assert(regex.h.test('hours')); assert(regex.h.test('hour')); assert(regex.h.test('hr')); assert(regex.h.test('h')); }); it('should match days:', function() { assert(regex.d.test('days')); assert(regex.d.test('day')); assert(regex.d.test('d')); }); it('should match weeks:', function() { assert(regex.w.test('weeks')); assert(regex.w.test('week')); assert(regex.w.test('wk')); assert(regex.w.test('w')); }); }); pretty-time-1.1.0/utils.js000066400000000000000000000012251332170672500155000ustar00rootroot00000000000000exports.nano = time => +time[0] * 1e9 + +time[1]; exports.scale = { 'w': 6048e11, 'd': 864e11, 'h': 36e11, 'm': 6e10, 's': 1e9, 'ms': 1e6, 'μs': 1e3, 'ns': 1, }; exports.regex = { 'w': /^(w((ee)?k)?s?)$/, 'd': /^(d(ay)?s?)$/, 'h': /^(h((ou)?r)?s?)$/, 'm': /^(min(ute)?s?|m)$/, 's': /^((sec(ond)?)s?|s)$/, 'ms': /^(milli(second)?s?|ms)$/, 'μs': /^(micro(second)?s?|μs)$/, 'ns': /^(nano(second)?s?|ns?)$/, }; exports.isSmallest = function(uom, unit) { return exports.regex[uom].test(unit); }; exports.round = function(num, digits) { const n = Math.abs(num); return /[0-9]/.test(digits) ? n.toFixed(digits) : Math.round(n); };