@symbolic/lib
Advanced tools
Comparing version 1.0.38 to 1.0.39
@@ -252,19 +252,27 @@ 'use strict'; | ||
time.toLabel = (minutes) => { | ||
time.toLabel = (minutes, {format='short'}={}) => { | ||
minutes = Math.round(minutes) || 0; //HINT prevent invalid minutes values | ||
var scale = _$1.find(time.scales, scale => minutes <= scale.maxMinutes) || _$1.last(time.scales); | ||
var number = Math.round(minutes/scale.minutes * 10) / 10; | ||
var scale = _$1.find(time.scales, scale => { | ||
var skipToNextScale = Math.abs(minutes) === scale.skipToNextScaleMinutes; //i.e. 1d even though less than max 1.9999d | ||
var lessThanMax = Math.abs(minutes) <= scale.maxMinutes; | ||
return `${number}${scale.abbreviation}`; | ||
return !skipToNextScale && lessThanMax; | ||
}) || _$1.last(time.scales); | ||
var number = Math.round(minutes / scale.minutes * 10) / 10; | ||
var suffix = format === 'short' ? scale.abbreviation : ` ${scale.title}s`; | ||
return number === 0 ? '' : `${number}${suffix}`; | ||
}; | ||
time.toMinutes = (label) => { | ||
label = label || '0m'; | ||
label = label || ''; | ||
var number = parseFloat(label) || 0; | ||
var abbreviation = label.replace(`${number}`, ''); | ||
var scale = _$1.find(time.scales, {abbreviation}); | ||
var scale = _$1.find(time.scales, {abbreviation}) || time.scales[1]; //HINT default to hours if someone just types a number | ||
return scale && number ? (number * scale.minutes) : 0; | ||
return number * scale.minutes; | ||
}; | ||
@@ -271,0 +279,0 @@ |
{ | ||
"name": "@symbolic/lib", | ||
"version": "1.0.38", | ||
"version": "1.0.39", | ||
"description": "plain js utilities", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -14,21 +14,29 @@ import _ from 'lodash'; | ||
time.toLabel = (minutes) => { | ||
time.toLabel = (minutes, {format='short'}={}) => { | ||
minutes = Math.round(minutes) || 0; //HINT prevent invalid minutes values | ||
var scale = _.find(time.scales, scale => minutes <= scale.maxMinutes) || _.last(time.scales); | ||
var number = Math.round(minutes/scale.minutes * 10) / 10; | ||
var scale = _.find(time.scales, scale => { | ||
var skipToNextScale = Math.abs(minutes) === scale.skipToNextScaleMinutes; //i.e. 1d even though less than max 1.9999d | ||
var lessThanMax = Math.abs(minutes) <= scale.maxMinutes; | ||
return `${number}${scale.abbreviation}`; | ||
return !skipToNextScale && lessThanMax; | ||
}) || _.last(time.scales); | ||
var number = Math.round(minutes / scale.minutes * 10) / 10; | ||
var suffix = format === 'short' ? scale.abbreviation : ` ${scale.title}s`; | ||
return number === 0 ? '' : `${number}${suffix}`; | ||
}; | ||
time.toMinutes = (label) => { | ||
label = label || '0m'; | ||
label = label || ''; | ||
var number = parseFloat(label) || 0; | ||
var abbreviation = label.replace(`${number}`, ''); | ||
var scale = _.find(time.scales, {abbreviation}); | ||
var scale = _.find(time.scales, {abbreviation}) || time.scales[1]; //HINT default to hours if someone just types a number | ||
return scale && number ? (number * scale.minutes) : 0; | ||
return number * scale.minutes; | ||
}; | ||
export default time; |
@@ -20,5 +20,5 @@ import time from './time'; | ||
expect(time.toMinutes('')).to.equal(0); | ||
expect(time.toMinutes('1')).to.equal(0); | ||
expect(time.toMinutes('1')).to.equal(1*60); | ||
expect(time.toMinutes('m')).to.equal(0); | ||
expect(time.toMinutes('12asdf')).to.equal(0); | ||
expect(time.toMinutes('12asdf')).to.equal(12*60); | ||
}); | ||
@@ -29,3 +29,3 @@ }); | ||
it('should properly format various minutes', () => { | ||
expect(time.toLabel(0)).to.equal('0m'); | ||
expect(time.toLabel(0)).to.equal(''); | ||
expect(time.toLabel(1)).to.equal('1m'); | ||
@@ -44,6 +44,10 @@ expect(time.toLabel(59)).to.equal('59m'); | ||
it('should properly handle invalid minutes', () => { | ||
expect(time.toLabel()).to.equal('0m'); | ||
expect(time.toLabel('asdf')).to.equal('0m'); | ||
expect(time.toLabel()).to.equal(''); | ||
expect(time.toLabel('asdf')).to.equal(''); | ||
}); | ||
it('should properly format', () => { | ||
expect(time.toLabel(5, {format: 'long'})).to.equal('5 minutes'); | ||
}); | ||
}); | ||
}); |
20332
550