From beb6207b7f53e40547c617fba6d5f1bba2232251 Mon Sep 17 00:00:00 2001 From: Matthew Rohr Date: Thu, 9 Feb 2017 13:24:57 -0500 Subject: [PATCH 1/3] fix(angular.copy) support for Map and Set --- src/Angular.js | 3 +++ test/AngularSpec.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Angular.js b/src/Angular.js index 48c31ea1a0b9..0b994baffcd7 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1031,6 +1031,9 @@ function copy(source, destination, maxDepth) { case '[object Blob]': return new source.constructor([source], {type: source.type}); + case '[object Map]': + case '[object Set]': + return new source.constructor(source); } if (isFunction(source.cloneNode)) { diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 9aaa2f5b8f83..d334448ad76b 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -249,6 +249,30 @@ describe('angular', function() { } }); + it('should handle Map objects', function() { + if (typeof Map !== 'undefined'){ + var src = new Map(); + src.set('foo', 'bar'); + var dst = copy(src); + expect(dst).not.toBe(src); + expect(dst.size).toBe(1); + expect(dst instanceof Map).toBeTruthy(); + expect(dst.get('foo')).toBe('bar'); + } + }); + + it('should handle Set objects', function() { + if (typeof Set !== 'undefined'){ + var src = new Set(); + src.add('foo'); + var dst = copy(src); + expect(dst).not.toBe(src); + expect(dst.size).toBe(1); + expect(dst instanceof Set).toBeTruthy(); + expect(dst.has('foo')).toBeTruthy(); + } + }); + it('should handle Uint16Array subarray', function() { if (typeof Uint16Array !== 'undefined') { var arr = new Uint16Array(4); From 90d60aa99afecda6c51d153aaed4ef3772ab7818 Mon Sep 17 00:00:00 2001 From: Matthew Rohr Date: Thu, 9 Feb 2017 14:18:12 -0500 Subject: [PATCH 2/3] fix(angular.copy): better browser support for Map and Set --- src/Angular.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 0b994baffcd7..140f88b175b3 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1032,8 +1032,17 @@ function copy(source, destination, maxDepth) { case '[object Blob]': return new source.constructor([source], {type: source.type}); case '[object Map]': + var copied = new source.constructor(); + source.forEach(function(value, key){ + copied.set(key, value); + }); + return copied; case '[object Set]': - return new source.constructor(source); + var copied = new source.constructor(); + source.forEach(function(value){ + copied.add(value); + }) + return copied; } if (isFunction(source.cloneNode)) { From d5dbb5973b4588e75afee9d81a0f008f69dc033d Mon Sep 17 00:00:00 2001 From: Matthew Rohr Date: Thu, 9 Feb 2017 14:36:00 -0500 Subject: [PATCH 3/3] fix(angular.copy): fix eslint issues, use literal constructor for Map and Set --- src/Angular.js | 24 +++++++++++++++--------- test/AngularSpec.js | 6 +++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 140f88b175b3..e7b109da1d31 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -1032,17 +1032,23 @@ function copy(source, destination, maxDepth) { case '[object Blob]': return new source.constructor([source], {type: source.type}); case '[object Map]': - var copied = new source.constructor(); - source.forEach(function(value, key){ - copied.set(key, value); + // If we're in this case we know the environment supports Map + /* eslint-disable no-undef */ + var copiedMap = new Map(); + /* eslint-enable */ + source.forEach(function(value, key) { + copiedMap.set(key, value); }); - return copied; + return copiedMap; case '[object Set]': - var copied = new source.constructor(); - source.forEach(function(value){ - copied.add(value); - }) - return copied; + // If we're in this case we know the environment supports Set + /* eslint-disable no-undef */ + var copiedSet = new Set(); + /* eslint-enable */ + source.forEach(function(value) { + copiedSet.add(value); + }); + return copiedSet; } if (isFunction(source.cloneNode)) { diff --git a/test/AngularSpec.js b/test/AngularSpec.js index d334448ad76b..0d7a78ff0eef 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -3,7 +3,7 @@ // Lots of typed array globals are used in this file and ESLint is // not smart enough to understand the `typeof !== 'undefined'` guards. /* globals Blob, Uint8ClampedArray, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, -Float32Array, Float64Array, */ +Float32Array, Float64Array, Map, Set */ describe('angular', function() { var element, document; @@ -250,7 +250,7 @@ describe('angular', function() { }); it('should handle Map objects', function() { - if (typeof Map !== 'undefined'){ + if (typeof Map !== 'undefined') { var src = new Map(); src.set('foo', 'bar'); var dst = copy(src); @@ -262,7 +262,7 @@ describe('angular', function() { }); it('should handle Set objects', function() { - if (typeof Set !== 'undefined'){ + if (typeof Set !== 'undefined') { var src = new Set(); src.add('foo'); var dst = copy(src);