[d24f17c] | 1 | import _includes from "./_includes.js";
|
---|
| 2 | import _map from "./_map.js";
|
---|
| 3 | import _quote from "./_quote.js";
|
---|
| 4 | import _toISOString from "./_toISOString.js";
|
---|
| 5 | import keys from "../keys.js";
|
---|
| 6 | import reject from "../reject.js";
|
---|
| 7 | export default function _toString(x, seen) {
|
---|
| 8 | var recur = function recur(y) {
|
---|
| 9 | var xs = seen.concat([x]);
|
---|
| 10 | return _includes(y, xs) ? '<Circular>' : _toString(y, xs);
|
---|
| 11 | }; // mapPairs :: (Object, [String]) -> [String]
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | var mapPairs = function (obj, keys) {
|
---|
| 15 | return _map(function (k) {
|
---|
| 16 | return _quote(k) + ': ' + recur(obj[k]);
|
---|
| 17 | }, keys.slice().sort());
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | switch (Object.prototype.toString.call(x)) {
|
---|
| 21 | case '[object Arguments]':
|
---|
| 22 | return '(function() { return arguments; }(' + _map(recur, x).join(', ') + '))';
|
---|
| 23 |
|
---|
| 24 | case '[object Array]':
|
---|
| 25 | return '[' + _map(recur, x).concat(mapPairs(x, reject(function (k) {
|
---|
| 26 | return /^\d+$/.test(k);
|
---|
| 27 | }, keys(x)))).join(', ') + ']';
|
---|
| 28 |
|
---|
| 29 | case '[object Boolean]':
|
---|
| 30 | return typeof x === 'object' ? 'new Boolean(' + recur(x.valueOf()) + ')' : x.toString();
|
---|
| 31 |
|
---|
| 32 | case '[object Date]':
|
---|
| 33 | return 'new Date(' + (isNaN(x.valueOf()) ? recur(NaN) : _quote(_toISOString(x))) + ')';
|
---|
| 34 |
|
---|
| 35 | case '[object Map]':
|
---|
| 36 | return 'new Map(' + recur(Array.from(x)) + ')';
|
---|
| 37 |
|
---|
| 38 | case '[object Null]':
|
---|
| 39 | return 'null';
|
---|
| 40 |
|
---|
| 41 | case '[object Number]':
|
---|
| 42 | return typeof x === 'object' ? 'new Number(' + recur(x.valueOf()) + ')' : 1 / x === -Infinity ? '-0' : x.toString(10);
|
---|
| 43 |
|
---|
| 44 | case '[object Set]':
|
---|
| 45 | return 'new Set(' + recur(Array.from(x).sort()) + ')';
|
---|
| 46 |
|
---|
| 47 | case '[object String]':
|
---|
| 48 | return typeof x === 'object' ? 'new String(' + recur(x.valueOf()) + ')' : _quote(x);
|
---|
| 49 |
|
---|
| 50 | case '[object Undefined]':
|
---|
| 51 | return 'undefined';
|
---|
| 52 |
|
---|
| 53 | default:
|
---|
| 54 | if (typeof x.toString === 'function') {
|
---|
| 55 | var repr = x.toString();
|
---|
| 56 |
|
---|
| 57 | if (repr !== '[object Object]') {
|
---|
| 58 | return repr;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | return '{' + mapPairs(x, keys(x)).join(', ') + '}';
|
---|
| 63 | }
|
---|
| 64 | } |
---|