MCPcopy Create free account
hub / github.com/formkit/formkit / clone

Function clone

packages/utils/src/index.ts:653–690  ·  view source on GitHub ↗
(
  obj: T,
  explicit: string[] = explicitKeys
)

Source from the content-addressed store, hash-verified

651 * @public
652 */
653export function clone<T extends Record<string, unknown> | unknown[] | null>(
654 obj: T,
655 explicit: string[] = explicitKeys
656): T {
657 if (
658 obj === null ||
659 obj instanceof RegExp ||
660 obj instanceof Date ||
661 obj instanceof Map ||
662 obj instanceof Set ||
663 (typeof File === 'function' && obj instanceof File)
664 )
665 return obj
666 let returnObject
667 if (Array.isArray(obj)) {
668 returnObject = obj.map((value) => {
669 if (typeof value === 'object') return clone(value as unknown[], explicit)
670 return value
671 }) as T
672 } else {
673 returnObject = Object.keys(obj).reduce((newObj, key) => {
674 newObj[key] =
675 typeof obj[key] === 'object'
676 ? clone(obj[key] as unknown[], explicit)
677 : obj[key]
678 return newObj
679 }, {} as Record<string, unknown>) as T
680 }
681 for (const key of explicit) {
682 if (key in obj) {
683 Object.defineProperty(returnObject, key, {
684 enumerable: false,
685 value: (obj as any)[key],
686 })
687 }
688 }
689 return returnObject
690}
691
692/**
693 * Clones anything. If the item is scalar, no worries, it passes it back. If it

Callers 11

utils.spec.tsFile · 0.90
defineFunction · 0.90
lists.spec.tsFile · 0.90
node.spec.tsFile · 0.90
parseRulesFunction · 0.90
createInputCountPluginFunction · 0.90
floatingLabelsPluginFunction · 0.90
handleSubmitFunction · 0.90
cloneAnyFunction · 0.85

Calls

no outgoing calls

Tested by 1