From fd2b3ccc6d4cb045e678e9dabe057f3208447097 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 5 Dec 2010 20:50:13 -0500 Subject: [PATCH] Added support for getters/setters in propParse() --- lib/util.js | 28 ++++++++++++++++++++++------ test/test-util-prop-parse.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/lib/util.js b/lib/util.js index 8130ba1..2e5e308 100644 --- a/lib/util.js +++ b/lib/util.js @@ -94,20 +94,35 @@ exports.defineSecureProp = function( obj, prop, value ) * * @return undefined */ -exports.propSort = function( data, options ) +exports.propParse = function( data, options ) { - var callback_prop = options.property || function() {}, - callback_method = options.method || function() {}, - callback_abstract_method = options.abstractMethod || function() {}, + var fvoid = function() {}, + callback_prop = options.property || fvoid, + callback_method = options.method || fvoid, + callback_abstract_method = options.abstractMethod || fvoid, + callback_getter = options.getter || fvoid, + callback_setter = options.setter || fvoid, callback = null; // for each of the given properties, determine what type of property we're // dealing with (in the classic OO sense) for ( prop in data ) { - var value = data[ prop ]; + var value = data[ prop ], + getter = ( ( getset ) ? data.__lookupGetter__( prop ) : null ), + setter = ( ( getset ) ? data.__lookupSetter__( prop ) : null ); - if ( value instanceof Function ) + // getter/setter + if ( getter || setter ) + { + callback_getter( prop, getter ); + callback_setter( prop, setter ); + + // we're done + continue; + } + // method + else if ( value instanceof Function ) { // concrete or abstract? callback = ( exports.isAbstractMethod( value ) ) @@ -115,6 +130,7 @@ exports.propSort = function( data, options ) : callback_method ; } + // simple property else { callback = callback_prop; diff --git a/test/test-util-prop-parse.js b/test/test-util-prop-parse.js index 38f97d0..6191169 100644 --- a/test/test-util-prop-parse.js +++ b/test/test-util-prop-parse.js @@ -40,6 +40,10 @@ var data = { // object (property) propObj: {}, + // getter/setter + get someFoo() {}, + set someFoo() {}, + // concrete method method: function() {}, @@ -50,7 +54,9 @@ var data = { var props = {}, methods = {}, - amethods = {}; + amethods = {}, + getters = {}, + setters = {}; util.propParse( data, { property: function( name, value ) @@ -67,7 +73,17 @@ util.propParse( data, { abstractMethod: function( name, def ) { amethods[ name ] = def; - } + }, + + getter: function( name, func ) + { + getters[ name ] = func; + }, + + setter: function( name, func ) + { + setters[ name ] = func; + }, } ); @@ -94,3 +110,15 @@ assert.equal( "Property parser properly detects abstract methods" ); +assert.equal( + getters.someFoo, + data.__lookupGetter__( 'someFoo' ), + "Property parser properly detects getters" +); + +assert.equal( + setters.someFoo, + data.__lookupSetter__( 'someFoo' ), + "Property parser properly detects setters" +); +