From 93043d002a5ec8646cbd9d5658434848a1d07a49 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Wed, 13 Feb 2013 12:14:20 -0500 Subject: [PATCH] Refactor: Data.prototype.access. Thanks to @RubyLouvre and @gibson042. Closes #1167 --- src/data.js | 41 +++++++++++++++++++++++++---------------- test/unit/data.js | 34 ++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/data.js b/src/data.js index f0f955c3c..f0699d2a7 100644 --- a/src/data.js +++ b/src/data.js @@ -108,24 +108,33 @@ Data.prototype = { cache : cache[ key ]; }, access: function( owner, key, value ) { - if ( value === undefined && (key && typeof key !== "object") ) { - // Assume this is a request to read the cached data + // In cases where either: + // + // 1. No key was specified + // 2. A string key was specified, but no value provided + // + // Take the "read" path and allow the get method to determine + // which value to return, respectively either: + // + // 1. The entire cache object + // 2. The data stored at the key + // + if ( key === undefined || + ((key && typeof key === "string") && value === undefined) ) { return this.get( owner, key ); - } else { - - // If only an owner was specified, return the entire - // cache object. - if ( key === undefined ) { - return this.get( owner ); - } - - // Allow setting or extending (existing objects) with an - // object of properties, or a key and val - this.set( owner, key, value ); - return value !== undefined ? value : key; } - // Otherwise, this is a read request. - return this.get( owner, key ); + + // [*]When the key is not a string, or both a key and value + // are specified, set or extend (existing objects) with either: + // + // 1. An object of properties + // 2. A key and value + // + this.set( owner, key, value ); + + // Since the "set" path can have two possible entry points + // return the expected data based on which path was taken[*] + return value !== undefined ? value : key; }, remove: function( owner, key ) { var i, l, name, diff --git a/test/unit/data.js b/test/unit/data.js index 4307ac2dc..64cd04e90 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -7,29 +7,47 @@ test("expando", function(){ }); test( "jQuery.data & removeData, expected returns", function() { - expect(2); + expect(4); + var elem = document.body; equal( - jQuery.data( document.body, "hello", "world" ), "world", + jQuery.data( elem, "hello", "world" ), "world", "jQuery.data( elem, key, value ) returns value" ); equal( - jQuery.removeData( document.body, "hello" ), undefined, + jQuery.data( elem, "hello" ), "world", + "jQuery.data( elem, key ) returns value" + ); + deepEqual( + jQuery.data( elem, { goodnight: "moon" }), { goodnight: "moon" }, + "jQuery.data( elem, key, obj ) returns obj" + ); + equal( + jQuery.removeData( elem, "hello" ), undefined, "jQuery.removeData( elem, key, value ) returns undefined" ); }); test( "jQuery._data & _removeData, expected returns", function() { - expect(2); + expect(4); + var elem = document.body; equal( - jQuery._data( document.body, "hello", "world" ), "world", - "jQuery.data( elem, key, value ) returns value" + jQuery._data( elem, "hello", "world" ), "world", + "jQuery._data( elem, key, value ) returns value" ); equal( - jQuery._removeData( document.body, "hello" ), undefined, - "jQuery.removeData( elem, key, value ) returns undefined" + jQuery._data( elem, "hello" ), "world", + "jQuery._data( elem, key ) returns value" + ); + deepEqual( + jQuery._data( elem, { goodnight: "moon" }), { goodnight: "moon" }, + "jQuery._data( elem, obj ) returns obj" + ); + equal( + jQuery._removeData( elem, "hello" ), undefined, + "jQuery._removeData( elem, key, value ) returns undefined" ); }); -- 2.39.5