@@ -22,8 +22,10 @@ const inRange = function (value, minValue, maxValue) {
2222 return value >= minValue && value <= maxValue ;
2323} ;
2424
25- function getExponent ( value ) {
26- return Math . abs ( parseInt ( value . toExponential ( ) . split ( 'e' ) [ 1 ] , 10 ) ) ;
25+ function getExponent ( value : number ) {
26+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
27+ const [ _ , exponentString ] = value . toExponential ( ) . split ( 'e' ) ;
28+ return Math . abs ( parseInt ( exponentString , 10 ) ) ;
2729}
2830
2931function getExponentialNotation ( value ) {
@@ -44,57 +46,75 @@ function multiplyInExponentialForm(value, exponentShift) {
4446 return parseFloat ( `${ exponentialNotation . mantissa } e${ exponentialNotation . exponent + exponentShift } ` ) ;
4547}
4648
47- // T570217
48- function isEdgeBug ( ) {
49- const value = 0.0003 ;
50- const correctValue = '0.000300' ;
51- const precisionValue = 3 ;
52- return correctValue !== value . toPrecision ( precisionValue ) ;
53- }
49+ const EXP_TO_CHANGE_NOTATION = 7 ;
50+ const MAX_PRECISION = 15 ;
51+ const MIN_PRECISION = 7 ;
5452
55- function adjust ( value , interval ?) {
56- let precision = getPrecision ( interval || 0 ) + 2 ;
57- const separatedValue = value . toString ( ) . split ( '.' ) ;
58- const sourceValue = value ;
53+ function adjust ( value : number , interval ?: number ) : number {
5954 const absValue = Math . abs ( value ) ;
60- let separatedAdjustedValue ;
61- const isExponentValue = isExponential ( value ) ;
6255 const integerPart = absValue > 1 ? 10 : 0 ;
6356
64- if ( separatedValue . length === 1 ) {
57+ const precision = getPrecision ( interval ?? 0 ) + 2 ;
58+
59+ const finalPrecision = precision > EXP_TO_CHANGE_NOTATION ? MAX_PRECISION : MIN_PRECISION ;
60+
61+ const [ integerValuePart , fractionalValuePart ] = value . toString ( ) . split ( '.' ) ;
62+ const sourceValue = value ;
63+
64+ const isExponentValue = isExponential ( value ) ;
65+
66+ if ( isExponentValue ) {
67+ return adjustExponential ( value , finalPrecision ) ;
68+ }
69+
70+ if ( ! fractionalValuePart ) {
6571 return value ;
6672 }
6773
68- if ( ! isExponentValue ) {
69- if ( isExponential ( interval ) ) {
70- precision = separatedValue [ 0 ] . length + getExponent ( interval ) ;
71- }
72- value = absValue ;
73- value = value - Math . floor ( value ) + integerPart ;
74+ if ( isExponential ( interval ) ) {
75+ const expPrecision = integerValuePart . length + getExponent ( interval ) ;
76+ return parseFloat ( sourceValue . toPrecision ( expPrecision ) ) ;
7477 }
7578
76- precision = ( isEdgeBug ( ) && ( getExponent ( value ) > 6 ) ) || precision > 7 ? 15 : 7 ; // fix toPrecision() bug in Edge (T570217)
79+ const fractionalPart = absValue - Math . floor ( absValue ) ;
80+ const adjustedValue = integerPart + fractionalPart ;
7781
78- if ( ! isExponentValue ) {
79- separatedAdjustedValue = parseFloat ( value . toPrecision ( precision ) ) . toString ( ) . split ( '.' ) ;
80- if ( separatedAdjustedValue [ 0 ] === integerPart . toString ( ) ) {
81- return parseFloat ( `${ separatedValue [ 0 ] } .${ separatedAdjustedValue [ 1 ] } ` ) ;
82- }
82+ const separatedAdjustedValue = parseFloat ( adjustedValue . toPrecision ( finalPrecision ) ) . toString ( ) . split ( '.' ) ;
83+
84+ const isIntPartNotChanged = separatedAdjustedValue [ 0 ] === integerPart . toString ( ) ;
85+ if ( isIntPartNotChanged ) {
86+ return parseFloat ( `${ integerValuePart } .${ separatedAdjustedValue [ 1 ] } ` ) ;
87+ }
88+
89+ return parseFloat ( sourceValue . toPrecision ( finalPrecision ) ) ;
90+ }
91+
92+ function adjustExponential ( value : number , precision : number ) {
93+ const expValue = value . toExponential ( ) ;
94+
95+ // eslint-disable-next-line @stylistic/max-len
96+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention
97+ const [ mantissa , _exponent ] = expValue . split ( 'e' ) ;
98+
99+ if ( ! mantissa . includes ( '.' ) ) {
100+ return parseFloat ( expValue ) ;
83101 }
84- return parseFloat ( sourceValue . toPrecision ( precision ) ) ;
102+
103+ return parseFloat ( value . toPrecision ( precision ) ) ;
85104}
86105
87- function getPrecision ( value ) {
106+ function getPrecision ( value : number ) {
88107 const str = value . toString ( ) ;
89108
90- if ( str . indexOf ( '.' ) < 0 ) {
109+ if ( ! str . includes ( '.' ) ) {
91110 return 0 ;
92111 }
93112
94- const mantissa = str . split ( '.' ) ;
95- const positionOfDelimiter = mantissa [ 1 ] . indexOf ( 'e' ) ;
113+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention
114+ const [ _ , fractionalPart ] = str . split ( '.' ) ;
115+ const positionOfDelimiter = fractionalPart . indexOf ( 'e' ) ;
96116
97- return positionOfDelimiter >= 0 ? positionOfDelimiter : mantissa [ 1 ] . length ;
117+ return positionOfDelimiter >= 0 ? positionOfDelimiter : fractionalPart . length ;
98118}
99119
100120function getRoot ( x , n ) {
@@ -180,7 +200,7 @@ function getExponentLength(value) {
180200 || 0 ;
181201}
182202
183- function roundFloatPart ( value , digitsCount = 0 ) {
203+ function roundFloatPart ( value : number , digitsCount = 0 ) : number {
184204 return parseFloat ( value . toFixed ( digitsCount ) ) ;
185205}
186206
0 commit comments