Skip to content

Commit 3a343e2

Browse files
authored
Merge pull request #866 from monfera/extendDeepNoArrays-with-typed-arrays
Ensure that extendDeepNoArrays includes typed arrays by reference
2 parents 7e4d8ab + 7151c12 commit 3a343e2

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

test/jasmine/.eslintrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"jasmine": true
66
},
77
"globals": {
8-
"Promise": true
8+
"Promise": true,
9+
"Float32Array": true,
10+
"Float64Array": true,
11+
"Int16Array": true,
12+
"Int32Array": true
913
}
1014
}

test/jasmine/tests/extend_test.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,26 @@ describe('extendDeepAll', function() {
454454
});
455455
});
456456

457-
describe('extendDeepNoArrays', function() {
457+
describe('array by reference vs deep-copy', function() {
458458
'use strict';
459459

460-
it('does not copy arrays', function() {
460+
it('extendDeep DOES deep-copy untyped source arrays', function() {
461+
var src = {foo: {bar: [1, 2, 3], baz: [5, 4, 3]}};
462+
var tar = {foo: {bar: [4, 5, 6], bop: [8, 2, 1]}};
463+
var ext = extendDeep(tar, src);
464+
465+
expect(ext).not.toBe(src);
466+
expect(ext).toBe(tar);
467+
468+
expect(ext.foo).not.toBe(src.foo);
469+
expect(ext.foo).toBe(tar.foo);
470+
471+
expect(ext.foo.bar).not.toBe(src.foo.bar);
472+
expect(ext.foo.baz).not.toBe(src.foo.baz);
473+
expect(ext.foo.bop).toBe(tar.foo.bop); // what comes from the target isn't deep copied
474+
});
475+
476+
it('extendDeepNoArrays includes by reference untyped arrays from source', function() {
461477
var src = {foo: {bar: [1, 2, 3], baz: [5, 4, 3]}};
462478
var tar = {foo: {bar: [4, 5, 6], bop: [8, 2, 1]}};
463479
var ext = extendDeepNoArrays(tar, src);
@@ -472,4 +488,37 @@ describe('extendDeepNoArrays', function() {
472488
expect(ext.foo.baz).toBe(src.foo.baz);
473489
expect(ext.foo.bop).toBe(tar.foo.bop);
474490
});
491+
492+
it('extendDeepNoArrays includes by reference typed arrays from source', function() {
493+
var src = {foo: {bar: new Int32Array([1, 2, 3]), baz: new Float32Array([5, 4, 3])}};
494+
var tar = {foo: {bar: new Int16Array([4, 5, 6]), bop: new Float64Array([8, 2, 1])}};
495+
var ext = extendDeepNoArrays(tar, src);
496+
497+
expect(ext).not.toBe(src);
498+
expect(ext).toBe(tar);
499+
500+
expect(ext.foo).not.toBe(src.foo);
501+
expect(ext.foo).toBe(tar.foo);
502+
503+
expect(ext.foo.bar).toBe(src.foo.bar);
504+
expect(ext.foo.baz).toBe(src.foo.baz);
505+
expect(ext.foo.bop).toBe(tar.foo.bop);
506+
});
507+
508+
it('extendDeep ALSO includes by reference typed arrays from source', function() {
509+
var src = {foo: {bar: new Int32Array([1, 2, 3]), baz: new Float32Array([5, 4, 3])}};
510+
var tar = {foo: {bar: new Int16Array([4, 5, 6]), bop: new Float64Array([8, 2, 1])}};
511+
var ext = extendDeep(tar, src);
512+
513+
expect(ext).not.toBe(src);
514+
expect(ext).toBe(tar);
515+
516+
expect(ext.foo).not.toBe(src.foo);
517+
expect(ext.foo).toBe(tar.foo);
518+
519+
expect(ext.foo.bar).toBe(src.foo.bar);
520+
expect(ext.foo.baz).toBe(src.foo.baz);
521+
expect(ext.foo.bop).toBe(tar.foo.bop);
522+
});
523+
475524
});

0 commit comments

Comments
 (0)