| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const isPrimitive = require('../../predicate/isPrimitive.js');
|
|---|
| 6 | const getTag = require('../_internal/getTag.js');
|
|---|
| 7 | const tags = require('../_internal/tags.js');
|
|---|
| 8 | const isArray = require('../predicate/isArray.js');
|
|---|
| 9 | const isTypedArray = require('../predicate/isTypedArray.js');
|
|---|
| 10 |
|
|---|
| 11 | function clone(obj) {
|
|---|
| 12 | if (isPrimitive.isPrimitive(obj)) {
|
|---|
| 13 | return obj;
|
|---|
| 14 | }
|
|---|
| 15 | const tag = getTag.getTag(obj);
|
|---|
| 16 | if (!isCloneableObject(obj)) {
|
|---|
| 17 | return {};
|
|---|
| 18 | }
|
|---|
| 19 | if (isArray.isArray(obj)) {
|
|---|
| 20 | const result = Array.from(obj);
|
|---|
| 21 | if (obj.length > 0 && typeof obj[0] === 'string' && Object.hasOwn(obj, 'index')) {
|
|---|
| 22 | result.index = obj.index;
|
|---|
| 23 | result.input = obj.input;
|
|---|
| 24 | }
|
|---|
| 25 | return result;
|
|---|
| 26 | }
|
|---|
| 27 | if (isTypedArray.isTypedArray(obj)) {
|
|---|
| 28 | const typedArray = obj;
|
|---|
| 29 | const Ctor = typedArray.constructor;
|
|---|
| 30 | return new Ctor(typedArray.buffer, typedArray.byteOffset, typedArray.length);
|
|---|
| 31 | }
|
|---|
| 32 | if (tag === tags.arrayBufferTag) {
|
|---|
| 33 | return new ArrayBuffer(obj.byteLength);
|
|---|
| 34 | }
|
|---|
| 35 | if (tag === tags.dataViewTag) {
|
|---|
| 36 | const dataView = obj;
|
|---|
| 37 | const buffer = dataView.buffer;
|
|---|
| 38 | const byteOffset = dataView.byteOffset;
|
|---|
| 39 | const byteLength = dataView.byteLength;
|
|---|
| 40 | const clonedBuffer = new ArrayBuffer(byteLength);
|
|---|
| 41 | const srcView = new Uint8Array(buffer, byteOffset, byteLength);
|
|---|
| 42 | const destView = new Uint8Array(clonedBuffer);
|
|---|
| 43 | destView.set(srcView);
|
|---|
| 44 | return new DataView(clonedBuffer);
|
|---|
| 45 | }
|
|---|
| 46 | if (tag === tags.booleanTag || tag === tags.numberTag || tag === tags.stringTag) {
|
|---|
| 47 | const Ctor = obj.constructor;
|
|---|
| 48 | const clone = new Ctor(obj.valueOf());
|
|---|
| 49 | if (tag === tags.stringTag) {
|
|---|
| 50 | cloneStringObjectProperties(clone, obj);
|
|---|
| 51 | }
|
|---|
| 52 | else {
|
|---|
| 53 | copyOwnProperties(clone, obj);
|
|---|
| 54 | }
|
|---|
| 55 | return clone;
|
|---|
| 56 | }
|
|---|
| 57 | if (tag === tags.dateTag) {
|
|---|
| 58 | return new Date(Number(obj));
|
|---|
| 59 | }
|
|---|
| 60 | if (tag === tags.regexpTag) {
|
|---|
| 61 | const regExp = obj;
|
|---|
| 62 | const clone = new RegExp(regExp.source, regExp.flags);
|
|---|
| 63 | clone.lastIndex = regExp.lastIndex;
|
|---|
| 64 | return clone;
|
|---|
| 65 | }
|
|---|
| 66 | if (tag === tags.symbolTag) {
|
|---|
| 67 | return Object(Symbol.prototype.valueOf.call(obj));
|
|---|
| 68 | }
|
|---|
| 69 | if (tag === tags.mapTag) {
|
|---|
| 70 | const map = obj;
|
|---|
| 71 | const result = new Map();
|
|---|
| 72 | map.forEach((obj, key) => {
|
|---|
| 73 | result.set(key, obj);
|
|---|
| 74 | });
|
|---|
| 75 | return result;
|
|---|
| 76 | }
|
|---|
| 77 | if (tag === tags.setTag) {
|
|---|
| 78 | const set = obj;
|
|---|
| 79 | const result = new Set();
|
|---|
| 80 | set.forEach(obj => {
|
|---|
| 81 | result.add(obj);
|
|---|
| 82 | });
|
|---|
| 83 | return result;
|
|---|
| 84 | }
|
|---|
| 85 | if (tag === tags.argumentsTag) {
|
|---|
| 86 | const args = obj;
|
|---|
| 87 | const result = {};
|
|---|
| 88 | copyOwnProperties(result, args);
|
|---|
| 89 | result.length = args.length;
|
|---|
| 90 | result[Symbol.iterator] = args[Symbol.iterator];
|
|---|
| 91 | return result;
|
|---|
| 92 | }
|
|---|
| 93 | const result = {};
|
|---|
| 94 | copyPrototype(result, obj);
|
|---|
| 95 | copyOwnProperties(result, obj);
|
|---|
| 96 | copySymbolProperties(result, obj);
|
|---|
| 97 | return result;
|
|---|
| 98 | }
|
|---|
| 99 | function isCloneableObject(object) {
|
|---|
| 100 | switch (getTag.getTag(object)) {
|
|---|
| 101 | case tags.argumentsTag:
|
|---|
| 102 | case tags.arrayTag:
|
|---|
| 103 | case tags.arrayBufferTag:
|
|---|
| 104 | case tags.dataViewTag:
|
|---|
| 105 | case tags.booleanTag:
|
|---|
| 106 | case tags.dateTag:
|
|---|
| 107 | case tags.float32ArrayTag:
|
|---|
| 108 | case tags.float64ArrayTag:
|
|---|
| 109 | case tags.int8ArrayTag:
|
|---|
| 110 | case tags.int16ArrayTag:
|
|---|
| 111 | case tags.int32ArrayTag:
|
|---|
| 112 | case tags.mapTag:
|
|---|
| 113 | case tags.numberTag:
|
|---|
| 114 | case tags.objectTag:
|
|---|
| 115 | case tags.regexpTag:
|
|---|
| 116 | case tags.setTag:
|
|---|
| 117 | case tags.stringTag:
|
|---|
| 118 | case tags.symbolTag:
|
|---|
| 119 | case tags.uint8ArrayTag:
|
|---|
| 120 | case tags.uint8ClampedArrayTag:
|
|---|
| 121 | case tags.uint16ArrayTag:
|
|---|
| 122 | case tags.uint32ArrayTag: {
|
|---|
| 123 | return true;
|
|---|
| 124 | }
|
|---|
| 125 | default: {
|
|---|
| 126 | return false;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | function copyOwnProperties(target, source) {
|
|---|
| 131 | for (const key in source) {
|
|---|
| 132 | if (Object.hasOwn(source, key)) {
|
|---|
| 133 | target[key] = source[key];
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | function copySymbolProperties(target, source) {
|
|---|
| 138 | const symbols = Object.getOwnPropertySymbols(source);
|
|---|
| 139 | for (let i = 0; i < symbols.length; i++) {
|
|---|
| 140 | const symbol = symbols[i];
|
|---|
| 141 | if (Object.prototype.propertyIsEnumerable.call(source, symbol)) {
|
|---|
| 142 | target[symbol] = source[symbol];
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | function cloneStringObjectProperties(target, source) {
|
|---|
| 147 | const stringLength = source.valueOf().length;
|
|---|
| 148 | for (const key in source) {
|
|---|
| 149 | if (Object.hasOwn(source, key) && (Number.isNaN(Number(key)) || Number(key) >= stringLength)) {
|
|---|
| 150 | target[key] = source[key];
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | function copyPrototype(target, source) {
|
|---|
| 155 | const proto = Object.getPrototypeOf(source);
|
|---|
| 156 | if (proto !== null) {
|
|---|
| 157 | const Ctor = source.constructor;
|
|---|
| 158 | if (typeof Ctor === 'function') {
|
|---|
| 159 | Object.setPrototypeOf(target, proto);
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | exports.clone = clone;
|
|---|