File size: 12,601 Bytes
82cdb59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fc133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82cdb59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// The formula function catalog, wave 52 (W52-T05, contract C3, instruction 5).
//
// This file owns ONE job: the local function data the suggestion dropdown, the ScreenTip
// (`formulaAssist.ts`) and the Formulas ribbon's offline fallback draw from. It is PURE data.
// Category names match the server catalogue so fallback never changes the picker's shape. No
// React, no DOM, nothing computed at runtime that a later ticket cannot re-derive from a formula's
// own text.
//
// ⛔ SIGNATURE SPACING IS THE OWNER'S OWN EXAMPLE, NOT MICROSOFT'S. Microsoft's SUM reference page
// prints `SUM(number1,[number2],...)` with no space after the comma. The owner's instruction 5
// spelled it "SUM(number1, [number 2], ...)" -- WITH a space after every comma. `ST-2/ST-3` in
// `AutoWiki/waves/wave52/proto/excel-behaviour-spec.md` says match the owner's spacing exactly.
// `buildSignature` below is the ONLY place that assembles a signature string, so every entry gets
// the same spacing by construction rather than by sixty hand-typed strings agreeing by luck.
//
// ⛔ NO EM DASH (U+2014) OR EN DASH (U+2013) IN ANY STRING HERE. `summary` reaches a screen the
// moment T06 renders the dropdown row (FA-15). Standing rule 2. `verify_formula_engine.py`'s new
// leg asserts this the same way the wave-50 catalogue leg already asserts it for the grid engine.

/** One argument in a function's signature. `optional` renders it in `[brackets]` (ST-3). */
export interface FunctionArg {
  name: string;
  optional?: boolean;
}

/**
 * `SUM(number1, [number2], ...)`.
 *
 * Bare = required. `[bracketed]` = optional (display only -- the brackets are never typed or
 * committed). A trailing `, ...` means the last optional argument repeats (ST-3), up to whatever
 * cap the function itself enforces; this catalog does not encode that cap, only the shape.
 */
function buildSignature(name: string, args: FunctionArg[], repeats = false): string {
  const parts = args.map((a) => (a.optional ? `[${a.name}]` : a.name));
  let body = parts.join(", ");
  if (repeats) body += body ? ", ..." : "...";
  return `${name}(${body})`;
}

export interface FunctionSpec {
  name: string;
  category: string;
  signature: string;
  summary: string;
  argNames: string[];
}

/** One row per catalog entry: name, its arg list, whether the last optional argument repeats. */
interface FunctionDef {
  name: string;
  category: string;
  args: FunctionArg[];
  repeats?: boolean;
  summary: string;
}

// ⚠ ORDERING HERE IS DELIBERATELY NOT ALPHABETICAL FOR EVERY PREFIX GROUP (the M group in
// particular). `suggest()` in formulaAssist.ts is the thing that must sort these into A-Z order
// (FA-8); a catalog that is already alphabetical would let a broken sort pass by accident.
const FUNCTION_DEFS: FunctionDef[] = [
  {
    name: "SUMPRODUCT", category: "Math",
    args: [{ name: "array1" }, { name: "array2", optional: true }], repeats: true,
    summary: "Adds the products of corresponding values in arrays.",
  },
  {
    name: "ISTEXT", category: "Information", args: [{ name: "value" }],
    summary: "Returns true when a value is text.",
  },
  {
    name: "WEEKNUM", category: "Date",
    args: [{ name: "date" }, { name: "return_type", optional: true }],
    summary: "Returns the week number with weeks beginning on Sunday or Monday.",
  },
  {
    name: "CONCATENATE", category: "Text",
    args: [{ name: "text1" }, { name: "text2", optional: true }], repeats: true,
    summary: "Joins text values together.",
  },
  {
    name: "YEAR", category: "Date", args: [{ name: "date" }],
    summary: "Returns the year of a date.",
  },
  {
    name: "MONTH", category: "Date", args: [{ name: "date" }],
    summary: "Returns the month number of a date.",
  },
  {
    name: "FILTER", category: "Lookup",
    args: [{ name: "array" }, { name: "include" }, { name: "if_empty", optional: true }],
    summary: "Returns the rows or columns that meet a condition.",
  },
  {
    name: "SORT", category: "Lookup",
    args: [{ name: "array" }, { name: "sort_index", optional: true },
      { name: "sort_order", optional: true }, { name: "by_col", optional: true }],
    summary: "Returns an array sorted by a row or column.",
  },
  {
    name: "OFFSET", category: "Lookup",
    args: [{ name: "reference" }, { name: "rows" }, { name: "cols" },
      { name: "height", optional: true }, { name: "width", optional: true }],
    summary: "Returns a reference shifted by a number of rows and columns.",
  },
  {
    name: "INDIRECT", category: "Lookup",
    args: [{ name: "ref_text" }, { name: "a1", optional: true }],
    summary: "Returns the reference named by a text value.",
  },
  {
    name: "RAND", category: "Math", args: [],
    summary: "Returns a random number between zero and one.",
  },
  {
    name: "SUM",
    category: "Math",
    args: [{ name: "number1" }, { name: "number2", optional: true }],
    repeats: true,
    summary: "Adds all the numbers in a range of cells.",
  },
  {
    name: "SUMIF",
    category: "Math",
    args: [
      { name: "range" },
      { name: "criteria" },
      { name: "sum_range", optional: true },
    ],
    summary: "Adds the cells specified by a given condition.",
  },
  {
    name: "MID",
    category: "Text",
    args: [{ name: "text" }, { name: "start_num" }, { name: "num_chars" }],
    summary: "Returns characters from the middle of a text string.",
  },
  {
    name: "MATCH",
    category: "Lookup",
    args: [
      { name: "lookup_value" },
      { name: "lookup_array" },
      { name: "match_type", optional: true },
    ],
    summary: "Returns the relative position of an item in a range.",
  },
  {
    name: "MIN",
    category: "Statistical",
    args: [{ name: "number1" }, { name: "number2", optional: true }],
    repeats: true,
    summary: "Returns the smallest number in a set of values.",
  },
  {
    name: "MAX",
    category: "Statistical",
    args: [{ name: "number1" }, { name: "number2", optional: true }],
    repeats: true,
    summary: "Returns the largest number in a set of values.",
  },
  {
    name: "AVERAGE",
    category: "Statistical",
    args: [{ name: "number1" }, { name: "number2", optional: true }],
    repeats: true,
    summary: "Returns the average of a set of numbers.",
  },
  {
    name: "AND",
    category: "Logical",
    args: [{ name: "logical1" }, { name: "logical2", optional: true }],
    repeats: true,
    summary: "Returns TRUE when every argument is true.",
  },
  {
    name: "OR",
    category: "Logical",
    args: [{ name: "logical1" }, { name: "logical2", optional: true }],
    repeats: true,
    summary: "Returns TRUE when any argument is true.",
  },
  {
    name: "NOT",
    category: "Logical",
    args: [{ name: "logical" }],
    summary: "Reverses the logic of its one argument.",
  },
  {
    name: "COUNT",
    category: "Statistical",
    args: [{ name: "value1" }, { name: "value2", optional: true }],
    repeats: true,
    summary: "Counts the numbers in a list of values.",
  },
  {
    name: "COUNTA",
    category: "Statistical",
    args: [{ name: "value1" }, { name: "value2", optional: true }],
    repeats: true,
    summary: "Counts the non blank values in a list.",
  },
  {
    name: "COUNTIF",
    category: "Statistical",
    args: [{ name: "range" }, { name: "criteria" }],
    summary: "Counts the cells that meet a given condition.",
  },
  {
    name: "IF",
    category: "Logical",
    args: [
      { name: "logical_test" },
      { name: "value_if_true" },
      { name: "value_if_false", optional: true },
    ],
    summary: "Returns one value if a condition is true and another if false.",
  },
  {
    name: "IFERROR",
    category: "Logical",
    args: [{ name: "value" }, { name: "value_if_error" }],
    summary: "Returns a fallback value when the first argument is an error.",
  },
  {
    name: "XLOOKUP",
    category: "Lookup",
    args: [
      { name: "lookup_value" },
      { name: "lookup_array" },
      { name: "return_array" },
      { name: "if_not_found", optional: true },
      { name: "match_mode", optional: true },
    ],
    summary: "Finds a value in one range and returns the matching value from another, with an answer of your own when nothing matches.",
  },
  {
    name: "VLOOKUP",
    category: "Lookup",
    args: [
      { name: "lookup_value" },
      { name: "table_array" },
      { name: "col_index_num" },
      { name: "range_lookup", optional: true },
    ],
    summary: "Looks up a value in the first column and returns a value in the same row.",
  },
  {
    name: "INDEX",
    category: "Lookup",
    args: [
      { name: "array" },
      { name: "row_num" },
      { name: "column_num", optional: true },
    ],
    summary: "Returns a value at a given position in a range.",
  },
  {
    name: "CHOOSE",
    category: "Lookup",
    args: [{ name: "index_num" }, { name: "value1" }, { name: "value2", optional: true }],
    repeats: true,
    summary: "Returns the value at the position you name in a list.",
  },
  {
    name: "CONCATENATE",
    category: "Text",
    args: [{ name: "text1" }, { name: "text2", optional: true }],
    repeats: true,
    summary: "Joins several text values into one text value.",
  },
  // W57-T07: offer only server-backed text functions, with hints built by buildSignature.
  {
    name: "TEXTJOIN",
    category: "Text",
    args: [
      { name: "delimiter" },
      { name: "ignore_empty" },
      { name: "text1" },
      { name: "text2", optional: true },
    ],
    repeats: true,
    summary: "Joins text values with a delimiter between them, optionally skipping the empty ones.",
  },
  {
    name: "SUBSTITUTE",
    category: "Text",
    args: [
      { name: "text" },
      { name: "old_text" },
      { name: "new_text" },
      { name: "instance_num", optional: true },
    ],
    summary: "Replaces old text with new text in a text value.",
  },
  {
    name: "ROUND",
    category: "Math",
    args: [{ name: "number" }, { name: "num_digits" }],
    summary: "Rounds a number to a given number of digits.",
  },
  {
    name: "ABS",
    category: "Math",
    args: [{ name: "number" }],
    summary: "Returns the absolute value of a number.",
  },
  {
    name: "TRIM",
    category: "Text",
    args: [{ name: "text" }],
    summary: "Removes extra spaces from a text value.",
  },
  {
    name: "LEN",
    category: "Text",
    args: [{ name: "text" }],
    summary: "Returns the number of characters in a text value.",
  },
  {
    name: "LEFT",
    category: "Text",
    args: [{ name: "text" }, { name: "num_chars", optional: true }],
    summary: "Returns the leftmost characters from a text value.",
  },
  {
    name: "RIGHT",
    category: "Text",
    args: [{ name: "text" }, { name: "num_chars", optional: true }],
    summary: "Returns the rightmost characters from a text value.",
  },
  // W57-T07: these date functions return ISO dates or counts on both sides of Refresh.
  {
    name: "EOMONTH",
    category: "Date",
    args: [{ name: "date" }, { name: "months" }],
    summary: "Returns the last day of the month a number of months away from a date.",
  },
  {
    name: "WORKDAY",
    category: "Date",
    args: [{ name: "start_date" }, { name: "days" }, { name: "holidays", optional: true }],
    summary: "Returns the date a number of working days away from a date, skipping weekends and any holidays you name.",
  },
  {
    name: "NETWORKDAYS",
    category: "Date",
    args: [{ name: "start_date" }, { name: "end_date" }, { name: "holidays", optional: true }],
    summary: "Counts the working days between two dates, skipping weekends and any holidays you name.",
  },
  {
    name: "TODAY",
    category: "Date",
    args: [],
    summary: "Returns the current date.",
  },
  {
    name: "NOW",
    category: "Date",
    args: [],
    summary: "Returns the current date and time.",
  },
];

export const FUNCTIONS: FunctionSpec[] = FUNCTION_DEFS.map((def) => ({
  name: def.name,
  category: def.category,
  signature: buildSignature(def.name, def.args, def.repeats),
  summary: def.summary,
  argNames: def.args.map((a) => a.name),
}));

/** True when `signature`'s last optional argument repeats (a trailing `, ...` or `...`). ST-3/ST-6. */
export function signatureRepeats(signature: string): boolean {
  return signature.trimEnd().endsWith("...)");
}

/** Look up one catalog entry by name, case insensitive. Returns undefined when unknown. */
export function findFunction(name: string): FunctionSpec | undefined {
  const upper = String(name || "").toUpperCase();
  return FUNCTIONS.find((f) => f.name === upper);
}