2008-09-29 23:04:25 +00:00
|
|
|
(function() {
|
2009-01-12 00:26:26 +00:00
|
|
|
|
|
|
|
function getClass(object) {
|
|
|
|
return Object.prototype.toString.call(object)
|
|
|
|
.match(/^\[object\s(.*)\]$/)[1];
|
|
|
|
}
|
|
|
|
|
2008-09-29 23:04:25 +00:00
|
|
|
function extend(destination, source) {
|
|
|
|
for (var property in source)
|
|
|
|
destination[property] = source[property];
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inspect(object) {
|
2008-12-11 10:42:15 +00:00
|
|
|
try {
|
2008-09-29 23:04:25 +00:00
|
|
|
if (isUndefined(object)) return 'undefined';
|
2008-12-11 10:42:15 +00:00
|
|
|
if (object === null) return 'null';
|
|
|
|
return object.inspect ? object.inspect() : String(object);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof RangeError) return '...';
|
|
|
|
throw e;
|
|
|
|
}
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toJSON(object) {
|
2008-12-11 10:42:15 +00:00
|
|
|
var type = typeof object;
|
|
|
|
switch (type) {
|
|
|
|
case 'undefined':
|
|
|
|
case 'function':
|
|
|
|
case 'unknown': return;
|
|
|
|
case 'boolean': return object.toString();
|
|
|
|
}
|
2008-09-29 23:04:25 +00:00
|
|
|
|
2008-12-11 10:42:15 +00:00
|
|
|
if (object === null) return 'null';
|
|
|
|
if (object.toJSON) return object.toJSON();
|
2008-09-29 23:04:25 +00:00
|
|
|
if (isElement(object)) return;
|
|
|
|
|
2008-12-11 10:42:15 +00:00
|
|
|
var results = [];
|
|
|
|
for (var property in object) {
|
2008-09-29 23:04:25 +00:00
|
|
|
var value = toJSON(object[property]);
|
|
|
|
if (!isUndefined(value))
|
2008-12-11 10:42:15 +00:00
|
|
|
results.push(property.toJSON() + ': ' + value);
|
|
|
|
}
|
2008-09-29 23:04:25 +00:00
|
|
|
|
2008-12-11 10:42:15 +00:00
|
|
|
return '{' + results.join(', ') + '}';
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toQueryString(object) {
|
2008-12-11 10:42:15 +00:00
|
|
|
return $H(object).toQueryString();
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toHTML(object) {
|
2008-12-11 10:42:15 +00:00
|
|
|
return object && object.toHTML ? object.toHTML() : String.interpret(object);
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function keys(object) {
|
2008-12-11 15:55:02 +00:00
|
|
|
var results = [];
|
2008-12-11 10:42:15 +00:00
|
|
|
for (var property in object)
|
2008-12-11 15:55:02 +00:00
|
|
|
results.push(property);
|
|
|
|
return results;
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function values(object) {
|
2008-12-11 15:55:02 +00:00
|
|
|
var results = [];
|
2008-12-11 10:42:15 +00:00
|
|
|
for (var property in object)
|
2008-12-11 15:55:02 +00:00
|
|
|
results.push(object[property]);
|
|
|
|
return results;
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clone(object) {
|
|
|
|
return extend({ }, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isElement(object) {
|
2008-12-11 10:42:15 +00:00
|
|
|
return !!(object && object.nodeType == 1);
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isArray(object) {
|
2009-01-12 00:26:26 +00:00
|
|
|
return getClass(object) === "Array";
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isHash(object) {
|
2008-12-11 10:42:15 +00:00
|
|
|
return object instanceof Hash;
|
|
|
|
}
|
2008-09-29 23:04:25 +00:00
|
|
|
|
|
|
|
function isFunction(object) {
|
|
|
|
return typeof object === "function";
|
|
|
|
}
|
|
|
|
|
|
|
|
function isString(object) {
|
2009-01-12 00:26:26 +00:00
|
|
|
return getClass(object) === "String";
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNumber(object) {
|
2009-01-12 00:26:26 +00:00
|
|
|
return getClass(object) === "Number";
|
2008-09-29 23:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isUndefined(object) {
|
|
|
|
return typeof object === "undefined";
|
|
|
|
}
|
|
|
|
|
|
|
|
extend(Object, {
|
|
|
|
extend: extend,
|
|
|
|
inspect: inspect,
|
|
|
|
toJSON: toJSON,
|
|
|
|
toQueryString: toQueryString,
|
|
|
|
toHTML: toHTML,
|
|
|
|
keys: keys,
|
|
|
|
values: values,
|
|
|
|
clone: clone,
|
|
|
|
isElement: isElement,
|
|
|
|
isArray: isArray,
|
|
|
|
isHash: isHash,
|
|
|
|
isFunction: isFunction,
|
|
|
|
isString: isString,
|
|
|
|
isNumber: isNumber,
|
|
|
|
isUndefined: isUndefined
|
|
|
|
});
|
|
|
|
})();
|