@beignet/react-hook-form 0.0.57 → 0.0.59

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.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Snapshot data containers without manufacturing invalid copies of opaque
3
+ * instances (which may depend on private fields or native internal slots).
4
+ * Opaque instances and functions remain references and must be treated as immutable.
5
+ */
6
+ export function snapshotInput<T>(input: T): T {
7
+ const seen = new WeakMap<object, object>();
8
+
9
+ function copy(value: unknown): unknown {
10
+ if (value === null || typeof value !== "object") return value;
11
+ const previous = seen.get(value);
12
+ if (previous) return previous;
13
+
14
+ const prototype = Object.getPrototypeOf(value);
15
+ let snapshot: object;
16
+ if (prototype === Date.prototype) {
17
+ snapshot = new Date(Date.prototype.getTime.call(value));
18
+ } else if (Array.isArray(value) && prototype === Array.prototype) {
19
+ snapshot = new Array(value.length);
20
+ } else if (prototype === Object.prototype || prototype === null) {
21
+ snapshot = Object.create(prototype);
22
+ } else {
23
+ return value;
24
+ }
25
+ seen.set(value, snapshot);
26
+
27
+ for (const key of Reflect.ownKeys(value)) {
28
+ if (Array.isArray(value) && key === "length") continue;
29
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
30
+ if (!descriptor) continue;
31
+ Object.defineProperty(snapshot, key, {
32
+ value: copy(Reflect.get(value, key)),
33
+ enumerable: descriptor.enumerable,
34
+ configurable: true,
35
+ writable: true,
36
+ });
37
+ }
38
+ return snapshot;
39
+ }
40
+
41
+ return copy(input) as T;
42
+ }