@datatypes/duration
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -24,2 +26,8 @@ | ||
if (!durationString) { | ||
return this; | ||
} | ||
console.assert(typeof durationString === 'string', 'Type of argument must be "string" and not "' + (typeof durationString === 'undefined' ? 'undefined' : _typeof(durationString)) + '"'); | ||
var durationPattern = '^P' + '(?:(\\d+)Y)?' + // Years | ||
@@ -38,5 +46,3 @@ '(?:(\\d+)M)?' + // Months | ||
if (!durationArray) { | ||
throw new Error('"' + durationString + '" is an invalid duration string'); | ||
} | ||
console.assert(durationArray, '"' + durationString + '" is an invalid duration string'); | ||
@@ -47,3 +53,3 @@ _fragments2.default.forEach(function (fragment, index) { | ||
if (typeof value === 'number' && !Number.isNaN(value)) { | ||
_this._precision = fragment.replace('s', ''); | ||
_this._precision = fragment.replace(/s$/, ''); | ||
_this[fragment] = value; | ||
@@ -55,2 +61,43 @@ } | ||
_createClass(Duration, [{ | ||
key: 'setYears', | ||
value: function setYears(years) { | ||
this.years = years;return this; | ||
} | ||
}, { | ||
key: 'setMonths', | ||
value: function setMonths(months) { | ||
this.months = months;return this; | ||
} | ||
}, { | ||
key: 'setWeeks', | ||
value: function setWeeks(weeks) { | ||
this.weeks = weeks;return this; | ||
} | ||
}, { | ||
key: 'setDays', | ||
value: function setDays(days) { | ||
this.days = days;return this; | ||
} | ||
}, { | ||
key: 'setHours', | ||
value: function setHours(hours) { | ||
this.hours = hours;return this; | ||
} | ||
}, { | ||
key: 'setMinutes', | ||
value: function setMinutes(minutes) { | ||
this.minutes = minutes;return this; | ||
} | ||
}, { | ||
key: 'setSeconds', | ||
value: function setSeconds(seconds) { | ||
this.seconds = seconds;return this; | ||
} | ||
}, { | ||
key: 'setMilliseconds', | ||
value: function setMilliseconds(milliseconds) { | ||
this.milliseconds = milliseconds; | ||
return this; | ||
} | ||
}, { | ||
key: 'toString', | ||
@@ -61,19 +108,147 @@ value: function toString() { | ||
}, { | ||
key: 'toJSON', | ||
value: function toJSON() { | ||
return this.string; | ||
} | ||
}, { | ||
key: 'toObject', | ||
value: function toObject() { | ||
var _this2 = this; | ||
return this.object; | ||
} | ||
}, { | ||
key: 'normalize', | ||
value: function normalize() { | ||
// Let all values bubble up as high as possible without changing | ||
// the accuracy | ||
// e.g 70000 ms = 1 minute and 10 seconds | ||
// Millisecond, second, hour & minute are considered accurate units | ||
// by ignoring leap seconds | ||
return _fragments2.default.reduce(function (object, fragment) { | ||
if (_this2[fragment] != null) object[fragment] = _this2[fragment]; | ||
return object; | ||
}, { | ||
string: this.string | ||
}); | ||
if (this._milliseconds >= 1000) { | ||
this._seconds = this._seconds || 0; | ||
this._seconds += Math.floor(this._milliseconds / 1000); | ||
this._milliseconds = this._milliseconds % 1000; | ||
} | ||
if (this._seconds >= 60) { | ||
this._minutes = this._minutes || 0; | ||
this._minutes += Math.floor(this._seconds / 60); | ||
this._seconds = this._seconds % 60; | ||
} | ||
if (this._minutes >= 60) { | ||
this._hours = this._hours || 0; | ||
this._hours += Math.floor(this._minutes / 60); | ||
this._minutes = this._minutes % 60; | ||
} | ||
// 1 day has not always 24 hours (+- leap second), | ||
// 1 month has not always 31 days and 1 year has not always 365 days. | ||
// Therefore they can't bubble up | ||
// But 1 year always has 12 months | ||
if (this._months >= 12) { | ||
this._years = this._years || 0; | ||
this._years += Math.floor(this._months / 12); | ||
this._months = this._months % 12; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'toJSON', | ||
value: function toJSON() { | ||
return this.toObject(); | ||
key: 'unsafeNormalize', | ||
value: function unsafeNormalize() { | ||
// Minimizes error by using ordinal dates and therefore | ||
// convertig surplus months to days and converting days to years | ||
this._isAccurate = false; | ||
this.normalize(); | ||
// Assmues that 1 day has 24 hours and 1 month has 30 days | ||
if (this._hours >= 24) { | ||
this._days = this._days || 0; | ||
this._days += Math.floor(this._hours / 24); | ||
this._hours = this._hours % 24; | ||
} | ||
if (this._months) { | ||
this._days += this._months * 30; | ||
delete this._months; | ||
} | ||
if (this._days >= 365) { | ||
this._years = this._years || 0; | ||
this._years += Math.floor(this._days / 365); | ||
this._days = this._days % 365; | ||
} | ||
return this; | ||
} | ||
}, { | ||
key: 'years', | ||
get: function get() { | ||
return this._years; | ||
}, | ||
set: function set(years) { | ||
this._years = years; | ||
} | ||
}, { | ||
key: 'months', | ||
get: function get() { | ||
return this._months; | ||
}, | ||
set: function set(months) { | ||
this._months = months; | ||
} | ||
}, { | ||
key: 'weeks', | ||
get: function get() { | ||
return this._weeks; | ||
}, | ||
set: function set(weeks) { | ||
this._weeks = weeks; | ||
} | ||
}, { | ||
key: 'days', | ||
get: function get() { | ||
return this._days; | ||
}, | ||
set: function set(days) { | ||
this._days = days; | ||
} | ||
}, { | ||
key: 'hours', | ||
get: function get() { | ||
return this._hours; | ||
}, | ||
set: function set(hours) { | ||
this._hours = hours; | ||
} | ||
}, { | ||
key: 'minutes', | ||
get: function get() { | ||
return this._minutes; | ||
}, | ||
set: function set(minutes) { | ||
this._minutes = minutes; | ||
} | ||
}, { | ||
key: 'seconds', | ||
get: function get() { | ||
return this._seconds; | ||
}, | ||
set: function set(seconds) { | ||
this._seconds = seconds; | ||
} | ||
}, { | ||
key: 'milliseconds', | ||
get: function get() { | ||
return this._milliseconds; | ||
}, | ||
set: function set(milliseconds) { | ||
this._milliseconds = milliseconds; | ||
} | ||
}, { | ||
key: 'precision', | ||
@@ -88,8 +263,25 @@ get: function get() { | ||
}, { | ||
key: 'isAccurate', | ||
get: function get() { | ||
if (this.hasOwnProperty('_isAccurate')) { | ||
return this._isAccurate; | ||
} else { | ||
// Millisecond, second, hour & minute are considered accurate units | ||
// by ignoring leap seconds (also check out this.normalize) | ||
return this.years == null && this.months == null && this.weeks == null && this.days == null; | ||
} | ||
}, | ||
set: function set(value) { | ||
if (value === undefined) { | ||
delete this._isAccurate; | ||
} | ||
this._isAccurate = value; | ||
} | ||
}, { | ||
key: 'string', | ||
get: function get() { | ||
var _this3 = this; | ||
var _this2 = this; | ||
return _fragments2.default.reduce(function (string, fragment) { | ||
if (_this3[fragment] == null) { | ||
if (_this2[fragment] == null) { | ||
return string; | ||
@@ -102,3 +294,3 @@ } | ||
string += _this3[fragment] + fragment.substr(0, 1); | ||
string += _this2[fragment] + fragment.substr(0, 1); | ||
@@ -109,3 +301,3 @@ if (fragment === 'days') { | ||
if (fragment === 'milliseconds') { | ||
string.replace('s', '.' + _this3[fragment] + 's'); | ||
string.replace('s', '.' + _this2[fragment] + 's'); | ||
} | ||
@@ -116,2 +308,15 @@ | ||
} | ||
}, { | ||
key: 'object', | ||
get: function get() { | ||
var _this3 = this; | ||
return _fragments2.default.reduce(function (object, fragment) { | ||
if (_this3[fragment] != null) object[fragment] = _this3[fragment]; | ||
return object; | ||
}, { | ||
string: this.string, | ||
isAccurate: this.isAccurate | ||
}); | ||
} | ||
}]); | ||
@@ -118,0 +323,0 @@ |
{ | ||
"name": "@datatypes/duration", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Full ISO 8601 compatible duration class", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
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
14221
10
500
1