Skip to content

Commit

Permalink
one more fix for #124 - undefined on .get
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 26, 2015
1 parent 0f4435b commit ed8d9f9
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 23 deletions.
2 changes: 1 addition & 1 deletion library/modules/$.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function(NAME, wrapper, methods, common, IS_MAP, IS_WEAK){
$.each.call('add,clear,delete,forEach,get,has,set,keys,values,entries'.split(','),function(KEY){
var IS_ADDER = KEY == 'add' || KEY == 'set';
if(KEY in proto && !(IS_WEAK && KEY == 'clear'))hide(C.prototype, KEY, function(a, b){
if(!IS_ADDER && IS_WEAK && !isObject(a))return false;
if(!IS_ADDER && IS_WEAK && !isObject(a))return KEY == 'get' ? undefined : false;
var result = this._c[KEY](a === 0 ? 0 : a, b);
return IS_ADDER ? this : result;
});
Expand Down
6 changes: 3 additions & 3 deletions modules/$.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function(NAME, wrapper, methods, common, IS_MAP, IS_WEAK){
} : KEY == 'has' ? function has(a){
return IS_WEAK && !isObject(a) ? false : fn.call(this, a === 0 ? 0 : a);
} : KEY == 'get' ? function get(a){
return IS_WEAK && !isObject(a) ? false : fn.call(this, a === 0 ? 0 : a);
return IS_WEAK && !isObject(a) ? undefined : fn.call(this, a === 0 ? 0 : a);
} : KEY == 'add' ? function add(a){ fn.call(this, a === 0 ? 0 : a); return this; }
: function set(a, b){ fn.call(this, a === 0 ? 0 : a, b); return this; }
);
Expand All @@ -34,7 +34,7 @@ module.exports = function(NAME, wrapper, methods, common, IS_MAP, IS_WEAK){
} else {
var instance = new C
// early implementations not supports chaining
, CHAINING = instance[ADDER](IS_WEAK ? {} : -0, 1)
, HASNT_CHAINING = instance[ADDER](IS_WEAK ? {} : -0, 1) != instance
// V8 ~ Chromium 40- weak-collections throws on primitives, but should return false
, THROWS_ON_PRIMITIVES = fails(function(){ instance.has(1); })
// most early implementations doesn't supports iterables, most modern - not close it correctly
Expand All @@ -59,7 +59,7 @@ module.exports = function(NAME, wrapper, methods, common, IS_MAP, IS_WEAK){
fixMethod('has');
IS_MAP && fixMethod('get');
}
if(BUGGY_ZERO || CHAINING !== instance)fixMethod(ADDER);
if(BUGGY_ZERO || HASNT_CHAINING)fixMethod(ADDER);
// weak collections should not contains .clear method
if(IS_WEAK && proto.clear)delete proto.clear;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/library/$.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = function(NAME, wrapper, methods, common, IS_MAP, IS_WEAK){
$.each.call('add,clear,delete,forEach,get,has,set,keys,values,entries'.split(','),function(KEY){
var IS_ADDER = KEY == 'add' || KEY == 'set';
if(KEY in proto && !(IS_WEAK && KEY == 'clear'))hide(C.prototype, KEY, function(a, b){
if(!IS_ADDER && IS_WEAK && !isObject(a))return false;
if(!IS_ADDER && IS_WEAK && !isObject(a))return KEY == 'get' ? undefined : false;
var result = this._c[KEY](a === 0 ? 0 : a, b);
return IS_ADDER ? this : result;
});
Expand Down
106 changes: 94 additions & 12 deletions tests/es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/library.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/library/es6.weak-map.ls
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test 'WeakMap#get' (assert)!->
assert.strictEqual M.get(a), 42, 'WeakMap .get() return value'
M.delete a
assert.strictEqual M.get(a), void, 'WeakMap .get() after .delete() return undefined'
assert.ok (try !M.get 1), 'return false on primitive'
assert.ok (try void is M.get 1), 'return undefined on primitive'

test 'WeakMap#has' (assert)!->
assert.isFunction WeakMap::has
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/tests/es6.weak-map.ls
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test 'WeakMap#get' (assert)!->
assert.strictEqual M.get(a), 42, 'WeakMap .get() return value'
M.delete a
assert.strictEqual M.get(a), void, 'WeakMap .get() after .delete() return undefined'
assert.ok (try !M.get 1), 'return false on primitive'
assert.ok (try void is M.get 1), 'return undefined on primitive'

test 'WeakMap#has' (assert)!->
assert.isFunction WeakMap::has
Expand Down

0 comments on commit ed8d9f9

Please sign in to comment.