29 lines
712 B
JavaScript
29 lines
712 B
JavaScript
function cloneRegExp(re) {
|
|
const regexMatch = /^\/(.*)\/([gimyu]*)$/.exec(re.toString());
|
|
return new RegExp(regexMatch[1], regexMatch[2]);
|
|
}
|
|
export default function clone(arg) {
|
|
if (typeof arg !== 'object') {
|
|
return arg;
|
|
}
|
|
if (arg === null) {
|
|
return null;
|
|
}
|
|
if (Array.isArray(arg)) {
|
|
return arg.map(clone);
|
|
}
|
|
if (arg instanceof Date) {
|
|
return new Date(arg.getTime());
|
|
}
|
|
if (arg instanceof RegExp) {
|
|
return cloneRegExp(arg);
|
|
}
|
|
const cloned = {};
|
|
for (const name in arg) {
|
|
if (Object.prototype.hasOwnProperty.call(arg, name)) {
|
|
cloned[name] = clone(arg[name]);
|
|
}
|
|
}
|
|
return cloned;
|
|
}
|