format-duration
Advanced tools
Comparing version 1.4.0 to 2.0.0
@@ -6,2 +6,11 @@ # format-duration change log | ||
## 2.0.0 - 2022-03-16 | ||
### Breaking Changes | ||
- Drop support for node 8 & 10 (now only supporting node LTS: 12, 14, 16). | ||
### Misc | ||
- No dependencies! Code from parse-ms and add-zero has been consolidated for easier maintenance and smaller module footprint. | ||
- Repo ownership transfer (hypermodules -> ungoldman). Same maintainers, new URL. | ||
## 1.4.0 - 2021-08-04 | ||
@@ -8,0 +17,0 @@ |
53
index.js
@@ -1,10 +0,45 @@ | ||
var parseMs = require('parse-ms') | ||
var addZero = require('add-zero') | ||
// adapted from https://github.com/sindresorhus/parse-ms. | ||
// moved to internal function because parse-ms is now pure ESM. | ||
function parseMs (milliseconds) { | ||
if (typeof milliseconds !== 'number') { | ||
throw new TypeError('Expected a number') | ||
} | ||
module.exports = function (ms, options) { | ||
var leading = options && options.leading | ||
var unsignedMs = ms < 0 ? -ms : ms | ||
var sign = ms <= -1000 ? '-' : '' | ||
var t = parseMs(unsignedMs) | ||
var seconds = addZero(t.seconds) | ||
return { | ||
days: Math.trunc(milliseconds / 86400000), | ||
hours: Math.trunc(milliseconds / 3600000) % 24, | ||
minutes: Math.trunc(milliseconds / 60000) % 60, | ||
seconds: Math.trunc(milliseconds / 1000) % 60, | ||
milliseconds: Math.trunc(milliseconds) % 1000 | ||
} | ||
} | ||
// adapted from https://github.com/rafaelrinaldi/add-zero. | ||
// moved to internal function b/c addZero is unmaintained (7+ years). | ||
// stripped out negative sign logic since we're already doing it elsewhere. | ||
function addZero (value, digits) { | ||
digits = digits || 2 | ||
let str = value.toString() | ||
let size = 0 | ||
size = digits - str.length + 1 | ||
str = new Array(size).join('0').concat(str) | ||
return str | ||
} | ||
/** | ||
* Convert a number in milliseconds to a standard duration string. | ||
* @param {number} ms - duration in milliseconds | ||
* @param {object} options - formatDuration options object | ||
* @param {boolean} [options.leading=false] - add leading zero | ||
* @returns string - formatted duration string | ||
*/ | ||
function formatDuration (ms, options) { | ||
const leading = options && options.leading | ||
const unsignedMs = ms < 0 ? -ms : ms | ||
const sign = ms <= -1000 ? '-' : '' | ||
const t = parseMs(unsignedMs) | ||
const seconds = addZero(t.seconds) | ||
if (t.days) return sign + t.days + ':' + addZero(t.hours) + ':' + addZero(t.minutes) + ':' + seconds | ||
@@ -14,1 +49,3 @@ if (t.hours) return sign + (leading ? addZero(t.hours) : t.hours) + ':' + addZero(t.minutes) + ':' + seconds | ||
} | ||
module.exports = formatDuration |
{ | ||
"name": "format-duration", | ||
"description": "Convert a number in milliseconds to a standard duration string.", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"author": "Nate Goldman <ungoldman@gmail.com> (https://ungoldman.com/)", | ||
"bugs": { | ||
"url": "https://github.com/hypermodules/format-duration/issues" | ||
"url": "https://github.com/ungoldman/format-duration/issues" | ||
}, | ||
"dependencies": { | ||
"add-zero": "^1.0.0", | ||
"parse-ms": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"is-es5": "^1.0.0", | ||
"standard": "^11.0.1", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.0.0" | ||
"standard": "^16.0.4", | ||
"tape": "^5.5.2" | ||
}, | ||
"homepage": "https://github.com/hypermodules/format-duration", | ||
"homepage": "https://github.com/ungoldman/format-duration", | ||
"keywords": [ | ||
@@ -39,4 +33,4 @@ "display", | ||
"scripts": { | ||
"test": "standard && tape test/index.js | tap-spec" | ||
"test": "standard && tape test/index.js" | ||
} | ||
} |
# format-duration | ||
[![npm][1]][2] | ||
[![travis][3]][4] | ||
[![standard][5]][6] | ||
[![downloads][7]][2] | ||
Convert a number in milliseconds to a standard duration string. | ||
[1]: https://img.shields.io/npm/v/format-duration.svg?style=flat-square | ||
[2]: https://www.npmjs.com/package/format-duration | ||
[3]: https://img.shields.io/travis/hypermodules/format-duration/master.svg?style=flat-square | ||
[4]: https://travis-ci.org/hypermodules/format-duration | ||
[5]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square | ||
[6]: http://standardjs.com/ | ||
[7]: https://img.shields.io/npm/dm/format-duration.svg?style=flat-square | ||
[![npm][npm-image]][npm-url] | ||
[![build][build-image]][build-url] | ||
[![downloads][downloads-image]][npm-url] | ||
Convert a number in milliseconds to a standard duration string. | ||
[npm-image]: https://img.shields.io/npm/v/format-duration.svg | ||
[npm-url]: https://www.npmjs.com/package/format-duration | ||
[build-image]: https://github.com/ungoldman/format-duration/actions/workflows/tests.yml/badge.svg | ||
[build-url]: https://github.com/ungoldman/format-duration/actions/workflows/tests.yml | ||
[downloads-image]: https://img.shields.io/npm/dm/format-duration.svg | ||
@@ -27,3 +24,3 @@ ## Install | ||
```js | ||
var format = require('format-duration') | ||
const format = require('format-duration') | ||
@@ -30,0 +27,0 @@ // anything under a second is rounded down to zero |
@@ -1,7 +0,3 @@ | ||
var test = require('tape') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var isES5 = require('is-es5') | ||
var f = require('../') | ||
var src = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf-8') | ||
const test = require('tape') | ||
const f = require('../') | ||
@@ -49,6 +45,1 @@ test('it works', function (t) { | ||
}) | ||
test('is ES5', function (t) { | ||
t.equal(isES5(src), true, 'is ES5') | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13145
0
2
10
99
0
78
- Removedadd-zero@^1.0.0
- Removedparse-ms@^1.0.1
- Removedadd-zero@1.0.0(transitive)
- Removedparse-ms@1.0.1(transitive)