Skip to content

Commit abb54cc

Browse files
blikblumjdalton
authored andcommitted
Simplify toString (#4441)
* Assume Symbol exists in toString tests * Use native array map instead of custom one in toString * Assume Symbol.prototype.toString exists in toString
1 parent 3ebb38d commit abb54cc

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

test/toString.test.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,12 @@ describe('toString', function() {
2828
assert.deepStrictEqual(toString(values), '-0,-0,0,0');
2929
});
3030

31-
it('should not error on symbols', function() {
32-
if (Symbol) {
33-
try {
34-
assert.strictEqual(toString(symbol), 'Symbol(a)');
35-
} catch (e) {
36-
assert.ok(false, e.message);
37-
}
38-
}
31+
it('should handle symbols', function() {
32+
assert.strictEqual(toString(symbol), 'Symbol(a)');
3933
});
4034

41-
it('should not error on an array of symbols', function() {
42-
if (Symbol) {
43-
try {
44-
assert.strictEqual(toString([symbol]), 'Symbol(a)');
45-
} catch (e) {
46-
assert.ok(false, e.message);
47-
}
48-
}
35+
it('should handle an array of symbols', function() {
36+
assert.strictEqual(toString([symbol]), 'Symbol(a)');
4937
});
5038

5139
it('should return the `toString` result of the wrapped value', function() {

toString.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import map from './map.js'
21
import isSymbol from './isSymbol.js'
32

43
/** Used as references for various `Number` constants. */
54
const INFINITY = 1 / 0
65

7-
/** Used to convert symbols to primitives and strings. */
8-
const symbolToString = Symbol.prototype.toString
9-
106
/**
117
* Converts `value` to a string. An empty string is returned for `null`
128
* and `undefined` values. The sign of `-0` is preserved.
@@ -36,10 +32,10 @@ function toString(value) {
3632
}
3733
if (Array.isArray(value)) {
3834
// Recursively convert values (susceptible to call stack limits).
39-
return `${map(value, (other) => other == null ? other : toString(other))}`
35+
return `${value.map((other) => other == null ? other : toString(other))}`
4036
}
4137
if (isSymbol(value)) {
42-
return symbolToString ? symbolToString.call(value) : ''
38+
return value.toString()
4339
}
4440
const result = `${value}`
4541
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy