Permalink
Browse files

(#5990) - Use native Map/Set

  • Loading branch information...
1 parent 9b3a3c2 commit 4e35f43c1b0b0338ef2e6bcdc9c9e20068b2fc21 @nolanlawson nolanlawson committed with daleharvey Dec 16, 2016
@@ -0,0 +1,44 @@
+function mangle(key) {
+ return '$' + key;
+}
+function unmangle(key) {
+ return key.substring(1);
+}
+function Map() {
+ this._store = {};
+}
+Map.prototype.get = function (key) {
+ var mangled = mangle(key);
+ return this._store[mangled];
+};
+Map.prototype.set = function (key, value) {
+ var mangled = mangle(key);
+ this._store[mangled] = value;
+ return true;
+};
+Map.prototype.has = function (key) {
+ var mangled = mangle(key);
+ return mangled in this._store;
+};
+Map.prototype.delete = function (key) {
+ var mangled = mangle(key);
+ var res = mangled in this._store;
+ delete this._store[mangled];
+ return res;
+};
+Map.prototype.forEach = function (cb) {
+ var keys = Object.keys(this._store);
+ for (var i = 0, len = keys.length; i < len; i++) {
+ var key = keys[i];
+ var value = this._store[key];
+ key = unmangle(key);
+ cb(value, key);
+ }
+};
+Object.defineProperty(Map.prototype, 'size', {
+ get: function () {
+ return Object.keys(this._store).length;
+ }
+});
+
+export default Map;
@@ -0,0 +1,20 @@
+import Map from './Map';
+
+function Set(array) {
+ this._store = new Map();
+
+ // init with an array
+ if (array && Array.isArray(array)) {
+ for (var i = 0, len = array.length; i < len; i++) {
+ this.add(array[i]);
+ }
+ }
+}
+Set.prototype.add = function (key) {
+ return this._store.set(key, true);
+};
+Set.prototype.has = function (key) {
+ return this._store.has(key);
+};
+
+export default Set;
@@ -1,65 +1,27 @@
// based on https://github.com/montagejs/collections
-function mangle(key) {
- return '$' + key;
-}
-function unmangle(key) {
- return key.substring(1);
-}
-function LazyMap() {
- this._store = {};
-}
-LazyMap.prototype.get = function (key) {
- var mangled = mangle(key);
- return this._store[mangled];
-};
-LazyMap.prototype.set = function (key, value) {
- var mangled = mangle(key);
- this._store[mangled] = value;
- return true;
-};
-LazyMap.prototype.has = function (key) {
- var mangled = mangle(key);
- return mangled in this._store;
-};
-LazyMap.prototype.delete = function (key) {
- var mangled = mangle(key);
- var res = mangled in this._store;
- delete this._store[mangled];
- return res;
-};
-LazyMap.prototype.forEach = function (cb) {
- var keys = Object.keys(this._store);
- for (var i = 0, len = keys.length; i < len; i++) {
- var key = keys[i];
- var value = this._store[key];
- key = unmangle(key);
- cb(value, key);
- }
-};
-Object.defineProperty(LazyMap.prototype, 'size', {
- get: function () {
- return Object.keys(this._store).length;
- }
-});
+/* global Map,Set */
-function LazySet(array) {
- this._store = new LazyMap();
+import ShimmedMap from './Map';
+import ShimmedSet from './Set';
+import supportsMapAndSet from './supportsMapAndSet';
- // init with an array
- if (array && Array.isArray(array)) {
- for (var i = 0, len = array.length; i < len; i++) {
- this.add(array[i]);
- }
+var ExportedSet;
+var ExportedMap;
+
+if (process.env.COVERAGE) { // don't penalize ourselves on coverage
+ ExportedSet = ShimmedSet;
+ ExportedMap = ShimmedMap;
+} else {
+ if (supportsMapAndSet()) { // prefer built-in Map/Set
+ ExportedSet = Set;
+ ExportedMap = Map;
+ } else { // fall back to our polyfill
+ ExportedSet = ShimmedSet;
+ ExportedMap = ShimmedMap;
}
}
-LazySet.prototype.add = function (key) {
- return this._store.set(key, true);
-};
-LazySet.prototype.has = function (key) {
- return this._store.has(key);
-};
export {
- LazySet as Set,
- LazyMap as Map
+ ExportedSet as Set,
+ ExportedMap as Map
};
@@ -0,0 +1,12 @@
+/* global Map,Set,Symbol */
+// Based on https://kangax.github.io/compat-table/es6/ we can sniff out
+// incomplete Map/Set implementations which would otherwise cause our tests to fail.
+// Notably they fail in IE11 and iOS 8.4, which this prevents.
+function supportsMapAndSet() {
+ if (typeof Symbol === 'undefined' || typeof Map === 'undefined' || typeof Set === 'undefined') {
+ return false;
+ }
+ var prop = Object.getOwnPropertyDescriptor(Map, Symbol.species);
+ return prop && 'get' in prop && Map[Symbol.species] === Map;
+}
+export default supportsMapAndSet;

0 comments on commit 4e35f43

Please sign in to comment.