Skip to content

Commit

Permalink
check builtins using the toString.call trick
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 7, 2012
1 parent 9681271 commit 459378b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,22 @@ function copy (src) {
if (Array_isArray(src)) {
dst = [];
}
else if (src instanceof Date) {
else if (isDate(src)) {
dst = new Date(src);
}
else if (src instanceof Error) {
else if (isRegExp(src)) {
dst = new RegExp(src);
}
else if (isError(src)) {
dst = { message: src.message };
}
else if (src instanceof Boolean) {
else if (isBoolean(src)) {
dst = new Boolean(src);
}
else if (src instanceof Number) {
else if (isNumber(src)) {
dst = new Number(src);
}
else if (src instanceof String) {
else if (isString(src)) {
dst = new String(src);
}
else if (Object.create && Object.getPrototypeOf) {
Expand Down Expand Up @@ -264,6 +267,14 @@ var Object_keys = Object.keys || function keys (obj) {
return res;
};

function toS (obj) { return Object.prototype.toString.call(obj) }
function isDate (obj) { return toS(obj) === '[object Date]' }
function isRegExp (obj) { return toS(obj) === '[object RegExp]' }
function isError (obj) { return toS(obj) === '[object Error]' }
function isBoolean (obj) { return toS(obj) === '[object Boolean]' }
function isNumber (obj) { return toS(obj) === '[object Number]' }
function isString (obj) { return toS(obj) === '[object String]' }

var Array_isArray = Array.isArray || function isArray (xs) {
return Object.prototype.toString.call(xs) === '[object Array]';
};
Expand Down

0 comments on commit 459378b

Please sign in to comment.