-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
_toString.js
46 lines (42 loc) · 1.71 KB
/
_toString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import _contains from './_contains';
import _map from './_map';
import _quote from './_quote';
import _toISOString from './_toISOString';
import keys from '../keys';
import reject from '../reject';
export default function _toString(x, seen) {
var recur = function recur(y) {
var xs = seen.concat([x]);
return _contains(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(', ') + '}';
}
}