From e2d53677ac9f496761288668d878a025a726b590 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Tue, 23 Jun 2026 17:00:40 +0100 Subject: [PATCH] Normalize the decimal separator on every component, not just the first parse() normalized only the first decimal separator: String.prototype .replace with a string argument replaces a single occurrence. In locales whose decimal separator is "," (de, fr, es, ru, pt, ...), every component after the first kept its comma and was mis-tokenized: parse.unit = de parse('1,5 s 1,5 s') // 6501 (should be 3000) parse('2,5h 3,5h') // 27180000 (should be 21600000) Use replaceAll so each component's separator is normalized. The en "." decimal is unaffected (replaceAll('.', '.') is a no-op). --- index.js | 2 +- test.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f226845..3e2b5dc 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ export default function parse(str = '', format = 'ms') { String(str) .replace(new RegExp(`(\\d)[${parse.unit.placeholder}${parse.unit.group}](\\d)`, 'g'), '$1$2') // clean up group separators / placeholders - .replace(parse.unit.decimal, '.') // normalize decimal separator + .replaceAll(parse.unit.decimal, '.') // normalize decimal separator .replace(durationRE, (_, n, units) => { // if no units, find next smallest units or fall back to format value // eg. 1h30 -> 1h30m diff --git a/test.js b/test.js index f846782..ed7c11e 100644 --- a/test.js +++ b/test.js @@ -177,5 +177,8 @@ t('locale separators', t => { t.equal(parse('30.000,65 seconds'), 30000650) t.equal(parse('30 000,65 seconds'), 30000650) t.equal(parse('30_000,65 seconds'), 30000650) + // every component's decimal separator must be normalized, not just the first + t.equal(parse('1,5 s 1,5 s'), 3000) + t.equal(parse('2,5h 3,5h'), 21600000) t.end() })