first commit
This commit is contained in:
195
unpackage/dist/dev/mp-weixin/request/luch-request/utils/clone.js
vendored
Normal file
195
unpackage/dist/dev/mp-weixin/request/luch-request/utils/clone.js
vendored
Normal file
@@ -0,0 +1,195 @@
|
||||
"use strict";
|
||||
var clone = function() {
|
||||
function _instanceof(obj, type) {
|
||||
return type != null && obj instanceof type;
|
||||
}
|
||||
var nativeMap;
|
||||
try {
|
||||
nativeMap = Map;
|
||||
} catch (_) {
|
||||
nativeMap = function() {
|
||||
};
|
||||
}
|
||||
var nativeSet;
|
||||
try {
|
||||
nativeSet = Set;
|
||||
} catch (_) {
|
||||
nativeSet = function() {
|
||||
};
|
||||
}
|
||||
var nativePromise;
|
||||
try {
|
||||
nativePromise = Promise;
|
||||
} catch (_) {
|
||||
nativePromise = function() {
|
||||
};
|
||||
}
|
||||
function clone2(parent, circular, depth, prototype, includeNonEnumerable) {
|
||||
if (typeof circular === "object") {
|
||||
depth = circular.depth;
|
||||
prototype = circular.prototype;
|
||||
includeNonEnumerable = circular.includeNonEnumerable;
|
||||
circular = circular.circular;
|
||||
}
|
||||
var allParents = [];
|
||||
var allChildren = [];
|
||||
var useBuffer = typeof Buffer != "undefined";
|
||||
if (typeof circular == "undefined")
|
||||
circular = true;
|
||||
if (typeof depth == "undefined")
|
||||
depth = Infinity;
|
||||
function _clone(parent2, depth2) {
|
||||
if (parent2 === null)
|
||||
return null;
|
||||
if (depth2 === 0)
|
||||
return parent2;
|
||||
var child;
|
||||
var proto;
|
||||
if (typeof parent2 != "object") {
|
||||
return parent2;
|
||||
}
|
||||
if (_instanceof(parent2, nativeMap)) {
|
||||
child = new nativeMap();
|
||||
} else if (_instanceof(parent2, nativeSet)) {
|
||||
child = new nativeSet();
|
||||
} else if (_instanceof(parent2, nativePromise)) {
|
||||
child = new nativePromise(function(resolve, reject) {
|
||||
parent2.then(function(value) {
|
||||
resolve(_clone(value, depth2 - 1));
|
||||
}, function(err) {
|
||||
reject(_clone(err, depth2 - 1));
|
||||
});
|
||||
});
|
||||
} else if (clone2.__isArray(parent2)) {
|
||||
child = [];
|
||||
} else if (clone2.__isRegExp(parent2)) {
|
||||
child = new RegExp(parent2.source, __getRegExpFlags(parent2));
|
||||
if (parent2.lastIndex)
|
||||
child.lastIndex = parent2.lastIndex;
|
||||
} else if (clone2.__isDate(parent2)) {
|
||||
child = new Date(parent2.getTime());
|
||||
} else if (useBuffer && Buffer.isBuffer(parent2)) {
|
||||
if (Buffer.from) {
|
||||
child = Buffer.from(parent2);
|
||||
} else {
|
||||
child = new Buffer(parent2.length);
|
||||
parent2.copy(child);
|
||||
}
|
||||
return child;
|
||||
} else if (_instanceof(parent2, Error)) {
|
||||
child = Object.create(parent2);
|
||||
} else {
|
||||
if (typeof prototype == "undefined") {
|
||||
proto = Object.getPrototypeOf(parent2);
|
||||
child = Object.create(proto);
|
||||
} else {
|
||||
child = Object.create(prototype);
|
||||
proto = prototype;
|
||||
}
|
||||
}
|
||||
if (circular) {
|
||||
var index = allParents.indexOf(parent2);
|
||||
if (index != -1) {
|
||||
return allChildren[index];
|
||||
}
|
||||
allParents.push(parent2);
|
||||
allChildren.push(child);
|
||||
}
|
||||
if (_instanceof(parent2, nativeMap)) {
|
||||
parent2.forEach(function(value, key) {
|
||||
var keyChild = _clone(key, depth2 - 1);
|
||||
var valueChild = _clone(value, depth2 - 1);
|
||||
child.set(keyChild, valueChild);
|
||||
});
|
||||
}
|
||||
if (_instanceof(parent2, nativeSet)) {
|
||||
parent2.forEach(function(value) {
|
||||
var entryChild = _clone(value, depth2 - 1);
|
||||
child.add(entryChild);
|
||||
});
|
||||
}
|
||||
for (var i in parent2) {
|
||||
var attrs = Object.getOwnPropertyDescriptor(parent2, i);
|
||||
if (attrs) {
|
||||
child[i] = _clone(parent2[i], depth2 - 1);
|
||||
}
|
||||
try {
|
||||
var objProperty = Object.getOwnPropertyDescriptor(parent2, i);
|
||||
if (objProperty.set === "undefined") {
|
||||
continue;
|
||||
}
|
||||
child[i] = _clone(parent2[i], depth2 - 1);
|
||||
} catch (e) {
|
||||
if (e instanceof TypeError) {
|
||||
continue;
|
||||
} else if (e instanceof ReferenceError) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.getOwnPropertySymbols) {
|
||||
var symbols = Object.getOwnPropertySymbols(parent2);
|
||||
for (var i = 0; i < symbols.length; i++) {
|
||||
var symbol = symbols[i];
|
||||
var descriptor = Object.getOwnPropertyDescriptor(parent2, symbol);
|
||||
if (descriptor && !descriptor.enumerable && !includeNonEnumerable) {
|
||||
continue;
|
||||
}
|
||||
child[symbol] = _clone(parent2[symbol], depth2 - 1);
|
||||
Object.defineProperty(child, symbol, descriptor);
|
||||
}
|
||||
}
|
||||
if (includeNonEnumerable) {
|
||||
var allPropertyNames = Object.getOwnPropertyNames(parent2);
|
||||
for (var i = 0; i < allPropertyNames.length; i++) {
|
||||
var propertyName = allPropertyNames[i];
|
||||
var descriptor = Object.getOwnPropertyDescriptor(parent2, propertyName);
|
||||
if (descriptor && descriptor.enumerable) {
|
||||
continue;
|
||||
}
|
||||
child[propertyName] = _clone(parent2[propertyName], depth2 - 1);
|
||||
Object.defineProperty(child, propertyName, descriptor);
|
||||
}
|
||||
}
|
||||
return child;
|
||||
}
|
||||
return _clone(parent, depth);
|
||||
}
|
||||
clone2.clonePrototype = function clonePrototype(parent) {
|
||||
if (parent === null)
|
||||
return null;
|
||||
var c = function() {
|
||||
};
|
||||
c.prototype = parent;
|
||||
return new c();
|
||||
};
|
||||
function __objToStr(o) {
|
||||
return Object.prototype.toString.call(o);
|
||||
}
|
||||
clone2.__objToStr = __objToStr;
|
||||
function __isDate(o) {
|
||||
return typeof o === "object" && __objToStr(o) === "[object Date]";
|
||||
}
|
||||
clone2.__isDate = __isDate;
|
||||
function __isArray(o) {
|
||||
return typeof o === "object" && __objToStr(o) === "[object Array]";
|
||||
}
|
||||
clone2.__isArray = __isArray;
|
||||
function __isRegExp(o) {
|
||||
return typeof o === "object" && __objToStr(o) === "[object RegExp]";
|
||||
}
|
||||
clone2.__isRegExp = __isRegExp;
|
||||
function __getRegExpFlags(re) {
|
||||
var flags = "";
|
||||
if (re.global)
|
||||
flags += "g";
|
||||
if (re.ignoreCase)
|
||||
flags += "i";
|
||||
if (re.multiline)
|
||||
flags += "m";
|
||||
return flags;
|
||||
}
|
||||
clone2.__getRegExpFlags = __getRegExpFlags;
|
||||
return clone2;
|
||||
}();
|
||||
exports.clone = clone;
|
||||
Reference in New Issue
Block a user