-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #995 from zloirock/number-to-exponential
- Loading branch information
Showing
22 changed files
with
437 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../modules/es.number.to-exponential'); | ||
var entryUnbind = require('../../internals/entry-unbind'); | ||
|
||
module.exports = entryUnbind('Number', 'toExponential'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require('../../../modules/es.number.to-exponential'); | ||
var entryVirtual = require('../../../internals/entry-virtual'); | ||
|
||
module.exports = entryVirtual('Number').toExponential; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var parent = require('../../stable/number/to-exponential'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var parent = require('../../../stable/number/virtual/to-exponential'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var log = Math.log; | ||
var LOG10E = Math.LOG10E; | ||
|
||
// eslint-disable-next-line es/no-math-log10 -- safe | ||
module.exports = Math.log10 || function log10(x) { | ||
return log(x) * LOG10E; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
var $ = require('../internals/export'); | ||
|
||
var log = Math.log; | ||
var LOG10E = Math.LOG10E; | ||
var log10 = require('../internals/math-log10'); | ||
|
||
// `Math.log10` method | ||
// https://tc39.es/ecma262/#sec-math.log10 | ||
$({ target: 'Math', stat: true }, { | ||
log10: function log10(x) { | ||
return log(x) * LOG10E; | ||
} | ||
log10: log10 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var global = require('../internals/global'); | ||
var uncurryThis = require('../internals/function-uncurry-this'); | ||
var toIntegerOrInfinity = require('../internals/to-integer-or-infinity'); | ||
var thisNumberValue = require('../internals/this-number-value'); | ||
var $repeat = require('../internals/string-repeat'); | ||
var log10 = require('../internals/math-log10'); | ||
var fails = require('../internals/fails'); | ||
|
||
var RangeError = global.RangeError; | ||
var String = global.String; | ||
var isFinite = global.isFinite; | ||
var abs = Math.abs; | ||
var floor = Math.floor; | ||
var pow = Math.pow; | ||
var round = Math.round; | ||
var un$ToExponential = uncurryThis(1.0.toExponential); | ||
var repeat = uncurryThis($repeat); | ||
var stringSlice = uncurryThis(''.slice); | ||
|
||
// Edge 17- | ||
var ROUNDS_PROPERLY = un$ToExponential(-6.9e-11, 4) === '-6.9000e-11' | ||
// IE11- && Edge 14- | ||
&& un$ToExponential(1.255, 2) === '1.25e+0'; | ||
// FF86-, enable after increasing maximum number of fraction digits in the implementation to 100 | ||
// && nativeToExponential.call(25, 0) === '3e+1'; | ||
|
||
// IE8- | ||
var THROWS_ON_INFINITY_FRACTION = fails(function () { | ||
un$ToExponential(1, Infinity); | ||
}) && fails(function () { | ||
un$ToExponential(1, -Infinity); | ||
}); | ||
|
||
// Safari <11 && FF <50 | ||
var PROPER_NON_FINITE_THIS_CHECK = !fails(function () { | ||
un$ToExponential(Infinity, Infinity); | ||
}) && !fails(function () { | ||
un$ToExponential(NaN, Infinity); | ||
}); | ||
|
||
var FORCED = !ROUNDS_PROPERLY || !THROWS_ON_INFINITY_FRACTION || !PROPER_NON_FINITE_THIS_CHECK; | ||
|
||
// `Number.prototype.toExponential` method | ||
// https://tc39.es/ecma262/#sec-number.prototype.toexponential | ||
$({ target: 'Number', proto: true, forced: FORCED }, { | ||
toExponential: function toExponential(fractionDigits) { | ||
var x = thisNumberValue(this); | ||
if (fractionDigits === undefined) return un$ToExponential(x); | ||
var f = toIntegerOrInfinity(fractionDigits); | ||
if (!isFinite(x)) return String(x); | ||
// TODO: ES2018 increased the maximum number of fraction digits to 100, need to improve the implementation | ||
if (f < 0 || f > 20) throw RangeError('Incorrect fraction digits'); | ||
if (ROUNDS_PROPERLY) return un$ToExponential(x, f); | ||
var s = ''; | ||
var m = ''; | ||
var e = 0; | ||
var c = ''; | ||
var d = ''; | ||
if (x < 0) { | ||
s = '-'; | ||
x = -x; | ||
} | ||
if (x === 0) { | ||
e = 0; | ||
m = repeat('0', f + 1); | ||
} else { | ||
// this block is based on https://gist.github.com/SheetJSDev/1100ad56b9f856c95299ed0e068eea08 | ||
// TODO: improve accuracy with big fraction digits | ||
var l = log10(x); | ||
e = floor(l); | ||
var n = 0; | ||
var w = pow(10, e - f); | ||
n = round(x / w); | ||
if (2 * x >= (2 * n + 1) * w) { | ||
n += 1; | ||
} | ||
if (n >= pow(10, f + 1)) { | ||
n /= 10; | ||
e += 1; | ||
} | ||
m = String(n); | ||
} | ||
if (f !== 0) { | ||
m = stringSlice(m, 0, 1) + '.' + stringSlice(m, 1); | ||
} | ||
if (e === 0) { | ||
c = '+'; | ||
d = '0'; | ||
} else { | ||
c = e > 0 ? '+' : '-'; | ||
d = String(abs(e)); | ||
} | ||
m += 'e' + c + d; | ||
return s + m; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var parent = require('../../es/number/to-exponential'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var parent = require('../../../es/number/virtual/to-exponential'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import toExponential from 'core-js-pure/features/number/virtual/to-exponential'; | ||
|
||
QUnit.test('Number#toExponential', assert => { | ||
assert.isFunction(toExponential); | ||
|
||
assert.same(toExponential.call(0.00008, 3), '8.000e-5'); | ||
assert.same(toExponential.call(0.9, 0), '9e-1'); | ||
assert.same(toExponential.call(1.255, 2), '1.25e+0'); | ||
assert.same(toExponential.call(1843654265.0774949, 5), '1.84365e+9'); | ||
assert.same(toExponential.call(1000000000000000128.0, 0), '1e+18'); | ||
|
||
assert.same(toExponential.call(1), '1e+0'); | ||
assert.same(toExponential.call(1, 0), '1e+0'); | ||
assert.same(toExponential.call(1, 1), '1.0e+0'); | ||
assert.same(toExponential.call(1, 1.1), '1.0e+0'); | ||
assert.same(toExponential.call(1, 0.9), '1e+0'); | ||
assert.same(toExponential.call(1, '0'), '1e+0'); | ||
assert.same(toExponential.call(1, '1'), '1.0e+0'); | ||
assert.same(toExponential.call(1, '1.1'), '1.0e+0'); | ||
assert.same(toExponential.call(1, '0.9'), '1e+0'); | ||
assert.same(toExponential.call(1, NaN), '1e+0'); | ||
assert.same(toExponential.call(1, 'some string'), '1e+0'); | ||
assert.notThrows(() => toExponential.call(1, -0.1) === '1e+0'); | ||
assert.same(toExponential.call(new Number(1)), '1e+0'); | ||
assert.same(toExponential.call(new Number(1), 0), '1e+0'); | ||
assert.same(toExponential.call(new Number(1), 1), '1.0e+0'); | ||
assert.same(toExponential.call(new Number(1), 1.1), '1.0e+0'); | ||
assert.same(toExponential.call(new Number(1), 0.9), '1e+0'); | ||
assert.same(toExponential.call(new Number(1), '0'), '1e+0'); | ||
assert.same(toExponential.call(new Number(1), '1'), '1.0e+0'); | ||
assert.same(toExponential.call(new Number(1), '1.1'), '1.0e+0'); | ||
assert.same(toExponential.call(new Number(1), '0.9'), '1e+0'); | ||
assert.same(toExponential.call(new Number(1), NaN), '1e+0'); | ||
assert.same(toExponential.call(new Number(1), 'some string'), '1e+0'); | ||
assert.notThrows(() => toExponential.call(new Number(1), -0.1) === '1e+0'); | ||
assert.same(toExponential.call(NaN), 'NaN'); | ||
assert.same(toExponential.call(NaN, 0), 'NaN'); | ||
assert.same(toExponential.call(NaN, 1), 'NaN'); | ||
assert.same(toExponential.call(NaN, 1.1), 'NaN'); | ||
assert.same(toExponential.call(NaN, 0.9), 'NaN'); | ||
assert.same(toExponential.call(NaN, '0'), 'NaN'); | ||
assert.same(toExponential.call(NaN, '1'), 'NaN'); | ||
assert.same(toExponential.call(NaN, '1.1'), 'NaN'); | ||
assert.same(toExponential.call(NaN, '0.9'), 'NaN'); | ||
assert.same(toExponential.call(NaN, NaN), 'NaN'); | ||
assert.same(toExponential.call(NaN, 'some string'), 'NaN'); | ||
assert.notThrows(() => toExponential.call(NaN, -0.1) === 'NaN'); | ||
|
||
assert.same(toExponential.call(new Number(1e21)), '1e+21'); | ||
assert.same(toExponential.call(new Number(1e21), 0), '1e+21'); | ||
assert.same(toExponential.call(new Number(1e21), 1), '1.0e+21'); | ||
assert.same(toExponential.call(new Number(1e21), 1.1), '1.0e+21'); | ||
assert.same(toExponential.call(new Number(1e21), 0.9), '1e+21'); | ||
assert.same(toExponential.call(new Number(1e21), '0'), '1e+21'); | ||
assert.same(toExponential.call(new Number(1e21), '1'), '1.0e+21'); | ||
assert.same(toExponential.call(new Number(1e21), '1.1'), '1.0e+21'); | ||
assert.same(toExponential.call(new Number(1e21), '0.9'), '1e+21'); | ||
assert.same(toExponential.call(new Number(1e21), NaN), '1e+21'); | ||
assert.same(toExponential.call(new Number(1e21), 'some string'), '1e+21'); | ||
|
||
assert.same(toExponential.call(5, 19), '5.0000000000000000000e+0'); | ||
|
||
// ported from tests262, the license: https://github.com/tc39/test262/blob/main/LICENSE | ||
assert.same(toExponential.call(123.456, 0), '1e+2'); | ||
assert.same(toExponential.call(123.456, 1), '1.2e+2'); | ||
assert.same(toExponential.call(123.456, 2), '1.23e+2'); | ||
assert.same(toExponential.call(123.456, 3), '1.235e+2'); | ||
assert.same(toExponential.call(123.456, 4), '1.2346e+2'); | ||
assert.same(toExponential.call(123.456, 5), '1.23456e+2'); | ||
assert.same(toExponential.call(123.456, 6), '1.234560e+2'); | ||
assert.same(toExponential.call(123.456, 7), '1.2345600e+2'); | ||
// assert.same(toExponential.call(123.456, 17), '1.23456000000000003e+2'); | ||
// assert.same(toExponential.call(123.456, 20), '1.23456000000000003070e+2'); | ||
|
||
assert.same(toExponential.call(-123.456, 0), '-1e+2'); | ||
assert.same(toExponential.call(-123.456, 1), '-1.2e+2'); | ||
assert.same(toExponential.call(-123.456, 2), '-1.23e+2'); | ||
assert.same(toExponential.call(-123.456, 3), '-1.235e+2'); | ||
assert.same(toExponential.call(-123.456, 4), '-1.2346e+2'); | ||
assert.same(toExponential.call(-123.456, 5), '-1.23456e+2'); | ||
assert.same(toExponential.call(-123.456, 6), '-1.234560e+2'); | ||
assert.same(toExponential.call(-123.456, 7), '-1.2345600e+2'); | ||
// assert.same(toExponential.call(-123.456, 17), '-1.23456000000000003e+2'); | ||
// assert.same(toExponential.call(-123.456, 20), '-1.23456000000000003070e+2'); | ||
|
||
assert.same(toExponential.call(0.0001, 0), '1e-4'); | ||
assert.same(toExponential.call(0.0001, 1), '1.0e-4'); | ||
assert.same(toExponential.call(0.0001, 2), '1.00e-4'); | ||
assert.same(toExponential.call(0.0001, 3), '1.000e-4'); | ||
assert.same(toExponential.call(0.0001, 4), '1.0000e-4'); | ||
// assert.same(toExponential.call(0.0001, 16), '1.0000000000000000e-4'); | ||
// assert.same(toExponential.call(0.0001, 17), '1.00000000000000005e-4'); | ||
// assert.same(toExponential.call(0.0001, 18), '1.000000000000000048e-4'); | ||
// assert.same(toExponential.call(0.0001, 19), '1.0000000000000000479e-4'); | ||
// assert.same(toExponential.call(0.0001, 20), '1.00000000000000004792e-4'); | ||
|
||
assert.same(toExponential.call(0.9999, 0), '1e+0'); | ||
assert.same(toExponential.call(0.9999, 1), '1.0e+0'); | ||
assert.same(toExponential.call(0.9999, 2), '1.00e+0'); | ||
assert.same(toExponential.call(0.9999, 3), '9.999e-1'); | ||
assert.same(toExponential.call(0.9999, 4), '9.9990e-1'); | ||
// assert.same(toExponential.call(0.9999, 16), '9.9990000000000001e-1'); | ||
// assert.same(toExponential.call(0.9999, 17), '9.99900000000000011e-1'); | ||
// assert.same(toExponential.call(0.9999, 18), '9.999000000000000110e-1'); | ||
// assert.same(toExponential.call(0.9999, 19), '9.9990000000000001101e-1'); | ||
// assert.same(toExponential.call(0.9999, 20), '9.99900000000000011013e-1'); | ||
|
||
assert.same(toExponential.call(25, 0), '3e+1'); | ||
assert.same(toExponential.call(12345, 3), '1.235e+4'); | ||
|
||
assert.same(toExponential.call(Number.prototype, 0), '0e+0', 'Number.prototype, 0'); | ||
assert.same(toExponential.call(0, 0), '0e+0', '0, 0'); | ||
assert.same(toExponential.call(-0, 0), '0e+0', '-0, 0'); | ||
assert.same(toExponential.call(0, -0), '0e+0', '0, -0'); | ||
assert.same(toExponential.call(-0, -0), '0e+0', '-0, -0'); | ||
assert.same(toExponential.call(0, 1), '0.0e+0', '0 and 1'); | ||
assert.same(toExponential.call(0, 2), '0.00e+0', '0 and 2'); | ||
assert.same(toExponential.call(0, 7), '0.0000000e+0', '0 and 7'); | ||
assert.same(toExponential.call(0, 20), '0.00000000000000000000e+0', '0 and 20'); | ||
assert.same(toExponential.call(-0, 1), '0.0e+0', '-0 and 1'); | ||
assert.same(toExponential.call(-0, 2), '0.00e+0', '-0 and 2'); | ||
assert.same(toExponential.call(-0, 7), '0.0000000e+0', '-0 and 7'); | ||
assert.same(toExponential.call(-0, 20), '0.00000000000000000000e+0', '-0 and 20'); | ||
|
||
assert.same(toExponential.call(NaN, 1000), 'NaN', 'NaN check before fractionDigits check'); | ||
assert.same(toExponential.call(Infinity, 1000), 'Infinity', 'Infinity check before fractionDigits check'); | ||
assert.notThrows(() => toExponential.call(new Number(1e21), -0.1) === '1e+21'); | ||
assert.throws(() => toExponential.call(1.0, -101), RangeError, 'If f < 0 or f > 20 (100), throw a RangeError exception.'); | ||
assert.throws(() => toExponential.call(1.0, 101), RangeError, 'If f < 0 or f > 20 (100), throw a RangeError exception.'); | ||
assert.throws(() => toExponential.call({}, 1), TypeError, '? thisNumberValue(this value)'); | ||
assert.throws(() => toExponential.call('123', 1), TypeError, '? thisNumberValue(this value)'); | ||
assert.throws(() => toExponential.call(false, 1), TypeError, '? thisNumberValue(this value)'); | ||
assert.throws(() => toExponential.call(null, 1), TypeError, '? thisNumberValue(this value)'); | ||
assert.throws(() => toExponential.call(undefined, 1), TypeError, '? thisNumberValue(this value)'); | ||
}); |
Oops, something went wrong.