Preserve sign of 0 in _.toString.

This commit is contained in:
Xotic750
2015-10-28 01:04:31 +01:00
committed by John-David Dalton
parent 9c342eb432
commit cd371ac66f
2 changed files with 42 additions and 37 deletions

View File

@@ -9760,6 +9760,10 @@
* @returns {string} Returns the string.
*/
function toString(value) {
// Preserve sign of `0`.
if (value === 0) {
return (1 / value) == INFINITY ? '0' : '-0';
}
// Exit early for strings to avoid a performance hit in some environments.
if (typeof value == 'string') {
return value;

View File

@@ -19423,6 +19423,44 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.toString');
(function() {
QUnit.test('should treat nullish values as empty strings', function(assert) {
assert.expect(1);
var values = [, null, undefined],
expected = lodashStable.map(values, lodashStable.constant(''));
var actual = lodashStable.map(values, function(value, index) {
return index ? _.toString(value) : _.toString();
});
assert.deepEqual(actual, expected);
});
QUnit.test('should preserve sign of `0`', function(assert) {
assert.expect(2);
assert.strictEqual(_.toString(0), '0');
assert.strictEqual(_.toString(-0), '-0');
});
QUnit.test('should return the `toString` result of the wrapped value', function(assert) {
assert.expect(1);
if (!isNpm) {
var wrapped = _([1, 2, 3]);
assert.strictEqual(wrapped.toString(), '1,2,3');
}
else {
skipTest(assert);
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.transform');
(function() {
@@ -21526,43 +21564,6 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash(...).toString');
(function() {
QUnit.test('should return the `toString` result of the wrapped value', function(assert) {
assert.expect(1);
if (!isNpm) {
var wrapped = _([1, 2, 3]);
assert.strictEqual(String(wrapped), '1,2,3');
}
else {
skipTest(assert);
}
});
QUnit.test('should treat nullish values as empty strings', function(assert) {
assert.expect(1);
if (!isNpm) {
var values = [, null, undefined],
expected = lodashStable.map(values, lodashStable.constant(''));
var actual = lodashStable.map(values, function(value, index) {
var wrapped = index ? _(value) : _();
return String(wrapped);
});
assert.deepEqual(actual, expected);
}
else {
skipTest(assert);
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash(...).value');
(function() {