Ext.ns('ieee.util');


/**
 * Strips slashes out of a string.
 * @param {String} str String to strip
 * @return {String} the stripped string
 */
ieee.util.stripSlashes = function(str) {
	return str.replace(/\\/g, '');
};

/**
 * Strips slashes out of nodes that match the passed in selector
 * @param {String} sel CSS selector to select nodes to strip slashes out of.
 */
ieee.util.stripSlashesOf = function(sel) {
	Ext.select(sel).each(function() {
		this.dom.innerHTML = ieee.util.stripSlashes(this.dom.innerHTML);
	});
};



/**
 * Helper function to turn arrays into strings with commas and the word "and"
 * where appropriate. Useful for XTemplates and other lists of stuff that needs
 * to be converted into English.<br/>
 * Example:
 * <pre>
 * arr = ['bob', 'sally', 'jane'];
 * wt.util.listify(arr); //returns "bob, sally, and jane"
 * wt.util.listify(arr, {noAnd:true}); //returns "bob, sally, jane"
 * wt.util.listify(arr, {noAnd: true, delimiter: ' | '}); //returns "bob | sally | jane"
 * wt.util.listify(['bob']); //returns "bob"
 * wt.util.listify(['bob', 'sally']); //returns "bob and sally"
 * </pre>
 * @param {Array} arr The array to "listify".
 * @param {Object} opts An object with any of the following properties:<ul>
 *  <li>
 *   <tt>noAnd</tt>: omits the word "and" from output string.
 *  </li>
 *  <li>
 *   <tt>delimiter</tt>: The string with which to separate the items. Defaults to a single comma and a space (', ').
 *  </li>
 * </ul>
 * @stable
 */
ieee.util.listify = function(arr, opts) {
     //deal with defaults:
     opts = opts || {};
     Ext.applyIf(opts, {
         noAnd: false
         ,delimiter: ', '
     });

     if (opts.noAnd) {
         return arr.join(opts.delimiter);
     }
     if (arr.length <= 2) {
         return arr.join(' and ');
     }
     var out = arr.slice(0,arr.length-1).join(opts.delimiter); //join all but the last element with the delimiter
     out += String.format("{0}and {1}", opts.delimiter, arr[arr.length-1]);
     return out;
};
