Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a83f60e

Browse files
committed
refactor(*): replace HashMap with NgMap (former ES6Map)
Also rename: - `ES6Map[Shim]` --> `NgMap[Shim]` - `$$HashMap` --> `$$Map`
1 parent fdba297 commit a83f60e

File tree

11 files changed

+107
-140
lines changed

11 files changed

+107
-140
lines changed

src/.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150

151151
/* apis.js */
152152
"hashKey": false,
153-
"HashMap": false,
153+
"NgMap": false,
154154

155155
/* urlUtils.js */
156156
"urlResolve": false,

src/Angular.js

+1-57
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
hasOwnProperty,
9090
createMap,
9191
stringify,
92-
ES6Map,
9392
9493
NODE_TYPE_ELEMENT,
9594
NODE_TYPE_ATTRIBUTE,
@@ -750,61 +749,6 @@ function arrayRemove(array, value) {
750749
return index;
751750
}
752751

753-
// A minimal ES6 Map implementation.
754-
// Should be bug/feature equivelent to the native implementations of supported browsers.
755-
// See https://kangax.github.io/compat-table/es6/#test-Map
756-
var nanPlaceholder = Object.create(null);
757-
function ES6MapShim() {
758-
this._keys = [];
759-
this._values = [];
760-
this._lastKey = NaN;
761-
this._lastIndex = -1;
762-
}
763-
ES6MapShim.prototype = {
764-
_idx: function(key) {
765-
if (key === this._lastKey) {
766-
return this._lastIndex;
767-
}
768-
return (this._lastIndex = (this._keys.indexOf(this._lastKey = key)));
769-
},
770-
_transformKey: function(key) {
771-
return isNumberNaN(key) ? nanPlaceholder : key;
772-
},
773-
get: function(key) {
774-
var idx = this._idx(this._transformKey(key));
775-
if (idx !== -1) {
776-
return this._values[idx];
777-
}
778-
},
779-
set: function(key, value) {
780-
key = this._transformKey(key);
781-
var idx = this._idx(key);
782-
if (idx === -1) {
783-
idx = this._lastIndex = this._keys.length;
784-
}
785-
this._keys[idx] = key;
786-
this._values[idx] = value;
787-
788-
// Support: IE11
789-
// Do not `return this` to simulate the partial IE11 implementation
790-
},
791-
delete: function(key) {
792-
var idx = this._idx(this._transformKey(key));
793-
if (idx === -1) {
794-
return false;
795-
}
796-
this._keys.splice(idx, 1);
797-
this._values.splice(idx, 1);
798-
this._lastKey = NaN;
799-
this._lastIndex = -1;
800-
return true;
801-
}
802-
};
803-
804-
var ES6Map = isFunction(window.Map) && toString.call(window.Map.prototype) === '[object Map]'
805-
? window.Map
806-
: ES6MapShim;
807-
808752
/**
809753
* @ngdoc function
810754
* @name angular.copy
@@ -898,7 +842,7 @@ function copy(source, destination) {
898842
return copyElement(source);
899843

900844
function copyRecurse(source, destination) {
901-
(stack || (stack = new ES6Map())).set(source, destination);
845+
(stack || (stack = new NgMap())).set(source, destination);
902846

903847
var h = destination.$$hashKey;
904848
var key;

src/AngularPublic.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
$$ForceReflowProvider,
7171
$InterpolateProvider,
7272
$IntervalProvider,
73-
$$HashMapProvider,
7473
$HttpProvider,
7574
$HttpParamSerializerProvider,
7675
$HttpParamSerializerJQLikeProvider,
@@ -79,6 +78,7 @@
7978
$jsonpCallbacksProvider,
8079
$LocationProvider,
8180
$LogProvider,
81+
$$MapProvider,
8282
$ModelOptionsProvider,
8383
$ParseProvider,
8484
$RootScopeProvider,
@@ -262,7 +262,7 @@ function publishExternalAPI(angular) {
262262
$window: $WindowProvider,
263263
$$rAF: $$RAFProvider,
264264
$$jqLite: $$jqLiteProvider,
265-
$$HashMap: $$HashMapProvider,
265+
$$Map: $$MapProvider,
266266
$$cookieReader: $$CookieReaderProvider
267267
});
268268
}

src/apis.js

+54-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict';
22

3-
/* global
4-
ES6Map */
5-
63
/**
74
* Computes a hash of an 'obj'.
85
* Hash of a:
@@ -35,44 +32,68 @@ function hashKey(obj, nextUidFn) {
3532
return key;
3633
}
3734

38-
/**
39-
* HashMap which can use objects as keys
40-
*/
41-
function HashMap(array) {
42-
this._map = new ES6Map();
43-
forEach(array, this.put, this);
35+
// A minimal ES2015 Map implementation.
36+
// Should be bug/feature equivalent to the native implementations of supported browsers
37+
// (for the features required in Angular).
38+
// See https://kangax.github.io/compat-table/es6/#test-Map
39+
var nanKey = Object.create(null);
40+
function NgMapShim() {
41+
this._keys = [];
42+
this._values = [];
43+
this._lastKey = NaN;
44+
this._lastIndex = -1;
4445
}
45-
HashMap.prototype = {
46-
/**
47-
* Store key value pair
48-
* @param key key to store can be any type
49-
* @param value value to store can be any type
50-
*/
51-
put: function(key, value) {
52-
this._map.set(key, value);
46+
NgMapShim.prototype = {
47+
_idx: function(key) {
48+
if (key === this._lastKey) {
49+
return this._lastIndex;
50+
}
51+
this._lastKey = key;
52+
this._lastIndex = this._keys.indexOf(key);
53+
return this._lastIndex;
54+
},
55+
_transformKey: function(key) {
56+
return isNumberNaN(key) ? nanKey : key;
5357
},
54-
55-
/**
56-
* @param key
57-
* @returns {Object} the value for the key
58-
*/
5958
get: function(key) {
60-
return this._map.get(key);
59+
key = this._transformKey(key);
60+
var idx = this._idx(key);
61+
if (idx !== -1) {
62+
return this._values[idx];
63+
}
6164
},
65+
set: function(key, value) {
66+
key = this._transformKey(key);
67+
var idx = this._idx(key);
68+
if (idx === -1) {
69+
idx = this._lastIndex = this._keys.length;
70+
}
71+
this._keys[idx] = key;
72+
this._values[idx] = value;
6273

63-
/**
64-
* Remove the key/value pair
65-
* @param key
66-
*/
67-
remove: function(key) {
68-
var value = this._map.get(key);
69-
this._map.delete(key);
70-
return value;
74+
// Support: IE11
75+
// Do not `return this` to simulate the partial IE11 implementation
76+
},
77+
delete: function(key) {
78+
key = this._transformKey(key);
79+
var idx = this._idx(key);
80+
if (idx === -1) {
81+
return false;
82+
}
83+
this._keys.splice(idx, 1);
84+
this._values.splice(idx, 1);
85+
this._lastKey = NaN;
86+
this._lastIndex = -1;
87+
return true;
7188
}
7289
};
7390

74-
var $$HashMapProvider = [/** @this */function() {
91+
var NgMap = isFunction(window.Map) && toString.call(window.Map.prototype) === '[object Map]'
92+
? window.Map
93+
: NgMapShim;
94+
95+
var $$MapProvider = [/** @this */function() {
7596
this.$get = [function() {
76-
return HashMap;
97+
return NgMap;
7798
}];
7899
}];

src/auto/injector.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ function createInjector(modulesToLoad, strictDi) {
649649
var INSTANTIATING = {},
650650
providerSuffix = 'Provider',
651651
path = [],
652-
loadedModules = new HashMap(),
652+
loadedModules = new NgMap(),
653653
providerCache = {
654654
$provide: {
655655
provider: supportObject(provider),
@@ -757,7 +757,7 @@ function createInjector(modulesToLoad, strictDi) {
757757
var runBlocks = [], moduleFn;
758758
forEach(modulesToLoad, function(module) {
759759
if (loadedModules.get(module)) return;
760-
loadedModules.put(module, true);
760+
loadedModules.set(module, true);
761761

762762
function runInvokeQueue(queue) {
763763
var i, ii;

src/ng/animate.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var $$CoreAnimateJsProvider = /** @this */ function() {
6060
// this is prefixed with Core since it conflicts with
6161
// the animateQueueProvider defined in ngAnimate/animateQueue.js
6262
var $$CoreAnimateQueueProvider = /** @this */ function() {
63-
var postDigestQueue = new HashMap();
63+
var postDigestQueue = new NgMap();
6464
var postDigestElements = [];
6565

6666
this.$get = ['$$AnimateRunner', '$rootScope',
@@ -139,7 +139,7 @@ var $$CoreAnimateQueueProvider = /** @this */ function() {
139139
jqLiteRemoveClass(elm, toRemove);
140140
}
141141
});
142-
postDigestQueue.remove(element);
142+
postDigestQueue.delete(element);
143143
}
144144
});
145145
postDigestElements.length = 0;
@@ -154,7 +154,7 @@ var $$CoreAnimateQueueProvider = /** @this */ function() {
154154

155155
if (classesAdded || classesRemoved) {
156156

157-
postDigestQueue.put(element, data);
157+
postDigestQueue.set(element, data);
158158
postDigestElements.push(element);
159159

160160
if (postDigestElements.length === 1) {

src/ng/directive/select.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var SelectController =
1616
['$element', '$scope', /** @this */ function($element, $scope) {
1717

1818
var self = this,
19-
optionsMap = new HashMap();
19+
optionsMap = new NgMap();
2020

2121
self.selectValueMap = {}; // Keys are the hashed values, values the original values
2222

@@ -135,7 +135,7 @@ var SelectController =
135135
self.emptyOption = element;
136136
}
137137
var count = optionsMap.get(value) || 0;
138-
optionsMap.put(value, count + 1);
138+
optionsMap.set(value, count + 1);
139139
// Only render at the end of a digest. This improves render performance when many options
140140
// are added during a digest and ensures all relevant options are correctly marked as selected
141141
scheduleRender();
@@ -146,12 +146,12 @@ var SelectController =
146146
var count = optionsMap.get(value);
147147
if (count) {
148148
if (count === 1) {
149-
optionsMap.remove(value);
149+
optionsMap.delete(value);
150150
if (value === '') {
151151
self.emptyOption = undefined;
152152
}
153153
} else {
154-
optionsMap.put(value, count - 1);
154+
optionsMap.set(value, count - 1);
155155
}
156156
}
157157
};
@@ -601,9 +601,9 @@ var selectDirective = function() {
601601

602602
// Write value now needs to set the selected property of each matching option
603603
selectCtrl.writeValue = function writeMultipleValue(value) {
604-
var items = new HashMap(value);
605604
forEach(element.find('option'), function(option) {
606-
option.selected = isDefined(items.get(option.value)) || isDefined(items.get(selectCtrl.selectValueMap[option.value]));
605+
option.selected = !!value && (includes(value, option.value) ||
606+
includes(value, selectCtrl.selectValueMap[option.value]));
607607
});
608608
};
609609

src/ngAnimate/animateQueue.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
100100
return hasMatchingClasses(nA, cR) || hasMatchingClasses(nR, cA);
101101
});
102102

103-
this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$HashMap',
103+
this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$Map',
104104
'$$animation', '$$AnimateRunner', '$templateRequest', '$$jqLite', '$$forceReflow',
105105
'$$isDocumentHidden',
106-
function($$rAF, $rootScope, $rootElement, $document, $$HashMap,
106+
function($$rAF, $rootScope, $rootElement, $document, $$Map,
107107
$$animation, $$AnimateRunner, $templateRequest, $$jqLite, $$forceReflow,
108108
$$isDocumentHidden) {
109109

110-
var activeAnimationsLookup = new $$HashMap();
111-
var disabledElementsLookup = new $$HashMap();
110+
var activeAnimationsLookup = new $$Map();
111+
var disabledElementsLookup = new $$Map();
112112
var animationsEnabled = null;
113113

114114
function postDigestTaskFactory() {
@@ -294,7 +294,7 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
294294
bool = !disabledElementsLookup.get(node);
295295
} else {
296296
// (element, bool) - Element setter
297-
disabledElementsLookup.put(node, !bool);
297+
disabledElementsLookup.set(node, !bool);
298298
}
299299
}
300300
}
@@ -597,7 +597,7 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
597597
animationDetails.runner.end();
598598
/* falls through */
599599
case PRE_DIGEST_STATE:
600-
activeAnimationsLookup.remove(child);
600+
activeAnimationsLookup.delete(child);
601601
break;
602602
}
603603
}
@@ -607,7 +607,7 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
607607
function clearElementAnimationState(element) {
608608
var node = getDomNode(element);
609609
node.removeAttribute(NG_ANIMATE_ATTR_NAME);
610-
activeAnimationsLookup.remove(node);
610+
activeAnimationsLookup.delete(node);
611611
}
612612

613613
function isMatchingElement(nodeOrElmA, nodeOrElmB) {
@@ -717,7 +717,7 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
717717
var newValue = oldValue
718718
? extend(oldValue, details)
719719
: details;
720-
activeAnimationsLookup.put(node, newValue);
720+
activeAnimationsLookup.set(node, newValue);
721721
}
722722
}];
723723
}];

0 commit comments

Comments
 (0)