@gram-lang/i18n 1.4.0 → 1.4.1

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
@@ -50,6 +50,64 @@ declare function normalizeUnit(rawUnit: string | undefined | null, lang?: string
50
50
  * return false since their values are already small.
51
51
  */
52
52
  declare function isCoarseUnit(rawUnit: string | undefined | null): boolean;
53
+ /**
54
+ * True for a metric unit of mass (mg, g, kg): a quantity in it is already a
55
+ * weight, so a standardized mass next to it would only say it again. An
56
+ * imperial mass (oz, lb) is not one: its weight in grams still tells something.
57
+ */
58
+ declare function isMetricMassUnit(rawUnit: string | undefined | null, lang?: string): boolean;
59
+ /**
60
+ * A metric mass or volume in kilograms or liters once it reaches 1000 g or
61
+ * 1000 ml (3900 g → 3.9 kg, 2250 ml → 2.25 l), rounded to 2 decimals; any
62
+ * other amount comes back as it was. For what a reader buys (a shopping list)
63
+ * or reads as a total (the weight of a recipe): what is weighed stays in grams,
64
+ * since a scale shows grams and a cook writes "1500 g of flour".
65
+ * Every surface reads a large amount through this, so a shopping list says
66
+ * the same thing in the renderer, `gram shop` and `gram cook`.
67
+ */
68
+ declare function toLargerMetricUnit(value: number, rawUnit: string | undefined | null, lang?: string): {
69
+ value: number;
70
+ unit: string;
71
+ };
72
+ /** True for a unit of mass or volume Gram knows (g, kg, cup, tbsp, c.à.s...). */
73
+ declare function isMeasureUnit(rawUnit: string | undefined | null, lang?: string): boolean;
74
+ /**
75
+ * True when a unit holds a quantity of its own: a number followed by a known
76
+ * unit, glued or not (`about 200g`, `boîte de 400 g`, `8oz tins`). A unit such
77
+ * as `gousse` or `tranche` holds none.
78
+ */
79
+ declare function unitContainsQuantity(rawUnit: string | undefined | null, lang?: string): boolean;
80
+
81
+ /**
82
+ * Numbers as a reader of a language writes them: `1.5` in English, `1,5` in
83
+ * French. Only for what is shown: a number a script or a file reads back (a
84
+ * data attribute, JSON, `.gram` source) keeps its point.
85
+ */
86
+ /**
87
+ * A number in the reader's language, rounded to `maxDecimals` (2 by default),
88
+ * with no thousands separator: `formatNumber(1.5, "fr")` is `"1,5"`,
89
+ * `formatNumber(1500, "fr")` is `"1500"`.
90
+ */
91
+ declare function formatNumber(value: number, lang?: string, maxDecimals?: number): string;
92
+ /** The decimal separator of a language: `"."` in English, `","` in French. */
93
+ declare function decimalSeparator(lang?: string): string;
94
+ /**
95
+ * The decimal points of a quantity already written as text (a range
96
+ * `1.5-2`, a timer `1.5`) in the reader's language: `"1,5-2"` in French.
97
+ * Only a point between two digits changes, so `1/2` or a unit stay as they are.
98
+ */
99
+ declare function localizeDecimals(text: string, lang?: string): string;
100
+ /** `"1/4"` for a value strictly between 0 and 1 that is (nearly) a common fraction. */
101
+ declare function commonFraction(value: number): string | undefined;
102
+ /** True for a unit read in fractions (spoons, cups, imperial measures). */
103
+ declare function isFractionUnit(rawUnit: string | undefined | null, lang?: string): boolean;
104
+ /**
105
+ * A quantity of a spoon, cup or imperial unit as a cook writes it: a whole
106
+ * number and a common fraction (`1.75` cup → `"1 3/4"`, `0.5` tsp → `"1/2"`),
107
+ * or `undefined` when it is not near one (`1.6`), or for any other unit:
108
+ * the caller then writes a decimal.
109
+ */
110
+ declare function toKitchenFraction(value: number, rawUnit: string | undefined | null, lang?: string): string | undefined;
53
111
 
54
112
  declare function getAiLanguageInstruction(lang?: string): string;
55
113
 
@@ -520,4 +578,4 @@ declare function getDictionary(lang?: string): GramLocale;
520
578
  */
521
579
  declare function fillTemplate(template: string, values: Readonly<Record<string, string>>): string;
522
580
 
523
- 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, fillTemplate, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, isCategoryKey, isCoarseUnit, normalizeUnit, resolveTimeUnit, unitKey };
581
+ 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, commonFraction, compileDictionary, decimalSeparator, fillTemplate, formatNumber, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, isCategoryKey, isCoarseUnit, isFractionUnit, isMeasureUnit, isMetricMassUnit, localizeDecimals, normalizeUnit, resolveTimeUnit, toKitchenFraction, toLargerMetricUnit, unitContainsQuantity, unitKey };
package/dist/index.js CHANGED
@@ -203,6 +203,115 @@ function isCoarseUnit(rawUnit) {
203
203
  }
204
204
  return false;
205
205
  }
206
+ var METRIC_LADDER = {
207
+ mass: { units: ["mg", "g", "kg"], large: "kg" },
208
+ volume: { units: ["ml", "cl", "dl", "l"], large: "l" }
209
+ };
210
+ function isMetricMassUnit(rawUnit, lang) {
211
+ return METRIC_LADDER.mass.units.includes(
212
+ normalizeUnit(rawUnit, lang)
213
+ );
214
+ }
215
+ function toLargerMetricUnit(value, rawUnit, lang) {
216
+ const unit = rawUnit ?? "";
217
+ const canonical = normalizeUnit(unit, lang);
218
+ for (const [category, ladder] of Object.entries(METRIC_LADDER)) {
219
+ if (canonical === ladder.large) break;
220
+ if (!ladder.units.includes(canonical)) continue;
221
+ const factor = UNIT_CONVERSIONS[category].map[canonical] ?? 1;
222
+ const base = value * factor;
223
+ if (base < 1e3) break;
224
+ return { value: Math.round(base / 10) / 100, unit: ladder.large };
225
+ }
226
+ return { value, unit };
227
+ }
228
+ function isMeasureUnit(rawUnit, lang) {
229
+ const canonical = normalizeUnit(rawUnit, lang);
230
+ return UNIT_CONVERSIONS.mass.map[canonical] !== void 0 || UNIT_CONVERSIONS.volume.map[canonical] !== void 0;
231
+ }
232
+ function unitContainsQuantity(rawUnit, lang) {
233
+ const words = (rawUnit ?? "").trim().split(/\s+/);
234
+ return words.some((word, i) => {
235
+ const number = word.match(/^\d+(?:[.,/]\d+)?(.*)$/);
236
+ if (!number) return false;
237
+ const next = number[1] || words[i + 1];
238
+ return !!next && isMeasureUnit(next, lang);
239
+ });
240
+ }
241
+
242
+ // src/numbers.ts
243
+ var formatters = /* @__PURE__ */ new Map();
244
+ function formatter(lang, maxDecimals) {
245
+ const key = `${lang ?? "en"}|${maxDecimals}`;
246
+ let format = formatters.get(key);
247
+ if (!format) {
248
+ const options = {
249
+ maximumFractionDigits: maxDecimals,
250
+ // "1500 g", as a recipe writes it: no thousands separator, which
251
+ // would also read as a decimal comma in English.
252
+ useGrouping: false
253
+ };
254
+ try {
255
+ format = new Intl.NumberFormat(lang || "en", options);
256
+ } catch {
257
+ format = new Intl.NumberFormat("en", options);
258
+ }
259
+ formatters.set(key, format);
260
+ }
261
+ return format;
262
+ }
263
+ function formatNumber(value, lang, maxDecimals = 2) {
264
+ return formatter(lang, maxDecimals).format(value);
265
+ }
266
+ function decimalSeparator(lang) {
267
+ return formatter(lang, 1).formatToParts(1.5).find((part) => part.type === "decimal")?.value ?? ".";
268
+ }
269
+ function localizeDecimals(text, lang) {
270
+ const separator = decimalSeparator(lang);
271
+ return separator === "." ? text : text.replace(/(\d)\.(\d)/g, `$1${separator}$2`);
272
+ }
273
+ var COMMON_FRACTIONS = [
274
+ { value: 0.5, text: "1/2" },
275
+ { value: 0.25, text: "1/4" },
276
+ { value: 0.75, text: "3/4" },
277
+ { value: 1 / 3, text: "1/3" },
278
+ { value: 2 / 3, text: "2/3" },
279
+ { value: 0.125, text: "1/8" },
280
+ { value: 0.375, text: "3/8" },
281
+ { value: 0.625, text: "5/8" },
282
+ { value: 0.875, text: "7/8" }
283
+ ];
284
+ function commonFraction(value) {
285
+ if (value <= 0 || value >= 1) return void 0;
286
+ return COMMON_FRACTIONS.find((f) => Math.abs(value - f.value) < 0.01)?.text;
287
+ }
288
+ var FRACTION_UNITS = /* @__PURE__ */ new Set([
289
+ "tsp",
290
+ "tbsp",
291
+ "tad",
292
+ "dash",
293
+ "pinch",
294
+ "smidgen",
295
+ "drop",
296
+ "cup",
297
+ "tasse",
298
+ "fl oz",
299
+ "pt",
300
+ "qt",
301
+ "gal",
302
+ "oz",
303
+ "lb"
304
+ ]);
305
+ function isFractionUnit(rawUnit, lang) {
306
+ return FRACTION_UNITS.has(normalizeUnit(rawUnit, lang));
307
+ }
308
+ function toKitchenFraction(value, rawUnit, lang) {
309
+ if (!isFractionUnit(rawUnit, lang) || value <= 0) return void 0;
310
+ const whole = Math.floor(value);
311
+ const fraction = commonFraction(value - whole);
312
+ if (!fraction) return void 0;
313
+ return whole > 0 ? `${whole} ${fraction}` : fraction;
314
+ }
206
315
 
207
316
  // src/ai.ts
208
317
  var LANGUAGE_NAMES = {
@@ -1217,15 +1326,25 @@ export {
1217
1326
  UNIT_CONVERSIONS,
1218
1327
  UNIT_DICTIONARIES,
1219
1328
  UNIT_GLOBAL,
1329
+ commonFraction,
1220
1330
  compileDictionary,
1331
+ decimalSeparator,
1221
1332
  fillTemplate,
1333
+ formatNumber,
1222
1334
  getAiLanguageInstruction,
1223
1335
  getCategoryLabels,
1224
1336
  getDefaultCategories,
1225
1337
  getDictionary,
1226
1338
  isCategoryKey,
1227
1339
  isCoarseUnit,
1340
+ isFractionUnit,
1341
+ isMeasureUnit,
1342
+ isMetricMassUnit,
1343
+ localizeDecimals,
1228
1344
  normalizeUnit,
1229
1345
  resolveTimeUnit,
1346
+ toKitchenFraction,
1347
+ toLargerMetricUnit,
1348
+ unitContainsQuantity,
1230
1349
  unitKey
1231
1350
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gram-lang/i18n",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Gram recipe i18n and unit normalization logic",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",