@gram-lang/i18n 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -21,6 +21,13 @@ declare const UNIT_DICTIONARIES: {
21
21
  };
22
22
  declare const UNIT_BY_LANG: Record<string, CompiledDict>;
23
23
  declare const UNIT_GLOBAL: CompiledDict;
24
+ /**
25
+ * Spelling-insensitive lookup key for a unit: lowercased, diacritics stripped,
26
+ * and abbreviation punctuation / spaces removed, so "c.à.s", "c. à s.", "c.a.s"
27
+ * and "càs" all collapse to "cas". Only used to *find* a canonical unit --
28
+ * unknown units are still returned as the author wrote them.
29
+ */
30
+ declare function unitKey(raw: string): string;
24
31
  interface UnitMap {
25
32
  base: string;
26
33
  map: Record<string, number>;
@@ -247,4 +254,4 @@ type GramLocale = typeof en;
247
254
 
248
255
  declare function getDictionary(lang?: string): GramLocale;
249
256
 
250
- export { CATEGORY_KEYS, type CategoryKey, type CompiledDict, type GramLocale, type LanguageDict, TIME_BY_LANG, TIME_DICTIONARIES, TIME_GLOBAL, TIME_TO_MINUTES, UNIT_BY_LANG, UNIT_CONVERSIONS, UNIT_DICTIONARIES, UNIT_GLOBAL, type UnitMap, compileDictionary, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, isCategoryKey, isCoarseUnit, normalizeUnit, resolveTimeUnit };
257
+ export { CATEGORY_KEYS, type CategoryKey, type CompiledDict, type GramLocale, type LanguageDict, TIME_BY_LANG, TIME_DICTIONARIES, TIME_GLOBAL, TIME_TO_MINUTES, UNIT_BY_LANG, UNIT_CONVERSIONS, UNIT_DICTIONARIES, UNIT_GLOBAL, type UnitMap, compileDictionary, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, isCategoryKey, isCoarseUnit, normalizeUnit, resolveTimeUnit, unitKey };
package/dist/index.js CHANGED
@@ -137,6 +137,14 @@ var fr2 = {
137
137
  };
138
138
  var UNIT_DICTIONARIES = { en: en2, fr: fr2 };
139
139
  var { byLang: UNIT_BY_LANG, global: UNIT_GLOBAL } = compileDictionary(UNIT_DICTIONARIES);
140
+ function unitKey(raw) {
141
+ return raw.normalize("NFD").replace(/\p{M}/gu, "").toLowerCase().replace(/[.'’\s]/g, "");
142
+ }
143
+ var foldTable = (table) => Object.fromEntries(Object.entries(table).map(([k, v]) => [unitKey(k), v]));
144
+ var UNIT_BY_LANG_FOLDED = Object.fromEntries(
145
+ Object.entries(UNIT_BY_LANG).map(([lang, table]) => [lang, foldTable(table)])
146
+ );
147
+ var UNIT_GLOBAL_FOLDED = foldTable(UNIT_GLOBAL);
140
148
  var UNIT_CONVERSIONS = {
141
149
  mass: {
142
150
  base: "g",
@@ -182,7 +190,9 @@ function normalizeUnit(rawUnit, lang) {
182
190
  if (lang && UNIT_BY_LANG[lang]?.[clean]) {
183
191
  return UNIT_BY_LANG[lang][clean];
184
192
  }
185
- return UNIT_GLOBAL[clean] || clean;
193
+ if (UNIT_GLOBAL[clean]) return UNIT_GLOBAL[clean];
194
+ const key = unitKey(clean);
195
+ return lang && UNIT_BY_LANG_FOLDED[lang]?.[key] || UNIT_GLOBAL_FOLDED[key] || clean;
186
196
  }
187
197
  function isCoarseUnit(rawUnit) {
188
198
  const normalized = normalizeUnit(rawUnit);
@@ -676,5 +686,6 @@ export {
676
686
  isCategoryKey,
677
687
  isCoarseUnit,
678
688
  normalizeUnit,
679
- resolveTimeUnit
689
+ resolveTimeUnit,
690
+ unitKey
680
691
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gram-lang/i18n",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "Gram recipe i18n and unit normalization logic",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",