( obj: T, explicit: string[] = explicitKeys )
| 651 | * @public |
| 652 | */ |
| 653 | export 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 |
no outgoing calls