diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/utils.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 00000000..7fc2a6b9 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,54 @@ +var utils = { + // probably doesn't handle some cases correctly, but it works fine for what + // we have now + deepCopy: function(original) { + var result; + if (typeof original == 'object') { + if (original === null) { + result = null; + } else { + result = original.constructor === Array ? [] : {}; + for (var i in original) + if (original.hasOwnProperty(i)) + result[i] = this.deepCopy(original[i]); + } + } else { + result = original; + } + + return result; + }, + + /* + * Extends 'original' with 'ext'. If a function in 'ext' also exists in + * 'original', let the 'original' function be accessible in the new object + * via a ._super(functionName as String) method. _Cannot_ be used on its + * result to achieve 'two-level' inheritance. + */ + extendWithSuper: function(original, ext) { + var result = this.deepCopy(original); + var tmpSuper = result._super; + result._superFunctions = {}; + result._super = function(fname) { return this._superFunctions[fname].bind(this); } + for (var i in ext) + if (ext.hasOwnProperty(i)) { + if (typeof ext[i] == 'function' && typeof original[i] == 'function') + result._superFunctions[i] = this.deepCopy(original[i]); + result[i] = this.deepCopy(ext[i]); + } + return result; + }, + + /* + * Takes a dot-notation object string and call the function + * that it points to with the correct value for 'this'. + */ + invokeCommandString: function(str, argArray) { + var components = str.split('.'); + var obj = window; + for (var i = 0; i < components.length - 1; i++) + obj = obj[components[i]]; + var func = obj[components.pop()]; + return func.apply(obj, argArray); + }, +}; |
