@datatypes/duration
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -31,10 +31,10 @@ 'use strict'; | ||
var durationPattern = '^P' + '(?:(\\d+)Y)?' + // Years | ||
'(?:(\\d+)M)?' + // Months | ||
'(?:(\\d+)W)?' + // Weeks | ||
'(?:(\\d+)D)?' + // Days | ||
'T?' + '(?:(\\d+)H)?' + // Hours | ||
'(?:(\\d+)M)?' + // Minutes | ||
'(?:(\\d+)?' + // Seconds | ||
'\\.?(\\d+)?S)?' + // Milliseconds | ||
var durationPattern = '^P' + '(?:([0-9]+)Y)?' + // Years | ||
'(?:([0-9]+)M)?' + // Months | ||
'(?:([0-9]+)W)?' + // Weeks | ||
'(?:([0-9]+)D)?' + // Days | ||
'T?' + '(?:([0-9]+)H)?' + // Hours | ||
'(?:([0-9]+)M)?' + // Minutes | ||
'(?:([0-9]+)?' + // Seconds | ||
'(?:\\.([0-9]{1,3}))?S)?' + // Milliseconds | ||
'$'; | ||
@@ -47,2 +47,5 @@ | ||
// Milliseconds | ||
durationArray[8] = Number('0.' + durationArray[8]) * 1000; | ||
_fragments2.default.forEach(function (fragment, index) { | ||
@@ -52,4 +55,3 @@ var value = Number(durationArray[index + 1]); | ||
if (typeof value === 'number' && !Number.isNaN(value)) { | ||
_this._precision = fragment.replace(/s$/, ''); | ||
_this[fragment] = value; | ||
_this['_' + fragment] = value; | ||
} | ||
@@ -253,3 +255,17 @@ }); | ||
get: function get() { | ||
return this._precision; | ||
var _this2 = this; | ||
var precision = void 0; | ||
// Clone array as .reverse() is in place | ||
Array.from(_fragments2.default).reverse().some(function (fragment) { | ||
var value = _this2[fragment]; | ||
if (typeof value === 'number' && !Number.isNaN(value)) { | ||
precision = fragment.replace(/s$/, ''); | ||
return true; | ||
} | ||
}); | ||
return precision; | ||
}, | ||
@@ -280,24 +296,22 @@ set: function set(precision) { | ||
get: function get() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
return _fragments2.default.reduce(function (string, fragment) { | ||
if (_this2[fragment] == null) { | ||
return _fragments2.default.reduce(function (string, fragment, fragmentIndex) { | ||
if (typeof _this3[fragment] !== 'number' || Number.isNaN(_this3[fragment])) { | ||
return string; | ||
} | ||
if (fragment === 'minutes' && !string.includes('t')) { | ||
string += 't'; | ||
// fragmentIndex > 3 means smaller than day | ||
if (!string.includes('T') && fragmentIndex > 3) { | ||
string += 'T'; | ||
} | ||
string += _this2[fragment] + fragment.substr(0, 1); | ||
if (fragment === 'days') { | ||
string += 't'; | ||
} | ||
if (fragment === 'milliseconds') { | ||
string.replace('s', '.' + _this2[fragment] + 's'); | ||
string = string.replace(/s$/, '.' + _this3.milliseconds + 'S'); | ||
} else { | ||
string += _this3[fragment] + fragment.substr(0, 1); | ||
} | ||
return string; | ||
}, 'p').replace(/t$/, '').toUpperCase(); | ||
}, 'p').toUpperCase(); | ||
} | ||
@@ -307,6 +321,6 @@ }, { | ||
get: function get() { | ||
var _this3 = this; | ||
var _this4 = this; | ||
return _fragments2.default.reduce(function (object, fragment) { | ||
if (_this3[fragment] != null) object[fragment] = _this3[fragment]; | ||
if (_this4[fragment] != null) object[fragment] = _this4[fragment]; | ||
return object; | ||
@@ -313,0 +327,0 @@ }, { |
{ | ||
"name": "@datatypes/duration", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Full ISO 8601 compatible duration class", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -58,3 +58,3 @@ import runTest from 'ava' | ||
{ | ||
string: test.title, | ||
string: 'PT7H', | ||
hours: 7, | ||
@@ -83,3 +83,3 @@ isAccurate: true, | ||
const referenceObject = { | ||
string: 'P7HT30M', | ||
string: 'PT7H30M', | ||
hours: 7, | ||
@@ -98,3 +98,3 @@ minutes: 30, | ||
{ | ||
string: test.title, | ||
string: 'PT7S', | ||
seconds: 7, | ||
@@ -111,3 +111,3 @@ isAccurate: true, | ||
{ | ||
string: 'P7S345M', | ||
string: 'PT7.345S', | ||
seconds: 7, | ||
@@ -120,2 +120,16 @@ milliseconds: 345, | ||
runTest('P7.3S', test => { | ||
expect( | ||
new Duration(test.title).toObject(), | ||
'to equal', | ||
{ | ||
string: 'PT7.300S', | ||
seconds: 7, | ||
milliseconds: 300, | ||
isAccurate: true, | ||
} | ||
) | ||
}) | ||
runTest('P7Y5M', test => { | ||
@@ -122,0 +136,0 @@ expect( |
import runTest from 'ava' | ||
import expect from 'unexpected' | ||
import Duration from '../source/index.js' | ||
import Duration from '../source/index' | ||
@@ -10,2 +10,7 @@ runTest('precision of P7D is day', test => { | ||
runTest('precision of P5Y is year', test => { | ||
const duration = new Duration('P5Y') | ||
expect(duration.precision, 'to equal', 'year') | ||
}) | ||
runTest('precision of P7D0H is hour', test => { | ||
@@ -12,0 +17,0 @@ const duration = new Duration('P7D0H') |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
15475
11
526
1
32
0