1 // ==========================================================================
  2 // Project:   SproutCore Unit Testing Library
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 // These utility methods are included from the SproutCore's foundation to 
  9 // make it easier write unit tests.  They only install themselves if a method
 10 // has not already been defined.
 11 
 12 if (!String.prototype.camelize) {
 13   String.prototype.camelize = function camelize() {
 14     var ret = this.replace(SC.STRING_TITLEIZE_REGEXP, 
 15       function(str,separater,character) { 
 16         return (character) ? character.toUpperCase() : '' ;
 17       }) ;
 18     var first = ret.charAt(0), lower = first.toLowerCase() ;
 19     return (first !== lower) ? (lower + ret.slice(1)) : ret ;
 20   };
 21 }
 22 
 23 if (!String.prototype.trim) {
 24   String.prototype.trim = function trim() {
 25     return this.replace(/^\s+|\s+$/g,"");
 26   } ;
 27 }
 28 
 29 if (!String.prototype.fmt) {
 30   String.prototype.fmt = function fmt() {
 31     // first, replace any ORDERED replacements.
 32     var args = arguments;
 33     var idx  = 0; // the current index for non-numerical replacements
 34     return this.replace(/%@([0-9]+)?/g, function(s, argIndex) {
 35       argIndex = (argIndex) ? parseInt(argIndex,0)-1 : idx++ ;
 36       s =args[argIndex];
 37       return ((s===null) ? '(null)' : (s===undefined) ? '' : s).toString(); 
 38     }) ;
 39   } ;
 40 }
 41 
 42 if (!Array.prototype.uniq) {
 43   Array.prototype.uniq = function uniq() {
 44     var ret = [], len = this.length, item, idx ;
 45     for(idx=0;idx<len;idx++) {
 46       item = this[idx];
 47       if (ret.indexOf(item) < 0) ret.push(item);
 48     }
 49     return ret ;
 50   };
 51 }
 52 
 53 if (!String.prototype.w) {
 54   String.prototype.w = function w() { 
 55     var ary = [], ary2 = this.split(' '), len = ary2.length ;
 56     for (var idx=0; idx<len; ++idx) {
 57       var str = ary2[idx] ;
 58       if (str.length !== 0) ary.push(str) ; // skip empty strings
 59     }
 60     return ary ;
 61   };
 62 }
 63