chronokinesis
Advanced tools
Comparing version 5.0.2 to 6.0.0
@@ -28,4 +28,9 @@ /** | ||
*/ | ||
interface TimezoneTraveling { | ||
/** Timezone */ | ||
export class TimeZoneTraveller { | ||
/** | ||
* | ||
* @param timeZone IANA time zone | ||
*/ | ||
constructor(timeZone: string); | ||
/** IANA time zone */ | ||
readonly timeZone: string; | ||
@@ -42,9 +47,10 @@ defrost: typeof defrost; | ||
*/ | ||
getTime: (...args: any[]) => number; | ||
getTime(...args: any[]): number; | ||
} | ||
/** | ||
* Freeze and travel in different time zones. | ||
* @param timeZone | ||
* Travel to time zone. | ||
* @param timeZone IANA time zone | ||
* @param args Optional travel to date arguments | ||
*/ | ||
export function timezone(timeZone: string): TimezoneTraveling; | ||
export function timezone(timeZone: string, ...args: any[]): TimeZoneTraveller; |
66
index.js
@@ -30,3 +30,3 @@ /** | ||
const tz = timezone(iana).getTime(this); | ||
const tz = new TimeZoneTraveller(iana).getTime(this); | ||
return Math.round((tz - this.getTime()) / 60000) + curr; | ||
@@ -44,3 +44,3 @@ }, | ||
FakeDate.now = function() { | ||
FakeDate.now = function fakeNow() { | ||
if (freezedAt) return freezedAt.getTime(); | ||
@@ -94,3 +94,4 @@ return time(); | ||
export function timezone(timeZone) { | ||
export function TimeZoneTraveller(timeZone) { | ||
this.timeZone = timeZone; | ||
const options = { | ||
@@ -105,40 +106,39 @@ year: 'numeric', | ||
}; | ||
const current = Intl.DateTimeFormat('UTC', options); | ||
const formatter = Intl.DateTimeFormat('UTC', { | ||
timeZone: timeZone, | ||
this.localFormatter = Intl.DateTimeFormat('UTC', options); | ||
this.timeZoneFormatter = Intl.DateTimeFormat('UTC', { | ||
timeZone, | ||
...options, | ||
}); | ||
} | ||
return { | ||
timeZone, | ||
defrost, | ||
reset, | ||
isKeepingTime, | ||
getTime, | ||
freeze: freezeInTimezone, | ||
travel: travelInTimezone, | ||
}; | ||
TimeZoneTraveller.prototype.defrost = defrost; | ||
TimeZoneTraveller.prototype.reset = reset; | ||
TimeZoneTraveller.prototype.isKeepingTime = isKeepingTime; | ||
function freezeInTimezone(...args) { | ||
if (!args.length && iana === timeZone) return freeze(); | ||
iana = timeZone; | ||
return freeze(getTime(...args)); | ||
} | ||
TimeZoneTraveller.prototype.getTime = function timeZoneGetTime(...args) { | ||
const realDate = instantiate(Date, args); | ||
const tz = new NativeDate(toUTC(this.timeZoneFormatter, realDate)); | ||
function travelInTimezone(...args) { | ||
if (!args.length && iana === timeZone) return travel(); | ||
iana = timeZone; | ||
return travel(getTime(...args)); | ||
} | ||
if (!args.length) return tz.getTime(); | ||
const currentTz = new NativeDate(toUTC(this.localFormatter, realDate)); | ||
function getTime(...args) { | ||
const realDate = instantiate(Date, args); | ||
const tz = new NativeDate(toUTC(formatter, realDate)); | ||
return realDate.getTime() + currentTz.getTime() - tz.getTime(); | ||
}; | ||
if (!args.length) return tz.getTime(); | ||
const currentTz = new NativeDate(toUTC(current, realDate)); | ||
TimeZoneTraveller.prototype.freeze = function freezeInTimezone(...args) { | ||
if (!args.length && iana === this.timeZone) return freeze(); | ||
iana = this.timeZone; | ||
return freeze(this.getTime(...args)); | ||
}; | ||
return realDate.getTime() + currentTz.getTime() - tz.getTime(); | ||
} | ||
TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) { | ||
if (!args.length && iana === this.timeZone) return travel(); | ||
iana = this.timeZone; | ||
return travel(this.getTime(...args)); | ||
}; | ||
export function timezone(timeZone, ...args) { | ||
const tz = new TimeZoneTraveller(timeZone); | ||
tz.travel(...args); | ||
return tz; | ||
} | ||
@@ -145,0 +145,0 @@ |
{ | ||
"name": "chronokinesis", | ||
"version": "5.0.2", | ||
"version": "6.0.0", | ||
"description": "Module for testing time-dependent code", | ||
@@ -25,2 +25,3 @@ "author": { | ||
"test:lcov": "c8 -r lcov -r text mocha && npm run lint", | ||
"cov:html": "c8 mocha -R dot && c8 report --reporter html", | ||
"posttest": "npm run lint && npm run dist", | ||
@@ -52,6 +53,6 @@ "prepack": "npm run dist", | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"c8": "^7.12.0", | ||
"@rollup/plugin-commonjs": "^25.0.3", | ||
"c8": "^8.0.1", | ||
"chai": "^4.3.7", | ||
"eslint": "^8.29.0", | ||
"eslint": "^8.46.0", | ||
"lodash.clonedeep": "^4.5.0", | ||
@@ -61,4 +62,4 @@ "markdown-toc": "^1.2.0", | ||
"moment": "^2.29.4", | ||
"rollup": "^3.7.0" | ||
"rollup": "^3.27.2" | ||
} | ||
} |
@@ -16,7 +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(timeZone[, ...args])`](#timezonetimezone-args) | ||
- [`new TimeZoneTraveller(timeZone)`](#new-timezonetravellertimezone) | ||
- [`timezone.freeze([...args])`](#timezonefreezeargs) | ||
- [`timezone.travel([...args])`](#timezonetravelargs) | ||
- [`timezone.reset()`](#timezonereset) | ||
- [`timezone.defrost()`](#timezonedefrost) | ||
- [Distributions](#distributions) | ||
@@ -171,6 +172,11 @@ - [Nodejs require](#nodejs-require) | ||
## `timezone(timeZone)` | ||
## `timezone(timeZone[, ...args])` | ||
Freeze and travel in different time zones. | ||
Travel to time zone. | ||
- `timeZone`: IANA time zone string | ||
- `...args`: Optional travel to date arguments | ||
Returns [`TimeZoneTraveller` api](#new-timezonetravellertimezone) | ||
```javascript | ||
@@ -181,18 +187,33 @@ import * as ck from 'chronokinesis'; | ||
// Now in Shanghai | ||
console.log(new Date()) | ||
tz.freeze(); | ||
``` | ||
### timezone `freeze([...args])` | ||
## `new TimeZoneTraveller(timeZone)` | ||
Time zone traveller api. | ||
```javascript | ||
import {TimeZoneTraveller} from 'chronokinesis'; | ||
const timezone = new TimeZoneTraveller('Asia/Shanghai'); | ||
timezone.freeze(); | ||
``` | ||
### `timezone.freeze([...args])` | ||
Freeze at the specific timezone. | ||
### timezone `travel([...args])` | ||
### `timezone.travel([...args])` | ||
Start traveling in the specific timezone. | ||
### timezone `reset()` | ||
### `timezone.reset()` | ||
Same as [#reset](#reset) | ||
### timezone `defrost()` | ||
### `timezone.defrost()` | ||
@@ -199,0 +220,0 @@ Same as [#defrost](#defrost) |
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
23675
538
236