source: node_modules/es-toolkit/dist/object/clone.mjs

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

Added visualizations

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