1 /*jslint 2 browser: true, 3 nomen: false, 4 debug: true, 5 forin: true, 6 undef: true, 7 white: false, 8 onevar: false 9 */ 10 var sc, jQuery; 11 12 /* 13 Helpers for fundamental javascript stuff 14 */ 15 16 /* 17 Return a boolean value telling whether 18 the first argument is a string. 19 * @member sc.helpers 20 */ 21 sc.helpers.isString = function(thing) { 22 if (typeof thing === 'string') {return true;} 23 if (typeof thing === 'object' && thing !== null) { 24 var criterion = thing.constructor.toString().match(/string/i); 25 return (criterion !== null); 26 } 27 return false; 28 }; 29 30 /** 31 * @member sc.helpers 32 */ 33 sc.helpers.isNumber = function(chk) { 34 return typeof chk === 'number'; 35 }; 36 37 38 39 /* 40 http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C720080D723 41 * @member sc.helpers 42 */ 43 sc.helpers.isArray = function(obj) { 44 if (!obj || !obj.constructor) { // short-circuit this if it's falsey 45 return false; 46 } 47 48 if (obj.constructor.toString().indexOf("Array") === -1) { 49 return false; 50 } else { 51 return true; 52 } 53 }; 54 55 /* 56 Returns a copy of the object using the _.extend() method 57 * @member sc.helpers 58 */ 59 sc.helpers.clone = function(oldObj) { 60 return _.extend({}/* clone */, oldObj); 61 }; 62 63 /** 64 * @todo 65 * @member sc.helpers 66 */ 67 sc.helpers.each = function(arr, f) { 68 69 }; 70 71 /** 72 * We use this to do a form of inheritance, where the child inherits 73 * the methods and properties of the supertype 74 * 75 * @link https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Inheritance 76 * 77 * @param {object} child the child type 78 * @param {object} supertype the parent we inherit from 79 * @member sc.helpers 80 */ 81 sc.helpers.extend = function(child, supertype) 82 { 83 child.prototype.__proto__ = supertype.prototype; 84 }; 85 86 /** 87 * Designed to fill in default values for an options argument passed to a 88 * function. Merges the provided defaults with the passed object, using items 89 * from defaults if they don't exist in passed 90 * 91 * @param {object} defaults the default key/val pairs 92 * @param {object} passed the values provided to the calling method 93 * @returns {object} a set of key/vals that have defaults filled-in 94 * @member sc.helpers 95 */ 96 sc.helpers.defaults = function(defaults, passed) { 97 98 var args = defaults; 99 100 /* override the defaults if necessary */ 101 for (var key in passed) { 102 args[key] = passed[key]; 103 } 104 105 return args; 106 }; 107 108 109