fake-current-time 0.1.1 → 0.3.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
@@ -68,22 +68,23 @@ startTransition(() => {
68
68
  ### UI Component
69
69
 
70
70
  ```typescript
71
- import { setOffset, clearOffset } from "fake-current-time/browser";
71
+ import { FakeTimeController } from "fake-current-time/react";
72
72
 
73
73
  // Restrict access to this page in production (e.g., via routing or middleware)
74
- export function TimeControl() {
75
- return (
76
- <div>
77
- <p>Current: {new Date().toString()}</p>
78
- {/* setOffset saves to cookie and reloads the page */}
79
- <button onClick={() => setOffset({ days: 1 })}>+1 Day</button>
80
- <button onClick={() => setOffset({ days: -1 })}>-1 Day</button>
81
- <button onClick={() => setOffset({ months: 3, days: 7 })}>+3 Months +7 Days</button>
82
- {/* clearOffset removes cookie and reloads the page */}
83
- <button onClick={clearOffset}>Reset</button>
84
- </div>
85
- );
74
+ export function TimeControlPage() {
75
+ return <FakeTimeController />;
86
76
  }
87
77
  ```
88
78
 
89
- `setOffset` accepts: `years`, `months`, `days`, `hours`, `minutes`, `seconds`, `milliseconds`
79
+ ![FakeTimeController UI](./ui.png)
80
+
81
+ ### Accessing the Original Date
82
+
83
+ If you need to access the real `Date` while time manipulation is active, use `OriginalDate`:
84
+
85
+ ```typescript
86
+ import { OriginalDate } from "fake-current-time";
87
+
88
+ const realNow = OriginalDate.now();
89
+ const realDate = new OriginalDate();
90
+ ```
package/dist/browser.js CHANGED
@@ -1,30 +1,10 @@
1
1
  import {
2
- COOKIE_NAME,
3
- encodeOffset,
4
- parseOffsetFromCookie,
5
- setupDateProxy
6
- } from "./chunk-APIG4RGO.js";
7
-
8
- // src/browser.ts
9
- var initialized = false;
10
- function setup() {
11
- if (initialized) {
12
- return;
13
- }
14
- setupDateProxy(getOffset);
15
- initialized = true;
16
- }
17
- function setOffset(offset) {
18
- document.cookie = `${COOKIE_NAME}=${encodeOffset(offset)}; path=/`;
19
- location.reload();
20
- }
21
- function clearOffset() {
22
- document.cookie = `${COOKIE_NAME}=; path=/; max-age=0`;
23
- location.reload();
24
- }
25
- function getOffset() {
26
- return parseOffsetFromCookie(document.cookie);
27
- }
2
+ clearOffset,
3
+ setOffset,
4
+ setup
5
+ } from "./chunk-2QPID2IG.js";
6
+ import "./chunk-Z7IHRBMK.js";
7
+ import "./chunk-XDOQVLFM.js";
28
8
  export {
29
9
  clearOffset,
30
10
  setOffset,
@@ -0,0 +1,35 @@
1
+ import {
2
+ COOKIE_NAME,
3
+ encodeOffset,
4
+ parseOffsetFromCookie
5
+ } from "./chunk-Z7IHRBMK.js";
6
+ import {
7
+ setupDateProxy
8
+ } from "./chunk-XDOQVLFM.js";
9
+
10
+ // src/browser.ts
11
+ var initialized = false;
12
+ function setup() {
13
+ if (initialized) {
14
+ return;
15
+ }
16
+ setupDateProxy(getOffset);
17
+ initialized = true;
18
+ }
19
+ function setOffset(offset) {
20
+ document.cookie = `${COOKIE_NAME}=${encodeOffset(offset)}; path=/`;
21
+ location.reload();
22
+ }
23
+ function clearOffset() {
24
+ document.cookie = `${COOKIE_NAME}=; path=/; max-age=0`;
25
+ location.reload();
26
+ }
27
+ function getOffset() {
28
+ return parseOffsetFromCookie(document.cookie);
29
+ }
30
+
31
+ export {
32
+ setup,
33
+ setOffset,
34
+ clearOffset
35
+ };
@@ -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";
@@ -0,0 +1,5 @@
1
+ import { JSX } from 'react';
2
+
3
+ declare function FakeTimeController(): JSX.Element;
4
+
5
+ export { FakeTimeController };
package/dist/react.js ADDED
@@ -0,0 +1,265 @@
1
+ import {
2
+ clearOffset,
3
+ setOffset
4
+ } from "./chunk-2QPID2IG.js";
5
+ import {
6
+ parseOffsetFromCookie
7
+ } from "./chunk-Z7IHRBMK.js";
8
+ import {
9
+ OriginalDate
10
+ } from "./chunk-XDOQVLFM.js";
11
+
12
+ // src/react.tsx
13
+ import { useEffect, useState } from "react";
14
+
15
+ // src/react.styles.ts
16
+ var timeDisplayContainer = {
17
+ marginBottom: "16px",
18
+ padding: "12px",
19
+ borderRadius: "4px",
20
+ border: "1px solid #e0e0e0"
21
+ };
22
+ var timeDisplayValue = {
23
+ fontSize: "20px",
24
+ fontFamily: "monospace"
25
+ };
26
+ var buttonBase = {
27
+ flex: 1,
28
+ padding: "10px 16px",
29
+ border: "none",
30
+ borderRadius: "4px",
31
+ fontSize: "14px",
32
+ fontWeight: "500",
33
+ cursor: "pointer"
34
+ };
35
+ var styles = {
36
+ container: {
37
+ fontFamily: "system-ui, -apple-system, sans-serif",
38
+ fontSize: "14px",
39
+ padding: "16px",
40
+ border: "1px solid #ccc",
41
+ borderRadius: "8px",
42
+ backgroundColor: "#f9f9f9",
43
+ maxWidth: "400px"
44
+ },
45
+ header: {
46
+ fontFamily: "system-ui, -apple-system, sans-serif",
47
+ margin: "0 0 12px 0",
48
+ fontSize: "16px",
49
+ fontWeight: "600",
50
+ color: "#333"
51
+ },
52
+ timeDisplay: {
53
+ current: {
54
+ container: { ...timeDisplayContainer, backgroundColor: "#fff" },
55
+ value: { ...timeDisplayValue, color: "#333" }
56
+ },
57
+ real: {
58
+ container: { ...timeDisplayContainer, backgroundColor: "#f5f5f5" },
59
+ value: { ...timeDisplayValue, color: "#666" }
60
+ },
61
+ label: {
62
+ fontSize: "12px",
63
+ color: "#666",
64
+ marginBottom: "4px"
65
+ }
66
+ },
67
+ offset: {
68
+ container: {
69
+ marginBottom: "16px",
70
+ padding: "8px 12px",
71
+ backgroundColor: "#e8f4fd",
72
+ borderRadius: "4px",
73
+ fontSize: "13px"
74
+ },
75
+ label: {
76
+ fontWeight: "600",
77
+ color: "#555"
78
+ },
79
+ value: {
80
+ marginLeft: "8px",
81
+ color: "#1a73e8"
82
+ }
83
+ },
84
+ form: {
85
+ fieldGroup: {
86
+ display: "grid",
87
+ gridTemplateColumns: "repeat(3, 1fr)",
88
+ gap: "12px",
89
+ marginBottom: "16px"
90
+ },
91
+ field: {
92
+ display: "flex",
93
+ flexDirection: "column"
94
+ },
95
+ label: {
96
+ fontSize: "12px",
97
+ color: "#666",
98
+ marginBottom: "4px"
99
+ },
100
+ input: {
101
+ padding: "8px",
102
+ border: "1px solid #ddd",
103
+ borderRadius: "4px",
104
+ fontSize: "14px",
105
+ width: "100%",
106
+ boxSizing: "border-box"
107
+ }
108
+ },
109
+ buttons: {
110
+ group: {
111
+ display: "flex",
112
+ gap: "8px"
113
+ },
114
+ apply: { ...buttonBase, backgroundColor: "#1a73e8", color: "#fff" },
115
+ clear: { ...buttonBase, backgroundColor: "#f1f3f4", color: "#333" }
116
+ }
117
+ };
118
+
119
+ // src/react.tsx
120
+ import { jsx, jsxs } from "react/jsx-runtime";
121
+ var FIELDS = [
122
+ "years",
123
+ "months",
124
+ "days",
125
+ "hours",
126
+ "minutes",
127
+ "seconds"
128
+ ];
129
+ var FIELD_LABELS = {
130
+ years: "Years",
131
+ months: "Months",
132
+ days: "Days",
133
+ hours: "Hours",
134
+ minutes: "Minutes",
135
+ seconds: "Seconds"
136
+ };
137
+ function getCurrentOffset() {
138
+ if (typeof document === "undefined") {
139
+ return void 0;
140
+ }
141
+ return parseOffsetFromCookie(document.cookie);
142
+ }
143
+ function formatCurrentOffset(offset) {
144
+ if (!offset) {
145
+ return "None";
146
+ }
147
+ const parts = [];
148
+ for (const field of FIELDS) {
149
+ const value = offset[field];
150
+ if (value !== void 0 && value !== 0) {
151
+ const sign = value > 0 ? "+" : "";
152
+ parts.push(`${sign}${value} ${field}`);
153
+ }
154
+ }
155
+ if (offset.milliseconds !== void 0 && offset.milliseconds !== 0) {
156
+ const sign = offset.milliseconds > 0 ? "+" : "";
157
+ parts.push(`${sign}${offset.milliseconds} milliseconds`);
158
+ }
159
+ return parts.length > 0 ? parts.join(", ") : "None";
160
+ }
161
+ function FakeTimeController() {
162
+ const [values, setValues] = useState({
163
+ years: "",
164
+ months: "",
165
+ days: "",
166
+ hours: "",
167
+ minutes: "",
168
+ seconds: ""
169
+ });
170
+ const [currentTime, setCurrentTime] = useState(null);
171
+ const [realTime, setRealTime] = useState(null);
172
+ useEffect(() => {
173
+ setCurrentTime(/* @__PURE__ */ new Date());
174
+ setRealTime(new OriginalDate());
175
+ const interval = setInterval(() => {
176
+ setCurrentTime(/* @__PURE__ */ new Date());
177
+ setRealTime(new OriginalDate());
178
+ }, 1e3);
179
+ return () => clearInterval(interval);
180
+ }, []);
181
+ const currentOffset = getCurrentOffset();
182
+ const handleFieldChange = (e) => {
183
+ const { name, value } = e.target;
184
+ setValues((prev) => ({ ...prev, [name]: value }));
185
+ };
186
+ const handleApply = () => {
187
+ const offset = {};
188
+ for (const field of FIELDS) {
189
+ const value = values[field];
190
+ if (value !== "") {
191
+ const num = Number.parseInt(value, 10);
192
+ if (!Number.isNaN(num) && num !== 0) {
193
+ offset[field] = num;
194
+ }
195
+ }
196
+ }
197
+ if (Object.keys(offset).length > 0) {
198
+ setOffset(offset);
199
+ }
200
+ };
201
+ return /* @__PURE__ */ jsxs("div", { style: styles.container, children: [
202
+ /* @__PURE__ */ jsx("h3", { style: styles.header, children: "Fake Time Controller" }),
203
+ /* @__PURE__ */ jsx(
204
+ TimeDisplay,
205
+ {
206
+ label: "Current Time (with offset applied)",
207
+ time: currentTime
208
+ }
209
+ ),
210
+ /* @__PURE__ */ jsx(
211
+ TimeDisplay,
212
+ {
213
+ label: "Real Time (original)",
214
+ time: realTime,
215
+ variant: "real"
216
+ }
217
+ ),
218
+ /* @__PURE__ */ jsxs("div", { style: styles.offset.container, children: [
219
+ /* @__PURE__ */ jsx("span", { style: styles.offset.label, children: "Current offset:" }),
220
+ /* @__PURE__ */ jsx("span", { style: styles.offset.value, children: formatCurrentOffset(currentOffset) })
221
+ ] }),
222
+ /* @__PURE__ */ jsxs("form", { action: handleApply, children: [
223
+ /* @__PURE__ */ jsx("div", { style: styles.form.fieldGroup, children: FIELDS.map((field) => /* @__PURE__ */ jsxs("label", { style: styles.form.field, children: [
224
+ /* @__PURE__ */ jsx("span", { style: styles.form.label, children: FIELD_LABELS[field] }),
225
+ /* @__PURE__ */ jsx(
226
+ "input",
227
+ {
228
+ type: "number",
229
+ name: field,
230
+ value: values[field],
231
+ onChange: handleFieldChange,
232
+ placeholder: "0",
233
+ style: styles.form.input
234
+ }
235
+ )
236
+ ] }, field)) }),
237
+ /* @__PURE__ */ jsxs("div", { style: styles.buttons.group, children: [
238
+ /* @__PURE__ */ jsx("button", { type: "submit", style: styles.buttons.apply, children: "Apply" }),
239
+ /* @__PURE__ */ jsx(
240
+ "button",
241
+ {
242
+ type: "button",
243
+ onClick: clearOffset,
244
+ style: styles.buttons.clear,
245
+ children: "Clear"
246
+ }
247
+ )
248
+ ] })
249
+ ] })
250
+ ] });
251
+ }
252
+ function TimeDisplay({
253
+ label,
254
+ time,
255
+ variant = "current"
256
+ }) {
257
+ const variantStyles = styles.timeDisplay[variant];
258
+ return /* @__PURE__ */ jsxs("div", { style: variantStyles.container, children: [
259
+ /* @__PURE__ */ jsx("div", { style: styles.timeDisplay.label, children: label }),
260
+ /* @__PURE__ */ jsx("div", { style: variantStyles.value, children: time?.toLocaleString() ?? "-" })
261
+ ] });
262
+ }
263
+ export {
264
+ FakeTimeController
265
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-current-time",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Manipulate current time in your application for development and staging environments",
5
5
  "keywords": [
6
6
  "time",
@@ -31,6 +31,18 @@
31
31
  "./browser": {
32
32
  "types": "./dist/browser.d.ts",
33
33
  "default": "./dist/browser.js"
34
+ },
35
+ "./react": {
36
+ "types": "./dist/react.d.ts",
37
+ "default": "./dist/react.js"
38
+ }
39
+ },
40
+ "peerDependencies": {
41
+ "react": "^18.0.0 || ^19.0.0"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "react": {
45
+ "optional": true
34
46
  }
35
47
  },
36
48
  "publishConfig": {
@@ -38,8 +50,11 @@
38
50
  "provenance": true
39
51
  },
40
52
  "devDependencies": {
41
- "@biomejs/biome": "2.3.7",
53
+ "@biomejs/biome": "2.3.8",
42
54
  "@types/node": "^24.10.1",
55
+ "@types/react": "^19.2.10",
56
+ "react": "^19.2.4",
57
+ "react-dom": "^19.2.4",
43
58
  "tsup": "^8.3.5",
44
59
  "typescript": "^5.7.2",
45
60
  "vitest": "^4.0.3"