@gram-lang/i18n 1.4.1 → 1.5.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/README.md +8 -3
- package/dist/index.d.ts +421 -4
- package/dist/index.js +940 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -21,14 +21,14 @@ function compileDictionary(dictionaries2) {
|
|
|
21
21
|
// src/time.ts
|
|
22
22
|
var en = {
|
|
23
23
|
d: ["day", "days"],
|
|
24
|
-
h: ["hour", "hours"],
|
|
24
|
+
h: ["hr", "hrs", "hour", "hours"],
|
|
25
25
|
m: ["min", "mins", "minute", "minutes"],
|
|
26
26
|
s: ["sec", "secs", "second", "seconds"]
|
|
27
27
|
};
|
|
28
28
|
var fr = {
|
|
29
29
|
d: ["j", "jour", "jours"],
|
|
30
30
|
h: ["heure", "heures"],
|
|
31
|
-
m: ["min", "mins", "minute", "minutes"],
|
|
31
|
+
m: ["min", "mn", "mins", "minute", "minutes"],
|
|
32
32
|
s: ["sec", "secs", "seconde", "secondes"]
|
|
33
33
|
};
|
|
34
34
|
var TIME_DICTIONARIES = { en, fr };
|
|
@@ -184,6 +184,51 @@ var UNIT_CONVERSIONS = {
|
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
};
|
|
187
|
+
var UNIT_LADDERS = [
|
|
188
|
+
{
|
|
189
|
+
id: "usVolume",
|
|
190
|
+
family: "volume",
|
|
191
|
+
steps: { tsp: 1, tbsp: 3, "fl oz": 6, cup: 48, pt: 96, qt: 192, gal: 768 }
|
|
192
|
+
},
|
|
193
|
+
{ id: "imperialMass", family: "mass", steps: { oz: 1, lb: 16 } },
|
|
194
|
+
{
|
|
195
|
+
id: "metricMass",
|
|
196
|
+
family: "mass",
|
|
197
|
+
// The French "livre" is the metric pound: exactly 500 g.
|
|
198
|
+
steps: { mg: 1, g: 1e3, livre: 5e5, kg: 1e6 }
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
id: "metricVolume",
|
|
202
|
+
family: "volume",
|
|
203
|
+
// The French "tasse" holds exactly 250 ml.
|
|
204
|
+
steps: { ml: 1, cl: 10, dl: 100, tasse: 250, l: 1e3 }
|
|
205
|
+
}
|
|
206
|
+
];
|
|
207
|
+
function ladderOf(rawUnit, lang) {
|
|
208
|
+
const unit = normalizeUnit(rawUnit, lang);
|
|
209
|
+
if (!unit) return null;
|
|
210
|
+
for (const ladder of UNIT_LADDERS) {
|
|
211
|
+
const size = ladder.steps[unit];
|
|
212
|
+
if (size !== void 0) return { ladder, unit, size };
|
|
213
|
+
}
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
function sameLadder(rawUnits, lang) {
|
|
217
|
+
if (rawUnits.length === 0) return false;
|
|
218
|
+
let id;
|
|
219
|
+
for (const raw of rawUnits) {
|
|
220
|
+
const step = ladderOf(raw, lang);
|
|
221
|
+
if (!step) return false;
|
|
222
|
+
if (id !== void 0 && step.ladder.id !== id) return false;
|
|
223
|
+
id = step.ladder.id;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
var UNIT_SPELLINGS = { fr: { tsp: "c\xE0c", tbsp: "c\xE0s" } };
|
|
228
|
+
function unitSpelling(canonical, lang) {
|
|
229
|
+
const base = lang?.toLowerCase().split("-")[0];
|
|
230
|
+
return base && UNIT_SPELLINGS[base]?.[canonical] || canonical;
|
|
231
|
+
}
|
|
187
232
|
function normalizeUnit(rawUnit, lang) {
|
|
188
233
|
if (!rawUnit) return "";
|
|
189
234
|
const clean = rawUnit.toLowerCase().trim();
|
|
@@ -225,6 +270,44 @@ function toLargerMetricUnit(value, rawUnit, lang) {
|
|
|
225
270
|
}
|
|
226
271
|
return { value, unit };
|
|
227
272
|
}
|
|
273
|
+
function convertUnitInFamily(value, fromUnit, toUnit, lang) {
|
|
274
|
+
const from = normalizeUnit(fromUnit, lang);
|
|
275
|
+
const to = normalizeUnit(toUnit, lang);
|
|
276
|
+
if (from === to) return value;
|
|
277
|
+
for (const family of [UNIT_CONVERSIONS.mass, UNIT_CONVERSIONS.volume]) {
|
|
278
|
+
const fromFactor = family.map[from];
|
|
279
|
+
const toFactor = family.map[to];
|
|
280
|
+
if (fromFactor !== void 0 && toFactor !== void 0) {
|
|
281
|
+
return value * fromFactor / toFactor;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
var PIECE_UNITS = /* @__PURE__ */ new Set([
|
|
287
|
+
"pi\xE8ce",
|
|
288
|
+
"pi\xE8ces",
|
|
289
|
+
"piece",
|
|
290
|
+
"pieces",
|
|
291
|
+
"pc",
|
|
292
|
+
"pcs",
|
|
293
|
+
"unit\xE9",
|
|
294
|
+
"unit\xE9s",
|
|
295
|
+
"unit",
|
|
296
|
+
"units"
|
|
297
|
+
]);
|
|
298
|
+
function sameCountUnit(a, b, lang) {
|
|
299
|
+
if (isMeasureUnit(a, lang) || isMeasureUnit(b, lang)) return false;
|
|
300
|
+
const countForm = (unit) => {
|
|
301
|
+
const form = normalizeUnit(unit, lang);
|
|
302
|
+
return PIECE_UNITS.has(form) ? "" : form;
|
|
303
|
+
};
|
|
304
|
+
const x = countForm(a);
|
|
305
|
+
const y = countForm(b);
|
|
306
|
+
if (x === y) return true;
|
|
307
|
+
const [short, long] = x.length <= y.length ? [x, y] : [y, x];
|
|
308
|
+
if (short.length < 3) return false;
|
|
309
|
+
return ["s", "es", "x"].some((ending) => long === short + ending);
|
|
310
|
+
}
|
|
228
311
|
function isMeasureUnit(rawUnit, lang) {
|
|
229
312
|
const canonical = normalizeUnit(rawUnit, lang);
|
|
230
313
|
return UNIT_CONVERSIONS.mass.map[canonical] !== void 0 || UNIT_CONVERSIONS.volume.map[canonical] !== void 0;
|
|
@@ -267,8 +350,10 @@ function decimalSeparator(lang) {
|
|
|
267
350
|
return formatter(lang, 1).formatToParts(1.5).find((part) => part.type === "decimal")?.value ?? ".";
|
|
268
351
|
}
|
|
269
352
|
function localizeDecimals(text, lang) {
|
|
270
|
-
|
|
271
|
-
|
|
353
|
+
return text.replace(/(\d)[.,](\d)/g, `$1${decimalSeparator(lang)}$2`);
|
|
354
|
+
}
|
|
355
|
+
function looksLikeThousands(text) {
|
|
356
|
+
return /(?<![\d.,])(?!0+,)\d{1,3},\d{3}(?![\d.,])/.test(text);
|
|
272
357
|
}
|
|
273
358
|
var COMMON_FRACTIONS = [
|
|
274
359
|
{ value: 0.5, text: "1/2" },
|
|
@@ -541,6 +626,11 @@ var en3 = {
|
|
|
541
626
|
runSheetInStock: "in stock",
|
|
542
627
|
runSheetProgramStep: "step {n}",
|
|
543
628
|
runSheetProgramSteps: "steps {from} to {to}",
|
|
629
|
+
/**
|
|
630
|
+
* @deprecated Since 1.5.0, will be removed in 2.0.0. (Ref: i18n-run-sheet-range)
|
|
631
|
+
* The run sheet writes a range with `formatDurationRange` (`8–16 h`), as the steps do.
|
|
632
|
+
*/
|
|
633
|
+
// @remove-in: 2.0.0 [i18n-run-sheet-range]
|
|
544
634
|
runSheetRange: "{min} to {max}",
|
|
545
635
|
runSheetPossibleFrom: "Possible from {time}.",
|
|
546
636
|
runSheetFixedService: "Can't start earlier: the \u201C{rest}\u201D wait ({section}) can't be longer than {length} and ends when you serve.",
|
|
@@ -591,13 +681,17 @@ var en3 = {
|
|
|
591
681
|
tooltipBakers: "Baker's %",
|
|
592
682
|
tooltipTakenFrom: "Taken from",
|
|
593
683
|
alternatives: "Alternatives",
|
|
684
|
+
// A ratio with units (`1 / 100 g @&flour`): its tooltip row, and either
|
|
685
|
+
// relative form (`70%`, `1 / 100 g`) while it stays unresolved.
|
|
686
|
+
tooltipRatio: "Ratio",
|
|
687
|
+
relativeOf: "{amount} of {target}",
|
|
594
688
|
// The deprecated portions stepper of the HTML recipe (`interactiveScaling`).
|
|
595
689
|
// @remove-in: 2.0.0 [renderer-interactive-scaling]
|
|
596
690
|
decreasePortions: "Decrease portions",
|
|
597
691
|
editPortions: "Edit portions directly",
|
|
598
692
|
increasePortions: "Increase portions",
|
|
599
693
|
returnToStep: "Return to step",
|
|
600
|
-
mixedUnitsTooltip: "
|
|
694
|
+
mixedUnitsTooltip: "Mixed units: couldn't be combined into one total \u2014 add a density to the ingredient database to merge them",
|
|
601
695
|
mixedUnits: "Mixed units",
|
|
602
696
|
copyShoppingList: "Copy list",
|
|
603
697
|
copied: "Copied!",
|
|
@@ -775,6 +869,10 @@ var en3 = {
|
|
|
775
869
|
stepQuantitiesDesc: "Writes the quantity of each ingredient in the text of the steps, after its name.",
|
|
776
870
|
nutritionBasis: "Nutrition basis",
|
|
777
871
|
nutritionBasisAuto: "Automatic (every basis, side by side)",
|
|
872
|
+
clock: "Time format",
|
|
873
|
+
clockAuto: "Automatic (as your region writes it)",
|
|
874
|
+
clock12h: "12 hours (1:00 PM)",
|
|
875
|
+
clock24h: "24 hours (13:00)",
|
|
778
876
|
rejectedSlots: "Unavailable slots:",
|
|
779
877
|
forceReference: "Force Reference:",
|
|
780
878
|
autoDetect: "Auto (detect @*)",
|
|
@@ -798,6 +896,7 @@ var en3 = {
|
|
|
798
896
|
scaleByIngredient: "Proportionally recalculates the entire recipe to match the exact quantity you have on hand for a specific ingredient (e.g. only 3 eggs or 250g of butter).",
|
|
799
897
|
bakersMath: "Expresses each ingredient as a percentage of a base ingredient set at 100% (typically flour, or any chosen ingredient), making ratios and formulation easy to evaluate.",
|
|
800
898
|
comments: "Shows or leaves out the comments written in the recipe with //: the notes alone on a line and the ones at the end of a line. They are set apart discreetly in the run sheet.",
|
|
899
|
+
clock: "How the run sheet and the suggested serving times write the time. Automatic follows your browser's region.",
|
|
801
900
|
stepQuantities: "Writes each quantity in the step that uses it, with its weight in grams when the masses are standardized. Leave it off for shorter steps: the quantities stay in the ingredient lists.",
|
|
802
901
|
nutritionBasis: "Which basis the nutrition panel shows, when the nutrition is estimated. Automatic shows per 100 g and per portion side by side, with the whole recipe under them, in every recipe format.",
|
|
803
902
|
bakersMathOnly: "Hides gram weights to display only relative percentages, turning the recipe into a pure ratio formula.",
|
|
@@ -831,7 +930,9 @@ var en3 = {
|
|
|
831
930
|
item: "Item",
|
|
832
931
|
show: "Show",
|
|
833
932
|
expandConsole: "Expand console",
|
|
834
|
-
collapseConsole: "Collapse console"
|
|
933
|
+
collapseConsole: "Collapse console",
|
|
934
|
+
notInDatabase: "Not in the example database.",
|
|
935
|
+
showDatabase: "See the database"
|
|
835
936
|
},
|
|
836
937
|
errorState: {
|
|
837
938
|
title: VIEWS_PAUSED.title,
|
|
@@ -839,6 +940,12 @@ var en3 = {
|
|
|
839
940
|
jumpToError: "Jump to error",
|
|
840
941
|
line: "Line"
|
|
841
942
|
},
|
|
943
|
+
database: {
|
|
944
|
+
tabLabel: "Ingredient database, read-only",
|
|
945
|
+
project: "In a project, this is your own ingredients.yaml, which the CLI fills for you. Here it is read-only.",
|
|
946
|
+
docLink: "How to manage an ingredient database",
|
|
947
|
+
docHref: "/docs/how-to/manage-database"
|
|
948
|
+
},
|
|
842
949
|
tabs: {
|
|
843
950
|
addFile: "Add file",
|
|
844
951
|
removeFile: "Remove file",
|
|
@@ -863,6 +970,18 @@ var en3 = {
|
|
|
863
970
|
statusValid: "Valid",
|
|
864
971
|
statusWarnings: "Warnings",
|
|
865
972
|
copyBtn: "Copy to Clipboard"
|
|
973
|
+
},
|
|
974
|
+
cook: {
|
|
975
|
+
/** Under a step of `gram cook`: the state that ends it (`={ribbon stage}`). */
|
|
976
|
+
targetState: "Target:",
|
|
977
|
+
/** When a timer of a step rings: the target states to check, `{states}`. */
|
|
978
|
+
checkTargetState: "Check: {states}"
|
|
979
|
+
},
|
|
980
|
+
languageServer: {
|
|
981
|
+
/** Hover on a section anchor: `{duration}` is the anticipation. */
|
|
982
|
+
anchorReady: "Ready {duration} before it is used.",
|
|
983
|
+
anchorInDays: "In days: opens a working day of its own.",
|
|
984
|
+
anchorInHours: "In hours or minutes: a deadline within the day, no working day of its own."
|
|
866
985
|
}
|
|
867
986
|
};
|
|
868
987
|
|
|
@@ -992,6 +1111,7 @@ var fr3 = {
|
|
|
992
1111
|
runSheetInStock: "en stock",
|
|
993
1112
|
runSheetProgramStep: "\xE9tape {n}",
|
|
994
1113
|
runSheetProgramSteps: "\xE9tapes {from} \xE0 {to}",
|
|
1114
|
+
// @remove-in: 2.0.0 [i18n-run-sheet-range]
|
|
995
1115
|
runSheetRange: "{min} \xE0 {max}",
|
|
996
1116
|
runSheetPossibleFrom: "Possible d\xE8s {time}.",
|
|
997
1117
|
runSheetFixedService: "D\xE9but non avan\xE7able : l'attente \xAB {rest} \xBB ({section}) ne peut pas d\xE9passer {length} et se termine au service.",
|
|
@@ -1042,13 +1162,18 @@ var fr3 = {
|
|
|
1042
1162
|
tooltipBakers: "% boulanger",
|
|
1043
1163
|
tooltipTakenFrom: "Pr\xE9lev\xE9 sur",
|
|
1044
1164
|
alternatives: "Alternatives",
|
|
1165
|
+
// Une proportion avec unités (`1 / 100 g @&farine`) : sa ligne dans la
|
|
1166
|
+
// bulle, et les deux formes relatives (`70 %`, `1 / 100 g`) tant
|
|
1167
|
+
// qu'elles ne sont pas calculées.
|
|
1168
|
+
tooltipRatio: "Proportion",
|
|
1169
|
+
relativeOf: "{amount} de {target}",
|
|
1045
1170
|
// The deprecated portions stepper of the HTML recipe (`interactiveScaling`).
|
|
1046
1171
|
// @remove-in: 2.0.0 [renderer-interactive-scaling]
|
|
1047
1172
|
decreasePortions: "Diminuer les portions",
|
|
1048
1173
|
editPortions: "Modifier directement les portions",
|
|
1049
1174
|
increasePortions: "Augmenter les portions",
|
|
1050
1175
|
returnToStep: "Retour \xE0 l'\xE9tape",
|
|
1051
|
-
mixedUnitsTooltip: "
|
|
1176
|
+
mixedUnitsTooltip: "Unit\xE9s mixtes : impossible de fusionner en un seul total \u2014 ajoutez une densit\xE9 \xE0 la base de donn\xE9es pour les convertir",
|
|
1052
1177
|
mixedUnits: "Unit\xE9s mixtes",
|
|
1053
1178
|
copyShoppingList: "Copier la liste",
|
|
1054
1179
|
copied: "Copi\xE9 !",
|
|
@@ -1210,6 +1335,10 @@ var fr3 = {
|
|
|
1210
1335
|
stepQuantitiesDesc: "\xC9crit la quantit\xE9 de chaque ingr\xE9dient dans le texte des \xE9tapes, juste apr\xE8s son nom.",
|
|
1211
1336
|
nutritionBasis: "Base nutritionnelle",
|
|
1212
1337
|
nutritionBasisAuto: "Automatique (toutes les bases c\xF4te \xE0 c\xF4te)",
|
|
1338
|
+
clock: "Format de l'heure",
|
|
1339
|
+
clockAuto: "Automatique (selon votre r\xE9gion)",
|
|
1340
|
+
clock12h: "12 heures (1:00 PM)",
|
|
1341
|
+
clock24h: "24 heures (13:00)",
|
|
1213
1342
|
rejectedSlots: "Cr\xE9neaux non retenus :",
|
|
1214
1343
|
forceReference: "Forcer la r\xE9f\xE9rence :",
|
|
1215
1344
|
autoDetect: "Auto (d\xE9tecte @*)",
|
|
@@ -1233,6 +1362,7 @@ var fr3 = {
|
|
|
1233
1362
|
scaleByIngredient: "Recalcule proportionnellement toute la recette pour l'ajuster \xE0 la quantit\xE9 exacte dont vous disposez pour un ingr\xE9dient donn\xE9 (ex. seulement 3 \u0153ufs ou 250 g de beurre).",
|
|
1234
1363
|
bakersMath: "Exprime chaque ingr\xE9dient en pourcentage d'un ingr\xE9dient de base fix\xE9 \xE0 100 % (par d\xE9faut la farine, ou l'ingr\xE9dient de votre choix), facilitant l'\xE9valuation des \xE9quilibres et des ratios.",
|
|
1235
1364
|
comments: "Affiche ou masque les commentaires \xE9crits dans la recette avec // : les notes seules sur une ligne et celles en fin de ligne. Dans le plan de marche, ils sont mis \xE0 part, discr\xE8tement.",
|
|
1365
|
+
clock: "La fa\xE7on d'\xE9crire l'heure dans le plan de marche et les horaires sugg\xE9r\xE9s. En automatique, elle suit la r\xE9gion de votre navigateur.",
|
|
1236
1366
|
stepQuantities: "Rappelle chaque quantit\xE9 dans l'\xE9tape qui l'utilise, avec son poids en grammes si les masses sont standardis\xE9es. D\xE9cochez pour des \xE9tapes plus courtes : les quantit\xE9s restent dans les listes d'ingr\xE9dients.",
|
|
1237
1367
|
nutritionBasis: "La base du panneau nutritionnel, quand la nutrition est estim\xE9e. En automatique, pour 100 g et par portion c\xF4te \xE0 c\xF4te, la recette enti\xE8re en dessous, dans tous les formats de la recette.",
|
|
1238
1368
|
bakersMathOnly: "Masque les poids en grammes pour n'afficher que les pourcentages relatifs, transformant la recette en une formule de proportions pure.",
|
|
@@ -1263,7 +1393,9 @@ var fr3 = {
|
|
|
1263
1393
|
item: "\xC9l\xE9ment",
|
|
1264
1394
|
show: "Afficher",
|
|
1265
1395
|
expandConsole: "D\xE9velopper la console",
|
|
1266
|
-
collapseConsole: "R\xE9duire la console"
|
|
1396
|
+
collapseConsole: "R\xE9duire la console",
|
|
1397
|
+
notInDatabase: "Absent de la base d'exemple.",
|
|
1398
|
+
showDatabase: "Voir la base"
|
|
1267
1399
|
},
|
|
1268
1400
|
errorState: {
|
|
1269
1401
|
title: VIEWS_PAUSED2.title,
|
|
@@ -1271,6 +1403,12 @@ var fr3 = {
|
|
|
1271
1403
|
jumpToError: "Aller \xE0 l'erreur",
|
|
1272
1404
|
line: "Ligne"
|
|
1273
1405
|
},
|
|
1406
|
+
database: {
|
|
1407
|
+
tabLabel: "Base d'ingr\xE9dients, en lecture seule",
|
|
1408
|
+
project: "Base d'exemple en lecture seule. Dans un projet, ce fichier ingredients.yaml est le v\xF4tre (modifiable \xE0 la main ou aid\xE9 par le CLI).",
|
|
1409
|
+
docLink: "G\xE9rer sa base de donn\xE9es d'ingr\xE9dients",
|
|
1410
|
+
docHref: "/fr/docs/how-to/manage-database"
|
|
1411
|
+
},
|
|
1274
1412
|
tabs: {
|
|
1275
1413
|
addFile: "Ajouter un fichier",
|
|
1276
1414
|
removeFile: "Supprimer le fichier",
|
|
@@ -1295,6 +1433,15 @@ var fr3 = {
|
|
|
1295
1433
|
statusValid: "Valide",
|
|
1296
1434
|
statusWarnings: "Avertissements",
|
|
1297
1435
|
copyBtn: "Copier dans le presse-papier"
|
|
1436
|
+
},
|
|
1437
|
+
cook: {
|
|
1438
|
+
targetState: "\xC9tat attendu :",
|
|
1439
|
+
checkTargetState: "V\xE9rifiez : {states}"
|
|
1440
|
+
},
|
|
1441
|
+
languageServer: {
|
|
1442
|
+
anchorReady: "Pr\xEAte {duration} avant d'\xEAtre utilis\xE9e.",
|
|
1443
|
+
anchorInDays: "En jours : ouvre sa propre journ\xE9e de travail.",
|
|
1444
|
+
anchorInHours: "En heures ou en minutes : une \xE9ch\xE9ance dans la journ\xE9e, sans journ\xE9e de travail \xE0 part."
|
|
1298
1445
|
}
|
|
1299
1446
|
};
|
|
1300
1447
|
|
|
@@ -1316,8 +1463,771 @@ function fillTemplate(template, values) {
|
|
|
1316
1463
|
(whole, key) => Object.hasOwn(values, key) ? values[key] : whole
|
|
1317
1464
|
);
|
|
1318
1465
|
}
|
|
1466
|
+
|
|
1467
|
+
// src/duration.ts
|
|
1468
|
+
var FRACTION_GLYPHS = {
|
|
1469
|
+
"\xBC": 1 / 4,
|
|
1470
|
+
"\xBD": 1 / 2,
|
|
1471
|
+
"\xBE": 3 / 4,
|
|
1472
|
+
"\u2153": 1 / 3,
|
|
1473
|
+
"\u2154": 2 / 3,
|
|
1474
|
+
"\u2155": 1 / 5,
|
|
1475
|
+
"\u2156": 2 / 5,
|
|
1476
|
+
"\u2157": 3 / 5,
|
|
1477
|
+
"\u2158": 4 / 5,
|
|
1478
|
+
"\u2159": 1 / 6,
|
|
1479
|
+
"\u215A": 5 / 6,
|
|
1480
|
+
"\u215B": 1 / 8,
|
|
1481
|
+
"\u215C": 3 / 8,
|
|
1482
|
+
"\u215D": 5 / 8,
|
|
1483
|
+
"\u215E": 7 / 8
|
|
1484
|
+
};
|
|
1485
|
+
var MINUTE_PRIMES = /* @__PURE__ */ new Set(["'", "\u2019", "\u2032"]);
|
|
1486
|
+
var SECOND_PRIMES = /* @__PURE__ */ new Set(['"', "\u201D", "\u2033"]);
|
|
1487
|
+
var DASHES = /* @__PURE__ */ new Set(["-", "\u2013"]);
|
|
1488
|
+
var STARS = /* @__PURE__ */ new Set(["*", "\xD7"]);
|
|
1489
|
+
var isDigit = (c) => c !== void 0 && c >= "0" && c <= "9";
|
|
1490
|
+
var isLetter = (c) => c !== void 0 && /[\p{L}\p{M}]/u.test(c);
|
|
1491
|
+
var ISO_8601 = /^p(?=\d|t\d)(\d+([.,]\d+)?[ymwd])*(t(\d+([.,]\d+)?[hms])+)?$/i;
|
|
1492
|
+
var resolveUnit = (word) => {
|
|
1493
|
+
const unit = TIME_GLOBAL[word];
|
|
1494
|
+
return unit !== void 0 && unit in TIME_TO_MINUTES ? unit : void 0;
|
|
1495
|
+
};
|
|
1496
|
+
function tokenizeDuration(text) {
|
|
1497
|
+
const s = text.normalize("NFC");
|
|
1498
|
+
const tokens = [];
|
|
1499
|
+
let space = false;
|
|
1500
|
+
let i = 0;
|
|
1501
|
+
while (i < s.length) {
|
|
1502
|
+
const c = s[i];
|
|
1503
|
+
if (/\s/u.test(c)) {
|
|
1504
|
+
space = true;
|
|
1505
|
+
i++;
|
|
1506
|
+
continue;
|
|
1507
|
+
}
|
|
1508
|
+
if (isDigit(c) || c in FRACTION_GLYPHS) {
|
|
1509
|
+
let j = i;
|
|
1510
|
+
while (isDigit(s[j])) j++;
|
|
1511
|
+
let num = "int";
|
|
1512
|
+
let value;
|
|
1513
|
+
if ((s[j] === "." || s[j] === ",") && isDigit(s[j + 1])) {
|
|
1514
|
+
j++;
|
|
1515
|
+
while (isDigit(s[j])) j++;
|
|
1516
|
+
num = "decimal";
|
|
1517
|
+
value = Number(s.slice(i, j).replace(",", "."));
|
|
1518
|
+
} else if (s[j] === "/" && j > i && isDigit(s[j + 1])) {
|
|
1519
|
+
const slash = j;
|
|
1520
|
+
j++;
|
|
1521
|
+
while (isDigit(s[j])) j++;
|
|
1522
|
+
const den = Number(s.slice(slash + 1, j));
|
|
1523
|
+
if (den === 0) return { ok: false, reason: "unexpected" };
|
|
1524
|
+
num = "fraction";
|
|
1525
|
+
value = Number(s.slice(i, slash)) / den;
|
|
1526
|
+
} else if (s[j] !== void 0 && s[j] in FRACTION_GLYPHS) {
|
|
1527
|
+
const whole = j > i ? Number(s.slice(i, j)) : 0;
|
|
1528
|
+
value = whole + FRACTION_GLYPHS[s[j]];
|
|
1529
|
+
j++;
|
|
1530
|
+
num = "fraction";
|
|
1531
|
+
} else {
|
|
1532
|
+
value = Number(s.slice(i, j));
|
|
1533
|
+
}
|
|
1534
|
+
tokens.push({ kind: "num", value, text: s.slice(i, j), num, space });
|
|
1535
|
+
i = j;
|
|
1536
|
+
} else if (isLetter(c)) {
|
|
1537
|
+
let j = i;
|
|
1538
|
+
while (isLetter(s[j])) j++;
|
|
1539
|
+
const word = s.slice(i, j).toLowerCase();
|
|
1540
|
+
if (s[j] === ".") j++;
|
|
1541
|
+
tokens.push({ kind: "word", text: word, unit: resolveUnit(word) });
|
|
1542
|
+
i = j;
|
|
1543
|
+
} else if (MINUTE_PRIMES.has(c)) {
|
|
1544
|
+
if (MINUTE_PRIMES.has(s[i + 1])) {
|
|
1545
|
+
tokens.push({ kind: "prime", unit: "s" });
|
|
1546
|
+
i += 2;
|
|
1547
|
+
} else {
|
|
1548
|
+
tokens.push({ kind: "prime", unit: "m" });
|
|
1549
|
+
i++;
|
|
1550
|
+
}
|
|
1551
|
+
} else if (SECOND_PRIMES.has(c)) {
|
|
1552
|
+
tokens.push({ kind: "prime", unit: "s" });
|
|
1553
|
+
i++;
|
|
1554
|
+
} else if (c === ":") {
|
|
1555
|
+
tokens.push({ kind: "colon" });
|
|
1556
|
+
i++;
|
|
1557
|
+
} else if (c === "+") {
|
|
1558
|
+
tokens.push({ kind: "plus" });
|
|
1559
|
+
i++;
|
|
1560
|
+
} else if (DASHES.has(c)) {
|
|
1561
|
+
tokens.push({ kind: "dash" });
|
|
1562
|
+
i++;
|
|
1563
|
+
} else if (STARS.has(c)) {
|
|
1564
|
+
tokens.push({ kind: "star" });
|
|
1565
|
+
i++;
|
|
1566
|
+
} else {
|
|
1567
|
+
return { ok: false, reason: "unexpected" };
|
|
1568
|
+
}
|
|
1569
|
+
space = false;
|
|
1570
|
+
}
|
|
1571
|
+
return { ok: true, tokens: mergeMixedFractions(tokens) };
|
|
1572
|
+
}
|
|
1573
|
+
function mergeMixedFractions(tokens) {
|
|
1574
|
+
const out = [];
|
|
1575
|
+
for (const token of tokens) {
|
|
1576
|
+
const prev = out[out.length - 1];
|
|
1577
|
+
if (token.kind === "num" && token.num === "fraction" && token.space && token.value < 1 && prev?.kind === "num" && prev.num === "int") {
|
|
1578
|
+
out[out.length - 1] = {
|
|
1579
|
+
...prev,
|
|
1580
|
+
value: prev.value + token.value,
|
|
1581
|
+
text: `${prev.text} ${token.text}`,
|
|
1582
|
+
num: "fraction"
|
|
1583
|
+
};
|
|
1584
|
+
} else {
|
|
1585
|
+
out.push(token);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
return out;
|
|
1589
|
+
}
|
|
1590
|
+
var RANK = { d: 0, h: 1, m: 2, s: 3 };
|
|
1591
|
+
var IMPLIED = { h: "m", m: "s" };
|
|
1592
|
+
var partMinutes = ({ value, unit }) => unit === "s" ? value / 60 : value * TIME_TO_MINUTES[unit];
|
|
1593
|
+
var makeSide = (parts, form) => ({
|
|
1594
|
+
minutes: parts.reduce((sum, part) => sum + partMinutes(part), 0),
|
|
1595
|
+
parts,
|
|
1596
|
+
form
|
|
1597
|
+
});
|
|
1598
|
+
var fail = (reason) => ({ ok: false, reason });
|
|
1599
|
+
function analyzeSide(tokens) {
|
|
1600
|
+
if (tokens.some((t) => t.kind === "colon")) return analyzeClock(tokens);
|
|
1601
|
+
return analyzeTerms(tokens);
|
|
1602
|
+
}
|
|
1603
|
+
function analyzeClock(tokens) {
|
|
1604
|
+
const nums = [];
|
|
1605
|
+
let k = 0;
|
|
1606
|
+
for (; ; ) {
|
|
1607
|
+
const t = tokens[k];
|
|
1608
|
+
if (t?.kind !== "num") return fail("clockFormat");
|
|
1609
|
+
nums.push(t);
|
|
1610
|
+
k++;
|
|
1611
|
+
if (tokens[k]?.kind !== "colon") break;
|
|
1612
|
+
k++;
|
|
1613
|
+
}
|
|
1614
|
+
const rest = tokens.slice(k);
|
|
1615
|
+
const unitToken = rest[0];
|
|
1616
|
+
if (nums.length < 2 || nums.length > 3 || rest.length > 1 || unitToken && unitToken.kind !== "word") {
|
|
1617
|
+
return fail("clockFormat");
|
|
1618
|
+
}
|
|
1619
|
+
const [first, ...others] = nums;
|
|
1620
|
+
if (first.num !== "int") return fail("clockFormat");
|
|
1621
|
+
for (const n of others) {
|
|
1622
|
+
if (n.num !== "int" || n.text.length !== 2) return fail("clockFormat");
|
|
1623
|
+
}
|
|
1624
|
+
if (others.some((n) => n.value >= 60)) return fail("overflow60");
|
|
1625
|
+
const unit = unitToken?.kind === "word" ? unitToken.unit : void 0;
|
|
1626
|
+
const values = nums.map((n) => n.value);
|
|
1627
|
+
if (nums.length === 3) {
|
|
1628
|
+
if (unitToken && unit !== "h") return fail("clockFormat");
|
|
1629
|
+
const [h, m, s] = values;
|
|
1630
|
+
const parts = [
|
|
1631
|
+
{ value: h, unit: "h" },
|
|
1632
|
+
{ value: m, unit: "m" },
|
|
1633
|
+
{ value: s, unit: "s" }
|
|
1634
|
+
];
|
|
1635
|
+
return { kind: "side", side: makeSide(parts, "clock") };
|
|
1636
|
+
}
|
|
1637
|
+
if (!unitToken)
|
|
1638
|
+
return { kind: "bareClock", nums };
|
|
1639
|
+
if (unit !== "h" && unit !== "m") return fail("clockFormat");
|
|
1640
|
+
return {
|
|
1641
|
+
kind: "side",
|
|
1642
|
+
side: clockSide(values[0], values[1], unit),
|
|
1643
|
+
clockUnit: unit
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
var clockSide = (a, b, unit) => makeSide(
|
|
1647
|
+
[
|
|
1648
|
+
{ value: a, unit },
|
|
1649
|
+
{ value: b, unit: unit === "h" ? "m" : "s" }
|
|
1650
|
+
],
|
|
1651
|
+
"clock"
|
|
1652
|
+
);
|
|
1653
|
+
function analyzeTerms(tokens) {
|
|
1654
|
+
const parts = [];
|
|
1655
|
+
let primes = 0;
|
|
1656
|
+
let k = 0;
|
|
1657
|
+
while (k < tokens.length) {
|
|
1658
|
+
const num = tokens[k];
|
|
1659
|
+
if (num?.kind !== "num") return fail("unexpected");
|
|
1660
|
+
const next = tokens[k + 1];
|
|
1661
|
+
if (next?.kind !== "word" && next?.kind !== "prime") {
|
|
1662
|
+
if (next !== void 0) return fail("bareNumber");
|
|
1663
|
+
if (parts.length === 0) return { kind: "bare", num };
|
|
1664
|
+
const prev = tokens[k - 1];
|
|
1665
|
+
const last2 = parts[parts.length - 1];
|
|
1666
|
+
const implied = IMPLIED[last2.unit];
|
|
1667
|
+
if (prev?.kind === "plus" || !implied) return fail("bareNumber");
|
|
1668
|
+
if (num.num !== "int") return fail("decimalInCompound");
|
|
1669
|
+
if (num.value >= 60) return fail("overflow60");
|
|
1670
|
+
parts.push({ value: num.value, unit: implied });
|
|
1671
|
+
k++;
|
|
1672
|
+
break;
|
|
1673
|
+
}
|
|
1674
|
+
const unit = next.unit;
|
|
1675
|
+
const last = parts[parts.length - 1];
|
|
1676
|
+
if (next.kind === "prime") {
|
|
1677
|
+
if (num.num !== "int") return fail("decimalPrime");
|
|
1678
|
+
if (last && num.value >= 60) return fail("overflow60");
|
|
1679
|
+
primes++;
|
|
1680
|
+
}
|
|
1681
|
+
if (last) {
|
|
1682
|
+
if (last.unit === unit) return fail("duplicateUnit");
|
|
1683
|
+
if (RANK[last.unit] > RANK[unit]) return fail("unitOrder");
|
|
1684
|
+
}
|
|
1685
|
+
parts.push({ value: num.value, unit });
|
|
1686
|
+
k += 2;
|
|
1687
|
+
if (tokens[k]?.kind === "plus") {
|
|
1688
|
+
if (tokens[k + 1]?.kind !== "num") return fail("unexpected");
|
|
1689
|
+
k++;
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
if (parts.length > 1 && tokens.some((t) => t.kind === "num" && t.num !== "int")) {
|
|
1693
|
+
return fail("decimalInCompound");
|
|
1694
|
+
}
|
|
1695
|
+
const terms = tokens.filter((t) => t.kind === "word" || t.kind === "prime");
|
|
1696
|
+
const side = makeSide(parts, primes === terms.length ? "prime" : "units");
|
|
1697
|
+
if (parts.length === 1) {
|
|
1698
|
+
const only = parts[0];
|
|
1699
|
+
return {
|
|
1700
|
+
kind: "side",
|
|
1701
|
+
side,
|
|
1702
|
+
single: { unit: only.unit, prime: primes === 1 }
|
|
1703
|
+
};
|
|
1704
|
+
}
|
|
1705
|
+
return { kind: "side", side };
|
|
1706
|
+
}
|
|
1707
|
+
function parseDuration(text) {
|
|
1708
|
+
const trimmed = text.trim();
|
|
1709
|
+
if (trimmed === "") return fail("empty");
|
|
1710
|
+
if (ISO_8601.test(trimmed.replace(/\s/gu, ""))) return fail("iso8601");
|
|
1711
|
+
const tokenized = tokenizeDuration(trimmed);
|
|
1712
|
+
if (!tokenized.ok) return tokenized;
|
|
1713
|
+
const { tokens } = tokenized;
|
|
1714
|
+
for (let k = 0; k < tokens.length; k++) {
|
|
1715
|
+
const t = tokens[k];
|
|
1716
|
+
const prev = tokens[k - 1];
|
|
1717
|
+
if (t.kind === "star") return fail("multiplication");
|
|
1718
|
+
if (t.kind !== "word") continue;
|
|
1719
|
+
if (t.text === "x" && prev?.kind === "num" && tokens[k + 1]?.kind === "num") {
|
|
1720
|
+
return fail("multiplication");
|
|
1721
|
+
}
|
|
1722
|
+
if (prev?.kind !== "num") return fail("words");
|
|
1723
|
+
if (!t.unit) return { ok: false, reason: "unknownUnit", unit: t.text };
|
|
1724
|
+
}
|
|
1725
|
+
const dashes = tokens.filter((t) => t.kind === "dash").length;
|
|
1726
|
+
if (dashes === 0) {
|
|
1727
|
+
const a = analyzeSide(tokens);
|
|
1728
|
+
if ("ok" in a) return a;
|
|
1729
|
+
if (a.kind === "bare") return fail("missingUnit");
|
|
1730
|
+
if (a.kind === "bareClock") return fail("ambiguousClock");
|
|
1731
|
+
return { ok: true, minutes: a.side.minutes, sides: [a.side] };
|
|
1732
|
+
}
|
|
1733
|
+
const dash = tokens.findIndex((t) => t.kind === "dash");
|
|
1734
|
+
const leftTokens = tokens.slice(0, dash);
|
|
1735
|
+
const rightTokens = tokens.slice(dash + 1);
|
|
1736
|
+
if (dashes > 1 || leftTokens.length === 0 || rightTokens.length === 0) {
|
|
1737
|
+
return fail("unexpected");
|
|
1738
|
+
}
|
|
1739
|
+
return parseRange(analyzeSide(leftTokens), analyzeSide(rightTokens));
|
|
1740
|
+
}
|
|
1741
|
+
function parseRange(left, right) {
|
|
1742
|
+
if ("ok" in left) return left;
|
|
1743
|
+
if ("ok" in right) return right;
|
|
1744
|
+
if (left.kind === "bare" && right.kind === "bare") return fail("missingUnit");
|
|
1745
|
+
if (left.kind === "bareClock" && right.kind === "bareClock") {
|
|
1746
|
+
return fail("ambiguousClock");
|
|
1747
|
+
}
|
|
1748
|
+
if (right.kind !== "side") return fail("ambiguousRange");
|
|
1749
|
+
let min;
|
|
1750
|
+
if (left.kind === "side") {
|
|
1751
|
+
min = left.side;
|
|
1752
|
+
} else if (left.kind === "bare") {
|
|
1753
|
+
const single = right.single;
|
|
1754
|
+
if (!single) return fail("ambiguousRange");
|
|
1755
|
+
if (single.prime && left.num.num !== "int") return fail("decimalPrime");
|
|
1756
|
+
min = makeSide(
|
|
1757
|
+
[{ value: left.num.value, unit: single.unit }],
|
|
1758
|
+
right.side.form
|
|
1759
|
+
);
|
|
1760
|
+
} else {
|
|
1761
|
+
const unit = right.clockUnit;
|
|
1762
|
+
if (unit !== "h" && unit !== "m") return fail("ambiguousRange");
|
|
1763
|
+
min = clockSide(left.nums[0].value, left.nums[1].value, unit);
|
|
1764
|
+
}
|
|
1765
|
+
const max = right.side;
|
|
1766
|
+
if (min.minutes > max.minutes) return fail("reversedRange");
|
|
1767
|
+
if (min.minutes === max.minutes) return fail("emptyRange");
|
|
1768
|
+
return {
|
|
1769
|
+
ok: true,
|
|
1770
|
+
minutes: (min.minutes + max.minutes) / 2,
|
|
1771
|
+
range: { min: min.minutes, max: max.minutes },
|
|
1772
|
+
sides: [min, max]
|
|
1773
|
+
};
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
// src/quantity.ts
|
|
1777
|
+
function localSpelling(unit, lang) {
|
|
1778
|
+
const base = lang?.toLowerCase().split("-")[0];
|
|
1779
|
+
if (!unit || !base || base === "en") return false;
|
|
1780
|
+
const local = UNIT_DICTIONARIES[base];
|
|
1781
|
+
if (!local) return false;
|
|
1782
|
+
const written = unit.toLowerCase().trim();
|
|
1783
|
+
const isIn = (dict) => Object.values(dict).some((aliases) => aliases.includes(written));
|
|
1784
|
+
return isIn(local) && !isIn(UNIT_DICTIONARIES.en);
|
|
1785
|
+
}
|
|
1786
|
+
var QuantityOverflowError = class extends RangeError {
|
|
1787
|
+
constructor(factor) {
|
|
1788
|
+
super(
|
|
1789
|
+
`Scaling by a factor of ${factor} overflowed a quantity to a non-finite value.`
|
|
1790
|
+
);
|
|
1791
|
+
this.factor = factor;
|
|
1792
|
+
this.name = "QuantityOverflowError";
|
|
1793
|
+
}
|
|
1794
|
+
factor;
|
|
1795
|
+
};
|
|
1796
|
+
var round2 = (value) => parseFloat(value.toFixed(2));
|
|
1797
|
+
function isTermValue(value) {
|
|
1798
|
+
if (typeof value === "number") return true;
|
|
1799
|
+
if (!value || typeof value !== "object") return false;
|
|
1800
|
+
const type = value.type;
|
|
1801
|
+
return type === "single" || type === "fraction" || type === "range";
|
|
1802
|
+
}
|
|
1803
|
+
function termValueNumber(value) {
|
|
1804
|
+
if (typeof value === "number") return value;
|
|
1805
|
+
return value.value ?? null;
|
|
1806
|
+
}
|
|
1807
|
+
function addedFirst(terms) {
|
|
1808
|
+
return [...terms.filter((t) => !t.minus), ...terms.filter((t) => t.minus)];
|
|
1809
|
+
}
|
|
1810
|
+
function termBounds(value) {
|
|
1811
|
+
if (typeof value === "number") return { min: value, max: value };
|
|
1812
|
+
if (value.type === "range" && value.range) {
|
|
1813
|
+
return { min: value.range.min, max: value.range.max };
|
|
1814
|
+
}
|
|
1815
|
+
const n = termValueNumber(value);
|
|
1816
|
+
return n === null ? null : { min: n, max: n };
|
|
1817
|
+
}
|
|
1818
|
+
var exact = (n) => Math.round(n * 1e9) / 1e9;
|
|
1819
|
+
function commonUnit(ordered) {
|
|
1820
|
+
const [first] = ordered;
|
|
1821
|
+
if (!first) return null;
|
|
1822
|
+
const firstKey = normalizeUnit(first.unit);
|
|
1823
|
+
if (ordered.every((t) => normalizeUnit(t.unit) === firstKey)) {
|
|
1824
|
+
return { unit: first.unit, weight: () => 1, divisor: 1, round: exact };
|
|
1825
|
+
}
|
|
1826
|
+
const units = ordered.map((t) => t.unit);
|
|
1827
|
+
if (sameLadder(units)) {
|
|
1828
|
+
const steps = ordered.map((t) => ladderOf(t.unit));
|
|
1829
|
+
let smallest = 0;
|
|
1830
|
+
steps.forEach((step, i) => {
|
|
1831
|
+
if (step.size < steps[smallest].size) smallest = i;
|
|
1832
|
+
});
|
|
1833
|
+
return {
|
|
1834
|
+
unit: ordered[smallest].unit,
|
|
1835
|
+
weight: (t) => ladderOf(t.unit).size,
|
|
1836
|
+
divisor: steps[smallest].size,
|
|
1837
|
+
round: exact
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
for (const family of [UNIT_CONVERSIONS.mass, UNIT_CONVERSIONS.volume]) {
|
|
1841
|
+
const factors = units.map((u) => family.map[normalizeUnit(u)]);
|
|
1842
|
+
if (factors.every((f) => f !== void 0)) {
|
|
1843
|
+
return {
|
|
1844
|
+
unit: family.base,
|
|
1845
|
+
weight: (t) => family.map[normalizeUnit(t.unit)],
|
|
1846
|
+
divisor: 1,
|
|
1847
|
+
round: round2
|
|
1848
|
+
};
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
return null;
|
|
1852
|
+
}
|
|
1853
|
+
function termsTotal(terms) {
|
|
1854
|
+
const ordered = addedFirst(terms);
|
|
1855
|
+
const [first] = ordered;
|
|
1856
|
+
if (!first) return null;
|
|
1857
|
+
if (ordered.length === 1 && !first.minus) {
|
|
1858
|
+
return { qty: first.value, unit: first.unit };
|
|
1859
|
+
}
|
|
1860
|
+
const common = commonUnit(ordered);
|
|
1861
|
+
if (!common) return null;
|
|
1862
|
+
let min = 0;
|
|
1863
|
+
let max = 0;
|
|
1864
|
+
let isRange = false;
|
|
1865
|
+
for (const term of ordered) {
|
|
1866
|
+
const bounds = termBounds(term.value);
|
|
1867
|
+
if (!bounds) return null;
|
|
1868
|
+
if (typeof term.value === "object" && term.value.type === "range") {
|
|
1869
|
+
isRange = true;
|
|
1870
|
+
}
|
|
1871
|
+
const weight = common.weight(term);
|
|
1872
|
+
if (term.minus) {
|
|
1873
|
+
min -= bounds.max * weight;
|
|
1874
|
+
max -= bounds.min * weight;
|
|
1875
|
+
} else {
|
|
1876
|
+
min += bounds.min * weight;
|
|
1877
|
+
max += bounds.max * weight;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
min = common.round(min / common.divisor);
|
|
1881
|
+
max = common.round(max / common.divisor);
|
|
1882
|
+
if (!isRange) return { qty: min, unit: common.unit };
|
|
1883
|
+
return {
|
|
1884
|
+
qty: { type: "range", value: exact((min + max) / 2), range: { min, max } },
|
|
1885
|
+
unit: common.unit
|
|
1886
|
+
};
|
|
1887
|
+
}
|
|
1888
|
+
function familyOf(unit) {
|
|
1889
|
+
const key = normalizeUnit(unit);
|
|
1890
|
+
if (UNIT_CONVERSIONS.mass.map[key] !== void 0) return "mass";
|
|
1891
|
+
if (UNIT_CONVERSIONS.volume.map[key] !== void 0) return "volume";
|
|
1892
|
+
return `unit:${key}`;
|
|
1893
|
+
}
|
|
1894
|
+
function totalAsTerm(total) {
|
|
1895
|
+
const { qty, unit } = total;
|
|
1896
|
+
if (typeof qty === "number") {
|
|
1897
|
+
return qty < 0 ? { value: -qty, unit, minus: true } : { value: qty, unit };
|
|
1898
|
+
}
|
|
1899
|
+
if (qty.type === "range" && qty.range.max < 0) {
|
|
1900
|
+
const min = -qty.range.max;
|
|
1901
|
+
const max = -qty.range.min;
|
|
1902
|
+
return {
|
|
1903
|
+
value: {
|
|
1904
|
+
type: "range",
|
|
1905
|
+
value: exact((min + max) / 2),
|
|
1906
|
+
range: { min, max }
|
|
1907
|
+
},
|
|
1908
|
+
unit,
|
|
1909
|
+
minus: true
|
|
1910
|
+
};
|
|
1911
|
+
}
|
|
1912
|
+
return { value: qty, unit };
|
|
1913
|
+
}
|
|
1914
|
+
function mergeKey(unit, mode) {
|
|
1915
|
+
if (mode === "family") return familyOf(unit);
|
|
1916
|
+
const step = mode === "ladder" ? ladderOf(unit) : null;
|
|
1917
|
+
return step ? `ladder:${step.ladder.id}` : `unit:${normalizeUnit(unit)}`;
|
|
1918
|
+
}
|
|
1919
|
+
function groupTerms(terms, mode) {
|
|
1920
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1921
|
+
for (const term of terms) {
|
|
1922
|
+
const key = mergeKey(term.unit, mode);
|
|
1923
|
+
const group = groups.get(key);
|
|
1924
|
+
if (group) group.push(term);
|
|
1925
|
+
else groups.set(key, [term]);
|
|
1926
|
+
}
|
|
1927
|
+
return [...groups.values()];
|
|
1928
|
+
}
|
|
1929
|
+
function isZero(term) {
|
|
1930
|
+
const bounds = termBounds(term.value);
|
|
1931
|
+
return !!bounds && bounds.min === 0 && bounds.max === 0;
|
|
1932
|
+
}
|
|
1933
|
+
function mergeTerms(terms, mode) {
|
|
1934
|
+
const merged = [];
|
|
1935
|
+
for (const group of groupTerms(addedFirst(terms), mode)) {
|
|
1936
|
+
const total = group.length > 1 ? termsTotal(group) : null;
|
|
1937
|
+
if (total) merged.push(totalAsTerm(total));
|
|
1938
|
+
else merged.push(...group);
|
|
1939
|
+
}
|
|
1940
|
+
const kept = merged.filter((term) => !isZero(term));
|
|
1941
|
+
return addedFirst(kept.length > 0 ? kept : merged.slice(0, 1));
|
|
1942
|
+
}
|
|
1943
|
+
function scaleTermValue(value, factor) {
|
|
1944
|
+
if (factor === 1) return value;
|
|
1945
|
+
const scale = (n) => {
|
|
1946
|
+
const scaled = n * factor;
|
|
1947
|
+
if (!Number.isFinite(scaled)) throw new QuantityOverflowError(factor);
|
|
1948
|
+
return round2(scaled);
|
|
1949
|
+
};
|
|
1950
|
+
if (typeof value === "number") return scale(value);
|
|
1951
|
+
if (!value || typeof value !== "object") return value;
|
|
1952
|
+
if (value.type === "single" && typeof value.value === "number") {
|
|
1953
|
+
return { ...value, value: scale(value.value), text: void 0 };
|
|
1954
|
+
}
|
|
1955
|
+
if (value.type === "range") {
|
|
1956
|
+
return {
|
|
1957
|
+
...value,
|
|
1958
|
+
value: typeof value.value === "number" ? scale(value.value) : value.value,
|
|
1959
|
+
range: value.range ? { min: scale(value.range.min), max: scale(value.range.max) } : value.range,
|
|
1960
|
+
text: void 0
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
if (value.type === "fraction" && typeof value.value === "number") {
|
|
1964
|
+
return { type: "single", value: scale(value.value) };
|
|
1965
|
+
}
|
|
1966
|
+
return value;
|
|
1967
|
+
}
|
|
1968
|
+
function scaleTerms(terms, factor) {
|
|
1969
|
+
return terms.map((term) => ({
|
|
1970
|
+
...term,
|
|
1971
|
+
value: scaleTermValue(term.value, factor)
|
|
1972
|
+
}));
|
|
1973
|
+
}
|
|
1974
|
+
function writeValue(value, unit, w) {
|
|
1975
|
+
if (typeof value === "number") return w.number(value, unit);
|
|
1976
|
+
if (value.text != null) return w.text(value.text);
|
|
1977
|
+
if (value.type === "range" && value.range) {
|
|
1978
|
+
return `${w.number(value.range.min, unit)}-${w.number(value.range.max, unit)}`;
|
|
1979
|
+
}
|
|
1980
|
+
const n = termValueNumber(value);
|
|
1981
|
+
return n === null ? String(value) : w.number(n, unit);
|
|
1982
|
+
}
|
|
1983
|
+
function writeTerm(term, w) {
|
|
1984
|
+
const text = writeValue(term.value, term.unit, w);
|
|
1985
|
+
if (!term.unit) return text;
|
|
1986
|
+
return term.unit === "%" ? `${text}%` : `${text} ${term.unit}`;
|
|
1987
|
+
}
|
|
1988
|
+
var RAW_WRITER = { number: (n) => String(n), text: (t) => t };
|
|
1989
|
+
function formatTerm(term) {
|
|
1990
|
+
return writeTerm(term, RAW_WRITER);
|
|
1991
|
+
}
|
|
1992
|
+
var MEASURED_FRACTIONS = {
|
|
1993
|
+
cup: [1 / 4, 1 / 3, 1 / 2, 2 / 3, 3 / 4],
|
|
1994
|
+
tbsp: [1 / 2],
|
|
1995
|
+
tsp: [1 / 8, 1 / 4, 1 / 2],
|
|
1996
|
+
"fl oz": [1 / 2],
|
|
1997
|
+
pt: [1 / 2],
|
|
1998
|
+
qt: [1 / 2],
|
|
1999
|
+
gal: [1 / 2],
|
|
2000
|
+
lb: [1 / 4, 1 / 2],
|
|
2001
|
+
oz: [1 / 4, 1 / 2, 3 / 4]
|
|
2002
|
+
};
|
|
2003
|
+
var RECOMPOSE_CHAINS = [
|
|
2004
|
+
["cup", "tbsp", "tsp"],
|
|
2005
|
+
["gal", "qt", "pt", "fl oz"],
|
|
2006
|
+
["lb", "oz"]
|
|
2007
|
+
];
|
|
2008
|
+
var TOLERANCE = 0.01;
|
|
2009
|
+
function measuredCandidates(value, unit) {
|
|
2010
|
+
const whole = Math.floor(value);
|
|
2011
|
+
const fractions = [0, ...MEASURED_FRACTIONS[unit] ?? [], 1];
|
|
2012
|
+
return fractions.map((f) => whole + f);
|
|
2013
|
+
}
|
|
2014
|
+
function measured(value, unit) {
|
|
2015
|
+
if (value <= 0) return void 0;
|
|
2016
|
+
return measuredCandidates(value, unit).find(
|
|
2017
|
+
(c) => c > 0 && Math.abs(value - c) < TOLERANCE
|
|
2018
|
+
);
|
|
2019
|
+
}
|
|
2020
|
+
function nearestMeasured(value, unit) {
|
|
2021
|
+
const smallest = Math.min(...MEASURED_FRACTIONS[unit] ?? [1]);
|
|
2022
|
+
const whole = Math.floor(value);
|
|
2023
|
+
const multiples = Array.from(
|
|
2024
|
+
{ length: Math.round(1 / smallest) + 1 },
|
|
2025
|
+
(_, k) => whole + k * smallest
|
|
2026
|
+
);
|
|
2027
|
+
let best = 0;
|
|
2028
|
+
for (const c of [...measuredCandidates(value, unit), ...multiples]) {
|
|
2029
|
+
if (Math.abs(value - c) < Math.abs(value - best) - 1e-9) best = c;
|
|
2030
|
+
}
|
|
2031
|
+
if (best > 0) return best;
|
|
2032
|
+
return smallest;
|
|
2033
|
+
}
|
|
2034
|
+
function decompose(total, chain, steps) {
|
|
2035
|
+
const out = [];
|
|
2036
|
+
let rest = total;
|
|
2037
|
+
for (let i = 0; i < chain.length; i++) {
|
|
2038
|
+
const unit = chain[i];
|
|
2039
|
+
const size = steps[unit];
|
|
2040
|
+
const value = rest / size;
|
|
2041
|
+
if (i === chain.length - 1 || out.length === 2) {
|
|
2042
|
+
if (value > TOLERANCE / 10 || out.length === 0) {
|
|
2043
|
+
out.push({ value: nearestMeasured(value, unit), unit });
|
|
2044
|
+
}
|
|
2045
|
+
break;
|
|
2046
|
+
}
|
|
2047
|
+
const exactly = measured(value, unit);
|
|
2048
|
+
if (exactly !== void 0) {
|
|
2049
|
+
out.push({ value: exactly, unit });
|
|
2050
|
+
break;
|
|
2051
|
+
}
|
|
2052
|
+
const whole = i === 0 ? Math.max(
|
|
2053
|
+
0,
|
|
2054
|
+
...measuredCandidates(value, unit).filter((c) => c <= value + 1e-9)
|
|
2055
|
+
) : Math.floor(value + 1e-9);
|
|
2056
|
+
if (whole > 0) {
|
|
2057
|
+
out.push({ value: whole, unit });
|
|
2058
|
+
rest -= whole * size;
|
|
2059
|
+
}
|
|
2060
|
+
if (rest <= 1e-9) break;
|
|
2061
|
+
}
|
|
2062
|
+
return out;
|
|
2063
|
+
}
|
|
2064
|
+
function recomposesExactly(parts, total, steps) {
|
|
2065
|
+
const last = parts[parts.length - 1];
|
|
2066
|
+
if (!last) return false;
|
|
2067
|
+
const sum = parts.reduce(
|
|
2068
|
+
(n, p) => n + p.value * steps[p.unit],
|
|
2069
|
+
0
|
|
2070
|
+
);
|
|
2071
|
+
return Math.abs(sum - total) < TOLERANCE * steps[last.unit];
|
|
2072
|
+
}
|
|
2073
|
+
function ladderBounds(terms, size) {
|
|
2074
|
+
let min = 0;
|
|
2075
|
+
let max = 0;
|
|
2076
|
+
for (const term of terms) {
|
|
2077
|
+
const bounds = termBounds(term.value);
|
|
2078
|
+
if (!bounds) return null;
|
|
2079
|
+
const s = size(term);
|
|
2080
|
+
if (term.minus) {
|
|
2081
|
+
min -= bounds.max * s;
|
|
2082
|
+
max -= bounds.min * s;
|
|
2083
|
+
} else {
|
|
2084
|
+
min += bounds.min * s;
|
|
2085
|
+
max += bounds.max * s;
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
return { min: exact(min), max: exact(max) };
|
|
2089
|
+
}
|
|
2090
|
+
function measuredAsTheyAre(terms, canonical, steps) {
|
|
2091
|
+
const units = terms.map(canonical);
|
|
2092
|
+
if (new Set(units).size !== units.length) return false;
|
|
2093
|
+
const sizes = terms.filter((t) => !t.minus).map((t) => steps[canonical(t)]);
|
|
2094
|
+
return terms.every((term) => {
|
|
2095
|
+
const bounds = termBounds(term.value);
|
|
2096
|
+
if (!bounds) return false;
|
|
2097
|
+
const unit = canonical(term);
|
|
2098
|
+
if (measured(bounds.min, unit) === void 0) return false;
|
|
2099
|
+
if (measured(bounds.max, unit) === void 0) return false;
|
|
2100
|
+
if (term.minus) return true;
|
|
2101
|
+
const size = steps[unit];
|
|
2102
|
+
const larger = sizes.filter((s) => s > size);
|
|
2103
|
+
return larger.length === 0 || bounds.max * size < Math.min(...larger);
|
|
2104
|
+
});
|
|
2105
|
+
}
|
|
2106
|
+
function computedGroup(group, w, options) {
|
|
2107
|
+
const { lang, buying } = options;
|
|
2108
|
+
const asWritten = () => addedFirst(group).map((t) => ({ text: writeTerm(t, w), minus: t.minus }));
|
|
2109
|
+
const first = group[0];
|
|
2110
|
+
const step = ladderOf(first.unit, lang);
|
|
2111
|
+
if (!step) {
|
|
2112
|
+
return mergeTerms(group, "unit").map((t) => ({
|
|
2113
|
+
text: writeTerm(t, w),
|
|
2114
|
+
minus: t.minus
|
|
2115
|
+
}));
|
|
2116
|
+
}
|
|
2117
|
+
const { steps } = step.ladder;
|
|
2118
|
+
const canonical = (t) => ladderOf(t.unit, lang).unit;
|
|
2119
|
+
const size = (t) => steps[canonical(t)];
|
|
2120
|
+
const total = ladderBounds(group, size);
|
|
2121
|
+
if (!total || total.min <= 0) return asWritten();
|
|
2122
|
+
let cap = first;
|
|
2123
|
+
for (const t of group) if (size(t) > size(cap)) cap = t;
|
|
2124
|
+
const capUnit = canonical(cap);
|
|
2125
|
+
const spell = (unit) => group.find((t) => canonical(t) === unit)?.unit ?? (group.some((t) => localSpelling(t.unit, lang)) ? unitSpelling(unit, lang) : unit);
|
|
2126
|
+
const write = (value, unit) => `${w.number(value, unit)} ${unit}`;
|
|
2127
|
+
const span = (min, max, unit) => min === max ? `${min} ${unit}` : `${min}-${max} ${unit}`;
|
|
2128
|
+
if (step.ladder.id === "metricMass" || step.ladder.id === "metricVolume") {
|
|
2129
|
+
const written = cap.unit;
|
|
2130
|
+
const capSize2 = steps[capUnit];
|
|
2131
|
+
const larger = buying ? toLargerMetricUnit(total.max / capSize2, written, lang).unit : written;
|
|
2132
|
+
const unit = larger === written ? written : larger;
|
|
2133
|
+
const unitSize = larger === written ? capSize2 : steps[larger];
|
|
2134
|
+
const num = (n) => larger === written ? w.number(n, unit) : formatNumber(round2(n), lang);
|
|
2135
|
+
const lo2 = num(total.min / unitSize);
|
|
2136
|
+
const hi2 = num(total.max / unitSize);
|
|
2137
|
+
return [{ text: span(lo2, hi2, unit) }];
|
|
2138
|
+
}
|
|
2139
|
+
const chain = RECOMPOSE_CHAINS.find((c) => c.includes(capUnit));
|
|
2140
|
+
if (!chain) return asWritten();
|
|
2141
|
+
const capSize = steps[capUnit];
|
|
2142
|
+
const climbed = buying ? chain.find((u) => {
|
|
2143
|
+
const size2 = steps[u];
|
|
2144
|
+
return size2 >= 4 * capSize && (total.min >= size2 || measured(total.min / size2, u) !== void 0);
|
|
2145
|
+
}) : void 0;
|
|
2146
|
+
const from = chain.slice(chain.indexOf(climbed ?? capUnit));
|
|
2147
|
+
const top = from[0];
|
|
2148
|
+
const topSize = steps[top];
|
|
2149
|
+
const lo = measured(total.min / topSize, top);
|
|
2150
|
+
const hi = measured(total.max / topSize, top);
|
|
2151
|
+
if (lo !== void 0 && hi !== void 0) {
|
|
2152
|
+
const unit = spell(top);
|
|
2153
|
+
return [{ text: span(w.number(lo, top), w.number(hi, top), unit) }];
|
|
2154
|
+
}
|
|
2155
|
+
if (!buying && measuredAsTheyAre(group, canonical, steps)) return asWritten();
|
|
2156
|
+
let a = decompose(total.min, from, steps);
|
|
2157
|
+
let b = decompose(total.max, from, steps);
|
|
2158
|
+
if (total.max !== total.min) {
|
|
2159
|
+
for (let cut = a.length - 1; cut >= 0; cut--) {
|
|
2160
|
+
const prefix = a.slice(0, cut);
|
|
2161
|
+
const unit = a[cut].unit;
|
|
2162
|
+
const size2 = steps[unit];
|
|
2163
|
+
const used = prefix.reduce(
|
|
2164
|
+
(n, p) => n + p.value * steps[p.unit],
|
|
2165
|
+
0
|
|
2166
|
+
);
|
|
2167
|
+
const fit = (value) => options.rounding === false ? measured(value, unit) : value > 0 ? nearestMeasured(value, unit) : void 0;
|
|
2168
|
+
const lo2 = fit((total.min - used) / size2);
|
|
2169
|
+
const hi2 = fit((total.max - used) / size2);
|
|
2170
|
+
const above = cut > 0 ? steps[prefix[cut - 1].unit] : Infinity;
|
|
2171
|
+
if (lo2 === void 0 || hi2 === void 0 || hi2 * size2 >= above) continue;
|
|
2172
|
+
a = [...prefix, { value: lo2, unit }];
|
|
2173
|
+
b = [...prefix, { value: hi2, unit }];
|
|
2174
|
+
break;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
if (options.rounding === false && !(recomposesExactly(a, total.min, steps) && recomposesExactly(b, total.max, steps))) {
|
|
2178
|
+
return asWritten();
|
|
2179
|
+
}
|
|
2180
|
+
const joinParts = (parts) => parts.map((p) => write(p.value, spell(p.unit))).join(options.plus ?? " + ");
|
|
2181
|
+
const same = (i) => a[i]?.unit === b[i]?.unit && a[i]?.value === b[i]?.value;
|
|
2182
|
+
if (a.length === b.length && a.every((_, i) => same(i))) {
|
|
2183
|
+
return a.map((p) => ({ text: write(p.value, spell(p.unit)) }));
|
|
2184
|
+
}
|
|
2185
|
+
const last = a.length - 1;
|
|
2186
|
+
if (a.length === b.length && a[last]?.unit === b[last]?.unit && a.slice(0, last).every((_, i) => same(i))) {
|
|
2187
|
+
const lastA = a[last];
|
|
2188
|
+
const lastB = b[last];
|
|
2189
|
+
return [
|
|
2190
|
+
...a.slice(0, last).map((p) => ({ text: write(p.value, spell(p.unit)) })),
|
|
2191
|
+
{
|
|
2192
|
+
text: span(
|
|
2193
|
+
w.number(lastA.value, lastA.unit),
|
|
2194
|
+
w.number(lastB.value, lastB.unit),
|
|
2195
|
+
spell(lastA.unit)
|
|
2196
|
+
)
|
|
2197
|
+
}
|
|
2198
|
+
];
|
|
2199
|
+
}
|
|
2200
|
+
return [{ text: `${joinParts(a)} \u2013 ${joinParts(b)}` }];
|
|
2201
|
+
}
|
|
2202
|
+
function computedNumber(lang) {
|
|
2203
|
+
return (value, unit) => {
|
|
2204
|
+
if (Math.abs(value - Math.round(value)) < TOLERANCE) {
|
|
2205
|
+
return String(Math.round(value));
|
|
2206
|
+
}
|
|
2207
|
+
return toKitchenFraction(value, unit, lang) ?? formatNumber(round2(value), lang);
|
|
2208
|
+
};
|
|
2209
|
+
}
|
|
2210
|
+
function formatTerms(terms, options = {}) {
|
|
2211
|
+
const plus = options.plus ?? " + ";
|
|
2212
|
+
const minus = options.minus ?? " \u2212 ";
|
|
2213
|
+
const computed = options.mode === "computed";
|
|
2214
|
+
const w = {
|
|
2215
|
+
number: options.number ?? (computed ? computedNumber(options.lang) : RAW_WRITER.number),
|
|
2216
|
+
text: options.text ?? RAW_WRITER.text
|
|
2217
|
+
};
|
|
2218
|
+
const source = terms[0]?.minus ? addedFirst(terms) : terms;
|
|
2219
|
+
const pieces = computed ? groupTerms(source, "ladder").flatMap(
|
|
2220
|
+
(group) => computedGroup(group, w, options)
|
|
2221
|
+
) : source.map((term) => ({ text: writeTerm(term, w), minus: term.minus }));
|
|
2222
|
+
return (pieces[0]?.minus ? addedFirst(pieces) : pieces).map((piece, i) => {
|
|
2223
|
+
if (i === 0)
|
|
2224
|
+
return piece.minus ? `${minus.trimStart()}${piece.text}` : piece.text;
|
|
2225
|
+
return `${piece.minus ? minus : plus}${piece.text}`;
|
|
2226
|
+
}).join("");
|
|
2227
|
+
}
|
|
1319
2228
|
export {
|
|
1320
2229
|
CATEGORY_KEYS,
|
|
2230
|
+
QuantityOverflowError,
|
|
1321
2231
|
TIME_BY_LANG,
|
|
1322
2232
|
TIME_DICTIONARIES,
|
|
1323
2233
|
TIME_GLOBAL,
|
|
@@ -1326,25 +2236,46 @@ export {
|
|
|
1326
2236
|
UNIT_CONVERSIONS,
|
|
1327
2237
|
UNIT_DICTIONARIES,
|
|
1328
2238
|
UNIT_GLOBAL,
|
|
2239
|
+
UNIT_LADDERS,
|
|
1329
2240
|
commonFraction,
|
|
1330
2241
|
compileDictionary,
|
|
2242
|
+
convertUnitInFamily,
|
|
1331
2243
|
decimalSeparator,
|
|
2244
|
+
familyOf,
|
|
1332
2245
|
fillTemplate,
|
|
1333
2246
|
formatNumber,
|
|
2247
|
+
formatTerm,
|
|
2248
|
+
formatTerms,
|
|
1334
2249
|
getAiLanguageInstruction,
|
|
1335
2250
|
getCategoryLabels,
|
|
1336
2251
|
getDefaultCategories,
|
|
1337
2252
|
getDictionary,
|
|
2253
|
+
groupTerms,
|
|
1338
2254
|
isCategoryKey,
|
|
1339
2255
|
isCoarseUnit,
|
|
1340
2256
|
isFractionUnit,
|
|
1341
2257
|
isMeasureUnit,
|
|
1342
2258
|
isMetricMassUnit,
|
|
2259
|
+
isTermValue,
|
|
2260
|
+
ladderOf,
|
|
1343
2261
|
localizeDecimals,
|
|
2262
|
+
looksLikeThousands,
|
|
2263
|
+
mergeKey,
|
|
2264
|
+
mergeTerms,
|
|
1344
2265
|
normalizeUnit,
|
|
2266
|
+
parseDuration,
|
|
1345
2267
|
resolveTimeUnit,
|
|
2268
|
+
round2,
|
|
2269
|
+
sameCountUnit,
|
|
2270
|
+
sameLadder,
|
|
2271
|
+
scaleTermValue,
|
|
2272
|
+
scaleTerms,
|
|
2273
|
+
termValueNumber,
|
|
2274
|
+
termsTotal,
|
|
1346
2275
|
toKitchenFraction,
|
|
1347
2276
|
toLargerMetricUnit,
|
|
2277
|
+
tokenizeDuration,
|
|
1348
2278
|
unitContainsQuantity,
|
|
1349
|
-
unitKey
|
|
2279
|
+
unitKey,
|
|
2280
|
+
unitSpelling
|
|
1350
2281
|
};
|