File tree 2 files changed +38
-3
lines changed
2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -33,8 +33,9 @@ exports.clone = function (obj, seen) {
33
33
34
34
let newObj ;
35
35
let cloneDeep = false ;
36
+ const isArray = Array . isArray ( obj ) ;
36
37
37
- if ( ! Array . isArray ( obj ) ) {
38
+ if ( ! isArray ) {
38
39
if ( Buffer . isBuffer ( obj ) ) {
39
40
newObj = Buffer . from ( obj ) ;
40
41
}
@@ -68,6 +69,11 @@ exports.clone = function (obj, seen) {
68
69
const keys = Object . getOwnPropertyNames ( obj ) ;
69
70
for ( let i = 0 ; i < keys . length ; ++ i ) {
70
71
const key = keys [ i ] ;
72
+
73
+ if ( isArray && key === 'length' ) {
74
+ continue ;
75
+ }
76
+
71
77
const descriptor = Object . getOwnPropertyDescriptor ( obj , key ) ;
72
78
if ( descriptor &&
73
79
( descriptor . get ||
@@ -76,9 +82,18 @@ exports.clone = function (obj, seen) {
76
82
Object . defineProperty ( newObj , key , descriptor ) ;
77
83
}
78
84
else {
79
- newObj [ key ] = exports . clone ( obj [ key ] , seen ) ;
85
+ Object . defineProperty ( newObj , key , {
86
+ enumerable : descriptor ? descriptor . enumerable : true ,
87
+ writable : true ,
88
+ configurable : true ,
89
+ value : exports . clone ( obj [ key ] , seen )
90
+ } ) ;
80
91
}
81
92
}
93
+
94
+ if ( isArray ) {
95
+ newObj . length = obj . length ;
96
+ }
82
97
}
83
98
84
99
return newObj ;
Original file line number Diff line number Diff line change @@ -371,6 +371,26 @@ describe('clone()', () => {
371
371
Object . getOwnPropertyDescriptor = oldGetOwnPropertyDescriptor ;
372
372
expect ( copy ) . to . equal ( obj ) ;
373
373
} ) ;
374
+
375
+ it ( 'clones own property when class property is not writable' , ( ) => {
376
+
377
+ const Cl = class {
378
+
379
+ get x ( ) {
380
+
381
+ return 'hi' ;
382
+ }
383
+ } ;
384
+
385
+ const obj = new Cl ( ) ;
386
+
387
+ Object . defineProperty ( obj , 'x' , {
388
+ value : 0 , writable : true
389
+ } ) ;
390
+
391
+ const copy = Hoek . clone ( obj ) ;
392
+ expect ( copy ) . to . equal ( obj ) ;
393
+ } ) ;
374
394
} ) ;
375
395
376
396
describe ( 'merge()' , ( ) => {
@@ -436,7 +456,7 @@ describe('merge()', () => {
436
456
it ( 'merges from null prototype objects' , ( ) => {
437
457
438
458
const a = { } ;
439
-
459
+
440
460
const b = Object . create ( null ) ;
441
461
b . x = true ;
442
462
You can’t perform that action at this time.
0 commit comments