@zag-js/store
Version:
The reactive store package for zag machines
53 lines (52 loc) • 1.84 kB
JavaScript
// src/clone.ts
import { canProxy } from "./utils.mjs";
function set(obj, key, val) {
if (typeof val.value === "object" && !canProxy(val.value)) val.value = clone(val.value);
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
Object.defineProperty(obj, key, val);
} else obj[key] = val.value;
}
function clone(x) {
if (typeof x !== "object") return x;
let i = 0, k, list, tmp, str = Object.prototype.toString.call(x);
if (str === "[object Object]") {
tmp = Object.create(Object.getPrototypeOf(x) || null);
} else if (str === "[object Array]") {
tmp = Array(x.length);
} else if (str === "[object Set]") {
tmp = /* @__PURE__ */ new Set();
x.forEach(function(val) {
tmp.add(clone(val));
});
} else if (str === "[object Map]") {
tmp = /* @__PURE__ */ new Map();
x.forEach(function(val, key) {
tmp.set(clone(key), clone(val));
});
} else if (str === "[object Date]") {
tmp = /* @__PURE__ */ new Date(+x);
} else if (str === "[object RegExp]") {
tmp = new RegExp(x.source, x.flags);
} else if (str === "[object DataView]") {
tmp = new x.constructor(clone(x.buffer));
} else if (str === "[object ArrayBuffer]") {
tmp = x.slice(0);
} else if (str === "[object Blob]") {
tmp = x.slice();
} else if (str.slice(-6) === "Array]") {
tmp = new x.constructor(x);
}
if (tmp) {
for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
}
for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
}
}
return tmp || x;
}
export {
clone
};