MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / clone

Function clone

packages/core/src/utils/clone.ts:30–160  ·  view source on GitHub ↗
(parent: T, respectCustomCloneMethod = true)

Source from the content-addressed store, hash-verified

28const TypedArray = Object.getPrototypeOf(Uint8Array);
29
30export function clone<T>(parent: T, respectCustomCloneMethod = true): T {
31 const allParents: unknown[] = [];
32 const allChildren: unknown[] = [];
33
34 function _clone(parent: any) {
35 // cloning null always returns null
36 if (parent === null) {
37 return null;
38 }
39
40 if (typeof parent !== 'object') {
41 return parent;
42 }
43
44 if (respectCustomCloneMethod && 'clone' in parent && typeof parent.clone === 'function') {
45 // an async `clone()` signals a live stateful resource (e.g. a PGlite instance
46 // in `driverOptions`, whose `clone()` boots a second WASM database) — this sync
47 // function cannot await it, so keep the instance by reference instead
48 if (parent.clone.constructor.name === 'AsyncFunction') {
49 return parent;
50 }
51
52 return parent.clone();
53 }
54
55 let child: unknown;
56 let proto;
57
58 if (parent instanceof Map) {
59 child = new Map();
60 } else if (parent instanceof Set) {
61 child = new Set();
62 } else if (parent instanceof Promise) {
63 child = new Promise((resolve, reject) => {
64 parent.then(resolve.bind(null, _clone), reject.bind(null, _clone));
65 });
66 } else if (Array.isArray(parent)) {
67 child = [];
68 } else if (parent instanceof RegExp) {
69 let flags = '';
70
71 if (parent.global) {
72 flags += 'g';
73 }
74
75 if (parent.ignoreCase) {
76 flags += 'i';
77 }
78
79 if (parent.multiline) {
80 flags += 'm';
81 }
82
83 child = new RegExp(parent.source, flags);
84
85 if (parent.lastIndex) {
86 (child as RegExp).lastIndex = parent.lastIndex;
87 }

Callers 1

copyMethod · 0.70

Calls 1

_cloneFunction · 0.85

Tested by

no test coverage detected