timezoned-date
Advanced tools
Comparing version 2.0.11 to 2.0.12
443
lib/index.js
@@ -0,29 +1,16 @@ | ||
/*jshint esversion: 6*/ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
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; }; }(); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var MILLISECONDS_PER_MINUTE = 60 * 1000, | ||
YYYY_MM_DD = /^\d\d\d\d(-\d\d){0,2}/, | ||
OFFSET_SUFFIX = /(((GMT)?[\+\-]\d\d:?\d\d)|Z)(\s*\(.+\))?$/, | ||
daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | ||
OriginalDate = global.Date; | ||
NativeDate = global.Date, | ||
nativeProto = NativeDate.prototype; | ||
function makeConstructor() { | ||
var _Object$definePropert; | ||
var boundOffset = arguments.length <= 0 || arguments[0] === undefined ? -new NativeDate().getTimezoneOffset() : arguments[0]; | ||
var boundOffset = arguments.length <= 0 || arguments[0] === undefined ? -new OriginalDate().getTimezoneOffset() : arguments[0]; | ||
// If the resulting constructor is "bound", | ||
@@ -37,165 +24,144 @@ // - it's signature is compatible with built-in Date, | ||
var TimezonedDate = function (_OriginalDate) { | ||
_inherits(TimezonedDate, _OriginalDate); | ||
var proto = Object.create(nativeProto); | ||
function TimezonedDate() { | ||
var _Object$getPrototypeO; | ||
_classCallCheck(this, TimezonedDate); | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
function TimezonedDate(_a1, _a2, _a3, _a4, _a5, _a6, _a7) { | ||
if (!(this instanceof TimezonedDate)) { | ||
// When Date() is called without new, it ignores its arguments and | ||
// returns same as new Date().toString() | ||
if (bound) { | ||
return new TimezonedDate().toString(); | ||
} | ||
return new TimezonedDate(_a1).toString(); | ||
} | ||
var args = Array.prototype.slice.call(arguments); | ||
var offset = bound ? boundOffset : args.pop(); | ||
if (!isOffset(offset)) { | ||
throw new TypeError('TimezonedDate requires an offset'); | ||
} | ||
var instance = new (Function.prototype.bind.apply(NativeDate, [null].concat(args)))(); | ||
Object.setPrototypeOf(instance, proto); | ||
instance.offset = function () { | ||
return offset; | ||
}; | ||
var offset = bound ? boundOffset : args.pop(); | ||
var inited = args.length === 0 || args.length === 1 && args[0] instanceof NativeDate || args.length === 1 && (typeof args[0] === 'number' || typeof args[0] === 'boolean') || args.length > 1 && isNaN(instance.getDate()); | ||
var _this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(TimezonedDate)).call.apply(_Object$getPrototypeO, [this].concat(args))); | ||
if (!inited && args.length > 1) { | ||
instance.setFullYear(args[0]); | ||
instance.setMonth(args[1]); | ||
instance.setDate(args[2] || 1); | ||
instance.setHours(args[3] || null); | ||
instance.setMinutes(args[4] || null); | ||
instance.setSeconds(args[5] || null); | ||
instance.setMilliseconds(args[6] || null); | ||
inited = true; | ||
} | ||
if (!isOffset(offset)) { | ||
throw new TypeError('TimezonedDate requires an offset'); | ||
if (!inited) { | ||
var string = args[0].toString(), | ||
date = new NativeDate(string), | ||
isYYYYmmdd = YYYY_MM_DD.test(string), | ||
isOffsetSpecified = OFFSET_SUFFIX.test(string), | ||
isLocal = !isYYYYmmdd && !isOffsetSpecified; | ||
if (isLocal) { | ||
applyOffset(date, -date.getTimezoneOffset() - offset); | ||
} | ||
_this.setTime(buildDate(args, offset)); | ||
_this.offset = function () { | ||
return offset; | ||
}; | ||
return _this; | ||
instance.setTime(date); | ||
} | ||
_createClass(TimezonedDate, [{ | ||
key: 'withOffset', | ||
value: function withOffset(offset) { | ||
return new ExportedTimezonedDate(this.getTime(), offset); | ||
return instance; | ||
} | ||
Object.setPrototypeOf(TimezonedDate, Date); | ||
var constructorPropertyDescriptors = makeMethodDescriptors({ | ||
UTC: NativeDate.UTC, | ||
parse: NativeDate.parse | ||
}); | ||
constructorPropertyDescriptors.prototype = { | ||
value: proto, | ||
writable: false, | ||
configurable: false | ||
}; | ||
Object.defineProperties(TimezonedDate, constructorPropertyDescriptors); | ||
var protoMethods = { | ||
constructor: TimezonedDate, | ||
withOffset: function withOffset(offset) { | ||
return new ExportedTimezonedDate(this.getTime(), offset); | ||
}, | ||
getTimezoneOffset: function getTimezoneOffset() { | ||
return -this.offset(); | ||
}, | ||
toDateString: function toDateString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid Date'; | ||
} | ||
}, { | ||
key: 'getTime', | ||
value: function getTime() { | ||
return this.date().getTime(); | ||
return [daysOfWeek[this.getDay()], months[this.getMonth()], addZero(this.getDate()), padYear(this.getFullYear())].join(' '); | ||
}, | ||
toTimeString: function toTimeString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid Date'; | ||
} | ||
}, { | ||
key: 'getTimezoneOffset', | ||
value: function getTimezoneOffset() { | ||
return -this.offset(); | ||
return [addZero(this.getHours()), ':', addZero(this.getMinutes()), ':', addZero(this.getSeconds()), ' ', formatOffset(this.offset())].join(''); | ||
}, | ||
toString: function toString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid Date'; | ||
} | ||
}, { | ||
key: 'toISOString', | ||
value: function toISOString() { | ||
if (typeof this.date !== 'function') { | ||
// This method is probably applied to an original Date instance | ||
return OriginalDate.prototype.toISOString.apply(this); | ||
} | ||
return this.date().toISOString(); | ||
return this.toDateString() + ' ' + this.toTimeString(); | ||
}, | ||
toUTCString: function toUTCString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid Date'; | ||
} | ||
}, { | ||
key: 'valueOf', | ||
value: function valueOf() { | ||
return this.getTime(); | ||
} | ||
}, { | ||
key: 'toDateString', | ||
value: function toDateString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid date'; | ||
} | ||
return [daysOfWeek[this.getDay()], months[this.getMonth()], addZero(this.getDate()), this.getFullYear()].join(' '); | ||
} | ||
}, { | ||
key: 'toTimeString', | ||
value: function toTimeString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid date'; | ||
} | ||
return [addZero(this.getHours()), ':', addZero(this.getMinutes()), ':', addZero(this.getSeconds()), ' ', formatOffset(this.offset())].join(''); | ||
} | ||
}, { | ||
key: 'toString', | ||
value: function toString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid date'; | ||
} | ||
return this.toDateString() + ' ' + this.toTimeString(); | ||
} | ||
}, { | ||
key: 'toUTCString', | ||
value: function toUTCString() { | ||
if (isNaN(this.getDate())) { | ||
return 'Invalid date'; | ||
} | ||
var gmtDate = new ExportedTimezonedDate(+this, 0); | ||
return [daysOfWeek[gmtDate.getDay()], ', ', months[gmtDate.getMonth()], ' ', addZero(gmtDate.getDate()), ' ', gmtDate.getFullYear(), ' ', addZero(gmtDate.getHours()), ':', addZero(gmtDate.getMinutes()), ':', addZero(gmtDate.getSeconds()), ' GMT'].join(''); | ||
} | ||
}, { | ||
key: 'getYear', | ||
value: function getYear() { | ||
return getLocalDate(this).getUTCFullYear() - 1900; | ||
} | ||
}, { | ||
key: 'setYear', | ||
value: function setYear(year) { | ||
return this.setFullYear(1900 + year); | ||
} | ||
}, { | ||
key: 'setTime', | ||
value: function setTime(date) { | ||
this.date = function () { | ||
return new OriginalDate(date); | ||
}; | ||
return this; | ||
} | ||
return [daysOfWeek[this.getUTCDay()], ', ', addZero(this.getUTCDate()), ' ', months[this.getUTCMonth()], ' ', this.getUTCFullYear(), ' ', addZero(this.getUTCHours()), ':', addZero(this.getUTCMinutes()), ':', addZero(this.getUTCSeconds()), ' GMT'].join(''); | ||
}, | ||
getYear: function getYear() { | ||
return getLocalDate(this).getUTCFullYear() - 1900; | ||
}, | ||
setYear: function setYear(year) { | ||
return this.setFullYear(1900 + year); | ||
}, | ||
// https://nodejs.org/api/util.html#util_custom_inspect_function_on_objects | ||
}, { | ||
key: 'inspect', | ||
value: function inspect() { | ||
return this.toString(); | ||
} | ||
}]); | ||
// https://nodejs.org/api/util.html#util_custom_inspect_function_on_objects | ||
inspect: function inspect() { | ||
return this.toString(); | ||
}, | ||
return TimezonedDate; | ||
}(OriginalDate); | ||
var proto = TimezonedDate.prototype; | ||
getTime: nativeProto.getTime, | ||
setTime: nativeProto.setTime, | ||
valueOf: nativeProto.valueOf, | ||
toLocaleString: nativeProto.toLocaleString, | ||
toLocaleDateString: nativeProto.toLocaleDateString, | ||
toLocaleTimeString: nativeProto.toLocaleTimeString | ||
}; | ||
function addGetters(property) { | ||
var getterName = 'get' + property, | ||
utcGetterName = 'getUTC' + property; | ||
Object.defineProperty(proto, getterName, { | ||
value: createFunction(function () { | ||
return getLocalDate(this)[utcGetterName](); | ||
}, 0, getterName), | ||
configurable: true, | ||
writable: true | ||
}); | ||
Object.defineProperty(proto, utcGetterName, { | ||
value: createFunction(function () { | ||
return this.date()[utcGetterName](); | ||
}, 0, utcGetterName), | ||
configurable: true, | ||
writable: true | ||
}); | ||
protoMethods[getterName] = createFunction(function () { | ||
return getLocalDate(this)[utcGetterName](); | ||
}, nativeProto[getterName].length, getterName); | ||
protoMethods[utcGetterName] = nativeProto[utcGetterName]; | ||
} | ||
function addSetters(property) { | ||
var length = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1]; | ||
var setterName = 'set' + property, | ||
utcSetterName = 'setUTC' + property; | ||
Object.defineProperty(proto, setterName, { | ||
value: createFunction(function () { | ||
var localDate = getLocalDate(this); | ||
localDate[utcSetterName].apply(localDate, arguments); | ||
return this.setTime(applyOffset(localDate, -this.offset())); | ||
}, length, setterName), | ||
configurable: true, | ||
writable: true | ||
}); | ||
Object.defineProperty(proto, utcSetterName, { | ||
value: createFunction(function () { | ||
var date = this.date(); | ||
date[utcSetterName].apply(date, arguments); | ||
return this.setTime(date); | ||
}, length, utcSetterName), | ||
configurable: true, | ||
writable: true | ||
}); | ||
protoMethods[setterName] = createFunction(function () { | ||
if (!(this instanceof TimezonedDate)) { | ||
throw new TypeError(); | ||
} | ||
var localDate = getLocalDate(this); | ||
nativeProto[utcSetterName].apply(localDate, arguments); | ||
return this.setTime(applyOffset(localDate, -this.offset())); | ||
}, nativeProto[setterName].length, setterName); | ||
protoMethods[utcSetterName] = nativeProto[utcSetterName]; | ||
} | ||
@@ -207,100 +173,32 @@ | ||
addGetters('FullYear'); | ||
addSetters('FullYear', 3); | ||
addSetters('FullYear'); | ||
addGetters('Hours'); | ||
addSetters('Hours', 4); | ||
addSetters('Hours'); | ||
addGetters('Milliseconds'); | ||
addSetters('Milliseconds'); | ||
addGetters('Minutes'); | ||
addSetters('Minutes', 3); | ||
addSetters('Minutes'); | ||
addGetters('Month'); | ||
addSetters('Month', 2); | ||
addSetters('Month'); | ||
addGetters('Seconds'); | ||
addSetters('Seconds', 2); | ||
addSetters('Seconds'); | ||
Object.defineProperties(proto, (_Object$definePropert = {}, _defineProperty(_Object$definePropert, Symbol.toStringTag, { | ||
value: 'Date' | ||
}), _defineProperty(_Object$definePropert, Symbol.toPrimitive, { | ||
value: OriginalDate.prototype[Symbol.toPrimitive], | ||
configurable: true | ||
}), _Object$definePropert)); | ||
var prototypePropertyDescriptors = makeMethodDescriptors(protoMethods); | ||
var ownPropsOfProto = ['toLocaleString', 'toLocaleDateString', 'toLocaleTimeString']; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = ownPropsOfProto[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var prop = _step.value; | ||
Object.defineProperty(proto, prop, { | ||
value: OriginalDate.prototype[prop], | ||
configurable: true, | ||
writable: true | ||
}); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
if (Symbol.toStringTag) { | ||
// Node v6+ or a polyfill | ||
prototypePropertyDescriptors[Symbol.toStringTag] = { | ||
value: 'Date' | ||
}; | ||
} | ||
if (Symbol.toPrimitive) { | ||
prototypePropertyDescriptors[Symbol.toPrimitive] = { | ||
value: nativeProto[Symbol.toPrimitive], | ||
configurable: true | ||
}; | ||
} | ||
var _TimezonedDate = function _TimezonedDate(_a1, _a2, _a3, _a4, _a5, _a6, _a7) { | ||
if (!(this instanceof _TimezonedDate)) { | ||
// When Date() is called without new, it ignores its arguments and | ||
// returns same as new Date().toString() | ||
if (bound) { | ||
return new TimezonedDate().toString(); | ||
} | ||
return new TimezonedDate(_a1).toString(); | ||
} | ||
return new (Function.prototype.bind.apply(TimezonedDate, [null].concat(Array.prototype.slice.call(arguments))))(); | ||
}; | ||
Object.defineProperty(_TimezonedDate, 'prototype', { | ||
value: proto, | ||
writable: false, | ||
configurable: false | ||
}); | ||
proto.constructor = _TimezonedDate; | ||
Object.setPrototypeOf(_TimezonedDate, TimezonedDate); | ||
var ownPropsOfCtor = ['UTC', 'parse']; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
Object.defineProperties(proto, prototypePropertyDescriptors); | ||
try { | ||
for (var _iterator2 = ownPropsOfCtor[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _prop = _step2.value; | ||
Object.defineProperty(_TimezonedDate, _prop, { | ||
value: OriginalDate[_prop], | ||
configurable: true, | ||
writable: true | ||
}); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
return _TimezonedDate; | ||
return TimezonedDate; | ||
} | ||
@@ -326,40 +224,5 @@ | ||
function getLocalDate(date) { | ||
return applyOffset(date.date(), date.offset()); | ||
return applyOffset(new NativeDate(date), date.offset()); | ||
} | ||
function buildDate(args, offset) { | ||
if (args.length === 0) { | ||
return new OriginalDate(); | ||
} | ||
if (args.length === 1 && args[0] instanceof OriginalDate) { | ||
return args[0]; | ||
} | ||
if (args.length === 1 && (typeof args[0] === 'number' || typeof args[0] === 'boolean')) { | ||
return new OriginalDate(args[0]); | ||
} | ||
if (args.length > 1) { | ||
args[3] = args[3] || null; | ||
args[4] = args[4] || null; | ||
args[5] = args[5] || null; | ||
args[6] = args[6] || null; | ||
date = new OriginalDate(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); | ||
return applyOffset(date, -date.getTimezoneOffset() - offset); | ||
} | ||
var string = args[0].toString(), | ||
date = new OriginalDate(string), | ||
isYYYYmmdd = /\d\d\d\d-\d\d-\d\d/.test(string), | ||
isOffsetSpecified = OFFSET_SUFFIX.test(string), | ||
isLocal = !isYYYYmmdd && !isOffsetSpecified; | ||
if (isLocal) { | ||
date = applyOffset(date, -date.getTimezoneOffset() - offset); | ||
} | ||
return date; | ||
} | ||
function formatOffset(offsetInMinutes) { | ||
@@ -373,2 +236,16 @@ var sign = offsetInMinutes >= 0 ? '+' : '-'; | ||
function makeMethodDescriptors(methods) { | ||
var result = {}; | ||
for (var key in methods) { | ||
if (methods.hasOwnProperty(key)) { | ||
result[key] = { | ||
value: methods[key], | ||
writable: true, | ||
configurable: true | ||
}; | ||
} | ||
} | ||
return result; | ||
} | ||
function createFunction(fn, length, name) { | ||
@@ -386,5 +263,9 @@ var argNames = []; | ||
function padYear(year) { | ||
var length = ('' + year).length; | ||
return (length < 4 ? ' '.slice(0, 4 - length) : '') + year; | ||
} | ||
var ExportedTimezonedDate = makeConstructor(false); | ||
ExportedTimezonedDate.makeConstructor = makeConstructor; | ||
exports.default = ExportedTimezonedDate; | ||
module.exports = exports['default']; | ||
module.exports = ExportedTimezonedDate; |
{ | ||
"name": "timezoned-date", | ||
"version": "2.0.11", | ||
"version": "2.0.12", | ||
"license": "Apache-2.0", | ||
@@ -27,3 +27,2 @@ "description": "Work with timezone-aware dates", | ||
"babel-eslint": "^6.0.4", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-preset-es2015": "^6.9.0", | ||
@@ -30,0 +29,0 @@ "eslint": "^2.10.2", |
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
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
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
6
6
17389
252
1