import { type } from '@jsmini/type';
// Object.create(null) 的对象,没有hasOwnProperty方法
function hasOwnProp(obj: Record<string, any>, key: string) {
return Object.prototype.hasOwnProperty.call(obj, key);
// 仅对对象和数组进行深拷贝,其他类型,直接返回
function isClone(x: any) {
return t === 'object' || t === 'array';
const UNIQUE_KEY = 'com.yanhaijing.jsmini.clone' + (new Date).getTime();
set(key: any, value: any) {
this.cacheArray.push(key);
for (let i = 0; i < this.cacheArray.length; i++) {
let key = this.cacheArray[i];
this.cacheArray.length = 0;
if (typeof WeakMap !== 'undefined' && type(WeakMap) == 'function') {
if (type(result) == 'weakmap') {
result = new SimpleWeakMap();
export function cloneForce(x: any) {
const uniqueData = getWeakMap();
} else if (t === 'object') {
const loopList: any[] = [
while (loopList.length) {
const node = loopList.pop();
const parent = node.parent;
const source = node.data;
// 初始化赋值目标,key为undefined则拷贝到父元素,否则拷贝到子元素
if (typeof key !== 'undefined') {
target = parent[key] = tt === 'array' ? [] : {};
let uniqueTarget = uniqueData.get(source);
parent[key] = uniqueTarget;
uniqueData.set(source, target);
for (let i = 0; i < source.length; i++) {
if (isClone(source[i])) {
} else if (tt === 'object') {
if (hasOwnProp(source, k)) {
if (k === UNIQUE_KEY) continue;
if (isClone(source[k])) {
(uniqueData as any).clear && (uniqueData as any).clear();