@lofcz/tinysheet-formula-parser 1.0.2 → 1.0.4

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +125 -4
  2. package/dist/index.js +10059 -444
  3. package/package.json +25 -29
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ declare const ERROR_NULL: "NULL";
7
7
  declare const ERROR_NUM: "NUM";
8
8
  declare const ERROR_REF: "REF";
9
9
  declare const ERROR_VALUE: "VALUE";
10
+ declare const ERROR_SPILL: "SPILL";
11
+ declare const ERROR_CALC: "CALC";
10
12
  declare const SUPPORTED_FORMULAS: string[];
11
13
  declare function error(type: string): string | null;
12
14
  type CellCoord = {
@@ -30,17 +32,136 @@ type ParseResult = {
30
32
  error: string | null;
31
33
  result: unknown;
32
34
  };
35
+ /**
36
+ * Options passed to `Parser#parse` and forwarded to the cell/range events.
37
+ * `row`/`column` (0-based) of the formula cell enable `@` implicit
38
+ * intersection over multi-cell references.
39
+ */
40
+ type ParseOptions = {
41
+ sheetId?: string;
42
+ row?: number;
43
+ column?: number;
44
+ [key: string]: unknown;
45
+ };
46
+ /**
47
+ * A reference written in a formula: 0-based inclusive indexes, `-1` marks a
48
+ * whole-column (`startRow`/`endRow`) or whole-row (`startColumn`/`endColumn`)
49
+ * span. Passed to custom functions and `callFunction` listeners as `refs`,
50
+ * and returned by `Parser#getReferences`.
51
+ */
52
+ type ReferenceInfo = {
53
+ sheetName: string | null;
54
+ startRow: number;
55
+ startColumn: number;
56
+ endRow: number;
57
+ endColumn: number;
58
+ };
59
+ /**
60
+ * A LAMBDA value: call it with the arguments; formula errors are thrown as
61
+ * `Error` values. Missing trailing arguments are "omitted" (ISOMITTED).
62
+ */
63
+ type LambdaValue = ((...args: unknown[]) => unknown) & {
64
+ readonly params: string[];
65
+ };
66
+ declare function isLambda(value: unknown): value is LambdaValue;
67
+ /** A reference descriptor made by `createReference`. */
68
+ type ReferenceValue = Readonly<ReferenceInfo>;
69
+ /**
70
+ * Make a reference a host function (`callFunction` listener, `setFunction`)
71
+ * or variable can return instead of values: the evaluator then uses it as a
72
+ * reference (range operand `MYREF():C5`, `ROWS(MYREF())`, INDEX/CELL/SUM
73
+ * arguments with `refs`) and reads it through callCellValue/callRangeValue
74
+ * where a value is needed. `endRow`/`endColumn` default to the start.
75
+ */
76
+ declare function createReference(info: {
77
+ sheetName?: string | null;
78
+ startRow: number;
79
+ startColumn: number;
80
+ endRow?: number;
81
+ endColumn?: number;
82
+ }): ReferenceValue;
83
+ declare function isReference(value: unknown): value is ReferenceValue;
84
+ /**
85
+ * A picture value (IMAGE(), a cell holding a picture). `sizing`: 0 fit
86
+ * keeping the aspect ratio, 1 fill, 2 original size, 3 custom `h` x `w`
87
+ * pixels. Its text form (`String(value)`) is the alt text.
88
+ */
89
+ type ImageValue = {
90
+ readonly type: "image";
91
+ src: string;
92
+ alt: string;
93
+ sizing: 0 | 1 | 2 | 3;
94
+ h?: number;
95
+ w?: number;
96
+ };
97
+ declare function isImageValue(value: unknown): value is ImageValue;
98
+ declare function createImageValue(props: {
99
+ src: string;
100
+ alt?: string;
101
+ sizing?: number;
102
+ h?: number;
103
+ w?: number;
104
+ }): ImageValue;
105
+ declare function imageValueText(value: ImageValue): string;
106
+ /** Only absolute http(s) URLs and data:image URLs may be loaded. */
107
+ declare function isAllowedImageSource(src: unknown): boolean;
108
+ /**
109
+ * A function registered with `Parser#setFunction`.
110
+ *
111
+ * `arrayParams` opts into Excel's array lifting: `true` means every
112
+ * parameter accepts arrays (never lifted), an index list names the
113
+ * parameters that accept arrays; any other parameter receiving an array
114
+ * makes the evaluator call the function once per element and return an
115
+ * array of the results. Without it a custom function whose name is not an
116
+ * Excel function receives arrays unchanged; one overriding an Excel
117
+ * function (e.g. TEXT) follows that function's traits.
118
+ */
119
+ type CustomFunction = ((params: unknown[], refs: Array<ReferenceInfo | null>) => unknown) & {
120
+ arrayParams?: boolean | number[] | ((index: number) => boolean);
121
+ /**
122
+ * Parameters that take a reference: a reference argument there is passed
123
+ * unread, as a `createReference` descriptor (`isReference(params[i])`),
124
+ * so no cell is read and errors in the referenced cells do not abort the
125
+ * call. Other arguments arrive as values. `true` means every parameter.
126
+ */
127
+ referenceParams?: boolean | number[] | ((index: number) => boolean);
128
+ /**
129
+ * The function may return `createReference` values: calls to it are then
130
+ * reference-capable (range operands, `ROWS(...)`, `INDEX(...)`, ...).
131
+ */
132
+ returnsReference?: boolean;
133
+ };
33
134
  declare class Parser {
34
135
  constructor();
35
- parse(expression: string, options?: Record<string, unknown>): ParseResult;
136
+ parse(expression: string, options?: ParseOptions): ParseResult;
137
+ /** Parse to the cached AST without evaluating; throws on syntax errors. */
138
+ getAst(expression: string): unknown;
139
+ /** Cell/range references of a formula, in source order. */
140
+ getReferences(expression: string): ReferenceInfo[];
36
141
  setVariable(name: string, value: unknown): this;
37
142
  getVariable(name: string): unknown;
38
- setFunction(name: string, fn: (...args: unknown[]) => unknown): this;
39
- getFunction(name: string): ((...args: unknown[]) => unknown) | undefined;
143
+ /**
144
+ * Register a function (it takes precedence over built-ins and special
145
+ * forms of the same name). `refs[i]` describes argument `i` when it was
146
+ * written as (or evaluated to) a reference: `A1:A3`, `INDEX(...)`, one
147
+ * area of a union `(A1,B2)` spread over several params, ...
148
+ */
149
+ setFunction(name: string, fn: CustomFunction): this;
150
+ /** Registered function (names are matched case-insensitively). */
151
+ getFunction(name: string): CustomFunction | undefined;
40
152
  on(event: string, listener: (...args: unknown[]) => void): this;
41
153
  once(event: string, listener: (...args: unknown[]) => void): this;
42
154
  off(event: string, listener?: (...args: unknown[]) => void): this;
43
155
  emit(event: string, ...args: unknown[]): this;
44
156
  }
157
+ /** Old underscore-style names (e.g. RANK_EQ) mapped to their Excel names. */
158
+ declare const LEGACY_FUNCTION_NAMES: Readonly<Record<string, string>>;
159
+ /**
160
+ * Excel database criteria (D-functions, Advanced Filter): a predicate for
161
+ * one criteria cell value, or null when the condition is empty. Text
162
+ * without an operator matches values beginning with it, "=text" matches
163
+ * exactly, wildcards and comparison operators work like COUNTIF.
164
+ */
165
+ declare function databaseCriterion(value: unknown): ((candidate: unknown) => boolean) | null;
45
166
  //#endregion
46
- export { CellCoord, ERROR, ERROR_DIV_ZERO, ERROR_NAME, ERROR_NOT_AVAILABLE, ERROR_NULL, ERROR_NUM, ERROR_REF, ERROR_VALUE, ParseResult, Parser, Parser as default, SUPPORTED_FORMULAS, columnIndexToLabel, columnLabelToIndex, error, extractLabel, rowIndexToLabel, rowLabelToIndex, toLabel };
167
+ export { CellCoord, CustomFunction, ERROR, ERROR_CALC, ERROR_DIV_ZERO, ERROR_NAME, ERROR_NOT_AVAILABLE, ERROR_NULL, ERROR_NUM, ERROR_REF, ERROR_SPILL, ERROR_VALUE, ImageValue, LEGACY_FUNCTION_NAMES, LambdaValue, ParseOptions, ParseResult, Parser, Parser as default, ReferenceInfo, ReferenceValue, SUPPORTED_FORMULAS, columnIndexToLabel, columnLabelToIndex, createImageValue, createReference, databaseCriterion, error, extractLabel, imageValueText, isAllowedImageSource, isImageValue, isLambda, isReference, rowIndexToLabel, rowLabelToIndex, toLabel };