MCPcopy
hub / github.com/vitest-dev/vitest / clone

Function clone

packages/utils/src/helpers.ts:201–257  ·  view source on GitHub ↗
(
  val: T,
  seen: WeakMap<any, any>,
  options: CloneOptions = defaultCloneOptions,
)

Source from the content-addressed store, hash-verified

199}
200
201export function clone<T>(
202 val: T,
203 seen: WeakMap<any, any>,
204 options: CloneOptions = defaultCloneOptions,
205): T {
206 let k: any, out: any
207 if (seen.has(val)) {
208 return seen.get(val)
209 }
210 if (Array.isArray(val)) {
211 out = Array.from({ length: (k = val.length) })
212 seen.set(val, out)
213 while (k--) {
214 out[k] = clone(val[k], seen, options)
215 }
216 return out as any
217 }
218
219 if (Object.prototype.toString.call(val) === '[object Object]') {
220 out = Object.create(Object.getPrototypeOf(val))
221 seen.set(val, out)
222 // we don't need properties from prototype
223 const props = getOwnProperties(val)
224 for (const k of props) {
225 const descriptor = Object.getOwnPropertyDescriptor(val, k)
226 if (!descriptor) {
227 continue
228 }
229 const cloned = clone((val as any)[k], seen, options)
230 if (options.forceWritable) {
231 Object.defineProperty(out, k, {
232 enumerable: descriptor.enumerable,
233 configurable: true,
234 writable: true,
235 value: cloned,
236 })
237 }
238 else if ('get' in descriptor) {
239 Object.defineProperty(out, k, {
240 ...descriptor,
241 get() {
242 return cloned
243 },
244 })
245 }
246 else {
247 Object.defineProperty(out, k, {
248 ...descriptor,
249 value: cloned,
250 })
251 }
252 }
253 return out
254 }
255
256 return val
257}
258

Callers 1

deepCloneFunction · 0.70

Calls 6

getOwnPropertiesFunction · 0.85
createMethod · 0.80
hasMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected