forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoString.js
199 lines (167 loc) · 7.82 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var assert = require('assert');
var R = require('../source/index.js');
var {Just} = require('./shared/Maybe.js');
describe('toString', function() {
it('returns the string representation of null', function() {
assert.strictEqual(R.toString(null), 'null');
});
it('returns the string representation of undefined', function() {
assert.strictEqual(R.toString(undefined), 'undefined');
});
it('returns the string representation of a Boolean primitive', function() {
assert.strictEqual(R.toString(true), 'true');
assert.strictEqual(R.toString(false), 'false');
});
it('returns the string representation of a number primitive', function() {
assert.strictEqual(R.toString(0), '0');
assert.strictEqual(R.toString(-0), '-0');
assert.strictEqual(R.toString(1.23), '1.23');
assert.strictEqual(R.toString(-1.23), '-1.23');
assert.strictEqual(R.toString(1e+23), '1e+23');
assert.strictEqual(R.toString(-1e+23), '-1e+23');
assert.strictEqual(R.toString(1e-23), '1e-23');
assert.strictEqual(R.toString(-1e-23), '-1e-23');
assert.strictEqual(R.toString(Infinity), 'Infinity');
assert.strictEqual(R.toString(-Infinity), '-Infinity');
assert.strictEqual(R.toString(NaN), 'NaN');
});
it('returns the string representation of a string primitive', function() {
assert.strictEqual(R.toString('abc'), '"abc"');
assert.strictEqual(R.toString('x "y" z'), '"x \\"y\\" z"');
assert.strictEqual(R.toString("' '"), '"\' \'"');
assert.strictEqual(R.toString('" "'), '"\\" \\""');
assert.strictEqual(R.toString('\b \b'), '"\\b \\b"');
assert.strictEqual(R.toString('\f \f'), '"\\f \\f"');
assert.strictEqual(R.toString('\n \n'), '"\\n \\n"');
assert.strictEqual(R.toString('\r \r'), '"\\r \\r"');
assert.strictEqual(R.toString('\t \t'), '"\\t \\t"');
assert.strictEqual(R.toString('\v \v'), '"\\v \\v"');
assert.strictEqual(R.toString('\0 \0'), '"\\0 \\0"');
assert.strictEqual(R.toString('\\ \\'), '"\\\\ \\\\"');
});
it('returns the string representation of a Boolean object', function() {
assert.strictEqual(R.toString(new Boolean(true)), 'new Boolean(true)');
assert.strictEqual(R.toString(new Boolean(false)), 'new Boolean(false)');
});
it('returns the string representation of a Map object', function() {
if (typeof Map !== 'function') {
return;
}
assert.strictEqual(R.toString(new Map([[1, 2], [3, 4]])), 'new Map([[1, 2], [3, 4]])');
});
it('returns the string representation of a Number object', function() {
assert.strictEqual(R.toString(new Number(0)), 'new Number(0)');
assert.strictEqual(R.toString(new Number(-0)), 'new Number(-0)');
});
it('returns the string representation of a Set object', function() {
if (typeof Set !== 'function') {
return;
}
assert.strictEqual(R.toString(new Set([2, 1])), 'new Set([1, 2])');
});
it('returns the string representation of a String object', function() {
assert.strictEqual(R.toString(new String('abc')), 'new String("abc")');
assert.strictEqual(R.toString(new String('x "y" z')), 'new String("x \\"y\\" z")');
assert.strictEqual(R.toString(new String("' '")), 'new String("\' \'")');
assert.strictEqual(R.toString(new String('" "')), 'new String("\\" \\"")');
assert.strictEqual(R.toString(new String('\b \b')), 'new String("\\b \\b")');
assert.strictEqual(R.toString(new String('\f \f')), 'new String("\\f \\f")');
assert.strictEqual(R.toString(new String('\n \n')), 'new String("\\n \\n")');
assert.strictEqual(R.toString(new String('\r \r')), 'new String("\\r \\r")');
assert.strictEqual(R.toString(new String('\t \t')), 'new String("\\t \\t")');
assert.strictEqual(R.toString(new String('\v \v')), 'new String("\\v \\v")');
assert.strictEqual(R.toString(new String('\0 \0')), 'new String("\\0 \\0")');
assert.strictEqual(R.toString(new String('\\ \\')), 'new String("\\\\ \\\\")');
});
it('returns the string representation of a Date object', function() {
assert.strictEqual(R.toString(new Date('2001-02-03T04:05:06.000Z')), 'new Date("2001-02-03T04:05:06.000Z")');
assert.strictEqual(R.toString(new Date('XXX')), 'new Date(NaN)');
});
it('returns the string representation of a RegExp object', function() {
assert.strictEqual(R.toString(/(?:)/), '/(?:)/');
assert.strictEqual(R.toString(/\//g), '/\\//g');
});
it('returns the string representation of a function', function() {
var add = function add(a, b) { return a + b; };
assert.strictEqual(R.toString(add), add.toString());
});
it('returns the string representation of an array', function() {
assert.strictEqual(R.toString([]), '[]');
assert.strictEqual(R.toString([1, 2, 3]), '[1, 2, 3]');
assert.strictEqual(R.toString([1, [2, [3]]]), '[1, [2, [3]]]');
assert.strictEqual(R.toString(['x', 'y']), '["x", "y"]');
});
it('returns the string representation of an array with non-numeric property names', function() {
var xs = [1, 2, 3];
xs.foo = 0;
xs.bar = 0;
xs.baz = 0;
assert.strictEqual(R.toString(xs), '[1, 2, 3, "bar": 0, "baz": 0, "foo": 0]');
});
it('returns the string representation of an arguments object', function() {
assert.strictEqual(R.toString((function() { return arguments; })()), '(function() { return arguments; }())');
assert.strictEqual(R.toString((function() { return arguments; })(1, 2, 3)), '(function() { return arguments; }(1, 2, 3))');
assert.strictEqual(R.toString((function() { return arguments; })(['x', 'y'])), '(function() { return arguments; }(["x", "y"]))');
});
it('returns the string representation of a plain object', function() {
assert.strictEqual(R.toString({}), '{}');
assert.strictEqual(R.toString({foo: 1, bar: 2, baz: 3}), '{"bar": 2, "baz": 3, "foo": 1}');
assert.strictEqual(R.toString({'"quoted"': true}), '{"\\"quoted\\"": true}');
assert.strictEqual(R.toString({a: {b: {c: {}}}}), '{"a": {"b": {"c": {}}}}');
});
it('treats instance without custom `toString` method as plain object', function() {
function Point(x, y) {
this.x = x;
this.y = y;
}
assert.strictEqual(R.toString(new Point(1, 2)), '{"x": 1, "y": 2}');
});
it('dispatches to custom `toString` method', function() {
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function() {
return 'new Point(' + this.x + ', ' + this.y + ')';
};
assert.strictEqual(R.toString(new Point(1, 2)), 'new Point(1, 2)');
assert.strictEqual(R.toString(Just(42)), 'Just(42)');
assert.strictEqual(R.toString(Just([1, 2, 3])), 'Just([1, 2, 3])');
assert.strictEqual(R.toString(Just(Just(Just('')))), 'Just(Just(Just("")))');
assert.strictEqual(R.toString({toString: R.always('x')}), 'x');
});
it('handles object with no `toString` method', function() {
if (typeof Object.create === 'function') {
var a = Object.create(null);
var b = Object.create(null); b.x = 1; b.y = 2;
assert.strictEqual(R.toString(a), '{}');
assert.strictEqual(R.toString(b), '{"x": 1, "y": 2}');
}
});
it('handles circular references', function() {
var a = [];
a[0] = a;
assert.strictEqual(R.toString(a), '[<Circular>]');
var o = {};
o.o = o;
assert.strictEqual(R.toString(o), '{"o": <Circular>}');
var b = ['bee'];
var c = ['see'];
b[1] = c;
c[1] = b;
assert.strictEqual(R.toString(b), '["bee", ["see", <Circular>]]');
assert.strictEqual(R.toString(c), '["see", ["bee", <Circular>]]');
var p = {};
var q = {};
p.q = q;
q.p = p;
assert.strictEqual(R.toString(p), '{"q": {"p": <Circular>}}');
assert.strictEqual(R.toString(q), '{"p": {"q": <Circular>}}');
var x = [];
var y = {};
x[0] = y;
y.x = x;
assert.strictEqual(R.toString(x), '[{"x": <Circular>}]');
assert.strictEqual(R.toString(y), '{"x": [<Circular>]}');
});
});