From f0c77dae5cf60eea9a63182a3da481e403703d49 Mon Sep 17 00:00:00 2001 From: kangax Date: Wed, 23 Jul 2008 10:09:28 -0400 Subject: [PATCH] Subclass Hash#include --- CHANGELOG | 2 ++ src/hash.js | 8 ++++++++ test/unit/hash_test.js | 11 +++++++++++ 3 files changed, 21 insertions(+), 0 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 1b51da5..cb426af 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Subclass Hash#include to return proper results. [kangax, jddalton, Tobie Langel] + * Improve NodeList detection for Safari's $A function. [Garrett Smith, jddalton] * Use different tactic to sniff for Opera in order to avoid false positives in IE. [Tobie Langel, jddalton] diff --git a/src/hash.js b/src/hash.js index ed96637..d997be0 100644 --- a/src/hash.js +++ b/src/hash.js @@ -93,6 +93,14 @@ var Hash = Class.create(Enumerable, (function() { clone: function() { return new Hash(this); + }, + + include: function(key) { + // simulating poorly supported hasOwnProperty + return key in this._object && ( + !(key in Object.prototype) || + Object.prototype[key] !== this._object[key] + ); } } })()); diff --git a/test/unit/hash_test.js b/test/unit/hash_test.js index cc3d083..8a0a608 100644 --- a/test/unit/hash_test.js +++ b/test/unit/hash_test.js @@ -173,6 +173,17 @@ new Test.Unit.Runner({ var foo = new FooMaker('bar'); this.assertEqual("key=bar", new Hash(foo).toQueryString()); this.assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString()); + }, + + testInclude: function() { + + var h = $H({ foo: undefined, toString: function(){ return 'foo' }, a: 'foo' }); + + this.assert(h.include('foo')); // should give true for keys with undefined values + this.assert(!h.include('bar')); // but false for inexistent ones + this.assert(!h.include('valueOf')); + this.assert(h.include('toString')); // should work around IE DontEnum bug + this.assert(h.include('a')); } }); \ No newline at end of file -- 1.5.5