From a2c894b6bb5ebc5559d4ab6665d3387d44274d6e Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 16 Dec 2010 21:11:07 -0500 Subject: [PATCH] Added ability to override propCopy() parser functions --- lib/util.js | 17 +++++++------- test/test-util-prop-copy.js | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/util.js b/lib/util.js index 23aaaca..971c000 100644 --- a/lib/util.js +++ b/lib/util.js @@ -161,9 +161,10 @@ exports.propParse = function( data, options ) * * @return undefined */ -exports.propCopy = function( props, dest, result_data ) +exports.propCopy = function( props, dest, result_data, actions ) { result_data = result_data || {}; + actions = actions || {}; // initialize result_data var abstract_methods = @@ -179,9 +180,9 @@ exports.propCopy = function( props, dest, result_data ) abstract_map[ method ] = i; } - // default functionality - var actions = { - each: function( name, value ) + // substitute default functionality if needed + actions = { + each: actions.each || function( name, value ) { // methods can only be overridden with methods if ( ( dest[ name ] instanceof Function ) @@ -192,22 +193,22 @@ exports.propCopy = function( props, dest, result_data ) } }, - property: function( name, value ) + property: actions.property || function( name, value ) { dest[ name ] = value; }, - getter: function( name, func ) + getter: actions.getter || function( name, func ) { dest.__defineGetter__( name, func ); }, - setter: function( name, func ) + setter: actions.setter || function( name, func ) { dest.__defineSetter__( name, func ); }, - method: function( name, func, is_abstract ) + method: actions.method || function( name, func, is_abstract ) { var data = { abstractModified: false }, pre = dest[ name ]; diff --git a/test/test-util-prop-copy.js b/test/test-util-prop-copy.js index 38e8054..c81e20a 100644 --- a/test/test-util-prop-copy.js +++ b/test/test-util-prop-copy.js @@ -30,6 +30,11 @@ var assert = require( 'assert' ), var props = { one: 1, two: 2, + + method: function() {}, + + get val() {}, + set val() {}, }; var dest = {}; @@ -40,3 +45,45 @@ assert.ok( "All properties should be copied to the destination object" ); +var each = false, + prop = false, + method = false, + getter = false, + setter = false; + +propCopy( props, dest, null, { + each: function() + { + each = true; + }, + + property: function() + { + prop = true; + }, + + method: function() + { + method = true; + }, + + getter: function() + { + getter = true; + }, + + setter: function() + { + setter = true; + }, +} ); + +[ each, prop, method, getter, setter ].forEach( function( item, i ) +{ + assert.equal( + item, + true, + "Can override propCopy() parser functions [" + i + "]" + ); +}); +