From 2da5b44e4a603cf699279d7ef6b8d5d1b07b17ad Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Fri, 4 May 2012 07:30:28 -0400 Subject: [PATCH 1/2] [CB-634] need an sprintf implementation in utils * added utils.format() to utils.js * added test cases for utils.format() in test.utils.js * added some additional safety checks in the packager for the debug bundles * drive-by adding --verbose to .wr file Note that I also "refactored the source" of the utils.js module, which I'll be discussing on the m/l. --- .wr | 1 + build/packager.js | 7 ++ lib/common/utils.js | 237 ++++++++++++++++++++++++++++---------------- test/test.utils.js | 75 ++++++++++++++ 4 files changed, 237 insertions(+), 83 deletions(-) diff --git a/.wr b/.wr index 02bb7897..be17e365 100644 --- a/.wr +++ b/.wr @@ -25,6 +25,7 @@ --stdoutcolor blue --stderrcolor red +--verbose jake diff --git a/build/packager.js b/build/packager.js index 33743c1c..def6fe52 100644 --- a/build/packager.js +++ b/build/packager.js @@ -166,6 +166,13 @@ function writeContents(oFile, fileName, contents, debug) { contents += '\n//@ sourceURL=' + fileName contents = 'eval(' + JSON.stringify(contents) + ')' + + // this bit makes it easier to identify modules + // with syntax errors in them + var handler = 'console.log("exception: in ' + fileName + ': " + e);' + handler += 'console.log(e.stack);' + + contents = 'try {' + contents + '} catch(e) {' + handler + '}' } else { diff --git a/lib/common/utils.js b/lib/common/utils.js index b2ee8cb2..97d09343 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -1,3 +1,147 @@ +var utils = exports + +/** + * Returns an indication of whether the argument is an array or not + */ +utils.isArray = function(a) { + return Object.prototype.toString.call(a) == '[object Array]'; +} + +/** + * Returns an indication of whether the argument is a Date or not + */ +utils.isDate = function(d) { + return Object.prototype.toString.call(d) == '[object Date]'; +} + +/** + * Does a deep clone of the object. + */ +utils.clone = function(obj) { + if(!obj || typeof obj == 'function' || utils.isDate(obj) || typeof obj != 'object') { + return obj; + } + + var retVal, i; + + if(utils.isArray(obj)){ + retVal = []; + for(i = 0; i < obj.length; ++i){ + retVal.push(utils.clone(obj[i])); + } + return retVal; + } + + retVal = {}; + for(i in obj){ + if(!(i in retVal) || retVal[i] != obj[i]) { + retVal[i] = utils.clone(obj[i]); + } + } + return retVal; +} + +/** + * Returns a wrappered version of the function + */ +utils.close = function(context, func, params) { + if (typeof params == 'undefined') { + return function() { + return func.apply(context, arguments); + }; + } else { + return function() { + return func.apply(context, params); + }; + } +} + +/** + * Create a UUID + */ +utils.createUUID = function() { + return UUIDcreatePart(4) + '-' + + UUIDcreatePart(2) + '-' + + UUIDcreatePart(2) + '-' + + UUIDcreatePart(2) + '-' + + UUIDcreatePart(6); +} + +/** + * Extends a child object from a parent object using classical inheritance + * pattern. + */ +utils.extend = function() { + // proxy used to establish prototype chain + var F = function() {}; + // extend Child from Parent + return function(Child, Parent) { + F.prototype = Parent.prototype; + Child.prototype = new F(); + Child.__super__ = Parent.prototype; + Child.prototype.constructor = Child; + }; +} + +/** + * Alerts a message in any available way: alert or console.log. + */ +utils.alert = function(msg) { + if (alert) { + alert(msg); + } else if (console && console.log) { + console.log(msg); + } +} + +/** + * Formats a string and arguments following it ala sprintf() + * + * format chars: + * %j - format arg as JSON + * %o - format arg as JSON + * %c - format arg as '' + * %% - replace with '%' + * any other char following % will format it's + * arg via toString(). + * + * for rationale, see FireBug's Console API: + * http://getfirebug.com/wiki/index.php/Console_API + */ +utils.format = function(formatString /* ,... */) { + if (formatString == null) return "" + if (arguments.length == 1) return formatString.toString() + + var pattern = /(.*?)%(.)(.*)/ + var rest = formatString.toString() + var result = [] + var args = [].slice.call(arguments,1) + + while (args.length) { + var arg = args.shift() + var match = pattern.exec(rest) + + if (!match) break + + rest = match[3] + + result.push(match[1]) + + if (match[2] == '%') { + result.push('%') + args.unshift(arg) + continue + } + + result.push(formatted(arg, match[2])) + } + + result.push(rest) + + return result.join('') +} + +//------------------------------------------------------------------------------ function UUIDcreatePart(length) { var uuidpart = ""; for (var i=0; i Date: Mon, 7 May 2012 10:27:05 -0400 Subject: [PATCH 2/2] revert to pre-CB-634 version of utils.extend While refactoring the source for CB-634, I broke utils.extend by removing it's outer function invocation. Something about that function still looks wrong, but this implementation should be the same as before, instead of the broken version that was introduced in CB-634. --- lib/common/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/common/utils.js b/lib/common/utils.js index 97d09343..c5bff130 100644 --- a/lib/common/utils.js +++ b/lib/common/utils.js @@ -71,7 +71,7 @@ utils.createUUID = function() { * Extends a child object from a parent object using classical inheritance * pattern. */ -utils.extend = function() { +utils.extend = (function() { // proxy used to establish prototype chain var F = function() {}; // extend Child from Parent @@ -81,7 +81,7 @@ utils.extend = function() { Child.__super__ = Parent.prototype; Child.prototype.constructor = Child; }; -} +}()) /** * Alerts a message in any available way: alert or console.log.