fake-current-time 0.1.1 → 0.2.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
@@ -87,3 +87,14 @@ export function TimeControl() {
87
87
  ```
88
88
 
89
89
  `setOffset` accepts: `years`, `months`, `days`, `hours`, `minutes`, `seconds`, `milliseconds`
90
+
91
+ ### Accessing the Original Date
92
+
93
+ If you need to access the real `Date` while time manipulation is active, use `OriginalDate`:
94
+
95
+ ```typescript
96
+ import { OriginalDate } from "fake-current-time";
97
+
98
+ const realNow = OriginalDate.now();
99
+ const realDate = new OriginalDate();
100
+ ```
package/dist/browser.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import {
2
2
  COOKIE_NAME,
3
3
  encodeOffset,
4
- parseOffsetFromCookie,
4
+ parseOffsetFromCookie
5
+ } from "./chunk-Z7IHRBMK.js";
6
+ import {
5
7
  setupDateProxy
6
- } from "./chunk-APIG4RGO.js";
8
+ } from "./chunk-XDOQVLFM.js";
7
9
 
8
10
  // src/browser.ts
9
11
  var initialized = false;
@@ -1,33 +1,12 @@
1
- // src/cookie.ts
2
- var COOKIE_NAME = "fake_current_time_offset";
3
- function parseOffsetFromCookie(cookieString) {
4
- if (!cookieString) {
5
- return void 0;
6
- }
7
- const cookies = cookieString.split(";");
8
- for (const cookie of cookies) {
9
- const [name, value] = cookie.trim().split("=");
10
- if (name === COOKIE_NAME && value) {
11
- return decodeOffset(value);
12
- }
13
- }
14
- return void 0;
15
- }
16
- function encodeOffset(offset) {
17
- return JSON.stringify(offset);
18
- }
19
- function decodeOffset(value) {
20
- return JSON.parse(value);
21
- }
22
-
23
1
  // src/core.ts
2
+ var OriginalDate = globalThis.Date;
24
3
  function setupDateProxy(getOffset) {
25
- const OriginalDate = globalThis.Date;
26
- const DateProxy = new Proxy(OriginalDate, {
4
+ const DateCtor = globalThis.Date;
5
+ const DateProxy = new Proxy(DateCtor, {
27
6
  construct(target, args, newTarget) {
28
7
  const offset = getOffset();
29
8
  if (offset && args.length === 0) {
30
- const t = fakeCurrentTime(OriginalDate, offset);
9
+ const t = fakeCurrentTime(DateCtor, offset);
31
10
  return Reflect.construct(target, [t], newTarget);
32
11
  }
33
12
  return Reflect.construct(target, args, newTarget);
@@ -36,7 +15,7 @@ function setupDateProxy(getOffset) {
36
15
  if (prop === "now") {
37
16
  const offset = getOffset();
38
17
  if (offset) {
39
- return () => fakeCurrentTime(OriginalDate, offset);
18
+ return () => fakeCurrentTime(DateCtor, offset);
40
19
  }
41
20
  }
42
21
  return Reflect.get(target, prop, receiver);
@@ -44,8 +23,8 @@ function setupDateProxy(getOffset) {
44
23
  apply(target, thisArg, args) {
45
24
  const offset = getOffset();
46
25
  if (offset && args.length === 0) {
47
- const t = fakeCurrentTime(OriginalDate, offset);
48
- return new OriginalDate(t).toString();
26
+ const t = fakeCurrentTime(DateCtor, offset);
27
+ return new DateCtor(t).toString();
49
28
  }
50
29
  return Reflect.apply(target, thisArg, args);
51
30
  }
@@ -79,8 +58,6 @@ function fakeCurrentTime(ctor, offset) {
79
58
  }
80
59
 
81
60
  export {
82
- COOKIE_NAME,
83
- parseOffsetFromCookie,
84
- encodeOffset,
61
+ OriginalDate,
85
62
  setupDateProxy
86
63
  };
@@ -0,0 +1,27 @@
1
+ // src/cookie.ts
2
+ var COOKIE_NAME = "fake_current_time_offset";
3
+ function parseOffsetFromCookie(cookieString) {
4
+ if (!cookieString) {
5
+ return void 0;
6
+ }
7
+ const cookies = cookieString.split(";");
8
+ for (const cookie of cookies) {
9
+ const [name, value] = cookie.trim().split("=");
10
+ if (name === COOKIE_NAME && value) {
11
+ return decodeOffset(value);
12
+ }
13
+ }
14
+ return void 0;
15
+ }
16
+ function encodeOffset(offset) {
17
+ return JSON.stringify(offset);
18
+ }
19
+ function decodeOffset(value) {
20
+ return JSON.parse(value);
21
+ }
22
+
23
+ export {
24
+ COOKIE_NAME,
25
+ parseOffsetFromCookie,
26
+ encodeOffset
27
+ };
package/dist/index.d.ts CHANGED
@@ -7,5 +7,6 @@ interface TimeOffset {
7
7
  seconds?: number;
8
8
  milliseconds?: number;
9
9
  }
10
+ declare const OriginalDate: DateConstructor;
10
11
 
11
- export type { TimeOffset };
12
+ export { OriginalDate, type TimeOffset };
package/dist/index.js CHANGED
@@ -0,0 +1,6 @@
1
+ import {
2
+ OriginalDate
3
+ } from "./chunk-XDOQVLFM.js";
4
+ export {
5
+ OriginalDate
6
+ };
package/dist/node.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import {
2
- parseOffsetFromCookie,
2
+ parseOffsetFromCookie
3
+ } from "./chunk-Z7IHRBMK.js";
4
+ import {
3
5
  setupDateProxy
4
- } from "./chunk-APIG4RGO.js";
6
+ } from "./chunk-XDOQVLFM.js";
5
7
 
6
8
  // src/node.ts
7
9
  import { AsyncLocalStorage } from "async_hooks";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-current-time",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Manipulate current time in your application for development and staging environments",
5
5
  "keywords": [
6
6
  "time",