From 4037cc1343bdd3c715cbbfe147891adad2eeab89 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sat, 4 Dec 2010 13:59:06 -0500 Subject: [PATCH] Began adding util.propParse() to simplify design (supports scalar, arr and obj props and concrete methods) --- lib/util.js | 36 ++++++++++++++++ test/test-util-prop-parse.js | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 test/test-util-prop-parse.js diff --git a/lib/util.js b/lib/util.js index 4ee8d7b..15bfef0 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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 * diff --git a/test/test-util-prop-parse.js b/test/test-util-prop-parse.js new file mode 100644 index 0000000..dc6e45b --- /dev/null +++ b/test/test-util-prop-parse.js @@ -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 . + * + * @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" +); +