Last 12 weeks ยท 0 commits
2 of 6 standards met
Closes #278 ## Summary Adds a option to for localized duration strings Ships five built-in locales: , , , , Exports a interface so users can supply any custom locale / are unchanged โ English-only as before ## Changes ### Core (, ) New interface (, , optional ) โ omitting it preserves current behavior exactly / accept a locale; replaces the old helper Built-in constant; re-exports , , , , ### Locales () ### Tests โ format tests (short + long, all units, negatives, rounding) โ documents round-trip behavior per locale โ type-level checks with where needed ### Docs README: new Localization section with usage examples, custom locale walkthrough, and field reference table ## Backward compatibility No existing behavior changes. All prior tests pass unmodified. The option defaults to the built-in definition which reproduces the original output exactly. ## Test plan [x] โ all tests pass, coverage stays at 100% [x] โ no TypeScript errors [x] โ dist builds cleanly [x] Manually verify a few locale outputs against native speaker expectations
Repository: vercel/ms. Description: Tiny millisecond conversion utility Stars: 5508, Forks: 299. Primary language: TypeScript. Languages: TypeScript (100%). License: MIT. Homepage: https://npmjs.com/ms Topics: conversion, milliseconds, utility. Latest release: 2.1.3 (5y ago). Open PRs: 9, open issues: 17. Last activity: 1mo ago. Community health: 62%. Top contributors: leo, rauchg, dimitropoulos, tj, styfle, greenkeeper[bot], Swatinem, mrmckeb, leerob, greenkeeperio-bot and others.
TypeScript
Summary currently formats millisecond values using English-only unit strings. This proposal adds a option to so callers can receive output in other languages without any change to parsing behavior. ## Motivation Libraries that surface human-readable durations (e.g. error messages, UI labels, CLI output) are often used in multilingual products. Today every consumer has to post-process output to localize it. A first-class option removes that burden. ## Proposed API ```ts import { format, fr, de, ar, es, zh } from 'ms'; // short format(60000, { locale: fr }); // '1min' format(3600000, { locale: de }); // '1Std' // long format(1000, { locale: fr, long: true }); // '1 seconde' format(2000, { locale: fr, long: true }); // '2 secondes' format(3600000, { locale: zh, long: true }); // '1 ๅฐๆถ' Design New exported LocaleDefinition interface with shortUnits, longUnits (singular/plural tuples), and an optional isPlural callback. Options gains an optional locale?: LocaleDefinition field. Omitting it keeps the exact existing English behavior โ fully backward compatible. Built-in locales: fr, ar, de, es, zh. parse() / parseStrict() remain English-only (locale-aware parsing would require per-language grammar rules that go beyond the scope of this package). Custom locales are trivially supported โ callers just pass their own LocaleDefinition object. Custom locale example const pt: LocaleDefinition = { shortUnits: { ms: 'ms', s: 's', m: 'min', h: 'h', d: 'd', w: 'sem', mo: 'mรชs', y: 'ano' }, longUnits: { millisecond: ['milissegundo', 'milissegundos'], second: ['segundo', 'segundos'], minute: ['minuto', 'minutos'], hour: ['hora', 'horas'], day: ['dia', 'dias'], week: ['semana', 'semanas'], month: ['mรชs', 'meses'], year: ['ano', 'anos'], }, isPlural: (v) => v !== 1, }; format(2000, { locale: pt, long: true }); // '2 segundos' Checklist Backward compatible โ existing tests unchanged New LocaleDefinition interface exported Built-in locales: fr, ar, de, es, zh Full test coverage (format, parse, parseStrict) per locale README updated with usage and custom locale walkthrough 100% statement/branch/function/line coverage maintained