upgrade standard

This commit is contained in:
Michael Barrett 2019-08-24 10:02:47 +01:00
parent db9e0ec172
commit 58615f756d
4 changed files with 548 additions and 471 deletions

View File

@ -38,18 +38,18 @@ const UNIT_MAP = {
/** /**
* Parse a timestring * Parse a timestring
* *
* @param {String} string * @param {string} string
* @param {String} returnUnit * @param {string} returnUnit
* @param {Object} opts * @param {Object} opts
* @return {Number} * @returns {number}
*/ */
function parseTimestring (string, returnUnit, opts) { function parseTimestring (string, returnUnit, opts) {
opts = Object.assign({}, DEFAULT_OPTS, opts || {}) opts = Object.assign({}, DEFAULT_OPTS, opts || {})
let totalSeconds = 0 let totalSeconds = 0
let unitValues = getUnitValues(opts) const unitValues = getUnitValues(opts)
let groups = string const groups = string
.toLowerCase() .toLowerCase()
.replace(/[^.\w+-]+/g, '') .replace(/[^.\w+-]+/g, '')
.match(/[-+]?[0-9.]+[a-z]+/g) .match(/[-+]?[0-9.]+[a-z]+/g)
@ -59,8 +59,8 @@ function parseTimestring (string, returnUnit, opts) {
} }
groups.forEach(group => { groups.forEach(group => {
let value = group.match(/[0-9.]+/g)[0] const value = group.match(/[0-9.]+/g)[0]
let unit = group.match(/[a-z]+/g)[0] const unit = group.match(/[a-z]+/g)[0]
totalSeconds += getSeconds(value, unit, unitValues) totalSeconds += getSeconds(value, unit, unitValues)
}) })
@ -80,7 +80,7 @@ function parseTimestring (string, returnUnit, opts) {
*/ */
function getUnitValues (opts) { function getUnitValues (opts) {
let unitValues = { const unitValues = {
ms: 0.001, ms: 0.001,
s: 1, s: 1,
m: 60, m: 60,
@ -98,12 +98,12 @@ function getUnitValues (opts) {
/** /**
* Get the key for a unit * Get the key for a unit
* *
* @param {String} unit * @param {string} unit
* @returns {String} * @returns {string}
*/ */
function getUnitKey (unit) { function getUnitKey (unit) {
for (let key of Object.keys(UNIT_MAP)) { for (const key of Object.keys(UNIT_MAP)) {
if (UNIT_MAP[key].indexOf(unit) > -1) { if (UNIT_MAP[key].indexOf(unit) > -1) {
return key return key
} }
@ -115,10 +115,10 @@ function getUnitKey (unit) {
/** /**
* Get the number of seconds for a value, based on the unit * Get the number of seconds for a value, based on the unit
* *
* @param {Number} value * @param {number} value
* @param {String} unit * @param {string} unit
* @param {Object} unitValues * @param {Object} unitValues
* @returns {Number} * @returns {number}
*/ */
function getSeconds (value, unit, unitValues) { function getSeconds (value, unit, unitValues) {
@ -128,10 +128,10 @@ function getSeconds (value, unit, unitValues) {
/** /**
* Convert a value from its existing unit to a new unit * Convert a value from its existing unit to a new unit
* *
* @param {Number} value * @param {number} value
* @param {String} unit * @param {string} unit
* @param {Object} unitValues * @param {Object} unitValues
* @returns {Number} * @returns {number}
*/ */
function convert (value, unit, unitValues) { function convert (value, unit, unitValues) {

969
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -30,11 +30,11 @@
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.2.0", "chai": "^4.2.0",
"coveralls": "^3.0.3", "coveralls": "^3.0.6",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"mocha": "^6.1.4", "mocha": "^6.2.0",
"mocha-lcov-reporter": "1.3.0", "mocha-lcov-reporter": "1.3.0",
"standard": "^12.0.1", "standard": "^13.1.0",
"watch": "^1.0.2" "watch": "^1.0.2"
} }
} }

View File

@ -1,10 +1,8 @@
/* globals describe, it */ /* globals describe, it */
const chai = require('chai') const { expect } = require('chai')
const timestring = require('./index') const timestring = require('./index')
const expect = chai.expect
describe('timestring', () => { describe('timestring', () => {
it('can parse a timestring', () => { it('can parse a timestring', () => {
expect(timestring('500ms')).to.equal(0.5) expect(timestring('500ms')).to.equal(0.5)
@ -18,7 +16,7 @@ describe('timestring', () => {
}) })
it('can parse different unit identifiers', () => { it('can parse different unit identifiers', () => {
let unitMap = { const unitMap = {
ms: ['ms', 'milli', 'millisecond', 'milliseconds'], ms: ['ms', 'milli', 'millisecond', 'milliseconds'],
s: ['s', 'sec', 'secs', 'second', 'seconds'], s: ['s', 'sec', 'secs', 'second', 'seconds'],
m: ['m', 'min', 'mins', 'minute', 'minutes'], m: ['m', 'min', 'mins', 'minute', 'minutes'],
@ -69,7 +67,7 @@ describe('timestring', () => {
}) })
it('uses the passed options instead of the defaults', () => { it('uses the passed options instead of the defaults', () => {
let opts = { const opts = {
hoursPerDay: 1, hoursPerDay: 1,
daysPerWeek: 2, daysPerWeek: 2,
weeksPerMonth: 3, weeksPerMonth: 3,