From 7c277fdb645073325bd1b91e812c49a3addd7b77 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Wed, 8 Jul 2026 17:50:02 +0100 Subject: [PATCH] Allow multiple whitespace characters between a value and its unit --- index.js | 2 +- test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d05d5c8..232c54b 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ import en from './locale/en.js' -const durationRE = /((?:\d{1,16}(?:\.\d{1,16})?|\.\d{1,16})(?:[eE][-+]?\d{1,4})?)\s?([\p{L}]{0,14})/gu +const durationRE = /((?:\d{1,16}(?:\.\d{1,16})?|\.\d{1,16})(?:[eE][-+]?\d{1,4})?)\s*([\p{L}]{0,14})/gu parse.unit = en diff --git a/test.js b/test.js index 422ee3d..40c9690 100644 --- a/test.js +++ b/test.js @@ -104,6 +104,13 @@ t('combined', t => { t.end() }) +t('whitespace between value and unit', t => { + t.equal(parse('5 s'), 5 * s) + t.equal(parse('10 m'), 10 * m) + t.equal(parse('2\t\th'), 2 * h) + t.end() +}) + t('edge cases', t => { t.equal(parse('1y.2mo.5days.12hours.34sec.20ms'), 1 * y + .2 * mo + .5 * d + .12 * h + .34 * s + .20 * ms) t.equal(parse('-1y.2mth.5days 12hours,34sec,20ms'), -1 * y - .2 * mo - .5 * d - 12 * h - 34 * s - 20 * ms)