11import type { Arrayable , Nullable } from './types'
22
3+ interface CloneOptions {
4+ forceWritable ?: boolean
5+ }
6+
37export function notNullish < T > ( v : T | null | undefined ) : v is NonNullable < T > {
48 return v != null
59}
@@ -72,20 +76,28 @@ export function getOwnProperties(obj: any) {
7276 return Array . from ( ownProps )
7377}
7478
75- export function deepClone < T > ( val : T ) : T {
79+ const defaultCloneOptions : CloneOptions = { forceWritable : false }
80+
81+ export function deepClone < T > (
82+ val : T ,
83+ options : CloneOptions = defaultCloneOptions ,
84+ ) : T {
7685 const seen = new WeakMap ( )
77- return clone ( val , seen )
86+ return clone ( val , seen , options )
7887}
7988
80- export function clone < T > ( val : T , seen : WeakMap < any , any > ) : T {
89+ export function clone < T > (
90+ val : T ,
91+ seen : WeakMap < any , any > ,
92+ options : CloneOptions = defaultCloneOptions ,
93+ ) : T {
8194 let k : any , out : any
8295 if ( seen . has ( val ) )
8396 return seen . get ( val )
8497 if ( Array . isArray ( val ) ) {
85- out = Array ( k = val . length )
98+ out = Array ( ( k = val . length ) )
8699 seen . set ( val , out )
87- while ( k -- )
88- out [ k ] = clone ( val [ k ] , seen )
100+ while ( k -- ) out [ k ] = clone ( val [ k ] , seen )
89101 return out as any
90102 }
91103
@@ -110,6 +122,7 @@ export function clone<T>(val: T, seen: WeakMap<any, any>): T {
110122 else {
111123 Object . defineProperty ( out , k , {
112124 ...descriptor ,
125+ writable : options . forceWritable ? true : descriptor . writable ,
113126 value : cloned ,
114127 } )
115128 }
0 commit comments