fake-current-time 0.2.0 → 0.3.1

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,25 +68,15 @@ 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)
90
80
 
91
81
  ### Accessing the Original Date
92
82
 
package/dist/browser.js CHANGED
@@ -1,32 +1,10 @@
1
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
- }
2
+ clearOffset,
3
+ setOffset,
4
+ setup
5
+ } from "./chunk-2QPID2IG.js";
6
+ import "./chunk-Z7IHRBMK.js";
7
+ import "./chunk-XDOQVLFM.js";
30
8
  export {
31
9
  clearOffset,
32
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
+ };
@@ -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,257 @@
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
+ margin: "0 0 12px 0",
47
+ fontSize: "16px",
48
+ fontWeight: "600",
49
+ color: "#333"
50
+ },
51
+ timeDisplay: {
52
+ current: {
53
+ container: { ...timeDisplayContainer, backgroundColor: "#fff" },
54
+ value: { ...timeDisplayValue, color: "#333" }
55
+ },
56
+ real: {
57
+ container: { ...timeDisplayContainer, backgroundColor: "#f5f5f5" },
58
+ value: { ...timeDisplayValue, color: "#666" }
59
+ },
60
+ label: {
61
+ fontSize: "12px",
62
+ color: "#666",
63
+ marginBottom: "4px"
64
+ }
65
+ },
66
+ offset: {
67
+ container: {
68
+ marginBottom: "16px",
69
+ padding: "8px 12px",
70
+ backgroundColor: "#e8f4fd",
71
+ borderRadius: "4px",
72
+ fontSize: "13px"
73
+ },
74
+ label: {
75
+ fontWeight: "600",
76
+ color: "#555"
77
+ },
78
+ value: {
79
+ marginLeft: "8px",
80
+ color: "#1a73e8"
81
+ }
82
+ },
83
+ form: {
84
+ fieldGroup: {
85
+ display: "grid",
86
+ gridTemplateColumns: "repeat(3, 1fr)",
87
+ gap: "12px",
88
+ marginBottom: "16px"
89
+ },
90
+ field: {
91
+ display: "flex",
92
+ flexDirection: "column"
93
+ },
94
+ label: {
95
+ fontSize: "12px",
96
+ color: "#666",
97
+ marginBottom: "4px"
98
+ },
99
+ input: {
100
+ padding: "8px",
101
+ border: "1px solid #ddd",
102
+ borderRadius: "4px",
103
+ fontSize: "14px",
104
+ width: "100%",
105
+ boxSizing: "border-box"
106
+ }
107
+ },
108
+ buttons: {
109
+ group: {
110
+ display: "flex",
111
+ gap: "8px"
112
+ },
113
+ apply: { ...buttonBase, backgroundColor: "#1a73e8", color: "#fff" },
114
+ clear: { ...buttonBase, backgroundColor: "#f1f3f4", color: "#333" }
115
+ }
116
+ };
117
+
118
+ // src/react.tsx
119
+ import { jsx, jsxs } from "react/jsx-runtime";
120
+ var FIELDS = [
121
+ "years",
122
+ "months",
123
+ "days",
124
+ "hours",
125
+ "minutes",
126
+ "seconds"
127
+ ];
128
+ var FIELD_LABELS = {
129
+ years: "Years",
130
+ months: "Months",
131
+ days: "Days",
132
+ hours: "Hours",
133
+ minutes: "Minutes",
134
+ seconds: "Seconds"
135
+ };
136
+ function getCurrentOffset() {
137
+ if (typeof document === "undefined") {
138
+ return void 0;
139
+ }
140
+ return parseOffsetFromCookie(document.cookie);
141
+ }
142
+ function formatCurrentOffset(offset) {
143
+ if (!offset) {
144
+ return "None";
145
+ }
146
+ const parts = [];
147
+ for (const field of FIELDS) {
148
+ const value = offset[field];
149
+ if (value !== void 0 && value !== 0) {
150
+ const sign = value > 0 ? "+" : "";
151
+ parts.push(`${sign}${value} ${field}`);
152
+ }
153
+ }
154
+ return parts.length > 0 ? parts.join(", ") : "None";
155
+ }
156
+ function FakeTimeController() {
157
+ const [values, setValues] = useState({
158
+ years: "",
159
+ months: "",
160
+ days: "",
161
+ hours: "",
162
+ minutes: "",
163
+ seconds: ""
164
+ });
165
+ const [currentTime, setCurrentTime] = useState(null);
166
+ const [realTime, setRealTime] = useState(null);
167
+ useEffect(() => {
168
+ setCurrentTime(/* @__PURE__ */ new Date());
169
+ setRealTime(new OriginalDate());
170
+ const interval = setInterval(() => {
171
+ setCurrentTime(/* @__PURE__ */ new Date());
172
+ setRealTime(new OriginalDate());
173
+ }, 1e3);
174
+ return () => clearInterval(interval);
175
+ }, []);
176
+ const currentOffset = getCurrentOffset();
177
+ const handleFieldChange = (e) => {
178
+ const { name, value } = e.target;
179
+ setValues((prev) => ({ ...prev, [name]: value }));
180
+ };
181
+ const handleApply = () => {
182
+ const offset = {};
183
+ for (const field of FIELDS) {
184
+ const value = values[field];
185
+ if (value !== "") {
186
+ const num = Number.parseInt(value, 10);
187
+ if (!Number.isNaN(num) && num !== 0) {
188
+ offset[field] = num;
189
+ }
190
+ }
191
+ }
192
+ if (Object.keys(offset).length > 0) {
193
+ setOffset(offset);
194
+ }
195
+ };
196
+ return /* @__PURE__ */ jsxs("div", { style: styles.container, children: [
197
+ /* @__PURE__ */ jsx("h3", { style: styles.header, children: "Fake Time Controller" }),
198
+ /* @__PURE__ */ jsx(
199
+ TimeDisplay,
200
+ {
201
+ label: "Current Time (with offset applied)",
202
+ time: currentTime,
203
+ variant: "current"
204
+ }
205
+ ),
206
+ /* @__PURE__ */ jsx(
207
+ TimeDisplay,
208
+ {
209
+ label: "Real Time (original)",
210
+ time: realTime,
211
+ variant: "real"
212
+ }
213
+ ),
214
+ /* @__PURE__ */ jsxs("div", { style: styles.offset.container, children: [
215
+ /* @__PURE__ */ jsx("span", { style: styles.offset.label, children: "Current offset:" }),
216
+ /* @__PURE__ */ jsx("span", { style: styles.offset.value, children: formatCurrentOffset(currentOffset) })
217
+ ] }),
218
+ /* @__PURE__ */ jsxs("form", { action: handleApply, children: [
219
+ /* @__PURE__ */ jsx("div", { style: styles.form.fieldGroup, children: FIELDS.map((field) => /* @__PURE__ */ jsxs("label", { style: styles.form.field, children: [
220
+ /* @__PURE__ */ jsx("span", { style: styles.form.label, children: FIELD_LABELS[field] }),
221
+ /* @__PURE__ */ jsx(
222
+ "input",
223
+ {
224
+ type: "number",
225
+ name: field,
226
+ value: values[field],
227
+ onChange: handleFieldChange,
228
+ placeholder: "0",
229
+ style: styles.form.input
230
+ }
231
+ )
232
+ ] }, field)) }),
233
+ /* @__PURE__ */ jsxs("div", { style: styles.buttons.group, children: [
234
+ /* @__PURE__ */ jsx("button", { type: "submit", style: styles.buttons.apply, children: "Apply" }),
235
+ /* @__PURE__ */ jsx(
236
+ "button",
237
+ {
238
+ type: "button",
239
+ onClick: clearOffset,
240
+ style: styles.buttons.clear,
241
+ children: "Clear"
242
+ }
243
+ )
244
+ ] })
245
+ ] })
246
+ ] });
247
+ }
248
+ function TimeDisplay({ label, time, variant }) {
249
+ const variantStyles = styles.timeDisplay[variant];
250
+ return /* @__PURE__ */ jsxs("div", { style: variantStyles.container, children: [
251
+ /* @__PURE__ */ jsx("div", { style: styles.timeDisplay.label, children: label }),
252
+ /* @__PURE__ */ jsx("div", { style: variantStyles.value, children: time?.toLocaleString() ?? "-" })
253
+ ] });
254
+ }
255
+ export {
256
+ FakeTimeController
257
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-current-time",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
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",
42
- "@types/node": "^24.10.1",
53
+ "@biomejs/biome": "2.3.12",
54
+ "@types/node": "^25.0.10",
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"