chronokinesis
Advanced tools
Comparing version 3.0.0 to 3.1.0
var chronokinesis = (function (exports) { | ||
'use strict'; | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelper(o, allowArrayLike) { | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (!it) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
var F = function () {}; | ||
return { | ||
s: F, | ||
n: function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}, | ||
e: function (e) { | ||
throw e; | ||
}, | ||
f: F | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var normalCompletion = true, | ||
didErr = false, | ||
err; | ||
return { | ||
s: function () { | ||
it = it.call(o); | ||
}, | ||
n: function () { | ||
var step = it.next(); | ||
normalCompletion = step.done; | ||
return step; | ||
}, | ||
e: function (e) { | ||
didErr = true; | ||
err = e; | ||
}, | ||
f: function () { | ||
try { | ||
if (!normalCompletion && it.return != null) it.return(); | ||
} finally { | ||
if (didErr) throw err; | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -10,3 +84,2 @@ * Inspired by Time keeper - EEasy testing of time-dependent code. | ||
*/ | ||
var NativeDate = Date; | ||
@@ -42,3 +115,4 @@ var freezedAt = null; | ||
reset: reset, | ||
isKeepingTime: isKeepingTime | ||
isKeepingTime: isKeepingTime, | ||
timezone: timezone | ||
}; | ||
@@ -90,2 +164,46 @@ | ||
function timezone(timeZone) { | ||
var formatter = Intl.DateTimeFormat('UTC', { | ||
timeZone: timeZone, | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: false | ||
}); | ||
return { | ||
timeZone: timeZone, | ||
defrost: defrost, | ||
reset: reset, | ||
isKeepingTime: isKeepingTime, | ||
getUTCOffset: getUTCOffset.bind(this, formatter), | ||
freeze: freezeInTimezone, | ||
travel: travelInTimezone | ||
}; | ||
function freezeInTimezone() { | ||
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
args[_key4] = arguments[_key4]; | ||
} | ||
if (isKeepingTime() && !args.length) return freeze(); | ||
var realDate = instantiate(NativeDate, args); | ||
var offset = getUTCOffset(formatter, realDate); | ||
return freeze(new NativeDate(realDate.getTime() + offset)); | ||
} | ||
function travelInTimezone() { | ||
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
args[_key5] = arguments[_key5]; | ||
} | ||
if (isKeepingTime() && !args.length) return travel(); | ||
var realDate = instantiate(NativeDate, args); | ||
var offset = getUTCOffset(formatter, realDate); | ||
return travel(new NativeDate(realDate.getTime() + offset)); | ||
} | ||
} | ||
function useFakeDate() { | ||
@@ -108,2 +226,56 @@ Date = FakeDate; // eslint-disable-line no-global-assign | ||
} | ||
function getUTCOffset(formatter, dt) { | ||
if (!dt) dt = new Date(); | ||
var dtSeconds = new NativeDate(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds()); | ||
var tzDate = new NativeDate(toUTC(formatter, dtSeconds)); | ||
return tzDate.getTime() - dtSeconds.getTime(); | ||
} | ||
function toUTC(formatter, dt) { | ||
var year, month, day, hour, minute, second; | ||
var _iterator = _createForOfIteratorHelper(formatter.formatToParts(dt)), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var _step$value = _step.value, | ||
type = _step$value.type, | ||
value = _step$value.value; | ||
switch (type) { | ||
case 'year': | ||
year = parseInt(value); | ||
break; | ||
case 'month': | ||
month = parseInt(value) - 1; | ||
break; | ||
case 'day': | ||
day = parseInt(value); | ||
break; | ||
case 'hour': | ||
hour = parseInt(value) % 24; | ||
break; | ||
case 'minute': | ||
minute = parseInt(value); | ||
break; | ||
case 'second': | ||
second = parseInt(value); | ||
break; | ||
} | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
} | ||
return NativeDate.UTC(year, month, day, hour, minute, second); | ||
} | ||
var chronokinesis_1 = chronokinesis.freeze; | ||
@@ -114,4 +286,5 @@ var chronokinesis_2 = chronokinesis.defrost; | ||
var chronokinesis_5 = chronokinesis.isKeepingTime; | ||
var chronokinesis_6 = chronokinesis.timezone; | ||
exports.default = chronokinesis; | ||
exports['default'] = chronokinesis; | ||
exports.defrost = chronokinesis_2; | ||
@@ -121,2 +294,3 @@ exports.freeze = chronokinesis_1; | ||
exports.reset = chronokinesis_4; | ||
exports.timezone = chronokinesis_6; | ||
exports.travel = chronokinesis_3; | ||
@@ -123,0 +297,0 @@ |
@@ -0,1 +1,75 @@ | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelper(o, allowArrayLike) { | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (!it) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
var F = function () {}; | ||
return { | ||
s: F, | ||
n: function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}, | ||
e: function (e) { | ||
throw e; | ||
}, | ||
f: F | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var normalCompletion = true, | ||
didErr = false, | ||
err; | ||
return { | ||
s: function () { | ||
it = it.call(o); | ||
}, | ||
n: function () { | ||
var step = it.next(); | ||
normalCompletion = step.done; | ||
return step; | ||
}, | ||
e: function (e) { | ||
didErr = true; | ||
err = e; | ||
}, | ||
f: function () { | ||
try { | ||
if (!normalCompletion && it.return != null) it.return(); | ||
} finally { | ||
if (didErr) throw err; | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -7,3 +81,2 @@ * Inspired by Time keeper - EEasy testing of time-dependent code. | ||
*/ | ||
var NativeDate = Date; | ||
@@ -39,3 +112,4 @@ var freezedAt = null; | ||
reset: reset, | ||
isKeepingTime: isKeepingTime | ||
isKeepingTime: isKeepingTime, | ||
timezone: timezone | ||
}; | ||
@@ -87,2 +161,46 @@ | ||
function timezone(timeZone) { | ||
var formatter = Intl.DateTimeFormat('UTC', { | ||
timeZone: timeZone, | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: false | ||
}); | ||
return { | ||
timeZone: timeZone, | ||
defrost: defrost, | ||
reset: reset, | ||
isKeepingTime: isKeepingTime, | ||
getUTCOffset: getUTCOffset.bind(this, formatter), | ||
freeze: freezeInTimezone, | ||
travel: travelInTimezone | ||
}; | ||
function freezeInTimezone() { | ||
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
args[_key4] = arguments[_key4]; | ||
} | ||
if (isKeepingTime() && !args.length) return freeze(); | ||
var realDate = instantiate(NativeDate, args); | ||
var offset = getUTCOffset(formatter, realDate); | ||
return freeze(new NativeDate(realDate.getTime() + offset)); | ||
} | ||
function travelInTimezone() { | ||
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
args[_key5] = arguments[_key5]; | ||
} | ||
if (isKeepingTime() && !args.length) return travel(); | ||
var realDate = instantiate(NativeDate, args); | ||
var offset = getUTCOffset(formatter, realDate); | ||
return travel(new NativeDate(realDate.getTime() + offset)); | ||
} | ||
} | ||
function useFakeDate() { | ||
@@ -105,2 +223,56 @@ Date = FakeDate; // eslint-disable-line no-global-assign | ||
} | ||
function getUTCOffset(formatter, dt) { | ||
if (!dt) dt = new Date(); | ||
var dtSeconds = new NativeDate(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds()); | ||
var tzDate = new NativeDate(toUTC(formatter, dtSeconds)); | ||
return tzDate.getTime() - dtSeconds.getTime(); | ||
} | ||
function toUTC(formatter, dt) { | ||
var year, month, day, hour, minute, second; | ||
var _iterator = _createForOfIteratorHelper(formatter.formatToParts(dt)), | ||
_step; | ||
try { | ||
for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
var _step$value = _step.value, | ||
type = _step$value.type, | ||
value = _step$value.value; | ||
switch (type) { | ||
case 'year': | ||
year = parseInt(value); | ||
break; | ||
case 'month': | ||
month = parseInt(value) - 1; | ||
break; | ||
case 'day': | ||
day = parseInt(value); | ||
break; | ||
case 'hour': | ||
hour = parseInt(value) % 24; | ||
break; | ||
case 'minute': | ||
minute = parseInt(value); | ||
break; | ||
case 'second': | ||
second = parseInt(value); | ||
break; | ||
} | ||
} | ||
} catch (err) { | ||
_iterator.e(err); | ||
} finally { | ||
_iterator.f(); | ||
} | ||
return NativeDate.UTC(year, month, day, hour, minute, second); | ||
} | ||
var chronokinesis_1 = chronokinesis.freeze; | ||
@@ -111,4 +283,4 @@ var chronokinesis_2 = chronokinesis.defrost; | ||
var chronokinesis_5 = chronokinesis.isKeepingTime; | ||
var chronokinesis_6 = chronokinesis.timezone; | ||
export default chronokinesis; | ||
export { chronokinesis_2 as defrost, chronokinesis_1 as freeze, chronokinesis_5 as isKeepingTime, chronokinesis_4 as reset, chronokinesis_3 as travel }; | ||
export { chronokinesis as default, chronokinesis_2 as defrost, chronokinesis_1 as freeze, chronokinesis_5 as isKeepingTime, chronokinesis_4 as reset, chronokinesis_6 as timezone, chronokinesis_3 as travel }; |
78
index.js
@@ -40,2 +40,3 @@ /** | ||
isKeepingTime, | ||
timezone, | ||
}; | ||
@@ -79,2 +80,42 @@ | ||
function timezone(timeZone) { | ||
const formatter = Intl.DateTimeFormat('UTC', { | ||
timeZone: timeZone, | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: false, | ||
}); | ||
return { | ||
timeZone, | ||
defrost, | ||
reset, | ||
isKeepingTime, | ||
getUTCOffset: getUTCOffset.bind(this, formatter), | ||
freeze: freezeInTimezone, | ||
travel: travelInTimezone, | ||
}; | ||
function freezeInTimezone(...args) { | ||
if (isKeepingTime() && !args.length) return freeze(); | ||
const realDate = instantiate(NativeDate, args); | ||
const offset = getUTCOffset(formatter, realDate); | ||
return freeze(new NativeDate(realDate.getTime() + offset)); | ||
} | ||
function travelInTimezone(...args) { | ||
if (isKeepingTime() && !args.length) return travel(); | ||
const realDate = instantiate(NativeDate, args); | ||
const offset = getUTCOffset(formatter, realDate); | ||
return travel(new NativeDate(realDate.getTime() + offset)); | ||
} | ||
} | ||
function useFakeDate() { | ||
@@ -97,1 +138,38 @@ Date = FakeDate; // eslint-disable-line no-global-assign | ||
} | ||
function getUTCOffset(formatter, dt) { | ||
if (!dt) dt = new Date(); | ||
const dtSeconds = new NativeDate(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds()); | ||
const tzDate = new NativeDate(toUTC(formatter, dtSeconds)); | ||
return tzDate.getTime() - dtSeconds.getTime(); | ||
} | ||
function toUTC(formatter, dt) { | ||
let year, month, day, hour, minute, second; | ||
for (const {type, value} of formatter.formatToParts(dt)) { | ||
switch (type) { | ||
case 'year': | ||
year = parseInt(value); | ||
break; | ||
case 'month': | ||
month = parseInt(value) - 1; | ||
break; | ||
case 'day': | ||
day = parseInt(value); | ||
break; | ||
case 'hour': | ||
hour = parseInt(value) % 24; | ||
break; | ||
case 'minute': | ||
minute = parseInt(value); | ||
break; | ||
case 'second': | ||
second = parseInt(value); | ||
break; | ||
} | ||
} | ||
return NativeDate.UTC(year, month, day, hour, minute, second); | ||
} |
{ | ||
"name": "chronokinesis", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Module for testing time-dependent code", | ||
@@ -23,2 +23,3 @@ "author": { | ||
"test": "lab -vcL", | ||
"posttest": "npm run prepare", | ||
"test:lcov": "npm t -- -r lcov -o coverage.lcov", | ||
@@ -40,2 +41,3 @@ "prepare": "npm run toc && rollup -c" | ||
"time", | ||
"timezone", | ||
"fake", | ||
@@ -59,3 +61,3 @@ "mock", | ||
"moment": "^2.29.1", | ||
"rollup": "^2.33.3", | ||
"rollup": "^2.56.3", | ||
"rollup-plugin-babel": "^4.4.0", | ||
@@ -62,0 +64,0 @@ "rollup-plugin-commonjs": "^10.1.0" |
@@ -16,2 +16,8 @@ chronokinesis | ||
- [`isKeepingTime()`](#iskeepingtime) | ||
- [`timezone(timeZone)`](#timezonetimezone) | ||
- [timezone `freeze([...args])`](#timezone-freezeargs) | ||
- [timezone `travel([...args])`](#timezone-travelargs) | ||
- [timezone `reset()`](#timezone-reset) | ||
- [timezone `defrost()`](#timezone-defrost) | ||
- [timezone `getUTCOffset([date])`](#timezone-getutcoffsetdate) | ||
- [Distributions](#distributions) | ||
@@ -26,3 +32,3 @@ - [Browser](#browser) | ||
Mock `Date` and `Date.now` in order to help you test time-dependent code. Provides `travel` and `freeze` functionality for your Node.js tests. | ||
Mock `Date` and `Date.now` in order to help you test time-dependent code. Provides `travel`, `freeze`, and timezone functionality for your Node.js tests. | ||
@@ -167,2 +173,34 @@ ```javascript | ||
## `timezone(timeZone)` | ||
Freeze and travel in different time zones. | ||
```javascript | ||
const ck = require('chronokinesis'); | ||
const tz = ck.timezone('Asia/Shanghai'); | ||
tz.freeze(); | ||
``` | ||
### timezone `freeze([...args])` | ||
Freeze at the specific timezone. | ||
### timezone `travel([...args])` | ||
Start traveling in the specific timezone. | ||
### timezone `reset()` | ||
Same as [#reset](#reset) | ||
### timezone `defrost()` | ||
Same as [#defrost](#defrost) | ||
### timezone `getUTCOffset([date])` | ||
Returns offset from UTC in milliseconds | ||
# Distributions | ||
@@ -169,0 +207,0 @@ |
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
25783
617
218