-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
173 lines (152 loc) · 4.4 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//. # sanctuary-show
//.
//. Haskell has a `show` function which can be applied to a compatible value to
//. produce a descriptive string representation of that value. The idea is that
//. the string representation should, if possible, be an expression which would
//. produce the original value if evaluated.
//.
//. This library provides a similar [`show`](#show) function.
//.
//. In general, this property should hold: `eval (show (x)) = x`. In some cases
//. parens are necessary to ensure correct interpretation (`{}`, for example,
//. is an empty block rather than an empty object in some contexts). Thus the
//. property is more accurately stated `eval ('(' + show (x) + ')') = x`.
//.
//. One can make values of a custom type compatible with [`show`](#show) by
//. defining a `@@show` method. For example:
//.
//. ```javascript
//. //# Maybe#@@show :: Maybe a ~> () -> String
//. //.
//. //. ```javascript
//. //. > show (Nothing)
//. //. 'Nothing'
//. //.
//. //. > show (Just (['foo', 'bar', 'baz']))
//. //. 'Just (["foo", "bar", "baz"])'
//. //. ```
//. Maybe.prototype['@@show'] = function() {
//. return this.isNothing ? 'Nothing' : 'Just (' + show (this.value) + ')';
//. };
//. ```
export {show as default};
// $$show :: String
const $$show = '@@show';
// seen :: Array Any
const seen = new WeakSet ();
// entry :: Object -> String -> String
const entry = o => k => show (k) + ': ' + show (o[k]);
// sortedKeys :: Object -> Array String
const sortedKeys = o => (Object.keys (o)).sort ();
//# show :: Showable a => a -> String
//.
//. Returns a useful string representation of the given value.
//.
//. Dispatches to the value's `@@show` method if present.
//.
//. Where practical, `show (eval ('(' + show (x) + ')')) = show (x)`.
//.
//. ```javascript
//. > show (null)
//. 'null'
//.
//. > show (undefined)
//. 'undefined'
//.
//. > show (true)
//. 'true'
//.
//. > show (new Boolean (false))
//. 'new Boolean (false)'
//.
//. > show (-0)
//. '-0'
//.
//. > show (NaN)
//. 'NaN'
//.
//. > show (new Number (Infinity))
//. 'new Number (Infinity)'
//.
//. > show ('foo\n"bar"\nbaz\n')
//. '"foo\\n\\"bar\\"\\nbaz\\n"'
//.
//. > show (new String (''))
//. 'new String ("")'
//.
//. > show (['foo', 'bar', 'baz'])
//. '["foo", "bar", "baz"]'
//.
//. > show ([[[[[0]]]]])
//. '[[[[[0]]]]]'
//.
//. > show ({x: [1, 2], y: [3, 4], z: [5, 6]})
//. '{"x": [1, 2], "y": [3, 4], "z": [5, 6]}'
//. ```
const show = x => {
if (x === null) return 'null';
if (x === undefined) return 'undefined';
if (typeof x[$$show] === 'function') return x[$$show] ();
if (seen.has (x)) return '<Circular>';
const repr = Object.prototype.toString.call (x);
switch (repr) {
case '[object Boolean]':
return typeof x === 'object' ?
'new Boolean (' + show (x.valueOf ()) + ')' :
x.toString ();
case '[object Number]':
return typeof x === 'object' ?
'new Number (' + show (x.valueOf ()) + ')' :
1 / x === -Infinity ? '-0' : x.toString (10);
case '[object String]':
return typeof x === 'object' ?
'new String (' + show (x.valueOf ()) + ')' :
JSON.stringify (x);
case '[object RegExp]':
return x.toString ();
case '[object Date]':
return 'new Date (' +
show (isNaN (x.valueOf ()) ? NaN : x.toISOString ()) +
')';
case '[object Error]':
return 'new ' + x.name + ' (' + show (x.message) + ')';
case '[object Arguments]':
return 'function () { return arguments; } (' +
(Array.prototype.map.call (x, show)).join (', ') +
')';
case '[object Array]':
seen.add (x);
try {
return '[' + ((x.map (show)).concat (
sortedKeys (x)
.filter (k => !(/^\d+$/.test (k)))
.map (entry (x))
)).join (', ') + ']';
} finally {
seen.delete (x);
}
case '[object Object]':
seen.add (x);
try {
return '{' + ((sortedKeys (x)).map (entry (x))).join (', ') + '}';
} finally {
seen.delete (x);
}
case '[object Set]':
seen.add (x);
try {
return 'new Set (' + show (Array.from (x.values ())) + ')';
} finally {
seen.delete (x);
}
case '[object Map]':
seen.add (x);
try {
return 'new Map (' + show (Array.from (x.entries ())) + ')';
} finally {
seen.delete (x);
}
default:
return repr.replace (/^\[(.*)\]$/, '<$1>');
}
};