1
0
Fork 0

Began adding util.propParse() to simplify design (supports scalar, arr and obj props and concrete methods)

closure/master
Mike Gerwitz 2010-12-04 13:59:06 -05:00
parent 729b977088
commit 4037cc1343
2 changed files with 117 additions and 0 deletions

View File

@ -85,6 +85,42 @@ exports.defineSecureProp = function( obj, prop, value )
} }
/**
* Parses object properties to determine how they should be interpreted in an
* Object Oriented manner
*
* @param {Object} data properties with names as the key
* @param {Object} options parser options and callbacks
*
* @return undefined
*/
exports.propParse = function( data, options )
{
var callback_prop = options.property || function() {},
callback_method = options.method || function() {},
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 ];
if ( value instanceof Function )
{
callback = callback_method;
}
else
{
callback = callback_prop;
}
// call the appropriate callback
callback( prop, value );
}
}
/** /**
* Copies properties to the destination object * Copies properties to the destination object
* *

View File

@ -0,0 +1,81 @@
/**
* Tests util.propParse
*
* Copyright (C) 2010 Mike Gerwitz
*
* This file is part of ease.js.
*
* ease.js is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mike Gerwitz
* @package test
*/
require( './common' );
var assert = require( 'assert' ),
propParse = require( '../lib/util' ).propParse;
var data = {
// scalars (properties)
propStr: 'string',
propBool: true,
propInt: 1,
propFloat: 1.23,
// array (property)
propArray: [],
// object (property)
propObj: {},
// concrete method
method: function() {},
};
var props = {},
methods = {};
propParse( data, {
property: function( name, value )
{
props[ name ] = value;
},
// concrete method
method: function( name, method )
{
methods[ name ] = method;
},
} );
// ensure properties were properly recognized
[ 'propStr', 'propBool', 'propInt', 'propFloat', 'propArray', 'propObj' ]
.forEach( function( item )
{
assert.equal(
props[ item ],
data[ item ],
"Property parser properly detects class properties"
);
});
assert.equal(
methods.method,
data.method,
"Property parser properly detects methods"
);