mirror of
https://github.com/samsonjs/batteries.git
synced 2026-04-27 15:07:42 +00:00
update repr
This commit is contained in:
parent
f03c8084d7
commit
6ca19ad0cd
1 changed files with 34 additions and 21 deletions
55
lib/repr.js
55
lib/repr.js
|
|
@ -2,32 +2,45 @@
|
||||||
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
||||||
|
|
||||||
// readable string representations of values
|
// readable string representations of values
|
||||||
exports.repr = function(x) {
|
exports.repr = repr;
|
||||||
if (x !== null && x !== undefined && typeof x.repr === 'function') return x.repr();
|
|
||||||
|
|
||||||
if (x === null || x === undefined ||
|
function repr(x) {
|
||||||
x instanceof Number || typeof x === 'number' ||
|
if (x !== null && x !== undefined && typeof x.repr === 'function') return x.repr();
|
||||||
x instanceof Boolean || typeof x === 'boolean' ||
|
|
||||||
x instanceof RegExp || x.constructor === RegExp)
|
|
||||||
{
|
|
||||||
return String(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (x instanceof String || typeof x === 'string')
|
var nativeToStringIsReadable =
|
||||||
return '"' + x.replace(/"/g, '\\"') + '"';
|
x === null
|
||||||
|
|| x === undefined
|
||||||
|
|| x instanceof Number
|
||||||
|
|| typeof x === 'number'
|
||||||
|
|| x instanceof Boolean
|
||||||
|
|| typeof x === 'boolean'
|
||||||
|
|| x instanceof RegExp
|
||||||
|
|| x.constructor === RegExp;
|
||||||
|
|
||||||
if (x instanceof Date || x.toUTCString)
|
if (nativeToStringIsReadable) {
|
||||||
return 'new Date(' + (+x) + ')'; // lame
|
return String(x);
|
||||||
|
}
|
||||||
|
|
||||||
if (Array.isArray(x))
|
if (x instanceof String || typeof x === 'string') {
|
||||||
return '[' + x.map(repr).join(',') + ']';
|
return '"' + x.replace(/"/g, '\\"') + '"';
|
||||||
|
}
|
||||||
|
|
||||||
if (x instanceof Function || typeof x === 'function')
|
if (x instanceof Date || x.toUTCString) {
|
||||||
return x.toString();
|
return 'new Date(' + (+x) + ')'; // lame
|
||||||
|
}
|
||||||
|
|
||||||
// TODO determine how far to go with this. should we include non-enumerable props too?
|
if (Array.isArray(x)) {
|
||||||
if (x instanceof Object || typeof x === 'object')
|
return '[' + x.map(repr).join(',') + ']';
|
||||||
return '{' + Object.keys(x).map(function(k) { return repr(k) + ':' + repr(x[k]); }).join(',') + '}';
|
}
|
||||||
|
|
||||||
throw new Error("don't know how to represent " + x);
|
if (x instanceof Function || typeof x === 'function') {
|
||||||
|
return x.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO determine how far to go with this. should we include non-enumerable props too?
|
||||||
|
if (x instanceof Object || typeof x === 'object') {
|
||||||
|
return '{' + Object.keys(x).map(function(k) { return repr(k) + ':' + repr(x[k]); }).join(',') + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("don't know how to represent " + x);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue