util.js
Summary
Shared utilities for the Mozile project.
Version: 0.8
$Id: overview-summary-util.js.html,v 1.15 2008/02/20 18:47:09 jameso Exp $
Author: James A. Overton
mozile.provide("mozile.util.*");
mozile.util = new Object();
mozile.util.prototype = new mozile.Module;
mozile.util.dumpKeys = function(obj) {
var keys = new Array();
for(var key in obj) keys.push(key);
keys.sort();
return keys.join(", ");
}
mozile.util.dumpValues = function(obj) {
var result = new Array();
for(var key in obj) {
result.push( key +" = "+ obj[key] );
}
result.sort();
return result.join("\n");
}
mozile.util.capitalize = function(string) {
if(!string) return string;
return string.replace(/\w+/g, function(a){
return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
});
}
mozile.util.pad = function(string, length, left) {
if(!string) string = " ";
else string = String(string);
if(!this._memory) this._memory = new Object();
var id = string +":"+ length +":"+ left;
if(this._memory[id]) return this._memory[id];
var space = "";
if(string.length < length) {
for(var s=0; s < length - string.length; s++) space += " ";
}
var result;
if(left) result = space + string;
else result = string + space;
this._memory[id] = result;
return result;
}
mozile.util.clone = function(obj, deep) {
var clone = new obj.constructor();
for(var property in obj) {
if(!deep)
clone[property] = obj[property];
else if(typeof obj[property] == "object")
clone[property] = this.clone(obj[property], deep);
else
clone[property] = obj[property];
}
return clone;
}
mozile.util.parseJSON = function(obj) {
try {
if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(obj)) {
return eval('(' + obj + ')');
}
} catch (e) {
}
throw new SyntaxError("parseJSON");
}
/**
* Take an object and return a JSON string.
* @param obj The object to convert.
* @type String
*/
mozile.util.json = function(obj) {
switch(typeof(obj)) {
case "boolean": return this.booleanToJSON(obj);
case "number": return this.numberToJSON(obj);
case "string": return this.stringToJSON(obj);
case "object":
switch(obj.constructor) {
case Array: return this.arrayToJSON(obj);
case Date: return this.dateToJSON(obj);
default: return this.objectToJSON(obj);
}
break;
}
return "";
}
/**
* Take an array and return a JSON string.
* @param {Array} obj The object to convert.
* @type String
*/
mozile.util.arrayToJSON = function(obj) {
var a = ['['], b, i, l = obj.length, v;
for (i = 0; i < l; i += 1) {
v = obj[i];
switch (typeof v) {
case 'undefined':
case 'function':
case 'unknown':
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : this.json(v));
b = true;
}
}
a.push(']');
return a.join('');
}
/**
* Take a boolean and return a JSON string.
* @param {Boolean} obj The object to convert.
* @type String
*/
mozile.util.booleanToJSON = function(obj) {
return String(obj);
}
/**
* Take a date and return a JSON string.
* @param {Date} obj The object to convert.
* @type String
*/
mozile.util.dateToJSON = function(obj) {
function f(n) {
return n < 10 ? '0' + n : n;
}
return '"' + obj.getFullYear() + '-' +
f(obj.getMonth() + 1) + '-' +
f(obj.getDate()) + 'T' +
f(obj.getHours()) + ':' +
f(obj.getMinutes()) + ':' +
f(obj.getSeconds()) + '"';
}
/**
* Take a number and return a JSON string.
* @param {Number} obj The object to convert.
* @type String
*/
mozile.util.numberToJSON = function(obj) {
return isFinite(obj) ? String(obj) : "null";
}
/**
* Take an object and return a JSON string.
* @param {Object} obj The object to convert.
* @type String
*/
mozile.util.objectToJSON = function(obj) {
var a = ['{'], b, i, v;
for (i in obj) {
if (obj.hasOwnProperty(i)) {
v = obj[i];
switch (typeof v) {
case 'undefined':
case 'function':
case 'unknown':
break;
default:
if (b) {
a.push(',');
}
a.push(this.json(i), ':',
v === null ? "null" : this.json(v));
b = true;
}
}
}
a.push('}');
return a.join('');
}
/**
* An associative array of string replacements.
* @type Object
*/
mozile.util.m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
/**
* Take a string and return a JSON string.
* @param {String} obj The object to convert.
* @type String
*/
mozile.util.stringToJSON = function (obj) {
if (/["\\\x00-\x1f]/.test(obj)) {
return '"' + obj.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = mozile.util.m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + obj + '"';
};
Documentation generated by
JSDoc on Wed Feb 20 13:25:28 2008