* Extract significant digits from a numeric string. * Strips sign, decimal point, leading zeros, and trailing zeros. * Returns only the significant digit sequence.
(s: string)
| 18 | * Returns only the significant digit sequence. |
| 19 | */ |
| 20 | function significantDigits(s: string): string { |
| 21 | // Remove sign |
| 22 | if (s.startsWith('-') || s.startsWith('+')) s = s.slice(1); |
| 23 | |
| 24 | // Handle scientific notation: normalize to plain digits |
| 25 | const eIdx = s.search(/[eE]/); |
| 26 | if (eIdx !== -1) { |
| 27 | // Extract mantissa digits (ignore exponent for digit comparison) |
| 28 | s = s.slice(0, eIdx); |
| 29 | } |
| 30 | |
| 31 | // Remove decimal point |
| 32 | s = s.replace('.', ''); |
| 33 | |
| 34 | // Remove leading zeros |
| 35 | s = s.replace(/^0+/, ''); |
| 36 | |
| 37 | return s; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Compare the first `matchDigits` significant digits of two numeric strings. |
no test coverage detected