clock-time
Advanced tools
Comparing version 2.2.4 to 2.3.0
37
index.js
@@ -1,13 +0,28 @@ | ||
module.exports = function() { | ||
let minutes = arguments[0]; | ||
let seconds = arguments[1]; | ||
if (arguments.length === 1) { | ||
// If only one arg is passed, it's in milliseconds and must be converted to | ||
// minutes and seconds | ||
const time = new Date(arguments[0]); | ||
minutes = time.getMinutes(); | ||
seconds = time.getSeconds(); | ||
function padSeconds(seconds) { | ||
return ('0' + seconds).slice(-2); | ||
} | ||
function toClockParts(ms, options = {}) { | ||
const total = Math.ceil(ms / 1000); | ||
const minutes = Math.floor(total / 60); | ||
const seconds = total - minutes * 60; | ||
return { | ||
total, | ||
minutes, | ||
seconds: options.noPad ? seconds : padSeconds(seconds) | ||
}; | ||
} | ||
const separator = ':'; | ||
function clockTime() { | ||
if (arguments.length > 1) { | ||
return arguments[0] + separator + padSeconds(arguments[1]); | ||
} | ||
return [minutes, ('0' + seconds).slice(-2)].join(':'); | ||
}; | ||
const clockParts = toClockParts(arguments[0]); | ||
return clockParts.minutes + separator + clockParts.seconds; | ||
} | ||
module.exports = clockTime; | ||
module.exports.toClockParts = toClockParts; | ||
module.exports.clockTime = clockTime; |
{ | ||
"name": "clock-time", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"description": "Formats milliseconds or minutes and seconds as clock time", | ||
@@ -22,6 +22,6 @@ "author": "Trevor Blades <tdblades@gmail.com>", | ||
"devDependencies": { | ||
"@knoword/eslint-config": "^6.11.0", | ||
"eslint": "^5.13.0", | ||
"jest": "^23.4.1" | ||
"@trevorblades/eslint-config": "^6.22.2", | ||
"eslint": "^6.0.1", | ||
"jest": "^24.8.0" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # Clock Time | ||
```shell | ||
$ npm install clock-time | ||
npm install clock-time | ||
``` | ||
@@ -30,1 +30,19 @@ | ||
``` | ||
### `toClockParts` | ||
Use the named export `toClockParts` to convert milliseconds to an object of minutes, zero-padded seconds, and total seconds. This can be useful when you want to use the parts of a clock time separately or in addition to the formatted string. | ||
`toClockParts` also accepts an object of options as a second argument. You can disable zero-padding on the returned `seconds` property by passing `{noPad: true}` to the function. | ||
```js | ||
import {toClockParts} from 'clock-time'; | ||
const { | ||
minutes, // 4 | ||
seconds, // 20 | ||
total // 260 | ||
} = toClockParts(260000); | ||
const formatted = `${minutes}:${seconds}`; // or clockTime(minutes, seconds) | ||
``` |
Sorry, the diff of this file is not supported yet
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
2636
24
47
4