├── .editorconfig ├── .github ├── FUNDING.yml └── workflows │ └── ci.yml ├── .gitignore ├── bench ├── index.js └── package.json ├── index.d.ts ├── license ├── package.json ├── readme.md ├── src └── index.js └── test └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = tab 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.{json,yml,md}] 13 | indent_style = space 14 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: lukeed 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | name: Node.js v${{ matrix.nodejs }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | matrix: 11 | nodejs: [10, 12, 14, 16, 18, 20] 12 | steps: 13 | - uses: actions/checkout@v3 14 | - uses: actions/setup-node@v3 15 | with: 16 | node-version: ${{ matrix.nodejs }} 17 | 18 | - name: Install 19 | run: | 20 | npm install 21 | npm install -g c8 22 | 23 | - name: Test 24 | if: matrix.nodejs < 20 25 | run: npm test 26 | 27 | - name: Test w/ Coverage 28 | if: matrix.nodejs >= 20 29 | run: c8 --include=src npm test 30 | 31 | - name: Report 32 | if: matrix.nodejs >= 20 33 | run: | 34 | c8 report --reporter=text-lcov > coverage.lcov 35 | bash <(curl -s https://codecov.io/bash) 36 | env: 37 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *-lock.* 4 | *.lock 5 | *.log 6 | 7 | /dist 8 | -------------------------------------------------------------------------------- /bench/index.js: -------------------------------------------------------------------------------- 1 | const zeit = require('ms'); 2 | const assert = require('assert'); 3 | const { Suite } = require('benchmark'); 4 | const ms = require('../dist'); 5 | 6 | const input_parse = [ 7 | ['100', '100', 100], 8 | ['1m', '1 minute', 60000], 9 | ['1m', '1min', 60000], 10 | ['1h', '1hr', 3600000], 11 | ['1h', '1 hr', 3600000], 12 | ['1h', '1 hour', 3600000], 13 | ['2d', '2 days', 172800000], 14 | ['3w', '3 week', 1814400000], 15 | ['3w', '3 weeks', 1814400000], 16 | ['1s', '1 sec', 1000], 17 | ['1s', '1 second', 1000], 18 | ['100ms', '100 milliseconds', 100], 19 | ['1.5h', '1.5 hours', 5400000], 20 | ['1 s', '1 secs', 1000], 21 | 22 | ['1.5H', '1.5 HOURS', 5400000], 23 | ['.5ms', '0.5 millisecond', 0.5], 24 | ['-100ms', '-100 ms', -100], 25 | ['-1.5h', '-1.5 hour', -5400000], 26 | ['-10.5h', '-10.5 hours', -37800000], 27 | ['-.5h', '-.5 hour', -1800000], 28 | ]; 29 | 30 | const input_format = [ 31 | [500, '500ms', '500 ms'], 32 | [-500, '-500ms', '-500 ms'], 33 | 34 | [1000, '1s', '1 second'], 35 | [10000, '10s', '10 seconds'], 36 | [-1000, '-1s', '-1 second'], 37 | [-10000, '-10s', '-10 seconds'], 38 | 39 | [60 * 1000, '1m', '1 minute'], 40 | [60 * 10000, '10m', '10 minutes'], 41 | [-1 * 60 * 1000, '-1m', '-1 minute'], 42 | [-1 * 60 * 10000, '-10m', '-10 minutes'], 43 | 44 | [60 * 60 * 1000, '1h', '1 hour'], 45 | [60 * 60 * 10000, '10h', '10 hours'], 46 | [-1 * 60 * 60 * 1000, '-1h', '-1 hour'], 47 | [-1 * 60 * 60 * 10000, '-10h', '-10 hours'], 48 | 49 | [24 * 60 * 60 * 1000, '1d', '1 day'], 50 | [24 * 60 * 60 * 10000, '10d', '10 days'], 51 | [-1 * 24 * 60 * 60 * 1000, '-1d', '-1 day'], 52 | [-1 * 24 * 60 * 60 * 10000, '-10d', '-10 days'], 53 | 54 | [234234234, '3d', '3 days'], 55 | [-234234234, '-3d', '-3 days'], 56 | ]; 57 | 58 | const lib_parse = { 59 | 'lukeed/ms': ms.parse, 60 | 'zeit/ms': zeit, 61 | }; 62 | 63 | const lib_format = { 64 | 'lukeed/ms': (x, long) => ms.format(x, long), 65 | 'zeit/ms': (x, long) => zeit(x, { long }), 66 | }; 67 | 68 | function pad(str) { 69 | return str + ' '.repeat(14 - str.length); 70 | } 71 | 72 | function runner(title, contenders, items) { 73 | const isFormat = title === 'format'; 74 | console.log(`\nValidation :: ${title}`); 75 | 76 | Object.keys(contenders).forEach(name => { 77 | try { 78 | const lib = contenders[name]; 79 | items.forEach(arr => { 80 | if (isFormat) { 81 | assert.equal(lib(arr[0]), arr[1], `short ~> ${arr[0]}`); 82 | assert.equal(lib(arr[0], true), arr[2], `long ~> ${arr[0]}`); 83 | } else { 84 | assert.equal(lib(arr[0]), arr[2], `short ~> ${arr[0]}`); 85 | assert.equal(lib(arr[1], true), arr[2], `long ~> ${arr[1]}`); 86 | } 87 | }); 88 | console.log(' ✔', pad(name)); 89 | } catch (err) { 90 | console.log(' ✘', pad(name), `(FAILED @ "${err.message}")`); 91 | } 92 | }); 93 | 94 | console.log(`\nBenchmark :: "${title}"`); 95 | const bench1 = new Suite().on('cycle', e => { 96 | console.log(' ' + e.target); 97 | }); 98 | 99 | Object.keys(contenders).forEach(name => { 100 | const lib = contenders[name]; 101 | bench1.add(pad(name), () => { 102 | items.forEach(arr => { 103 | lib(arr[0]); 104 | }); 105 | }); 106 | }); 107 | 108 | bench1.run(); 109 | 110 | console.log(`\nBenchmark :: "${title}" (long)`); 111 | const bench = new Suite().on('cycle', e => { 112 | console.log(' ' + e.target); 113 | }); 114 | 115 | const IDX = isFormat ? 0 : 1; 116 | Object.keys(contenders).forEach(name => { 117 | const lib = contenders[name]; 118 | bench.add(pad(name), () => { 119 | items.forEach(arr => { 120 | lib(arr[IDX], true); 121 | }); 122 | }); 123 | }); 124 | 125 | bench.run(); 126 | } 127 | 128 | // --- 129 | 130 | runner('parse', lib_parse, input_parse); 131 | runner('format', lib_format, input_format); 132 | -------------------------------------------------------------------------------- /bench/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "devDependencies": { 4 | "benchmark": "2.1.4", 5 | "ms": "2.1.2" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export function format(millis: number, long?: boolean): string; 2 | export function parse(input: string): number | undefined; 3 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Luke Edwards (lukeed.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "umd:name": "ms", 3 | "version": "2.0.2", 4 | "name": "@lukeed/ms", 5 | "repository": "lukeed/ms", 6 | "description": "A tiny (414B) and fast utility to convert milliseconds to and from strings.", 7 | "unpkg": "dist/index.min.js", 8 | "module": "dist/index.mjs", 9 | "main": "dist/index.js", 10 | "types": "index.d.ts", 11 | "license": "MIT", 12 | "author": { 13 | "name": "Luke Edwards", 14 | "email": "luke.edwards05@gmail.com", 15 | "url": "https://lukeed.com" 16 | }, 17 | "exports": { 18 | ".": { 19 | "types": "./index.d.ts", 20 | "import": "./dist/index.mjs", 21 | "require": "./dist/index.js" 22 | }, 23 | "./package.json": "./package.json" 24 | }, 25 | "engines": { 26 | "node": ">=8" 27 | }, 28 | "scripts": { 29 | "build": "bundt", 30 | "test": "uvu -r esm test" 31 | }, 32 | "files": [ 33 | "*.d.ts", 34 | "dist" 35 | ], 36 | "keywords": [ 37 | "ms", 38 | "time", 39 | "format", 40 | "milliseconds", 41 | "convert" 42 | ], 43 | "devDependencies": { 44 | "bundt": "1.1.2", 45 | "esm": "3.2.25", 46 | "uvu": "0.5.1" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ms [![CI](https://github.com/lukeed/ms/workflows/CI/badge.svg)](https://github.com/lukeed/ms/actions) [![codecov](https://badgen.net/codecov/c/github/lukeed/ms)](https://codecov.io/gh/lukeed/ms) 2 | 3 | > A tiny (414B) and [fast](#benchmarks) utility to convert milliseconds to and from strings. 4 | 5 | --- 6 | 7 | ***NOTICE:** This is a fork of [vercel/ms](https://github.com/vercel/ms)!*
8 | In June 2019, I [opened a PR](https://github.com/zeit/ms/pull/120) with signficiant performance and code size improvements. After nearly 2 years of silence, it was eventually closed. :cry: A year into my wait, I started anew (this repo), hoping to improve upon my own improvements. 9 | 10 | --- 11 | 12 | This module is delivered as: 13 | 14 | * **CommonJS**: [`dist/index.js`](https://unpkg.com/@lukeed/ms/dist/index.js) 15 | * **ES Module**: [`dist/index.mjs`](https://unpkg.com/@lukeed/ms/dist/index.mjs) 16 | * **UMD**: [`dist/index.min.js`](https://unpkg.com/@lukeed/ms/dist/index.min.js) 17 | 18 | ## Install 19 | 20 | ``` 21 | $ npm install --save @lukeed/ms 22 | ``` 23 | 24 | 25 | ## Usage 26 | 27 | ```js 28 | import { parse, format } from '@lukeed/ms'; 29 | 30 | // string => number 31 | parse('2 days'); //=> 172800000 32 | parse('1d'); //=> 86400000 33 | parse('10h'); //=> 36000000 34 | parse('2.5 hrs'); //=> 9000000 35 | parse('2h'); //=> 7200000 36 | parse('1m'); //=> 60000 37 | parse('5s'); //=> 5000 38 | parse('1y'); //=> 31557600000 39 | parse('100'); //=> 100 40 | parse('-3 days'); //=> -259200000 41 | parse('-1h'); //=> -3600000 42 | parse('-200'); //=> -200 43 | 44 | // number => string 45 | format(60000); //=> '1m' 46 | format(2 * 60000); //=> '2m' 47 | format(-3 * 60000); //=> '-3m' 48 | format(parse('10 hours')); //=> '10h' 49 | 50 | // number => string (long) 51 | format(60000, true); //=> '1 minute' 52 | format(2 * 60000, true); //=> '2 minutes' 53 | format(-3 * 60000, true); //=> '-3 minutes' 54 | format(parse('10 hours'), true); //=> '10 hours' 55 | ``` 56 | 57 | 58 | ## API 59 | 60 | ### ms.parse(input) 61 | Returns: `Number`| `undefined` 62 | 63 | Parses the input string, returning the number of milliseconds or `undefined` if the value can't be parsed successfully. 64 | 65 | #### input 66 | Type: `String` 67 | 68 | The human-readable time string; eg: `10min`, `10m`, `10 minutes`. 69 | 70 | 71 | ### ms.format(milli, long?) 72 | Returns: `Number` 73 | 74 | Formats the millisecond count to a human-readable time string. 75 | 76 | > **Important:** The output will be rounded to the nearest whole integer. 77 | 78 | #### milli 79 | Type: `Number` 80 | 81 | The number of milliseconds. 82 | 83 | #### long 84 | Type: `Boolean`
85 | Default: `false` 86 | 87 | Whether or not the output should use the interval's long/full form; eg `hour` or `hours` instead of `h`. 88 | 89 | > **Note:** When `long`, the count and interval will be separated by a single space.
Also, when `long`, the interval may be pluralized; eg `1 second` vs `2 seconds`. 90 | 91 | 92 | ## Benchmarks 93 | 94 | > Running on Node.js v12.18.4 95 | 96 | ``` 97 | Validation :: parse 98 | ✔ lukeed/ms 99 | ✔ zeit/ms 100 | 101 | Benchmark :: "parse" 102 | lukeed/ms x 351,319 ops/sec ±0.31% (96 runs sampled) 103 | zeit/ms x 245,576 ops/sec ±1.66% (94 runs sampled) 104 | 105 | Benchmark :: "parse" (long) 106 | lukeed/ms x 335,538 ops/sec ±0.50% (94 runs sampled) 107 | zeit/ms x 265,410 ops/sec ±1.72% (95 runs sampled) 108 | 109 | 110 | Validation :: format 111 | ✔ lukeed/ms 112 | ✔ zeit/ms 113 | 114 | Benchmark :: "format" 115 | lukeed/ms x 4,109,440 ops/sec ±0.35% (94 runs sampled) 116 | zeit/ms x 3,420,198 ops/sec ±1.61% (94 runs sampled) 117 | 118 | Benchmark :: "format" (long) 119 | lukeed/ms x 3,402,872 ops/sec ±0.14% (97 runs sampled) 120 | zeit/ms x 1,344,908 ops/sec ±3.68% (96 runs sampled) 121 | ``` 122 | 123 | 124 | ## Credits 125 | 126 | This is obviously a fork of [zeit/ms](https://github.com/zeit/ms). 127 | 128 | I opened a [PR in June 2019](https://github.com/zeit/ms/pull/120) that introduced significant performance gains and code reduction — it was ignored for nearly two years. This repository is a from-scratch re-implementation that takes the goals of that PR a bit further. 129 | 130 | 131 | ## License 132 | 133 | MIT © [Luke Edwards](https://lukeed.com) 134 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | var RGX = /^(-?(?:\d+)?\.?\d+) *(m(?:illiseconds?|s(?:ecs?)?))?(s(?:ec(?:onds?|s)?)?)?(m(?:in(?:utes?|s)?)?)?(h(?:ours?|rs?)?)?(d(?:ays?)?)?(w(?:eeks?|ks?)?)?(y(?:ears?|rs?)?)?$/, 2 | SEC = 1e3, 3 | MIN = SEC * 60, 4 | HOUR = MIN * 60, 5 | DAY = HOUR * 24, 6 | YEAR = DAY * 365.25; 7 | 8 | export function parse(val) { 9 | var num, arr = val.toLowerCase().match(RGX); 10 | if (arr != null && (num = parseFloat(arr[1]))) { 11 | if (arr[3] != null) return num * SEC; 12 | if (arr[4] != null) return num * MIN; 13 | if (arr[5] != null) return num * HOUR; 14 | if (arr[6] != null) return num * DAY; 15 | if (arr[7] != null) return num * DAY * 7; 16 | if (arr[8] != null) return num * YEAR; 17 | return num; 18 | } 19 | } 20 | 21 | function fmt(val, pfx, str, long) { 22 | var num = (val | 0) === val ? val : ~~(val + 0.5); 23 | return pfx + num + (long ? (' ' + str + (num != 1 ? 's' : '')) : str[0]); 24 | } 25 | 26 | export function format(num, long) { 27 | var pfx = num < 0 ? '-' : '', abs = num < 0 ? -num : num; 28 | if (abs < SEC) return num + (long ? ' ms' : 'ms'); 29 | if (abs < MIN) return fmt(abs / SEC, pfx, 'second', long); 30 | if (abs < HOUR) return fmt(abs / MIN, pfx, 'minute', long); 31 | if (abs < DAY) return fmt(abs / HOUR, pfx, 'hour', long); 32 | if (abs < YEAR) return fmt(abs / DAY, pfx, 'day', long); 33 | return fmt(abs / YEAR, pfx, 'year', long); 34 | } 35 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Tests ported from zeit/ms 3 | * @see https://github.com/lukeed/zeit-ms/blob/master/tests.js 4 | */ 5 | 6 | import { test } from 'uvu'; 7 | import * as assert from 'uvu/assert'; 8 | import * as ms from '../src'; 9 | 10 | function parse(input, expect) { 11 | const output = ms.parse(input); 12 | assert.is(output, expect, `parse("${input}") ~> ${expect}`); 13 | assert.is(typeof output, typeof expect, `~> returns "${typeof expect}" type`); 14 | } 15 | 16 | function format(input, expect, long) { 17 | const output = ms.format(input, long); 18 | assert.is(output, expect, `format(${input}) ~> "${expect}"`); 19 | assert.is(typeof output, typeof expect, `~> returns "${typeof expect}" type`); 20 | } 21 | 22 | // --- 23 | 24 | test('exports an object', () => { 25 | assert.type(ms, 'object'); 26 | }); 27 | 28 | test('exports "parse" function', () => { 29 | assert.type(ms.parse, 'function'); 30 | }); 31 | 32 | test('exports "format" function', () => { 33 | assert.type(ms.format, 'function'); 34 | }); 35 | 36 | 37 | test('parse()', () => { 38 | parse('100', 100); 39 | parse('1m', 60000); 40 | parse('1h', 3600000); 41 | parse('2d', 172800000); 42 | parse('3w', 1814400000); 43 | parse('1s', 1000); 44 | parse('100ms', 100); 45 | parse('1.5h', 5400000); 46 | parse('1 s', 1000); 47 | 48 | parse('☃', undefined); 49 | parse('10-.5', undefined); 50 | 51 | parse('1.5H', 5400000); 52 | parse('.5ms', 0.5); 53 | parse('-100ms', -100); 54 | parse('-1.5h', -5400000); 55 | parse('-10.5h', -37800000); 56 | parse('-.5h', -1800000); 57 | 58 | parse('1.5y', 31557600000 * 1.5); 59 | }); 60 | 61 | 62 | test('parse() :: long', () => { 63 | parse('53 milliseconds', 53); 64 | parse('17 msecs', 17); 65 | parse('1 sec', 1000); 66 | parse('1 min', 60000); 67 | parse('1 hr', 3600000); 68 | parse('2 days', 172800000); 69 | parse('1.5 hours', 5400000); 70 | parse('-100 milliseconds', -100); 71 | parse('-1.5 hours', -5400000); 72 | parse('-.5 hr', -1800000); 73 | 74 | const YEAR = 31557600000; 75 | parse('1.5 years', YEAR * 1.5); 76 | parse('-12yr', YEAR * -12); 77 | parse('6 yrs', YEAR * 6); 78 | }); 79 | 80 | 81 | test('format()', () => { 82 | format(500, '500ms'); 83 | format(-500, '-500ms'); 84 | 85 | // seconds 86 | format(1000, '1s'); 87 | format(10000, '10s'); 88 | format(-10000, '-10s'); 89 | format(-1000, '-1s'); 90 | 91 | // minutes 92 | const MIN = 60 * 1000; 93 | format(MIN, '1m'); 94 | format(MIN * 10, '10m'); 95 | format(MIN * -10, '-10m'); 96 | format(MIN * -1, '-1m'); 97 | 98 | // hours 99 | const HOUR = 60 * MIN; 100 | format(HOUR, '1h'); 101 | format(HOUR * 10, '10h'); 102 | format(HOUR * -10, '-10h'); 103 | format(HOUR * -1, '-1h'); 104 | 105 | // days 106 | const DAY = HOUR * 24; 107 | format(DAY, '1d'); 108 | format(DAY * 10, '10d'); 109 | format(DAY * -10, '-10d'); 110 | format(DAY * -1, '-1d'); 111 | 112 | // years 113 | const YEAR = 31557600000; // internal 114 | format(3.154e10, '365d'); // via Google 115 | format(YEAR, '1y'); 116 | format(YEAR * 10, '10y'); 117 | format(YEAR * -10, '-10y'); 118 | format(YEAR * -1, '-1y'); 119 | 120 | // rounding 121 | format(234234234, '3d'); 122 | format(-234234234, '-3d'); 123 | }); 124 | 125 | 126 | test('format() :: long', () => { 127 | // milliseconds 128 | format(500, '500 ms', true); 129 | format(-500, '-500 ms', true); 130 | 131 | // seconds 132 | format(1000, '1 second', true); 133 | format(1200, '1 second', true); 134 | format(10000, '10 seconds', true); 135 | format(-1000, '-1 second', true); 136 | format(-1200, '-1 second', true); 137 | format(-10000, '-10 seconds', true); 138 | 139 | // minutes 140 | format(60 * 1000, '1 minute', true); 141 | format(60 * 1200, '1 minute', true); 142 | format(60 * 10000, '10 minutes', true); 143 | format(-1 * 60 * 1000, '-1 minute', true); 144 | format(-1 * 60 * 1200, '-1 minute', true); 145 | format(-1 * 60 * 10000, '-10 minutes', true); 146 | 147 | // hours 148 | format(60 * 60 * 1000, '1 hour', true); 149 | format(60 * 60 * 1200, '1 hour', true); 150 | format(60 * 60 * 10000, '10 hours', true); 151 | format(-1 * 60 * 60 * 1000, '-1 hour', true); 152 | format(-1 * 60 * 60 * 1200, '-1 hour', true); 153 | format(-1 * 60 * 60 * 10000, '-10 hours', true); 154 | 155 | // days 156 | format(24 * 60 * 60 * 1000, '1 day', true); 157 | format(24 * 60 * 60 * 1200, '1 day', true); 158 | format(24 * 60 * 60 * 10000, '10 days', true); 159 | format(-1 * 24 * 60 * 60 * 1000, '-1 day', true); 160 | format(-1 * 24 * 60 * 60 * 1200, '-1 day', true); 161 | format(-1 * 24 * 60 * 60 * 10000, '-10 days', true); 162 | 163 | // years 164 | const YEAR = 31557600000; // internal 165 | format(3.154e10, '365 days', true); // via Google 166 | format(YEAR, '1 year', true); 167 | format(YEAR * 10, '10 years', true); 168 | format(YEAR * -10, '-10 years', true); 169 | format(YEAR * -1, '-1 year', true); 170 | 171 | // rounding 172 | format(234234234, '3 days', true); 173 | format(-234234234, '-3 days', true); 174 | }); 175 | 176 | 177 | test.run(); 178 | --------------------------------------------------------------------------------