source: node_modules/es-toolkit/dist/object/clone.js@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.8 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const isPrimitive = require('../predicate/isPrimitive.js');
6const isTypedArray = require('../predicate/isTypedArray.js');
7
8function clone(obj) {
9 if (isPrimitive.isPrimitive(obj)) {
10 return obj;
11 }
12 if (Array.isArray(obj) ||
13 isTypedArray.isTypedArray(obj) ||
14 obj instanceof ArrayBuffer ||
15 (typeof SharedArrayBuffer !== 'undefined' && obj instanceof SharedArrayBuffer)) {
16 return obj.slice(0);
17 }
18 const prototype = Object.getPrototypeOf(obj);
19 if (prototype == null) {
20 return Object.assign(Object.create(prototype), obj);
21 }
22 const Constructor = prototype.constructor;
23 if (obj instanceof Date || obj instanceof Map || obj instanceof Set) {
24 return new Constructor(obj);
25 }
26 if (obj instanceof RegExp) {
27 const newRegExp = new Constructor(obj);
28 newRegExp.lastIndex = obj.lastIndex;
29 return newRegExp;
30 }
31 if (obj instanceof DataView) {
32 return new Constructor(obj.buffer.slice(0));
33 }
34 if (obj instanceof Error) {
35 let newError;
36 if (obj instanceof AggregateError) {
37 newError = new Constructor(obj.errors, obj.message, { cause: obj.cause });
38 }
39 else {
40 newError = new Constructor(obj.message, { cause: obj.cause });
41 }
42 newError.stack = obj.stack;
43 Object.assign(newError, obj);
44 return newError;
45 }
46 if (typeof File !== 'undefined' && obj instanceof File) {
47 const newFile = new Constructor([obj], obj.name, { type: obj.type, lastModified: obj.lastModified });
48 return newFile;
49 }
50 if (typeof obj === 'object') {
51 const newObject = Object.create(prototype);
52 return Object.assign(newObject, obj);
53 }
54 return obj;
55}
56
57exports.clone = clone;
Note: See TracBrowser for help on using the repository browser.