@gram-lang/i18n 1.5.0 → 1.5.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 +23 -1
- package/dist/index.js +28 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -147,6 +147,19 @@ declare function unitContainsQuantity(rawUnit: string | undefined | null, lang?:
|
|
|
147
147
|
* `formatNumber(1500, "fr")` is `"1500"`.
|
|
148
148
|
*/
|
|
149
149
|
declare function formatNumber(value: number, lang?: string, maxDecimals?: number): string;
|
|
150
|
+
/**
|
|
151
|
+
* How many decimals an amount keeps when it is shown: 3 significant digits,
|
|
152
|
+
* never fewer than its whole part (`152.37` → `152`, `15.24` → `15.2`,
|
|
153
|
+
* `1.15` → `1.15`), and 2 significant digits below 1 (`0.15`, `0.03`,
|
|
154
|
+
* `0.0047`), so a small amount is never shown as `0`. The precision follows
|
|
155
|
+
* the size of the amount, not its unit: rounded to a fixed decimal in the
|
|
156
|
+
* author's unit, `0.15 kg` read `0.2 kg` and `0.02 kg` read `0 kg` (#230).
|
|
157
|
+
*/
|
|
158
|
+
declare function amountDecimals(value: number): number;
|
|
159
|
+
/** An amount rounded as it is shown (see `amountDecimals`). */
|
|
160
|
+
declare function roundAmount(value: number): number;
|
|
161
|
+
/** An amount as it is shown, in the reader's language (see `amountDecimals`). */
|
|
162
|
+
declare function formatAmount(value: number, lang?: string): string;
|
|
150
163
|
/** The decimal separator of a language: `"."` in English, `","` in French. */
|
|
151
164
|
declare function decimalSeparator(lang?: string): string;
|
|
152
165
|
/**
|
|
@@ -851,6 +864,15 @@ declare class QuantityOverflowError extends RangeError {
|
|
|
851
864
|
* representation, which differs from IEEE round-half-to-even on exact ties.
|
|
852
865
|
*/
|
|
853
866
|
declare const round2: (value: number) => number;
|
|
867
|
+
/**
|
|
868
|
+
* Rounds a stored quantity or mass like `round2`, but never to fewer than 2
|
|
869
|
+
* significant digits: `1.234` → `1.23` and `0.333` → `0.33` as before, but
|
|
870
|
+
* `0.0047` stays `0.0047` instead of becoming `0`. Rounded to 2 decimals in
|
|
871
|
+
* the author's unit, a recipe divided by 10 lost its `0.02 kg` of salt, and
|
|
872
|
+
* a ratio of a pinch weighed `0 g` (#230). The views round what they show
|
|
873
|
+
* (`formatAmount`).
|
|
874
|
+
*/
|
|
875
|
+
declare const roundQuantity: (value: number) => number;
|
|
854
876
|
/** True for a value a term can hold (a number, or a single / fraction / range). */
|
|
855
877
|
declare function isTermValue(value: unknown): value is TermValue;
|
|
856
878
|
/**
|
|
@@ -995,4 +1017,4 @@ interface FormatTermsOptions {
|
|
|
995
1017
|
*/
|
|
996
1018
|
declare function formatTerms(terms: readonly Term[], options?: FormatTermsOptions): string;
|
|
997
1019
|
|
|
998
|
-
export { CATEGORY_KEYS, type CategoryKey, type CompiledDict, type DurationFailure, type DurationForm, type DurationPart, type DurationResult, type DurationSide, type DurationUnit, type FormatTermsOptions, type GramLocale, type LadderStep, type LanguageDict, type MergeMode, QuantityOverflowError, type QuantityValue, TIME_BY_LANG, TIME_DICTIONARIES, TIME_GLOBAL, TIME_TO_MINUTES, type Term, type TermValue, type TermsTotal, UNIT_BY_LANG, UNIT_CONVERSIONS, UNIT_DICTIONARIES, UNIT_GLOBAL, UNIT_LADDERS, type UnitLadder, type UnitMap, commonFraction, compileDictionary, convertUnitInFamily, decimalSeparator, familyOf, fillTemplate, formatNumber, formatTerm, formatTerms, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, groupTerms, isCategoryKey, isCoarseUnit, isFractionUnit, isMeasureUnit, isMetricMassUnit, isTermValue, ladderOf, localizeDecimals, looksLikeThousands, mergeKey, mergeTerms, normalizeUnit, parseDuration, resolveTimeUnit, round2, sameCountUnit, sameLadder, scaleTermValue, scaleTerms, termValueNumber, termsTotal, toKitchenFraction, toLargerMetricUnit, tokenizeDuration, unitContainsQuantity, unitKey, unitSpelling };
|
|
1020
|
+
export { CATEGORY_KEYS, type CategoryKey, type CompiledDict, type DurationFailure, type DurationForm, type DurationPart, type DurationResult, type DurationSide, type DurationUnit, type FormatTermsOptions, type GramLocale, type LadderStep, type LanguageDict, type MergeMode, QuantityOverflowError, type QuantityValue, TIME_BY_LANG, TIME_DICTIONARIES, TIME_GLOBAL, TIME_TO_MINUTES, type Term, type TermValue, type TermsTotal, UNIT_BY_LANG, UNIT_CONVERSIONS, UNIT_DICTIONARIES, UNIT_GLOBAL, UNIT_LADDERS, type UnitLadder, type UnitMap, amountDecimals, commonFraction, compileDictionary, convertUnitInFamily, decimalSeparator, familyOf, fillTemplate, formatAmount, formatNumber, formatTerm, formatTerms, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, groupTerms, isCategoryKey, isCoarseUnit, isFractionUnit, isMeasureUnit, isMetricMassUnit, isTermValue, ladderOf, localizeDecimals, looksLikeThousands, mergeKey, mergeTerms, normalizeUnit, parseDuration, resolveTimeUnit, round2, roundAmount, roundQuantity, sameCountUnit, sameLadder, scaleTermValue, scaleTerms, termValueNumber, termsTotal, toKitchenFraction, toLargerMetricUnit, tokenizeDuration, unitContainsQuantity, unitKey, unitSpelling };
|
package/dist/index.js
CHANGED
|
@@ -346,6 +346,19 @@ function formatter(lang, maxDecimals) {
|
|
|
346
346
|
function formatNumber(value, lang, maxDecimals = 2) {
|
|
347
347
|
return formatter(lang, maxDecimals).format(value);
|
|
348
348
|
}
|
|
349
|
+
function amountDecimals(value) {
|
|
350
|
+
const size = Math.abs(value);
|
|
351
|
+
if (size === 0 || !Number.isFinite(size)) return 0;
|
|
352
|
+
const magnitude = Math.floor(Math.log10(size));
|
|
353
|
+
if (magnitude >= 0) return Math.max(0, 2 - magnitude);
|
|
354
|
+
return Math.min(1 - magnitude, 6);
|
|
355
|
+
}
|
|
356
|
+
function roundAmount(value) {
|
|
357
|
+
return Number(value.toFixed(amountDecimals(value)));
|
|
358
|
+
}
|
|
359
|
+
function formatAmount(value, lang) {
|
|
360
|
+
return formatNumber(value, lang, amountDecimals(value));
|
|
361
|
+
}
|
|
349
362
|
function decimalSeparator(lang) {
|
|
350
363
|
return formatter(lang, 1).formatToParts(1.5).find((part) => part.type === "decimal")?.value ?? ".";
|
|
351
364
|
}
|
|
@@ -1794,6 +1807,12 @@ var QuantityOverflowError = class extends RangeError {
|
|
|
1794
1807
|
factor;
|
|
1795
1808
|
};
|
|
1796
1809
|
var round2 = (value) => parseFloat(value.toFixed(2));
|
|
1810
|
+
var roundQuantity = (value) => {
|
|
1811
|
+
const size = Math.abs(value);
|
|
1812
|
+
if (size >= 1 || size === 0 || !Number.isFinite(size)) return round2(value);
|
|
1813
|
+
const decimals = Math.min(1 - Math.floor(Math.log10(size)), 15);
|
|
1814
|
+
return parseFloat(value.toFixed(Math.max(2, decimals)));
|
|
1815
|
+
};
|
|
1797
1816
|
function isTermValue(value) {
|
|
1798
1817
|
if (typeof value === "number") return true;
|
|
1799
1818
|
if (!value || typeof value !== "object") return false;
|
|
@@ -1844,7 +1863,7 @@ function commonUnit(ordered) {
|
|
|
1844
1863
|
unit: family.base,
|
|
1845
1864
|
weight: (t) => family.map[normalizeUnit(t.unit)],
|
|
1846
1865
|
divisor: 1,
|
|
1847
|
-
round:
|
|
1866
|
+
round: roundQuantity
|
|
1848
1867
|
};
|
|
1849
1868
|
}
|
|
1850
1869
|
}
|
|
@@ -1945,7 +1964,7 @@ function scaleTermValue(value, factor) {
|
|
|
1945
1964
|
const scale = (n) => {
|
|
1946
1965
|
const scaled = n * factor;
|
|
1947
1966
|
if (!Number.isFinite(scaled)) throw new QuantityOverflowError(factor);
|
|
1948
|
-
return
|
|
1967
|
+
return roundQuantity(scaled);
|
|
1949
1968
|
};
|
|
1950
1969
|
if (typeof value === "number") return scale(value);
|
|
1951
1970
|
if (!value || typeof value !== "object") return value;
|
|
@@ -2131,7 +2150,7 @@ function computedGroup(group, w, options) {
|
|
|
2131
2150
|
const larger = buying ? toLargerMetricUnit(total.max / capSize2, written, lang).unit : written;
|
|
2132
2151
|
const unit = larger === written ? written : larger;
|
|
2133
2152
|
const unitSize = larger === written ? capSize2 : steps[larger];
|
|
2134
|
-
const num = (n) => larger === written ? w.number(n, unit) :
|
|
2153
|
+
const num = (n) => larger === written ? w.number(n, unit) : formatAmount(n, lang);
|
|
2135
2154
|
const lo2 = num(total.min / unitSize);
|
|
2136
2155
|
const hi2 = num(total.max / unitSize);
|
|
2137
2156
|
return [{ text: span(lo2, hi2, unit) }];
|
|
@@ -2201,10 +2220,10 @@ function computedGroup(group, w, options) {
|
|
|
2201
2220
|
}
|
|
2202
2221
|
function computedNumber(lang) {
|
|
2203
2222
|
return (value, unit) => {
|
|
2204
|
-
if (Math.abs(value - Math.round(value)) < TOLERANCE) {
|
|
2223
|
+
if (Math.abs(value - Math.round(value)) < TOLERANCE && (Math.round(value) !== 0 || value === 0)) {
|
|
2205
2224
|
return String(Math.round(value));
|
|
2206
2225
|
}
|
|
2207
|
-
return toKitchenFraction(value, unit, lang) ??
|
|
2226
|
+
return toKitchenFraction(value, unit, lang) ?? formatAmount(value, lang);
|
|
2208
2227
|
};
|
|
2209
2228
|
}
|
|
2210
2229
|
function formatTerms(terms, options = {}) {
|
|
@@ -2237,12 +2256,14 @@ export {
|
|
|
2237
2256
|
UNIT_DICTIONARIES,
|
|
2238
2257
|
UNIT_GLOBAL,
|
|
2239
2258
|
UNIT_LADDERS,
|
|
2259
|
+
amountDecimals,
|
|
2240
2260
|
commonFraction,
|
|
2241
2261
|
compileDictionary,
|
|
2242
2262
|
convertUnitInFamily,
|
|
2243
2263
|
decimalSeparator,
|
|
2244
2264
|
familyOf,
|
|
2245
2265
|
fillTemplate,
|
|
2266
|
+
formatAmount,
|
|
2246
2267
|
formatNumber,
|
|
2247
2268
|
formatTerm,
|
|
2248
2269
|
formatTerms,
|
|
@@ -2266,6 +2287,8 @@ export {
|
|
|
2266
2287
|
parseDuration,
|
|
2267
2288
|
resolveTimeUnit,
|
|
2268
2289
|
round2,
|
|
2290
|
+
roundAmount,
|
|
2291
|
+
roundQuantity,
|
|
2269
2292
|
sameCountUnit,
|
|
2270
2293
|
sameLadder,
|
|
2271
2294
|
scaleTermValue,
|