8000 Do not throw when hashing non-extensible obj in ES6 · powercoder23/immutable-js@55f4d5e · GitHub
[go: up one dir, main page]

Skip to content

Commit 55f4d5e

Browse files
committed
Do not throw when hashing non-extensible obj in ES6
ES6 environments have WeakMap which lets us successfully hash non-extensible objects, so we shouldn't throw.
1 parent 1299a7c commit 55f4d5e

File tree

3 files changed

+81
-59
lines changed

3 files changed

+81
-59
lines changed

dist/immutable.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -811,21 +811,34 @@
811811
}
812812

813813
function hashJSObj(obj) {
814-
var hash = weakMap && weakMap.get(obj);
815-
if (hash) return hash;
814+
var hash;
815+
if (usingWeakMap) {
816+
hash = weakMap.get(obj);
817+
if (hash !== undefined) {
818+
return hash;
819+
}
820+
}
816821

817822
hash = obj[UID_HASH_KEY];
818-
if (hash) return hash;
823+
if (hash !== undefined) {
824+
return hash;
825+
}
819826

820827
if (!canDefineProperty) {
821828
hash = obj.propertyIsEnumerable && obj.propertyIsEnumerable[UID_HASH_KEY];
822-
if (hash) return hash;
829+
if (hash !== undefined) {
830+
return hash;
831+
}
8 10000 23832

824833
hash = getIENodeHash(obj);
825-
if (hash) return hash;
834+
if (hash !== undefined) {
835+
return hash;
836+
}
826837
}
827838

828-
if (Object.isExtensible && !Object.isExtensible(obj)) {
839+
if (!usingWeakMap &&
840+
typeof Object.isExtensible === 'function' &&
841+
Object.isExtensible(obj) === false) {
829842
throw new Error('Non-extensible objects are not allowed as keys.');
830843
}
831844

@@ -834,7 +847,7 @@
834847
objHashUID = 0;
835848
}
836849

837-
if (weakMap) {
850+
if (usingWeakMap) {
838851
weakMap.set(obj, hash);
839852
} else if (canDefineProperty) {
840853
Object.defineProperty(obj, UID_HASH_KEY, {
@@ -843,7 +856,7 @@
843856
'writable': false,
844857
'value': hash
845858
});
846-
} else if (obj.propertyIsEnumerable &&
859+
} else if (obj.propertyIsEnumerable !== undefined &&
847860
obj.propertyIsEnumerable === obj.constructor.prototype.propertyIsEnumerable) {
848861
// Since we can't define a non-enumerable property on the object
849862
// we'll hijack one of the less-used non-enumerable properties to
@@ -853,7 +866,7 @@
853866
return this.constructor.prototype.propertyIsEnumerable.apply(this, arguments);
854867
};
855868
obj.propertyIsEnumerable[UID_HASH_KEY] = hash;
856-
} else if (obj.nodeType) {
869+
} else if (obj.nodeType !== undefined) {
857870
// At this point we couldn't get the IE `uniqueID` to use as a hash
858871
// and we couldn't use a non-enumerable property to exploit the
859872
// dontEnum bug so we simply add the `UID_HASH_KEY` on the node
@@ -890,7 +903,11 @@
890903
}
891904

892905
// If possible, use a WeakMap.
893-
var weakMap = typeof WeakMap === 'function' && new WeakMap();
906+
var usingWeakMap = typeof WeakMap === 'function';
907+
var weakMap;
908+
if (usingWeakMap) {
909+
weakMap = new WeakMap();
910+
}
894911

895912
var objHashUID = 0;
896913

0 commit comments

Comments
 (0)
0