date-duration
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -1,41 +0,34 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = createDuration; | ||
var _freeze = require('babel-runtime/core-js/object/freeze'); | ||
require("core-js/modules/es6.object.freeze"); | ||
var _freeze2 = _interopRequireDefault(_freeze); | ||
require("core-js/modules/es6.object.assign"); | ||
var _assign = require('babel-runtime/core-js/object/assign'); | ||
require("core-js/modules/es6.string.iterator"); | ||
var _assign2 = _interopRequireDefault(_assign); | ||
require("core-js/modules/es6.set"); | ||
var _set = require('babel-runtime/core-js/set'); | ||
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
var _set2 = _interopRequireDefault(_set); | ||
require("core-js/modules/es6.array.iterator"); | ||
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | ||
require("core-js/modules/es6.object.keys"); | ||
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | ||
require("core-js/modules/web.dom.iterable"); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
require("core-js/modules/es6.regexp.match"); | ||
var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); | ||
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
var _defineProperty3 = _interopRequireDefault(_defineProperty2); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
exports.default = createDuration; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var clone = function clone(value) { | ||
if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object' && typeof value.toDate === 'function') { | ||
if ((0, _typeof2.default)(value) === 'object' && typeof value.toDate === 'function') { | ||
return value.toDate(); | ||
@@ -48,5 +41,3 @@ } | ||
var dateParser = /P(\d+Y)?(\d+M)?(\d+W)?(\d+D)?/; | ||
var timeParser = /T(\d+H)?(\d+M)?(\d+S)?/; | ||
var methods = { | ||
@@ -66,6 +57,5 @@ Y: 'FullYear', | ||
return (iso.match(parser) || [undefined]).slice(1).filter(function (part) { | ||
return (/^[0-9]+[A-Z]$/i.test(part) | ||
); | ||
return /^[0-9]+[A-Z]$/i.test(part); | ||
}).map(function (part) { | ||
return (0, _defineProperty3.default)({}, part[part.length - 1], parseInt(part.slice(0, -1), 10)); | ||
return (0, _defineProperty2.default)({}, part[part.length - 1], parseInt(part.slice(0, -1), 10)); | ||
}); | ||
@@ -75,10 +65,9 @@ }; | ||
var applyParts = function applyParts(date, parts, methods, operator) { | ||
(0, _keys2.default)(parts).forEach(function (key) { | ||
Object.keys(parts).forEach(function (key) { | ||
if (key === 'T') { | ||
date = applyParts(date, parts.T, methods.T, operator); | ||
} else { | ||
date['set' + methods[key]](operator(date['get' + methods[key]](), key !== 'W' ? parts[key] : parts[key] * 7)); | ||
date["set".concat(methods[key])](operator(date["get".concat(methods[key])](), key !== 'W' ? parts[key] : parts[key] * 7)); | ||
} | ||
}); | ||
return date; | ||
@@ -88,6 +77,6 @@ }; | ||
var joinParts = function joinParts(parts, range) { | ||
return (0, _keys2.default)(parts || {}).filter(function (key) { | ||
return Object.keys(parts || {}).filter(function (key) { | ||
return range.indexOf(key) >= 0; | ||
}).map(function (key) { | ||
return '' + parts[key] + key; | ||
return "".concat(parts[key]).concat(key); | ||
}).join(''); | ||
@@ -97,3 +86,3 @@ }; | ||
var mergeParts = function mergeParts(left, right, operator) { | ||
return [].concat((0, _toConsumableArray3.default)(new _set2.default((0, _keys2.default)(left).concat((0, _keys2.default)(right))))).reduce(function (result, key) { | ||
return (0, _toConsumableArray2.default)(new Set(Object.keys(left).concat(Object.keys(right)))).reduce(function (result, key) { | ||
if (key === 'T') { | ||
@@ -108,6 +97,12 @@ result[key] = mergeParts(left[key], right[key], operator); | ||
}; | ||
/** | ||
* @constructor | ||
* @param {string|object} iso Duration as a string (ISO 8601 notation) or object | ||
* @return {object} Duration | ||
*/ | ||
function createDuration(iso) { | ||
if (!iso) { | ||
throw new Error('Invalid duration: no input'); | ||
throw new Error("Invalid duration: no input"); | ||
} | ||
@@ -118,22 +113,25 @@ | ||
if (typeof iso === 'string') { | ||
parts = _assign2.default.apply(Object, [{}].concat((0, _toConsumableArray3.default)(parseIso(dateParser, iso)))); | ||
parts = Object.assign.apply(Object, [{}].concat((0, _toConsumableArray2.default)(parseIso(dateParser, iso)))); | ||
var timeParts = parseIso(timeParser, iso); | ||
if (timeParts.length > 0) { | ||
(0, _assign2.default)(parts, { T: _assign2.default.apply(Object, [{}].concat((0, _toConsumableArray3.default)(timeParts))) }); | ||
Object.assign(parts, { | ||
T: Object.assign.apply(Object, [{}].concat((0, _toConsumableArray2.default)(timeParts))) | ||
}); | ||
} | ||
if ((0, _keys2.default)(parts) <= 0) { | ||
throw new Error('Invalid duration: invalid ISO format (' + iso + ')'); | ||
if (Object.keys(parts) <= 0) { | ||
throw new Error("Invalid duration: invalid ISO format (".concat(iso, ")")); | ||
} | ||
} else if ('P' in iso) { | ||
parts = (0, _assign2.default)({}, iso.P); | ||
parts = Object.assign({}, iso.P); | ||
} else { | ||
throw new Error('Invalid duration: invalid input (' + iso + ')'); | ||
throw new Error("Invalid duration: invalid input (".concat(iso, ")")); | ||
} | ||
return (0, _freeze2.default)((0, _assign2.default)({ P: parts }, { | ||
return Object.freeze(Object.assign({ | ||
P: parts | ||
}, { | ||
toString: function toString() { | ||
return 'P' + joinParts(parts, ['Y', 'M', 'W', 'D']) + (parts.T ? 'T' + joinParts(parts.T, ['H', 'M', 'S']) : ''); | ||
return "P".concat(joinParts(parts, ['Y', 'M', 'W', 'D'])).concat(parts.T ? "T".concat(joinParts(parts.T, ['H', 'M', 'S'])) : ''); | ||
}, | ||
@@ -151,5 +149,7 @@ addTo: function addTo(date) { | ||
add: function add(duration) { | ||
return createDuration({ P: mergeParts(parts, duration.P, function (left, right) { | ||
return createDuration({ | ||
P: mergeParts(parts, duration.P, function (left, right) { | ||
return left + right; | ||
}) }); | ||
}) | ||
}); | ||
} | ||
@@ -156,0 +156,0 @@ })); |
{ | ||
"name": "date-duration", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Manipulate Date objects with ISO 8601-formatted durations", | ||
"main": "dist/duration.js", | ||
"module": "src/duration.js", | ||
"scripts": { | ||
"lint": "eslint src test", | ||
"build": "babel --source-maps -d dist src", | ||
"test-timezone1": "TZ=Europe/Brussels mocha --compilers js:babel-register test", | ||
"test-timezone2": "TZ=America/New_York mocha --compilers js:babel-register test", | ||
"test-timezone1": "export TZ=Europe/Brussels && mocha --require @babel/register", | ||
"test-timezone2": "export TZ=America/New_York && mocha --require @babel/register", | ||
"test": "npm run test-timezone1 && npm run test-timezone2", | ||
@@ -38,18 +39,24 @@ "clean": "rm -rf dist", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.26.0", | ||
"eslint": "^4.18.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.9.0", | ||
"eslint-plugin-node": "^6.0.0", | ||
"eslint-plugin-promise": "^3.6.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"mocha": "^3.5.3" | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.3.3", | ||
"@babel/plugin-transform-runtime": "^7.2.0", | ||
"@babel/preset-env": "^7.3.1", | ||
"@babel/register": "^7.0.0", | ||
"eslint": "^5.14.1", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"mocha": "^6.0.0" | ||
}, | ||
"dependencies": { | ||
"babel-runtime": "^6.26.0" | ||
} | ||
"@babel/runtime": "^7.3.1" | ||
}, | ||
"browserslist": [ | ||
"last 1 version", | ||
"> 1%", | ||
"maintained node versions", | ||
"not dead" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
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
16450
140
+ Added@babel/runtime@^7.3.1
+ Added@babel/runtime@7.26.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removedbabel-runtime@^6.26.0
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedregenerator-runtime@0.11.1(transitive)