Skip to content

fix: read the short unit names Intl prints in de, ru, es and id - #82

Merged
dy merged 1 commit into
jkroso:masterfrom
bardrop-oss:fix-locale-short-units
Nov 26, 2026
Merged

dy merged 1 commit into
jkroso:masterfrom
bardrop-oss:fix-locale-short-units

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What is wrong

The de, ru, es and id locales can't read the short unit names that Intl.NumberFormat prints with unitDisplay: 'short'. The long names work, the short ones are dropped.

import parse from 'parse-duration'
import de from 'parse-duration/locale/de.js'
import ru from 'parse-duration/locale/ru.js'

const s = new Intl.NumberFormat('de', { style: 'unit', unit: 'hour', unitDisplay: 'short' }).format(1.5)
// '1,5 Std.'
parse.unit = de
parse(s)            // null, expected 5400000
parse('10 Sek.')    // null, expected 10000

parse.unit = ru
parse('10 с')       // null, expected 10000  (Intl 'ru' short for seconds)
parse('3 дн.')      // null, expected 259200000

Missing names (from Node 24 / ICU, short display):

  • de: Mon. (month), Wo. (week), Tg. (day), Std. (hour), Sek. (second)
  • ru: л. (years, as in 5 л.), дн. (day), с (second)
  • es: sem. (week). fr and pt already have sem.
  • id: md (millisecond), ndtk (nanosecond)

Cause

locale/de.js, locale/ru.js, locale/es.js and locale/id.js only list the long names and a few short ones, so lookupUnit finds nothing for these.

Fix

Add these short names as aliases next to their units. I left out the one-letter month forms (m., M, м.) and н. on purpose, since m is already minute. Greek μs is also left out, as the README says to add it yourself.

I checked every locale by formatting all 10 units in long/short/narrow display for the numbers 1, 2, 5, 21 and 1.5 with Intl.NumberFormat, then parsing the result back. Not counting μs, 127 strings did not parse back before this change. After it, 45 are left, and all of them are the one-letter month forms above.

Test

New locale short forms test in test.js with 13 asserts. All 13 fail on master and pass with the fix. npm test: 201/201 pass (188 before).

@dy
dy merged commit b876b28 into jkroso:master Nov 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants