58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
|
import _includes from "./_includes.js";
|
||
|
import _map from "./_map.js";
|
||
|
import _quote from "./_quote.js";
|
||
|
import _toISOString from "./_toISOString.js";
|
||
|
import keys from "../keys.js";
|
||
|
import reject from "../reject.js";
|
||
|
export default function _toString(x, seen) {
|
||
|
var recur = function recur(y) {
|
||
|
var xs = seen.concat([x]);
|
||
|
return _includes(y, xs) ? '<Circular>' : _toString(y, xs);
|
||
|
}; // mapPairs :: (Object, [String]) -> [String]
|
||
|
|
||
|
|
||
|
var mapPairs = function (obj, keys) {
|
||
|
return _map(function (k) {
|
||
|
return _quote(k) + ': ' + recur(obj[k]);
|
||
|
}, keys.slice().sort());
|
||
|
};
|
||
|
|
||
|
switch (Object.prototype.toString.call(x)) {
|
||
|
case '[object Arguments]':
|
||
|
return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
|
||
|
|
||
|
case '[object Array]':
|
||
|
return '[' + _map(recur, x).concat(mapPairs(x, reject(function (k) {
|
||
|
return /^\d+$/.test(k);
|
||
|
}, keys(x)))).join(', ') + ']';
|
||
|
|
||
|
case '[object Boolean]':
|
||
|
return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
|
||
|
|
||
|
case '[object Date]':
|
||
|
return 'new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : _quote(_toISOString(x))) + ')';
|
||
|
|
||
|
case '[object Null]':
|
||
|
return 'null';
|
||
|
|
||
|
case '[object Number]':
|
||
|
return typeof x === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
|
||
|
|
||
|
case '[object String]':
|
||
|
return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
|
||
|
|
||
|
case '[object Undefined]':
|
||
|
return 'undefined';
|
||
|
|
||
|
default:
|
||
|
if (typeof x.toString === 'function') {
|
||
|
var repr = x.toString();
|
||
|
|
||
|
if (repr !== '[object Object]') {
|
||
|
return repr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return '{' + mapPairs(x, keys(x)).join(', ') + '}';
|
||
|
}
|
||
|
}
|