southbay
Version:
Cocoa-inspired key-value coding, binding, and observing for node
40 lines (39 loc) • 1.29 kB
JavaScript
/**
* Detect is an argument is a function
* @param {*} arg Something to test
* @returns {Bool} true of the arg is a function
*/
Function.isFunction = function(arg) {
return typeof arg === 'function';
};
/**
* Method to match Array.isArray syntax for the old favorite
* toString method of detecting a javascript object
* @param {*} arg Something to test
* @returns {Bool} true if the arg is an actual object
*/
Object.isObject = function(arg) {
return this.prototype.toString.call(arg) === '[object Object]';
};
/**
* Method to copy the declared, non-inherited, properties from one object to another.
* Used internally by Kvc.create to allow a properties object to be passed in by the user
* when making a new class instance
*/
Object.extend = function(to, from) {
for(var p in from) {
if(from.hasOwnProperty(p)) to[p] = from[p];
}
return to;
};
/**
* String method that replaces delimiters with passed in object values.
* As this method is on the String prototype `.expand({...})` can be called
* on any string
* @param {Object} data An object containing the data to insert into the string
* @returns {String} the modified string
*/
String.prototype.expand = function(data) {
return this.replace(/\$\{(.+?)\}/g,
function(match, key) {return data[key];});
};