chronokinesis 6.0.0 → 7.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 CHANGED
@@ -1,5 +1,5 @@
1
- chronokinesis
2
- =============
1
+ # chronokinesis
2
+
3
3
  [![Build](https://github.com/paed01/chronokinesis/actions/workflows/build.yaml/badge.svg)](https://github.com/paed01/chronokinesis/actions/workflows/build.yaml) [![Coverage Status](https://coveralls.io/repos/github/paed01/chronokinesis/badge.svg?branch=master)](https://coveralls.io/github/paed01/chronokinesis?branch=master)
4
4
 
5
5
  Mock time and date for traveling and freezing. Inspired and borrowed from [timekeeper](https://github.com/vesln/timekeeper).
@@ -43,6 +43,35 @@ setTimeout(() => {
43
43
  }, 2000);
44
44
  ```
45
45
 
46
+ With arguments:
47
+
48
+ ```javascript
49
+ import * as ck from 'chronokinesis';
50
+ import assert from 'node:assert';
51
+
52
+ ck.freeze(1980, 0, 1);
53
+
54
+ assert.equal(true, ck.isKeepingTime());
55
+ assert.deepEqual(new Date(), new Date(1980, 0, 1));
56
+
57
+ ck.reset();
58
+ ```
59
+
60
+ or use with [`luxon`](https://moment.github.io/luxon):
61
+
62
+ ```javascript
63
+ import { DateTime } from 'luxon';
64
+ import * as ck from 'chronokinesis';
65
+
66
+ ck.travel(DateTime.now().plus({ year: 1 }).toMillis());
67
+
68
+ setTimeout(() => {
69
+ console.log('Traveled with Luxon DateTime one year and some', new Date());
70
+
71
+ ck.reset();
72
+ }, 2000);
73
+ ```
74
+
46
75
  or use with [`moment`](http://momentjs.com):
47
76
 
48
77
  ```javascript
@@ -52,8 +81,7 @@ import * as ck from 'chronokinesis';
52
81
  ck.travel(moment().add(1, 'year'));
53
82
 
54
83
  setTimeout(() => {
55
- // Date traveled one year and some
56
- console.log(new Date());
84
+ console.log('Traveled with Moment Date one year and some', new Date());
57
85
 
58
86
  ck.reset();
59
87
  }, 2000);
@@ -92,7 +120,7 @@ let date = new Date(2018, 0, 31);
92
120
 
93
121
  ck.travel(date);
94
122
 
95
- setTimeout(function() {
123
+ setTimeout(function () {
96
124
  console.log(new Date());
97
125
  ck.reset();
98
126
  }, 1500);
@@ -110,7 +138,7 @@ ck.freeze(date);
110
138
 
111
139
  ck.travel(moment().add(1, 'year'));
112
140
 
113
- setTimeout(function() {
141
+ setTimeout(function () {
114
142
  console.log(`Still frozen but one year ahead ${new Date()}`);
115
143
 
116
144
  ck.reset();
@@ -148,12 +176,12 @@ Resets Date to current glory.
148
176
  import * as ck from 'chronokinesis';
149
177
 
150
178
  ck.freeze(2060, 0, 1);
151
- console.log(`end of time is reached at ${new Date()} according to Newton`)
179
+ console.log(`end of time is reached at ${new Date()} according to Newton`);
152
180
 
153
181
  ck.reset();
154
182
 
155
183
  // Today
156
- console.log(new Date())
184
+ console.log(new Date());
157
185
  ```
158
186
 
159
187
  ## `isKeepingTime()`
@@ -166,6 +194,8 @@ import * as ck from 'chronokinesis';
166
194
  console.log(ck.isKeepingTime() ? 'Is' : 'Not', 'keeping time');
167
195
  ck.travel(1893448800000);
168
196
  console.log(ck.isKeepingTime() ? 'Is' : 'Not', 'keeping time');
197
+
198
+ ck.reset();
169
199
  ```
170
200
 
171
201
  ## `timezone(timeZone[, ...args])`
@@ -180,12 +210,15 @@ Returns [`TimeZoneTraveller` api](#new-timezonetravellertimezone)
180
210
  ```javascript
181
211
  import * as ck from 'chronokinesis';
182
212
 
213
+ ck.reset();
214
+
183
215
  const tz = ck.timezone('Asia/Shanghai');
184
216
 
185
- // Now in Shanghai
186
- console.log(new Date())
217
+ console.log('Now in Shanghai', new Date());
187
218
 
188
219
  tz.freeze();
220
+
221
+ ck.reset();
189
222
  ```
190
223
 
191
224
  ## `new TimeZoneTraveller(timeZone)`
@@ -193,11 +226,13 @@ tz.freeze();
193
226
  Time zone traveller api.
194
227
 
195
228
  ```javascript
196
- import {TimeZoneTraveller} from 'chronokinesis';
229
+ import { TimeZoneTraveller, reset } from 'chronokinesis';
197
230
 
198
231
  const timezone = new TimeZoneTraveller('Asia/Shanghai');
199
232
 
200
233
  timezone.freeze();
234
+
235
+ reset();
201
236
  ```
202
237
 
203
238
  ### `timezone.freeze([...args])`
@@ -10,7 +10,9 @@
10
10
  * Veselin Todorov <hi@vesln.com>
11
11
  * MIT License.
12
12
  */
13
- const NativeDate = Date;
13
+ const kNativeDate = Symbol.for('chronokinesis native date');
14
+
15
+ const NativeDate = Date[kNativeDate] || Date;
14
16
  const nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;
15
17
 
16
18
  let freezedAt = null;
@@ -41,6 +43,8 @@
41
43
  return dt;
42
44
  }
43
45
 
46
+ FakeDate[kNativeDate] = NativeDate;
47
+
44
48
  FakeDate.UTC = NativeDate.UTC;
45
49
  FakeDate.parse = NativeDate.parse;
46
50
 
@@ -146,11 +150,11 @@
146
150
  }
147
151
 
148
152
  function useFakeDate() {
149
- Date = FakeDate; // eslint-disable-line no-global-assign
153
+ Date = FakeDate;
150
154
  }
151
155
 
152
156
  function useNativeDate() {
153
- Date = NativeDate; // eslint-disable-line no-global-assign
157
+ Date = FakeDate[kNativeDate];
154
158
  }
155
159
 
156
160
  function time() {
@@ -166,7 +170,7 @@
166
170
  function toUTC(formatter, dt) {
167
171
  let year, month, day, hour, minute, second;
168
172
 
169
- for (const {type, value} of formatter.formatToParts(dt)) {
173
+ for (const { type, value } of formatter.formatToParts(dt)) {
170
174
  switch (type) {
171
175
  case 'year':
172
176
  year = parseInt(value);
@@ -192,6 +196,7 @@
192
196
  return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
193
197
  }
194
198
 
199
+ exports.FakeDate = FakeDate;
195
200
  exports.TimeZoneTraveller = TimeZoneTraveller;
196
201
  exports.defrost = defrost;
197
202
  exports.freeze = freeze;
package/dist/index.cjs CHANGED
@@ -6,7 +6,9 @@
6
6
  * Veselin Todorov <hi@vesln.com>
7
7
  * MIT License.
8
8
  */
9
- const NativeDate = Date;
9
+ const kNativeDate = Symbol.for('chronokinesis native date');
10
+
11
+ const NativeDate = Date[kNativeDate] || Date;
10
12
  const nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;
11
13
 
12
14
  let freezedAt = null;
@@ -37,6 +39,8 @@ function FakeDate(...args) {
37
39
  return dt;
38
40
  }
39
41
 
42
+ FakeDate[kNativeDate] = NativeDate;
43
+
40
44
  FakeDate.UTC = NativeDate.UTC;
41
45
  FakeDate.parse = NativeDate.parse;
42
46
 
@@ -142,11 +146,11 @@ function timezone(timeZone, ...args) {
142
146
  }
143
147
 
144
148
  function useFakeDate() {
145
- Date = FakeDate; // eslint-disable-line no-global-assign
149
+ Date = FakeDate;
146
150
  }
147
151
 
148
152
  function useNativeDate() {
149
- Date = NativeDate; // eslint-disable-line no-global-assign
153
+ Date = FakeDate[kNativeDate];
150
154
  }
151
155
 
152
156
  function time() {
@@ -162,7 +166,7 @@ function instantiate(type, args) {
162
166
  function toUTC(formatter, dt) {
163
167
  let year, month, day, hour, minute, second;
164
168
 
165
- for (const {type, value} of formatter.formatToParts(dt)) {
169
+ for (const { type, value } of formatter.formatToParts(dt)) {
166
170
  switch (type) {
167
171
  case 'year':
168
172
  year = parseInt(value);
@@ -188,6 +192,7 @@ function toUTC(formatter, dt) {
188
192
  return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
189
193
  }
190
194
 
195
+ exports.FakeDate = FakeDate;
191
196
  exports.TimeZoneTraveller = TimeZoneTraveller;
192
197
  exports.defrost = defrost;
193
198
  exports.freeze = freeze;
package/index.d.ts CHANGED
@@ -22,6 +22,11 @@ export function reset(): void;
22
22
  */
23
23
  export function isKeepingTime(): boolean;
24
24
 
25
+ /**
26
+ * Fake date
27
+ */
28
+ export class FakeDate extends Date {}
29
+
25
30
  /**
26
31
  * Timezone traveling
27
32
  */
package/index.js CHANGED
@@ -4,7 +4,9 @@
4
4
  * Veselin Todorov <hi@vesln.com>
5
5
  * MIT License.
6
6
  */
7
- const NativeDate = Date;
7
+ const kNativeDate = Symbol.for('chronokinesis native date');
8
+
9
+ const NativeDate = Date[kNativeDate] || Date;
8
10
  const nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;
9
11
 
10
12
  let freezedAt = null;
@@ -12,7 +14,7 @@ let traveledTo = null;
12
14
  let started = null;
13
15
  let iana = null;
14
16
 
15
- function FakeDate(...args) {
17
+ export function FakeDate(...args) {
16
18
  const length = args.length;
17
19
  if (!length) {
18
20
  if (freezedAt) args = [freezedAt];
@@ -35,6 +37,8 @@ function FakeDate(...args) {
35
37
  return dt;
36
38
  }
37
39
 
40
+ FakeDate[kNativeDate] = NativeDate;
41
+
38
42
  FakeDate.UTC = NativeDate.UTC;
39
43
  FakeDate.parse = NativeDate.parse;
40
44
 
@@ -140,11 +144,11 @@ export function timezone(timeZone, ...args) {
140
144
  }
141
145
 
142
146
  function useFakeDate() {
143
- Date = FakeDate; // eslint-disable-line no-global-assign
147
+ Date = FakeDate;
144
148
  }
145
149
 
146
150
  function useNativeDate() {
147
- Date = NativeDate; // eslint-disable-line no-global-assign
151
+ Date = FakeDate[kNativeDate];
148
152
  }
149
153
 
150
154
  function time() {
@@ -160,7 +164,7 @@ function instantiate(type, args) {
160
164
  function toUTC(formatter, dt) {
161
165
  let year, month, day, hour, minute, second;
162
166
 
163
- for (const {type, value} of formatter.formatToParts(dt)) {
167
+ for (const { type, value } of formatter.formatToParts(dt)) {
164
168
  switch (type) {
165
169
  case 'year':
166
170
  year = parseInt(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chronokinesis",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "description": "Module for testing time-dependent code",
5
5
  "author": {
6
6
  "name": "Pål Edman",
@@ -15,15 +15,20 @@
15
15
  "sideEffects": true,
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "git://github.com/paed01/chronokinesis"
18
+ "url": "git://github.com/paed01/chronokinesis.git"
19
+ },
20
+ "exports": {
21
+ "import": "./index.js",
22
+ "require": "./dist/index.cjs",
23
+ "types": "./index.d.ts"
19
24
  },
20
25
  "scripts": {
21
26
  "toc": "node generate-api-toc.cjs",
22
27
  "test": "mocha",
23
- "lint": "eslint . --cache",
28
+ "lint": "eslint . --cache && prettier . --check --cache",
24
29
  "test:lcov": "c8 -r lcov -r text mocha && npm run lint",
25
30
  "cov:html": "c8 mocha -R dot && c8 report --reporter html",
26
- "posttest": "npm run lint && npm run dist",
31
+ "posttest": "npm run lint && npm run dist && texample -g",
27
32
  "prepack": "npm run dist",
28
33
  "dist": "npm run toc && rollup -c"
29
34
  },
@@ -49,14 +54,17 @@
49
54
  "fake now"
50
55
  ],
51
56
  "devDependencies": {
52
- "@rollup/plugin-commonjs": "^25.0.3",
53
- "c8": "^8.0.1",
54
- "chai": "^4.3.7",
55
- "eslint": "^8.46.0",
57
+ "@rollup/plugin-commonjs": "^28.0.2",
58
+ "c8": "^10.1.2",
59
+ "chai": "^5.1.0",
60
+ "eslint": "^9.3.0",
56
61
  "lodash.clonedeep": "^4.5.0",
62
+ "luxon": "^3.7.1",
57
63
  "markdown-toc": "^1.2.0",
58
- "mocha": "^10.1.0",
59
- "moment": "^2.29.4",
60
- "rollup": "^3.27.2"
64
+ "mocha": "^11.0.1",
65
+ "moment": "^2.30.1",
66
+ "prettier": "^3.2.5",
67
+ "rollup": "^4.12.1",
68
+ "texample": "^0.0.8"
61
69
  }
62
70
  }