@rowsncolumns/utils 9.2.43 → 9.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/cjs/formula-locale/formula-locale-data.generated.d.ts +44 -0
- package/dist/cjs/formula-locale/formula-locale-data.generated.d.ts.map +1 -0
- package/dist/cjs/formula-locale/formula-locale-data.generated.js +5567 -0
- package/dist/cjs/formula-locale.d.ts +159 -0
- package/dist/cjs/formula-locale.d.ts.map +1 -0
- package/dist/cjs/formula-locale.js +804 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/selection.d.ts +3 -4
- package/dist/cjs/selection.d.ts.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/formula-locale/formula-locale-data.generated.d.ts +44 -0
- package/dist/esm/formula-locale/formula-locale-data.generated.d.ts.map +1 -0
- package/dist/esm/formula-locale/formula-locale-data.generated.js +5564 -0
- package/dist/esm/formula-locale.d.ts +159 -0
- package/dist/esm/formula-locale.d.ts.map +1 -0
- package/dist/esm/formula-locale.js +0 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/selection.d.ts +3 -4
- package/dist/esm/selection.d.ts.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { CalculatorFunction } from "@rowsncolumns/common-types";
|
|
2
|
+
import { type FormulaLocaleSyntaxData } from "./formula-locale/formula-locale-data.generated";
|
|
3
|
+
export type { FormulaLocaleDataEntry, FormulaLocaleSyntaxData, } from "./formula-locale/formula-locale-data.generated";
|
|
4
|
+
export { FORMULA_LOCALE_DATA, FORMULA_LOCALE_DATA_SOURCES, } from "./formula-locale/formula-locale-data.generated";
|
|
5
|
+
/**
|
|
6
|
+
* Locale-aware formula SYNTAX (rowsncolumns/spreadsheet#655), the way Excel does it: a workbook
|
|
7
|
+
* stores and evaluates every formula in one canonical dialect — English function names, `.` decimal
|
|
8
|
+
* mark, `,` argument separator, `{1,2;3,4}` array constants, `#VALUE!` error values — while the cell
|
|
9
|
+
* editor, formula bar, autocomplete, argument hints and error display speak the user's language
|
|
10
|
+
* (`=СУММ(1,5;A1)` in ru-RU, `=SUMME(1,5;A1)` in de-DE, `=SUMA(1,5;A1)` in es-ES).
|
|
11
|
+
*
|
|
12
|
+
* `localizeFormula` / `canonicalizeFormula` are exact inverses on well-formed canonical formulas: they
|
|
13
|
+
* rewrite ONLY the characters Excel localizes (number literals, separators, function names, TRUE/FALSE,
|
|
14
|
+
* error values) and copy everything else verbatim — strings, quoted sheet names, structured references
|
|
15
|
+
* (`Table1[[#Headers],[Qty]]` stays as typed), R1C1 text, operators, whitespace.
|
|
16
|
+
*
|
|
17
|
+
* Dictionaries come from the generated table (`formula-locale/formula-locale-data.generated.ts`,
|
|
18
|
+
* built from PhpSpreadsheet's Excel translations + Microsoft's localized function reference) and can be
|
|
19
|
+
* extended or overridden per application through {@link registerFormulaLocale} or an inline
|
|
20
|
+
* {@link FormulaLocaleDefinition}.
|
|
21
|
+
*/
|
|
22
|
+
/** The separators that vary per locale, plus Excel's union operator (always the argument separator). */
|
|
23
|
+
export interface FormulaSyntax extends FormulaLocaleSyntaxData {
|
|
24
|
+
/** Reference-union operator (`SUM((A1:A2,B1:B2))`) — Excel uses the argument separator for it. */
|
|
25
|
+
unionOperator: string;
|
|
26
|
+
}
|
|
27
|
+
/** A resolved formula locale: syntax + bidirectional function/error dictionaries. */
|
|
28
|
+
export interface FormulaLocale extends FormulaSyntax {
|
|
29
|
+
/** The tag this locale was resolved from (`"de"`, `"pt-BR"`, or a registered custom id). */
|
|
30
|
+
id: string;
|
|
31
|
+
/** Display name of the language, in the language. */
|
|
32
|
+
name: string;
|
|
33
|
+
/** Canonical (uppercase English) function name → localized name. Names without an entry keep
|
|
34
|
+
* their English spelling. */
|
|
35
|
+
functions: ReadonlyMap<string, string>;
|
|
36
|
+
/** Case-folded localized function name → canonical name. */
|
|
37
|
+
canonicalFunctions: ReadonlyMap<string, string>;
|
|
38
|
+
/** Canonical error value (`#VALUE!`) → localized error value (`#ЗНАЧ!`). */
|
|
39
|
+
errors: ReadonlyMap<string, string>;
|
|
40
|
+
/** Case-folded localized error value → canonical error value. */
|
|
41
|
+
canonicalErrors: ReadonlyMap<string, string>;
|
|
42
|
+
/** `true` when the locale IS the canonical dialect (every translation is the identity), so
|
|
43
|
+
* callers can skip the scanner altogether. */
|
|
44
|
+
isCanonical: boolean;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* An application-supplied locale: a new language, or overrides layered on a built-in one via
|
|
48
|
+
* `extends`. Every field is optional; omitted syntax fields inherit from the base (or from `en`), and
|
|
49
|
+
* a `null` dictionary entry removes the inherited translation so the name falls back to English.
|
|
50
|
+
*/
|
|
51
|
+
export interface FormulaLocaleDefinition {
|
|
52
|
+
/** Identifier reported as {@link FormulaLocale.id}; defaults to the tag it is registered under. */
|
|
53
|
+
id?: string;
|
|
54
|
+
name?: string;
|
|
55
|
+
/** Built-in or registered tag whose syntax and dictionaries this definition starts from. */
|
|
56
|
+
extends?: string;
|
|
57
|
+
decimalSeparator?: string;
|
|
58
|
+
argumentSeparator?: string;
|
|
59
|
+
arrayColumnSeparator?: string;
|
|
60
|
+
arrayRowSeparator?: string;
|
|
61
|
+
/** Canonical function name → localized name (`null` = use the English name). */
|
|
62
|
+
functions?: Readonly<Record<string, string | null>>;
|
|
63
|
+
/** Canonical error value → localized error value (`null` = use the English value). */
|
|
64
|
+
errors?: Readonly<Record<string, string | null>>;
|
|
65
|
+
}
|
|
66
|
+
/** What {@link getFormulaLocale} accepts: a BCP-47 tag, an already-resolved locale, or an inline
|
|
67
|
+
* definition (resolved once per object identity). */
|
|
68
|
+
export type FormulaLocaleInput = string | FormulaLocale | FormulaLocaleDefinition;
|
|
69
|
+
/** Excel error values, in their canonical (English) spelling. */
|
|
70
|
+
export declare const CANONICAL_ERROR_VALUES: readonly string[];
|
|
71
|
+
/** The canonical dialect every formula is stored and evaluated in. */
|
|
72
|
+
export declare const CANONICAL_FORMULA_LOCALE: FormulaLocale;
|
|
73
|
+
/**
|
|
74
|
+
* Resolve a locale for formula syntax. Accepts a BCP-47 tag (`"ru-RU"` → the `ru` table, `"de-AT"` →
|
|
75
|
+
* `de`, `"de-CH"` → the Swiss row; unknown languages → canonical English), an already-resolved
|
|
76
|
+
* {@link FormulaLocale} (returned as-is), or an inline {@link FormulaLocaleDefinition} (resolved once
|
|
77
|
+
* per object). Resolution is memoized.
|
|
78
|
+
*/
|
|
79
|
+
export declare const getFormulaLocale: (locale?: FormulaLocaleInput | null) => FormulaLocale;
|
|
80
|
+
/**
|
|
81
|
+
* Add a formula locale or override a built-in one for the whole application: `registerFormulaLocale
|
|
82
|
+
* ("de", { functions: { SUM: "SUMMEX" } })` layers onto the German table, `registerFormulaLocale("xx",
|
|
83
|
+
* { extends: "fr", argumentSeparator: "," })` derives a new language. Later registrations for the
|
|
84
|
+
* same tag replace earlier ones. Resolved locales are re-derived lazily.
|
|
85
|
+
*/
|
|
86
|
+
export declare const registerFormulaLocale: (tag: string, definition: FormulaLocaleDefinition) => void;
|
|
87
|
+
/** Remove an application registration (built-in tables are never removed). */
|
|
88
|
+
export declare const unregisterFormulaLocale: (tag: string) => void;
|
|
89
|
+
/** Every tag with a formula-syntax table: the built-in languages plus application registrations. */
|
|
90
|
+
export declare const getFormulaLocaleTags: () => string[];
|
|
91
|
+
/** Localized spelling of a canonical function name (`SUM` → `СУММ`); names without a translation
|
|
92
|
+
* are returned unchanged. */
|
|
93
|
+
export declare const localizeFunctionName: (name: string, locale?: FormulaLocaleInput | null) => string;
|
|
94
|
+
/** Canonical (English, uppercase) spelling of a localized function name (`СУММ` → `SUM`); names
|
|
95
|
+
* without a translation are returned unchanged, so English names are accepted in every locale. */
|
|
96
|
+
export declare const canonicalizeFunctionName: (name: string, locale?: FormulaLocaleInput | null) => string;
|
|
97
|
+
/** Localized spelling of a canonical error value (`#VALUE!` → `#ЗНАЧ!`); other text passes through. */
|
|
98
|
+
export declare const localizeErrorValue: <T>(value: T, locale?: FormulaLocaleInput | null) => T | string;
|
|
99
|
+
/** Canonical spelling of a localized error value (`#ЗНАЧ!` → `#VALUE!`); other text passes through. */
|
|
100
|
+
export declare const canonicalizeErrorValue: <T>(value: T, locale?: FormulaLocaleInput | null) => T | string;
|
|
101
|
+
export interface TranslateFormulaOptions {
|
|
102
|
+
/**
|
|
103
|
+
* `preserve` (default) keeps the typed casing of names that have no translation in either
|
|
104
|
+
* direction; `upper` normalizes every known function name to uppercase, the way Excel does on
|
|
105
|
+
* commit (used to canonicalize test corpora).
|
|
106
|
+
*/
|
|
107
|
+
functionNameCase?: "preserve" | "upper";
|
|
108
|
+
/** Build the offset maps (see {@link TranslatedFormula}); off by default. */
|
|
109
|
+
trackOffsets?: boolean;
|
|
110
|
+
}
|
|
111
|
+
export interface TranslatedFormula {
|
|
112
|
+
text: string;
|
|
113
|
+
/**
|
|
114
|
+
* `toOutput[i]` is the output offset of source offset `i` (`0 ≤ i ≤ source.length`). Inside a
|
|
115
|
+
* rewritten segment (a translated name, a swapped separator) every interior offset maps to the
|
|
116
|
+
* segment's start; segment boundaries map exactly. Present when `trackOffsets` was requested.
|
|
117
|
+
*/
|
|
118
|
+
toOutput?: Int32Array;
|
|
119
|
+
/** Inverse of {@link toOutput}: `toSource[j]` for `0 ≤ j ≤ text.length`. */
|
|
120
|
+
toSource?: Int32Array;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Rewrite formula text from one locale's syntax to another's. This is the single scanner behind
|
|
124
|
+
* {@link localizeFormula} (canonical → locale) and {@link canonicalizeFormula} (locale → canonical);
|
|
125
|
+
* see the module comment for what is and is not rewritten.
|
|
126
|
+
*/
|
|
127
|
+
export declare const translateFormula: (text: string, from: FormulaLocaleInput, to: FormulaLocaleInput, options?: TranslateFormulaOptions) => TranslatedFormula;
|
|
128
|
+
/**
|
|
129
|
+
* Canonical (stored) formula text → the locale's display syntax: `=SUM(1.5,A1)` → `=СУММ(1,5;A1)`
|
|
130
|
+
* (ru), `=SUMME(1,5;A1)` (de). Identity for the canonical locale.
|
|
131
|
+
*/
|
|
132
|
+
export declare const localizeFormula: (formula: string, locale?: FormulaLocaleInput | null, options?: TranslateFormulaOptions) => string;
|
|
133
|
+
/**
|
|
134
|
+
* Text typed in the locale's syntax → canonical formula text for storage and evaluation:
|
|
135
|
+
* `=СУММ(1,5;A1)` → `=SUM(1.5,A1)`. Exact inverse of {@link localizeFormula} on canonical input;
|
|
136
|
+
* lenient on mixed input (an English name or `,` argument separator typed in a `;` locale is
|
|
137
|
+
* accepted as long as it is unambiguous).
|
|
138
|
+
*/
|
|
139
|
+
export declare const canonicalizeFormula: (formula: string, locale?: FormulaLocaleInput | null, options?: TranslateFormulaOptions) => string;
|
|
140
|
+
/**
|
|
141
|
+
* What the editor / formula bar SHOWS for a stored cell entry: a formula in the locale's syntax, a
|
|
142
|
+
* typed error value in the locale's spelling, any other text unchanged (`1,5` typed in de-DE is a
|
|
143
|
+
* number literal the datatype parser already reads with the locale's separators).
|
|
144
|
+
*/
|
|
145
|
+
export declare const localizeCellInput: (text: string, locale?: FormulaLocaleInput | null) => string;
|
|
146
|
+
/** Inverse of {@link localizeCellInput}: what the editor COMMITS — canonical formula text / error value. */
|
|
147
|
+
export declare const canonicalizeCellInput: (text: string, locale?: FormulaLocaleInput | null) => string;
|
|
148
|
+
/**
|
|
149
|
+
* Localize a function's documented syntax (`SUM(value1, [value2, ...])` → `СУММ(value1; [value2;
|
|
150
|
+
* ...])`): the leading name through the dictionary, every `,` outside double quotes through the
|
|
151
|
+
* argument separator. The square brackets here mark OPTIONAL arguments (not structured references),
|
|
152
|
+
* so — unlike {@link localizeFormula} — the separators inside them are localized too.
|
|
153
|
+
*/
|
|
154
|
+
export declare const localizeFunctionSyntax: (syntax: string, locale?: FormulaLocaleInput | null) => string;
|
|
155
|
+
/** A {@link CalculatorFunction} with its name, syntax, example and usage lines localized. */
|
|
156
|
+
export declare const localizeFunctionDescription: (fn: CalculatorFunction, locale?: FormulaLocaleInput | null) => CalculatorFunction;
|
|
157
|
+
/** {@link localizeFunctionDescription} over a catalog; the same array instance for the canonical locale. */
|
|
158
|
+
export declare const localizeFunctionDescriptions: <T extends CalculatorFunction[] | null | undefined>(functions: T, locale?: FormulaLocaleInput | null) => T;
|
|
159
|
+
//# sourceMappingURL=formula-locale.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formula-locale.d.ts","sourceRoot":"","sources":["../../formula-locale.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAGrE,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,gDAAgD,CAAC;AAExD,YAAY,EACV,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,gDAAgD,CAAC;AACxD,OAAO,EACL,mBAAmB,EACnB,2BAA2B,GAC5B,MAAM,gDAAgD,CAAC;AAExD;;;;;;;;;;;;;;;;GAgBG;AAEH,wGAAwG;AACxG,MAAM,WAAW,aAAc,SAAQ,uBAAuB;IAC5D,kGAAkG;IAClG,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,qFAAqF;AACrF,MAAM,WAAW,aAAc,SAAQ,aAAa;IAClD,4FAA4F;IAC5F,EAAE,EAAE,MAAM,CAAC;IACX,qDAAqD;IACrD,IAAI,EAAE,MAAM,CAAC;IACb;iCAC6B;IAC7B,SAAS,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC,4DAA4D;IAC5D,kBAAkB,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChD,4EAA4E;IAC5E,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CA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