Array.prototype.indexOf || (Array.prototype.indexOf = function(value) {
for (var i in this) {
if (this[i] == value) {
return i;
}
}
return -1;
});
Array.prototype.remove = function(b) {
var a = this.indexOf(b);
if (a >= 0) {
this.splice(a, 1);
return true;
}
return false;
};
window.jlUtil = {
getJSON: function(obj, skip_arr) {
if (typeof obj == "number"
|| typeof obj == "boolean") {
return obj;
} else if (typeof obj == "string") {
return "'" + obj.replace("'", "\\'") + "'";
} else if (!obj) {
return "''";
}
var json = [];
skip_arr = skip_arr ? skip_arr : [];
for (var key in obj) {
//skip function and the keys in skip arr
if (typeof obj[key] == "function"
|| skip_arr.indexOf(key) != -1) {
continue;
}
//in Array loop
if (!isNaN(key) || key.constructor != String) {
json.push(jlUtil.getJSON(obj[key], skip_arr));
continue;
}
//else recurrency
json.push("'" + key + "':" + jlUtil.getJSON(obj[key], skip_arr));
}
if (Object.prototype.toString.apply(obj) === '[object Array]') {
return "[" + json.join(",") + "]";
} else {
return "{" + json.join(",") + "}";
}
},
extend: function(obj) {
if (arguments.length == 1) {
this.extend(this, obj);
return this;
} else if (arguments.length >= 2) {
var dst = arguments[0];
//extend the rest args to args[0]
for (var i = 1; i < arguments.length; ++i) {
for (var item in arguments[i]) {
var tmp = arguments[i][item];
if (typeof arguments[i][item] == "object") {
//make a copy while is a object, needs jQuery here
var tmp = $.extend({}, arguments[i][item]);
}
//if is object try recurrency
if (dst[item] && typeof dst[item] == "object") {
try {
this.extend(dst[item], tmp);
} catch (e) {
dst[item] = tmp;
}
} else {
dst[item] = tmp;
}
}
}
return dst;
}
}
};