chronokinesis 5.0.2 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -11
- package/dist/chronokinesis.cjs +34 -33
- package/dist/index.cjs +34 -33
- package/index.d.ts +12 -6
- package/index.js +33 -33
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -13,11 +13,12 @@ Mock time and date for traveling and freezing. Inspired and borrowed from [timek
|
|
|
13
13
|
- [`defrost()`](#defrost)
|
|
14
14
|
- [`reset()`](#reset)
|
|
15
15
|
- [`isKeepingTime()`](#iskeepingtime)
|
|
16
|
-
- [`timezone(timeZone)`](#timezonetimezone)
|
|
17
|
-
|
|
18
|
-
- [timezone
|
|
19
|
-
- [timezone
|
|
20
|
-
- [timezone
|
|
16
|
+
- [`timezone(timeZone[, ...args])`](#timezonetimezone-args)
|
|
17
|
+
- [`new TimeZoneTraveller(timeZone)`](#new-timezonetravellertimezone)
|
|
18
|
+
- [`timezone.freeze([...args])`](#timezonefreezeargs)
|
|
19
|
+
- [`timezone.travel([...args])`](#timezonetravelargs)
|
|
20
|
+
- [`timezone.reset()`](#timezonereset)
|
|
21
|
+
- [`timezone.defrost()`](#timezonedefrost)
|
|
21
22
|
- [Distributions](#distributions)
|
|
22
23
|
- [Nodejs require](#nodejs-require)
|
|
23
24
|
- [Browser (UMD)](#browser-umd)
|
|
@@ -167,31 +168,51 @@ ck.travel(1893448800000);
|
|
|
167
168
|
console.log(ck.isKeepingTime() ? 'Is' : 'Not', 'keeping time');
|
|
168
169
|
```
|
|
169
170
|
|
|
170
|
-
## `timezone(timeZone)`
|
|
171
|
+
## `timezone(timeZone[, ...args])`
|
|
171
172
|
|
|
172
|
-
|
|
173
|
+
Travel to time zone.
|
|
174
|
+
|
|
175
|
+
- `timeZone`: IANA time zone string
|
|
176
|
+
- `...args`: Optional travel to date arguments
|
|
177
|
+
|
|
178
|
+
Returns [`TimeZoneTraveller` api](#new-timezonetravellertimezone)
|
|
173
179
|
|
|
174
180
|
```javascript
|
|
175
181
|
import * as ck from 'chronokinesis';
|
|
176
182
|
|
|
177
183
|
const tz = ck.timezone('Asia/Shanghai');
|
|
178
184
|
|
|
185
|
+
// Now in Shanghai
|
|
186
|
+
console.log(new Date())
|
|
187
|
+
|
|
179
188
|
tz.freeze();
|
|
180
189
|
```
|
|
181
190
|
|
|
182
|
-
|
|
191
|
+
## `new TimeZoneTraveller(timeZone)`
|
|
192
|
+
|
|
193
|
+
Time zone traveller api.
|
|
194
|
+
|
|
195
|
+
```javascript
|
|
196
|
+
import {TimeZoneTraveller} from 'chronokinesis';
|
|
197
|
+
|
|
198
|
+
const timezone = new TimeZoneTraveller('Asia/Shanghai');
|
|
199
|
+
|
|
200
|
+
timezone.freeze();
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `timezone.freeze([...args])`
|
|
183
204
|
|
|
184
205
|
Freeze at the specific timezone.
|
|
185
206
|
|
|
186
|
-
### timezone
|
|
207
|
+
### `timezone.travel([...args])`
|
|
187
208
|
|
|
188
209
|
Start traveling in the specific timezone.
|
|
189
210
|
|
|
190
|
-
### timezone
|
|
211
|
+
### `timezone.reset()`
|
|
191
212
|
|
|
192
213
|
Same as [#reset](#reset)
|
|
193
214
|
|
|
194
|
-
### timezone
|
|
215
|
+
### `timezone.defrost()`
|
|
195
216
|
|
|
196
217
|
Same as [#defrost](#defrost)
|
|
197
218
|
|
package/dist/chronokinesis.cjs
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
const curr = nativeGetTimezoneOffset.call(this);
|
|
34
34
|
if (!iana) return curr;
|
|
35
35
|
|
|
36
|
-
const tz =
|
|
36
|
+
const tz = new TimeZoneTraveller(iana).getTime(this);
|
|
37
37
|
return Math.round((tz - this.getTime()) / 60000) + curr;
|
|
38
38
|
},
|
|
39
39
|
});
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
|
|
47
47
|
FakeDate.prototype = NativeDate.prototype;
|
|
48
48
|
|
|
49
|
-
FakeDate.now = function() {
|
|
49
|
+
FakeDate.now = function fakeNow() {
|
|
50
50
|
if (freezedAt) return freezedAt.getTime();
|
|
51
51
|
return time();
|
|
52
52
|
};
|
|
@@ -95,7 +95,8 @@
|
|
|
95
95
|
return Date === FakeDate;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
function
|
|
98
|
+
function TimeZoneTraveller(timeZone) {
|
|
99
|
+
this.timeZone = timeZone;
|
|
99
100
|
const options = {
|
|
100
101
|
year: 'numeric',
|
|
101
102
|
month: 'numeric',
|
|
@@ -105,44 +106,43 @@
|
|
|
105
106
|
second: 'numeric',
|
|
106
107
|
hour12: false,
|
|
107
108
|
};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
timeZone: timeZone,
|
|
109
|
+
this.localFormatter = Intl.DateTimeFormat('UTC', options);
|
|
110
|
+
this.timeZoneFormatter = Intl.DateTimeFormat('UTC', {
|
|
111
|
+
timeZone,
|
|
112
112
|
...options,
|
|
113
113
|
});
|
|
114
|
+
}
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
reset,
|
|
119
|
-
isKeepingTime,
|
|
120
|
-
getTime,
|
|
121
|
-
freeze: freezeInTimezone,
|
|
122
|
-
travel: travelInTimezone,
|
|
123
|
-
};
|
|
116
|
+
TimeZoneTraveller.prototype.defrost = defrost;
|
|
117
|
+
TimeZoneTraveller.prototype.reset = reset;
|
|
118
|
+
TimeZoneTraveller.prototype.isKeepingTime = isKeepingTime;
|
|
124
119
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return freeze(getTime(...args));
|
|
129
|
-
}
|
|
120
|
+
TimeZoneTraveller.prototype.getTime = function timeZoneGetTime(...args) {
|
|
121
|
+
const realDate = instantiate(Date, args);
|
|
122
|
+
const tz = new NativeDate(toUTC(this.timeZoneFormatter, realDate));
|
|
130
123
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
124
|
+
if (!args.length) return tz.getTime();
|
|
125
|
+
const currentTz = new NativeDate(toUTC(this.localFormatter, realDate));
|
|
126
|
+
|
|
127
|
+
return realDate.getTime() + currentTz.getTime() - tz.getTime();
|
|
128
|
+
};
|
|
136
129
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
130
|
+
TimeZoneTraveller.prototype.freeze = function freezeInTimezone(...args) {
|
|
131
|
+
if (!args.length && iana === this.timeZone) return freeze();
|
|
132
|
+
iana = this.timeZone;
|
|
133
|
+
return freeze(this.getTime(...args));
|
|
134
|
+
};
|
|
140
135
|
|
|
141
|
-
|
|
142
|
-
|
|
136
|
+
TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) {
|
|
137
|
+
if (!args.length && iana === this.timeZone) return travel();
|
|
138
|
+
iana = this.timeZone;
|
|
139
|
+
return travel(this.getTime(...args));
|
|
140
|
+
};
|
|
143
141
|
|
|
144
|
-
|
|
145
|
-
|
|
142
|
+
function timezone(timeZone, ...args) {
|
|
143
|
+
const tz = new TimeZoneTraveller(timeZone);
|
|
144
|
+
tz.travel(...args);
|
|
145
|
+
return tz;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
function useFakeDate() {
|
|
@@ -192,6 +192,7 @@
|
|
|
192
192
|
return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
+
exports.TimeZoneTraveller = TimeZoneTraveller;
|
|
195
196
|
exports.defrost = defrost;
|
|
196
197
|
exports.freeze = freeze;
|
|
197
198
|
exports.isKeepingTime = isKeepingTime;
|
package/dist/index.cjs
CHANGED
|
@@ -29,7 +29,7 @@ function FakeDate(...args) {
|
|
|
29
29
|
const curr = nativeGetTimezoneOffset.call(this);
|
|
30
30
|
if (!iana) return curr;
|
|
31
31
|
|
|
32
|
-
const tz =
|
|
32
|
+
const tz = new TimeZoneTraveller(iana).getTime(this);
|
|
33
33
|
return Math.round((tz - this.getTime()) / 60000) + curr;
|
|
34
34
|
},
|
|
35
35
|
});
|
|
@@ -42,7 +42,7 @@ FakeDate.parse = NativeDate.parse;
|
|
|
42
42
|
|
|
43
43
|
FakeDate.prototype = NativeDate.prototype;
|
|
44
44
|
|
|
45
|
-
FakeDate.now = function() {
|
|
45
|
+
FakeDate.now = function fakeNow() {
|
|
46
46
|
if (freezedAt) return freezedAt.getTime();
|
|
47
47
|
return time();
|
|
48
48
|
};
|
|
@@ -91,7 +91,8 @@ function isKeepingTime() {
|
|
|
91
91
|
return Date === FakeDate;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
function
|
|
94
|
+
function TimeZoneTraveller(timeZone) {
|
|
95
|
+
this.timeZone = timeZone;
|
|
95
96
|
const options = {
|
|
96
97
|
year: 'numeric',
|
|
97
98
|
month: 'numeric',
|
|
@@ -101,44 +102,43 @@ function timezone(timeZone) {
|
|
|
101
102
|
second: 'numeric',
|
|
102
103
|
hour12: false,
|
|
103
104
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
timeZone: timeZone,
|
|
105
|
+
this.localFormatter = Intl.DateTimeFormat('UTC', options);
|
|
106
|
+
this.timeZoneFormatter = Intl.DateTimeFormat('UTC', {
|
|
107
|
+
timeZone,
|
|
108
108
|
...options,
|
|
109
109
|
});
|
|
110
|
+
}
|
|
110
111
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
reset,
|
|
115
|
-
isKeepingTime,
|
|
116
|
-
getTime,
|
|
117
|
-
freeze: freezeInTimezone,
|
|
118
|
-
travel: travelInTimezone,
|
|
119
|
-
};
|
|
112
|
+
TimeZoneTraveller.prototype.defrost = defrost;
|
|
113
|
+
TimeZoneTraveller.prototype.reset = reset;
|
|
114
|
+
TimeZoneTraveller.prototype.isKeepingTime = isKeepingTime;
|
|
120
115
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return freeze(getTime(...args));
|
|
125
|
-
}
|
|
116
|
+
TimeZoneTraveller.prototype.getTime = function timeZoneGetTime(...args) {
|
|
117
|
+
const realDate = instantiate(Date, args);
|
|
118
|
+
const tz = new NativeDate(toUTC(this.timeZoneFormatter, realDate));
|
|
126
119
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
120
|
+
if (!args.length) return tz.getTime();
|
|
121
|
+
const currentTz = new NativeDate(toUTC(this.localFormatter, realDate));
|
|
122
|
+
|
|
123
|
+
return realDate.getTime() + currentTz.getTime() - tz.getTime();
|
|
124
|
+
};
|
|
132
125
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
126
|
+
TimeZoneTraveller.prototype.freeze = function freezeInTimezone(...args) {
|
|
127
|
+
if (!args.length && iana === this.timeZone) return freeze();
|
|
128
|
+
iana = this.timeZone;
|
|
129
|
+
return freeze(this.getTime(...args));
|
|
130
|
+
};
|
|
136
131
|
|
|
137
|
-
|
|
138
|
-
|
|
132
|
+
TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) {
|
|
133
|
+
if (!args.length && iana === this.timeZone) return travel();
|
|
134
|
+
iana = this.timeZone;
|
|
135
|
+
return travel(this.getTime(...args));
|
|
136
|
+
};
|
|
139
137
|
|
|
140
|
-
|
|
141
|
-
|
|
138
|
+
function timezone(timeZone, ...args) {
|
|
139
|
+
const tz = new TimeZoneTraveller(timeZone);
|
|
140
|
+
tz.travel(...args);
|
|
141
|
+
return tz;
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
function useFakeDate() {
|
|
@@ -188,6 +188,7 @@ function toUTC(formatter, dt) {
|
|
|
188
188
|
return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
+
exports.TimeZoneTraveller = TimeZoneTraveller;
|
|
191
192
|
exports.defrost = defrost;
|
|
192
193
|
exports.freeze = freeze;
|
|
193
194
|
exports.isKeepingTime = isKeepingTime;
|
package/index.d.ts
CHANGED
|
@@ -25,8 +25,13 @@ export function isKeepingTime(): boolean;
|
|
|
25
25
|
/**
|
|
26
26
|
* Timezone traveling
|
|
27
27
|
*/
|
|
28
|
-
|
|
29
|
-
/**
|
|
28
|
+
export class TimeZoneTraveller {
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @param timeZone IANA time zone
|
|
32
|
+
*/
|
|
33
|
+
constructor(timeZone: string);
|
|
34
|
+
/** IANA time zone */
|
|
30
35
|
readonly timeZone: string;
|
|
31
36
|
defrost: typeof defrost;
|
|
32
37
|
reset: typeof reset;
|
|
@@ -38,11 +43,12 @@ interface TimezoneTraveling {
|
|
|
38
43
|
* @param args Same arguments as Date constructor.
|
|
39
44
|
* @returns number of milliseconds since the epoch (January 1, 1970, UTC)
|
|
40
45
|
*/
|
|
41
|
-
getTime
|
|
46
|
+
getTime(...args: any[]): number;
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @param timeZone
|
|
50
|
+
* Travel to time zone.
|
|
51
|
+
* @param timeZone IANA time zone
|
|
52
|
+
* @param args Optional travel to date arguments
|
|
47
53
|
*/
|
|
48
|
-
export function timezone(timeZone: string):
|
|
54
|
+
export function timezone(timeZone: string, ...args: any[]): TimeZoneTraveller;
|
package/index.js
CHANGED
|
@@ -27,7 +27,7 @@ function FakeDate(...args) {
|
|
|
27
27
|
const curr = nativeGetTimezoneOffset.call(this);
|
|
28
28
|
if (!iana) return curr;
|
|
29
29
|
|
|
30
|
-
const tz =
|
|
30
|
+
const tz = new TimeZoneTraveller(iana).getTime(this);
|
|
31
31
|
return Math.round((tz - this.getTime()) / 60000) + curr;
|
|
32
32
|
},
|
|
33
33
|
});
|
|
@@ -40,7 +40,7 @@ FakeDate.parse = NativeDate.parse;
|
|
|
40
40
|
|
|
41
41
|
FakeDate.prototype = NativeDate.prototype;
|
|
42
42
|
|
|
43
|
-
FakeDate.now = function() {
|
|
43
|
+
FakeDate.now = function fakeNow() {
|
|
44
44
|
if (freezedAt) return freezedAt.getTime();
|
|
45
45
|
return time();
|
|
46
46
|
};
|
|
@@ -89,7 +89,8 @@ export function isKeepingTime() {
|
|
|
89
89
|
return Date === FakeDate;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
export function
|
|
92
|
+
export function TimeZoneTraveller(timeZone) {
|
|
93
|
+
this.timeZone = timeZone;
|
|
93
94
|
const options = {
|
|
94
95
|
year: 'numeric',
|
|
95
96
|
month: 'numeric',
|
|
@@ -99,44 +100,43 @@ export function timezone(timeZone) {
|
|
|
99
100
|
second: 'numeric',
|
|
100
101
|
hour12: false,
|
|
101
102
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
timeZone: timeZone,
|
|
103
|
+
this.localFormatter = Intl.DateTimeFormat('UTC', options);
|
|
104
|
+
this.timeZoneFormatter = Intl.DateTimeFormat('UTC', {
|
|
105
|
+
timeZone,
|
|
106
106
|
...options,
|
|
107
107
|
});
|
|
108
|
+
}
|
|
108
109
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
reset,
|
|
113
|
-
isKeepingTime,
|
|
114
|
-
getTime,
|
|
115
|
-
freeze: freezeInTimezone,
|
|
116
|
-
travel: travelInTimezone,
|
|
117
|
-
};
|
|
110
|
+
TimeZoneTraveller.prototype.defrost = defrost;
|
|
111
|
+
TimeZoneTraveller.prototype.reset = reset;
|
|
112
|
+
TimeZoneTraveller.prototype.isKeepingTime = isKeepingTime;
|
|
118
113
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return freeze(getTime(...args));
|
|
123
|
-
}
|
|
114
|
+
TimeZoneTraveller.prototype.getTime = function timeZoneGetTime(...args) {
|
|
115
|
+
const realDate = instantiate(Date, args);
|
|
116
|
+
const tz = new NativeDate(toUTC(this.timeZoneFormatter, realDate));
|
|
124
117
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
118
|
+
if (!args.length) return tz.getTime();
|
|
119
|
+
const currentTz = new NativeDate(toUTC(this.localFormatter, realDate));
|
|
120
|
+
|
|
121
|
+
return realDate.getTime() + currentTz.getTime() - tz.getTime();
|
|
122
|
+
};
|
|
130
123
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
TimeZoneTraveller.prototype.freeze = function freezeInTimezone(...args) {
|
|
125
|
+
if (!args.length && iana === this.timeZone) return freeze();
|
|
126
|
+
iana = this.timeZone;
|
|
127
|
+
return freeze(this.getTime(...args));
|
|
128
|
+
};
|
|
134
129
|
|
|
135
|
-
|
|
136
|
-
|
|
130
|
+
TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) {
|
|
131
|
+
if (!args.length && iana === this.timeZone) return travel();
|
|
132
|
+
iana = this.timeZone;
|
|
133
|
+
return travel(this.getTime(...args));
|
|
134
|
+
};
|
|
137
135
|
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
export function timezone(timeZone, ...args) {
|
|
137
|
+
const tz = new TimeZoneTraveller(timeZone);
|
|
138
|
+
tz.travel(...args);
|
|
139
|
+
return tz;
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
function useFakeDate() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chronokinesis",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Module for testing time-dependent code",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pål Edman",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"test": "mocha",
|
|
23
23
|
"lint": "eslint . --cache",
|
|
24
24
|
"test:lcov": "c8 -r lcov -r text mocha && npm run lint",
|
|
25
|
+
"cov:html": "c8 mocha -R dot && c8 report --reporter html",
|
|
25
26
|
"posttest": "npm run lint && npm run dist",
|
|
26
27
|
"prepack": "npm run dist",
|
|
27
28
|
"dist": "npm run toc && rollup -c"
|
|
@@ -48,14 +49,14 @@
|
|
|
48
49
|
"fake now"
|
|
49
50
|
],
|
|
50
51
|
"devDependencies": {
|
|
51
|
-
"@rollup/plugin-commonjs": "^
|
|
52
|
-
"c8": "^
|
|
52
|
+
"@rollup/plugin-commonjs": "^25.0.3",
|
|
53
|
+
"c8": "^8.0.1",
|
|
53
54
|
"chai": "^4.3.7",
|
|
54
|
-
"eslint": "^8.
|
|
55
|
+
"eslint": "^8.46.0",
|
|
55
56
|
"lodash.clonedeep": "^4.5.0",
|
|
56
57
|
"markdown-toc": "^1.2.0",
|
|
57
58
|
"mocha": "^10.1.0",
|
|
58
59
|
"moment": "^2.29.4",
|
|
59
|
-
"rollup": "^3.
|
|
60
|
+
"rollup": "^3.27.2"
|
|
60
61
|
}
|
|
61
62
|
}
|