MCPcopy Create free account
hub / github.com/arktypeio/arktype / copyOf

Function copyOf

ark/schema/shared/transform.ts:80–116  ·  view source on GitHub ↗
(data: object)

Source from the content-addressed store, hash-verified

78 : Object.prototype.propertyIsEnumerable.call(data, k)
79
80export const copyOf = (data: object): object => {
81 if (isArray(data)) {
82 const copy: unknown[] = Array.prototype.slice.call(data)
83 const keys = Object.keys(data)
84 // an array's own keys list its indices first, which slice copies, and
85 // are all the usual array holds. Each key one holds besides is defined
86 // from its spread, which reads the enumerable own keys in their order,
87 // as a value of its own. An empty array's is spread too, to no keys
88 return (
89 keys.length !== copy.length ||
90 keys[copy.length - 1] !== `${copy.length - 1}` ||
91 Object.getOwnPropertySymbols(data).length
92 ) ?
93 Object.defineProperties(
94 copy,
95 Object.getOwnPropertyDescriptors({ ...data })
96 )
97 : copy
98 }
99 const prototype = Object.getPrototypeOf(data)
100 if (prototype === Object.prototype) return { ...data }
101 // a copy standing in for an object written in place is written as it is
102 if (joinsKept && copiedInPlace?.has(data)) return data
103 // a builtin keeps state in internal slots, so it's written in place. Each
104 // has its own toString tag, as does a host object, where a class instance
105 // is tagged Object
106 if (Object.prototype.toString.call(data) === "[object Object]")
107 return Object.setPrototypeOf({ ...data }, prototype)
108 if (!joinsKept || !copiedInPlace) return data
109 // each branch of a union joining what they keep writes a copy of the keys
110 // it could delete, which the union writes from once it has joined them.
111 // Without a prototype, the copy is passed here again rather than spread
112 const copy = Object.create(null)
113 for (const k of deletableKeysOf(data)) copy[k] = data[k as never]
114 copiedInPlace.set(copy, data)
115 return copy
116}
117
118/**
119 * what a union whose branches delete different undeclared keys makes of data

Callers 3

copyMethod · 0.90
copyDeclaredFunction · 0.70
mergeTransformedFunction · 0.70

Calls 4

callMethod · 0.80
deletableKeysOfFunction · 0.70
hasMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected