Object.js
Summary
Contains extensions to standard JavaScript objects - Array and Object.
Any other classes implicitly extend Object class and hence inherit the following important functionality.
This is part of Troika.ASP Framework - web development MVC framework for ASP 3.0.
Please visit www.troika-asp.com for more details.
Version: $Revision: 1.3 $
Author: Pavel Chuchev pav@troika-asp.com
Array.prototype.contains = function (match) {
for (var i = 0; i < this.length; i++) {
if (this[i] == match) {
return true;
}
}
return false;
};
Object.prototype.toXML = function () {
var numRE = /^\d+$/;
var slashRE = /\
function marshal(variable, level, name) {
var result = [];
level = level ? level : 0;
var tag = "";
if (name) {
tag = name.replace(slashRE, "_");
if (numRE.test(tag)) {
tag = "e" + name.replace(slashRE, "_");
}
}
else {
tag = getClassName(variable).replace(slashRE, "_");
}
switch (typeof (variable)) {
case "object":
openTag(result, tag, level);
nl(result);
processObject(result, variable, level);
tab(result, level);
closeTag(result, tag);
break;
case "string":
if (!variable.length) {
break;
}
openTag(result, tag, level);
if (tag == "_xml_") {
result.push(variable);
}
else {
result.push(xmlSafe(variable));
}
closeTag(result, tag);
break;
case "boolean":
openTag(result, tag, level);
result.push(variable ? "true" : "false");
closeTag(result, tag);
nl(result);
break;
case "number":
openTag(result, tag, level);
result.push(variable);
closeTag(result, tag);
}
return result.join("");
}
function openTag(result, tag, level) {
tab(result, level);
result.push("<");
result.push(tag);
result.push(">");
}
function closeTag(result, tag) {
result.push("</");
result.push(tag);
result.push(">");
nl(result);
}
function nl(result) {
result.push("\r\n");
}
function getClassName(variable) {
var funcDef = "" + variable.constructor;
var start = funcDef.indexOf(" ") + 1;
var end = funcDef.indexOf("(");
return funcDef.substring(start, end);
}
function tab(result, level) {
for (var i = 0; i < level; i++) {
result.push("\t");
}
}
function xmlSafe(xml) {
xml = xml.replace(/&/g, "&");
xml = xml.replace(/</g, "<");
xml = xml.replace(/>/g, ">");
xml = xml.replace(/`/g, "'");
xml = xml.replace(/\"/g, """);
xml = xml.replace(/\'/g, "'");
return xml;
}
function processObject(result, variable, level) {
for (var key in variable) {
var value = variable[key];
if (value) {
var tabs = level + 1;
result.push(marshal(value, tabs, key));
}
}
return result.join("");
}
return marshal(this);
};
/**
* Converts the instance to JavaScript literal object notation in JSON string format.
*
* @return The serialized object as JSON string.
* @type String
*/
Object.prototype.toString = function () {
return JSON.stringify(this);
};
/**
* Takes JSON string and converts it back to native JavaScript object.
*
* @param {String} str JSON string.
* @return The deserialized object instance.
* @type Object
*/
Object.prototype.valueOf = function (str) {
var obj = eval("(" + str + ")");
for (var k in obj) {
this[k] = obj[k];
}
return this;
};
/**
* Converts each instance properties to string.
*/
Object.prototype.flatten = function () {
for (var k in this) {
if (typeof (this[k]) != "function") {
this[k] = this[k] ? "" + this[k] : "";
}
}
};
/**
* Copies values of corresponding properties from fromObj to this instance.
*
* @param {Object} fromObj The instance to copy property values from.
*/
Object.prototype.assign = function (fromObj) {
for (var i in this) {
var value = fromObj[i];
if (value && typeof (value) != "function") {
this[i] = value ? "" + value : "";
}
}
};
/**
* Returns an array of all properties of this object.
*
* @return The array of all properties of this object.
* @type Array
*/
Object.prototype.properties = function () {
var result = [];
for (var k in this) {
if (typeof (this[k]) != "function") {
result.push(k);
}
}
return result;
};
www.troika-asp.com
Documentation generated on Sun Jun 15 17:59:32 2008