prototype/src/lang/object.js

123 lines
2.7 KiB
JavaScript
Raw Normal View History

2008-09-29 23:04:25 +00:00
(function() {
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) {
return object != null && typeof object === "object" &&
2008-12-11 10:42:15 +00:00
'splice' in object && 'join' in object;
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) {
try {
return typeof (object && object.valueOf()) === "string";
} catch(e) {
return false;
}
2008-09-29 23:04:25 +00:00
}
function isNumber(object) {
try {
return typeof (object && object.valueOf()) === "number";
} catch(e) {
return false;
}
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
});
})();