@jsb188/mday 1.1.10 → 1.1.12

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 (210) hide show
  1. package/package.json +140 -54
  2. package/src/constants/agentMemory.ts +123 -0
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +10 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +236 -0
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +21 -21
  10. package/src/constants/dataTableFieldTypes.generated.ts +59 -0
  11. package/src/constants/docAccess.ts +33 -0
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +125 -7
  14. package/src/constants/fieldDoc.ts +104 -0
  15. package/src/constants/graphqlReactivity.ts +196 -0
  16. package/src/constants/integration.ts +8 -9
  17. package/src/constants/log.ts +14 -408
  18. package/src/constants/logDisplay/README.md +88 -0
  19. package/src/constants/logDisplay/defaults.ts +39 -0
  20. package/src/constants/logDisplay/index.ts +27 -0
  21. package/src/constants/logDisplay/operations/arable.ts +16 -0
  22. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  23. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  24. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  25. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  26. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  27. package/src/constants/logTaxonomy.generated.ts +2207 -0
  28. package/src/constants/noteDoc.ts +77 -0
  29. package/src/constants/organization.ts +18 -21
  30. package/src/constants/pageRoute.ts +23 -0
  31. package/src/constants/qrCode.ts +30 -0
  32. package/src/constants/sheet.ts +126 -14
  33. package/src/constants/sms.ts +18 -0
  34. package/src/constants/square.ts +6 -6
  35. package/src/constants/storage.ts +80 -7
  36. package/src/constants/usage.ts +82 -0
  37. package/src/constants/workflowSteps.ts +60 -0
  38. package/src/schemas/editLogEntry.ts +836 -780
  39. package/src/schemas/inboundContact.ts +60 -55
  40. package/src/schemas/logsQuery.ts +55 -49
  41. package/src/types/account.d.ts +4 -1
  42. package/src/types/agentMemory.d.ts +98 -0
  43. package/src/types/agentNote.d.ts +443 -0
  44. package/src/types/billing.d.ts +537 -0
  45. package/src/types/campaign.d.ts +1533 -0
  46. package/src/types/contact.d.ts +15 -0
  47. package/src/types/csvExport.d.ts +213 -0
  48. package/src/types/dataAnalysis.d.ts +44 -0
  49. package/src/types/dataTable.d.ts +115 -403
  50. package/src/types/definitionElement.d.ts +35 -0
  51. package/src/types/docAccess.d.ts +96 -0
  52. package/src/types/dt.d.ts +257 -0
  53. package/src/types/email.d.ts +184 -24
  54. package/src/types/emailReplyReport.d.ts +76 -0
  55. package/src/types/fieldDoc.d.ts +702 -0
  56. package/src/types/fieldDocContract.d.ts +90 -0
  57. package/src/types/inboundContact.d.ts +10 -1
  58. package/src/types/log.d.ts +254 -295
  59. package/src/types/logDisplay.types.ts +85 -0
  60. package/src/types/organization.d.ts +91 -43
  61. package/src/types/pageRoute.d.ts +48 -0
  62. package/src/types/qrCode.d.ts +96 -0
  63. package/src/types/richText.d.ts +81 -0
  64. package/src/types/sheet.d.ts +678 -129
  65. package/src/types/sms.d.ts +38 -0
  66. package/src/types/storage.d.ts +449 -129
  67. package/src/types/usage.d.ts +106 -0
  68. package/src/types/workflow.d.ts +315 -54
  69. package/src/utils/agentMemory.ts +15 -0
  70. package/src/utils/agentNote.ts +850 -0
  71. package/src/utils/aiChat.ts +43 -0
  72. package/src/utils/aiChatWork.ts +64 -0
  73. package/src/utils/billing.ts +37 -0
  74. package/src/utils/campaign.ts +1174 -0
  75. package/src/utils/campaignAI.ts +72 -0
  76. package/src/utils/campaignAccess.ts +239 -0
  77. package/src/utils/campaignEmail.ts +601 -0
  78. package/src/utils/campaignEmailCompiler.ts +372 -0
  79. package/src/utils/campaignEmailImages.ts +60 -0
  80. package/src/utils/campaignEmailStyles.ts +544 -0
  81. package/src/utils/campaignLifecycle.ts +104 -0
  82. package/src/utils/campaignPreview.ts +285 -0
  83. package/src/utils/contact.ts +50 -0
  84. package/src/utils/csvExport.ts +19 -0
  85. package/src/utils/dataTable.ts +95 -618
  86. package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
  87. package/src/utils/dataTableRelativeDate.ts +447 -0
  88. package/src/utils/definitionElement.ts +84 -0
  89. package/src/utils/docAccess.ts +383 -0
  90. package/src/utils/documentName.ts +53 -0
  91. package/src/utils/email.ts +317 -51
  92. package/src/utils/emailReplyReport.test.ts +187 -0
  93. package/src/utils/emailReplyReport.ts +143 -0
  94. package/src/utils/fieldDoc.ts +157 -0
  95. package/src/utils/fieldDocAnswers.ts +128 -0
  96. package/src/utils/fieldDocDefaults.ts +36 -0
  97. package/src/utils/fieldDocProgram.ts +36 -0
  98. package/src/utils/fieldDocSummary.ts +30 -0
  99. package/src/utils/log.ts +276 -192
  100. package/src/utils/logDisplay.helpers.ts +177 -0
  101. package/src/utils/logDisplay.ts +87 -0
  102. package/src/utils/logRegistryValue.ts +90 -0
  103. package/src/utils/logStoreRead.ts +262 -0
  104. package/src/utils/logTaxonomy.helpers.ts +17 -0
  105. package/src/utils/logTaxonomy.ts +76 -0
  106. package/src/utils/logTaxonomyDisplay.ts +395 -0
  107. package/src/utils/noteDoc.ts +609 -0
  108. package/src/utils/organization.ts +337 -84
  109. package/src/utils/pageRoute.ts +71 -0
  110. package/src/utils/qrCode.helpers.ts +189 -0
  111. package/src/utils/qrCode.test.ts +519 -0
  112. package/src/utils/qrCode.ts +273 -0
  113. package/src/utils/richTextHTML.ts +385 -0
  114. package/src/utils/sheet.ts +199 -107
  115. package/src/utils/sheetAxisMove.ts +616 -0
  116. package/src/utils/sheetBootstrap.ts +1402 -0
  117. package/src/utils/sheetCellLiteral.ts +101 -0
  118. package/src/utils/sheetCommand.ts +358 -0
  119. package/src/utils/sheetDesign.ts +705 -0
  120. package/src/utils/sheetExport.ts +186 -0
  121. package/src/utils/sheetFeatureReference.ts +183 -0
  122. package/src/utils/sheetFormulaArgs.ts +168 -0
  123. package/src/utils/sheetFormulaCompile.ts +475 -0
  124. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  125. package/src/utils/sheetFormulaCriteria.ts +656 -0
  126. package/src/utils/sheetFormulaDate.ts +444 -0
  127. package/src/utils/sheetFormulaEvaluation.ts +899 -0
  128. package/src/utils/sheetFormulaEvaluator.ts +997 -0
  129. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  130. package/src/utils/sheetFormulaLogical.ts +116 -0
  131. package/src/utils/sheetFormulaLookup.ts +488 -0
  132. package/src/utils/sheetFormulaMath.ts +48 -0
  133. package/src/utils/sheetFormulaParser.ts +729 -0
  134. package/src/utils/sheetFormulaRange.ts +330 -0
  135. package/src/utils/sheetFormulaReference.ts +703 -0
  136. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  137. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  138. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  139. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  140. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  141. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  142. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  143. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  144. package/src/utils/sheetFormulaText.ts +430 -0
  145. package/src/utils/sheetJournal.ts +200 -0
  146. package/src/utils/sheetNumberFormat.ts +1012 -0
  147. package/src/utils/sheetPreview.ts +68 -0
  148. package/src/utils/sheetSequence.ts +118 -0
  149. package/src/utils/sheetSnapshot.ts +739 -0
  150. package/src/utils/sheetStructureEdit.ts +914 -0
  151. package/src/utils/sheetStructureReferences.ts +389 -0
  152. package/src/utils/square.ts +22 -12
  153. package/src/utils/storage.ts +198 -0
  154. package/src/utils/table-route.ts +127 -11
  155. package/src/utils/time.ts +12 -0
  156. package/src/utils/workflow.ts +334 -40
  157. package/src/utils/workflowLogFingerprint.ts +302 -0
  158. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  159. package/src/utils/workflowProgress.ts +280 -0
  160. package/src/utils/workflowTriggers.ts +61 -0
  161. package/tests/agentNote.test.ts +395 -0
  162. package/tests/campaignAI.test.ts +119 -0
  163. package/tests/campaignAccess.test.ts +239 -0
  164. package/tests/campaignEmail.test.ts +441 -0
  165. package/tests/campaignLifecycle.test.ts +134 -0
  166. package/tests/campaignPublication.test.ts +309 -0
  167. package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
  168. package/tests/dataTableRelativeDate.test.ts +412 -0
  169. package/tests/docAccess.test.ts +525 -0
  170. package/tests/editLogEntry.test.ts +112 -0
  171. package/tests/email.test.ts +39 -0
  172. package/tests/fieldDocProgram.test.ts +114 -0
  173. package/tests/logDisplay.fixtures.ts +67 -0
  174. package/tests/logDisplay.test.ts +227 -0
  175. package/tests/noteDoc.test.ts +514 -0
  176. package/tests/richTextHTML.test.ts +185 -0
  177. package/tests/sheetAxisMove.test.ts +352 -0
  178. package/tests/sheetBootstrap.test.ts +1967 -0
  179. package/tests/sheetFeatureReference.test.ts +22 -0
  180. package/tests/sheetFormulaCriteria.test.ts +269 -0
  181. package/tests/sheetFormulaEvaluator.test.ts +468 -0
  182. package/tests/sheetFormulaFunctions.test.ts +538 -0
  183. package/tests/sheetFormulaLanguage.test.ts +361 -0
  184. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  185. package/tests/sheetFormulaParity.test.ts +188 -0
  186. package/tests/sheetFormulaRange.test.ts +167 -0
  187. package/tests/sheetFormulaReference.test.ts +78 -0
  188. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  189. package/tests/sheetFormulaTextDate.test.ts +512 -0
  190. package/tests/sheetSequence.test.ts +22 -0
  191. package/tests/sheetSnapshot.test.ts +134 -0
  192. package/tests/sheetStructureEdit.test.ts +537 -0
  193. package/tests/sheetStructureReferences.test.ts +589 -0
  194. package/tests/workflowLifecycleStatus.test.ts +60 -0
  195. package/tests/workflowLogFingerprint.test.ts +203 -0
  196. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  197. package/tests/workflowProgress.test.ts +180 -0
  198. package/tests/workflowTriggers.test.ts +29 -0
  199. package/tsconfig.json +10 -10
  200. package/vitest.config.ts +8 -0
  201. package/src/constants/log-activityTexts.ts +0 -235
  202. package/src/constants/product.ts +0 -42
  203. package/src/constants/report.ts +0 -44
  204. package/src/schemas/organization.ts +0 -65
  205. package/src/schemas/productsQuery.ts +0 -53
  206. package/src/schemas/reportsQuery.ts +0 -20
  207. package/src/types/product.d.ts +0 -84
  208. package/src/types/report.d.ts +0 -290
  209. package/src/utils/product-calEvent.ts +0 -455
  210. package/src/utils/report.ts +0 -158
@@ -0,0 +1,403 @@
1
+ import {
2
+ evaluateSheetFormulaCountBlankFunction,
3
+ evaluateSheetFormulaCountIfFunction,
4
+ evaluateSheetFormulaMultiConditionAggregate,
5
+ evaluateSheetFormulaSingleConditionAggregate,
6
+ evaluateSheetFormulaSumProductFunction,
7
+ } from './sheetFormulaConditionalAggregate.ts';
8
+ import {
9
+ evaluateSheetFormulaDateDifferenceFunction,
10
+ evaluateSheetFormulaDaysFunction,
11
+ evaluateSheetFormulaMonthShiftFunction,
12
+ evaluateSheetFormulaNetworkDaysFunction,
13
+ evaluateSheetFormulaWeekNumberFunction,
14
+ evaluateSheetFormulaWeekdayFunction,
15
+ evaluateSheetFormulaWorkdayFunction,
16
+ } from './sheetFormulaDate.ts';
17
+ import {
18
+ evaluateSheetFormulaAggregateFunction,
19
+ evaluateSheetFormulaDateFunction,
20
+ evaluateSheetFormulaLookupFunction,
21
+ evaluateSheetFormulaMathFunction,
22
+ getSheetFormulaPureErrorResult,
23
+ type SheetFormulaBuiltinFunctionName,
24
+ type SheetFormulaEagerFunctionContext,
25
+ type SheetFormulaEagerFunctionHandler,
26
+ type SheetFormulaPureEvaluationResult,
27
+ } from './sheetFormulaEvaluation.ts';
28
+ import { evaluateSheetFormulaLogicalCombinator, evaluateSheetFormulaNotFunction } from './sheetFormulaLogical.ts';
29
+ import {
30
+ evaluateSheetFormulaIndexFunction,
31
+ evaluateSheetFormulaMatchFunction,
32
+ evaluateSheetFormulaTableLookupFunction,
33
+ evaluateSheetFormulaUniqueFunction,
34
+ } from './sheetFormulaLookup.ts';
35
+ import { evaluateSheetFormulaTruncFunction, evaluateSheetFormulaUnaryMathFunction } from './sheetFormulaMath.ts';
36
+ import {
37
+ evaluateSheetFormulaConcatFunction,
38
+ evaluateSheetFormulaFindFunction,
39
+ evaluateSheetFormulaLeftRightFunction,
40
+ evaluateSheetFormulaMidFunction,
41
+ evaluateSheetFormulaReplaceFunction,
42
+ evaluateSheetFormulaSubstituteFunction,
43
+ evaluateSheetFormulaTextFunction,
44
+ evaluateSheetFormulaTextJoinFunction,
45
+ evaluateSheetFormulaTextTransformFunction,
46
+ evaluateSheetFormulaValueFunction,
47
+ } from './sheetFormulaText.ts';
48
+
49
+ /**
50
+ * Types
51
+ */
52
+
53
+ /* Accepted argument counts of one eager builtin. `parity` requires an even
54
+ * (range/criterion pairs) or odd (value range plus pairs) count. */
55
+ type SheetFormulaFunctionArity = {
56
+ max: number;
57
+ min: number;
58
+ parity?: 'EVEN' | 'ODD';
59
+ text: string;
60
+ };
61
+
62
+ /*
63
+ * Return the result for a builtin name no eager handler implements.
64
+ */
65
+ function getSheetFormulaUnknownFunctionResult() {
66
+ return getSheetFormulaPureErrorResult('UNKNOWN_FUNCTION', 'Formula function is not supported.');
67
+ }
68
+
69
+ /*
70
+ * Return an eager handler that delegates to one topic evaluator keyed by
71
+ * function name (the topic evaluators return null for names they do not own).
72
+ */
73
+ function getSheetFormulaTopicHandler(
74
+ name: SheetFormulaBuiltinFunctionName,
75
+ evaluate: (name: string, values: unknown[], context: SheetFormulaEagerFunctionContext) => SheetFormulaPureEvaluationResult | null,
76
+ ): SheetFormulaEagerFunctionHandler {
77
+ return (values, context) => evaluate(name, values, context) || getSheetFormulaUnknownFunctionResult();
78
+ }
79
+
80
+ /*
81
+ * Return an eager handler that validates the argument count first, reporting
82
+ * INVALID_FORMULA "<NAME> requires ..." like the lazy builtins do.
83
+ */
84
+ function withSheetFormulaFunctionArity(
85
+ name: SheetFormulaBuiltinFunctionName,
86
+ arity: SheetFormulaFunctionArity,
87
+ handler: SheetFormulaEagerFunctionHandler,
88
+ ): SheetFormulaEagerFunctionHandler {
89
+ return (values, context) => {
90
+ const count = values.length;
91
+ const parityMismatch = (arity.parity === 'EVEN' && count % 2 !== 0) || (arity.parity === 'ODD' && count % 2 !== 1);
92
+ if (count < arity.min || count > arity.max || parityMismatch) {
93
+ return getSheetFormulaPureErrorResult('INVALID_FORMULA', `${name} requires ${arity.text}.`);
94
+ }
95
+
96
+ return handler(values, context);
97
+ };
98
+ }
99
+
100
+ /*
101
+ * Evaluate an aggregate builtin with the handler context.
102
+ */
103
+ function evaluateSheetFormulaAggregateHandler(name: string, values: unknown[], context: SheetFormulaEagerFunctionContext) {
104
+ return evaluateSheetFormulaAggregateFunction(name, values, context.timeZone);
105
+ }
106
+
107
+ /*
108
+ * Evaluate a date builtin with the handler context.
109
+ */
110
+ function evaluateSheetFormulaDateHandler(name: string, values: unknown[], context: SheetFormulaEagerFunctionContext) {
111
+ return evaluateSheetFormulaDateFunction(name, values, context.timeZone, context.now);
112
+ }
113
+
114
+ /*
115
+ * Evaluate a math builtin (context-free).
116
+ */
117
+ function evaluateSheetFormulaMathHandler(name: string, values: unknown[]) {
118
+ return evaluateSheetFormulaMathFunction(name, values);
119
+ }
120
+
121
+ /*
122
+ * Evaluate a lookup builtin (context-free).
123
+ */
124
+ function evaluateSheetFormulaLookupHandler(name: string, values: unknown[]) {
125
+ return evaluateSheetFormulaLookupFunction(name, values);
126
+ }
127
+
128
+ const FILE_CONSTANTS = {
129
+ logicalArity: {
130
+ max: Number.POSITIVE_INFINITY,
131
+ min: 1,
132
+ text: 'at least one TRUE/FALSE value',
133
+ },
134
+ singleConditionArity: {
135
+ max: 3,
136
+ min: 2,
137
+ text: 'a range, a criterion, and an optional value range',
138
+ },
139
+ multiConditionArity: {
140
+ max: Number.POSITIVE_INFINITY,
141
+ min: 3,
142
+ parity: 'ODD',
143
+ text: 'a value range followed by criteria range and criterion pairs',
144
+ },
145
+ tableLookupArity: {
146
+ max: 4,
147
+ min: 3,
148
+ text: 'a search key, a range, an index, and an optional is_sorted flag',
149
+ },
150
+ concatArity: {
151
+ max: Number.POSITIVE_INFINITY,
152
+ min: 1,
153
+ text: 'at least one value',
154
+ },
155
+ oneValueArity: { max: 1, min: 1, text: 'one value' },
156
+ textCountArity: {
157
+ max: 2,
158
+ min: 1,
159
+ text: 'a text and an optional number of characters',
160
+ },
161
+ findArity: {
162
+ max: 3,
163
+ min: 2,
164
+ text: 'a search text, a text to search, and an optional starting position',
165
+ },
166
+ monthShiftArity: { max: 2, min: 2, text: 'a start date and a month count' },
167
+ weekArity: { max: 2, min: 1, text: 'a date and an optional type' },
168
+ workdayCountArity: {
169
+ max: 3,
170
+ min: 2,
171
+ text: 'two dates or a date and a day count, and optional holidays',
172
+ },
173
+ } satisfies Record<string, SheetFormulaFunctionArity>;
174
+
175
+ /* Eager builtin dispatch: one handler per non-lazy builtin name. Every builtin
176
+ * is either lazy (isSheetFormulaConditionalFunctionName) or listed here; a
177
+ * completeness test pins that invariant. */
178
+ export const SHEET_FORMULA_EAGER_FUNCTION_HANDLERS: Partial<
179
+ Record<SheetFormulaBuiltinFunctionName, SheetFormulaEagerFunctionHandler>
180
+ > = {
181
+ ABS: getSheetFormulaTopicHandler('ABS', evaluateSheetFormulaMathHandler),
182
+ AND: withSheetFormulaFunctionArity('AND', FILE_CONSTANTS.logicalArity, (values) =>
183
+ evaluateSheetFormulaLogicalCombinator('AND', values),
184
+ ),
185
+ AVERAGE: getSheetFormulaTopicHandler('AVERAGE', evaluateSheetFormulaAggregateHandler),
186
+ AVERAGEIF: withSheetFormulaFunctionArity('AVERAGEIF', FILE_CONSTANTS.singleConditionArity, (values, context) =>
187
+ evaluateSheetFormulaSingleConditionAggregate('AVERAGEIF', values, context.timeZone),
188
+ ),
189
+ AVERAGEIFS: withSheetFormulaFunctionArity('AVERAGEIFS', FILE_CONSTANTS.multiConditionArity, (values, context) =>
190
+ evaluateSheetFormulaMultiConditionAggregate('AVERAGEIFS', values, context.timeZone),
191
+ ),
192
+ CEILING: getSheetFormulaTopicHandler('CEILING', evaluateSheetFormulaMathHandler),
193
+ CONCAT: withSheetFormulaFunctionArity('CONCAT', FILE_CONSTANTS.concatArity, (values) =>
194
+ evaluateSheetFormulaConcatFunction('CONCAT', values),
195
+ ),
196
+ CONCATENATE: withSheetFormulaFunctionArity('CONCATENATE', FILE_CONSTANTS.concatArity, (values) =>
197
+ evaluateSheetFormulaConcatFunction('CONCATENATE', values),
198
+ ),
199
+ COUNT: getSheetFormulaTopicHandler('COUNT', evaluateSheetFormulaAggregateHandler),
200
+ COUNTA: getSheetFormulaTopicHandler('COUNTA', evaluateSheetFormulaAggregateHandler),
201
+ COUNTBLANK: withSheetFormulaFunctionArity(
202
+ 'COUNTBLANK',
203
+ { max: 1, min: 1, text: 'one range' },
204
+ evaluateSheetFormulaCountBlankFunction,
205
+ ),
206
+ COUNTIF: withSheetFormulaFunctionArity('COUNTIF', { max: 2, min: 2, text: 'a range and a criterion' }, (values, context) =>
207
+ evaluateSheetFormulaCountIfFunction('COUNTIF', values, context.timeZone),
208
+ ),
209
+ COUNTIFS: withSheetFormulaFunctionArity(
210
+ 'COUNTIFS',
211
+ {
212
+ max: Number.POSITIVE_INFINITY,
213
+ min: 2,
214
+ parity: 'EVEN',
215
+ text: 'criteria range and criterion pairs',
216
+ },
217
+ (values, context) => evaluateSheetFormulaCountIfFunction('COUNTIFS', values, context.timeZone),
218
+ ),
219
+ DATE: getSheetFormulaTopicHandler('DATE', evaluateSheetFormulaDateHandler),
220
+ DATEDIF: withSheetFormulaFunctionArity(
221
+ 'DATEDIF',
222
+ { max: 3, min: 3, text: 'a start date, an end date, and a unit' },
223
+ (values, context) => evaluateSheetFormulaDateDifferenceFunction(values, context.timeZone),
224
+ ),
225
+ DAY: getSheetFormulaTopicHandler('DAY', evaluateSheetFormulaDateHandler),
226
+ DAYS: withSheetFormulaFunctionArity('DAYS', { max: 2, min: 2, text: 'an end date and a start date' }, (values, context) =>
227
+ evaluateSheetFormulaDaysFunction(values, context.timeZone),
228
+ ),
229
+ EDATE: withSheetFormulaFunctionArity('EDATE', FILE_CONSTANTS.monthShiftArity, (values, context) =>
230
+ evaluateSheetFormulaMonthShiftFunction('EDATE', values, context.timeZone),
231
+ ),
232
+ EOMONTH: withSheetFormulaFunctionArity('EOMONTH', FILE_CONSTANTS.monthShiftArity, (values, context) =>
233
+ evaluateSheetFormulaMonthShiftFunction('EOMONTH', values, context.timeZone),
234
+ ),
235
+ FIND: withSheetFormulaFunctionArity('FIND', FILE_CONSTANTS.findArity, (values) => evaluateSheetFormulaFindFunction('FIND', values)),
236
+ FLOOR: getSheetFormulaTopicHandler('FLOOR', evaluateSheetFormulaMathHandler),
237
+ HLOOKUP: withSheetFormulaFunctionArity('HLOOKUP', FILE_CONSTANTS.tableLookupArity, (values) =>
238
+ evaluateSheetFormulaTableLookupFunction('HLOOKUP', values),
239
+ ),
240
+ INDEX: withSheetFormulaFunctionArity(
241
+ 'INDEX',
242
+ { max: 3, min: 2, text: 'a range, a row, and an optional column' },
243
+ evaluateSheetFormulaIndexFunction,
244
+ ),
245
+ INT: withSheetFormulaFunctionArity('INT', FILE_CONSTANTS.oneValueArity, (values) =>
246
+ evaluateSheetFormulaUnaryMathFunction('INT', values),
247
+ ),
248
+ LEFT: withSheetFormulaFunctionArity('LEFT', FILE_CONSTANTS.textCountArity, (values) =>
249
+ evaluateSheetFormulaLeftRightFunction('LEFT', values),
250
+ ),
251
+ LEN: withSheetFormulaFunctionArity('LEN', FILE_CONSTANTS.oneValueArity, (values) =>
252
+ evaluateSheetFormulaTextTransformFunction('LEN', values),
253
+ ),
254
+ LOWER: withSheetFormulaFunctionArity('LOWER', FILE_CONSTANTS.oneValueArity, (values) =>
255
+ evaluateSheetFormulaTextTransformFunction('LOWER', values),
256
+ ),
257
+ MATCH: withSheetFormulaFunctionArity(
258
+ 'MATCH',
259
+ {
260
+ max: 3,
261
+ min: 2,
262
+ text: 'a search key, a range, and an optional match type',
263
+ },
264
+ evaluateSheetFormulaMatchFunction,
265
+ ),
266
+ MAX: getSheetFormulaTopicHandler('MAX', evaluateSheetFormulaAggregateHandler),
267
+ MID: withSheetFormulaFunctionArity(
268
+ 'MID',
269
+ { max: 3, min: 3, text: 'a text, a starting position, and a length' },
270
+ evaluateSheetFormulaMidFunction,
271
+ ),
272
+ MIN: getSheetFormulaTopicHandler('MIN', evaluateSheetFormulaAggregateHandler),
273
+ MOD: getSheetFormulaTopicHandler('MOD', evaluateSheetFormulaMathHandler),
274
+ MONTH: getSheetFormulaTopicHandler('MONTH', evaluateSheetFormulaDateHandler),
275
+ NETWORKDAYS: withSheetFormulaFunctionArity('NETWORKDAYS', FILE_CONSTANTS.workdayCountArity, (values, context) =>
276
+ evaluateSheetFormulaNetworkDaysFunction(values, context.timeZone),
277
+ ),
278
+ NOT: withSheetFormulaFunctionArity('NOT', { max: 1, min: 1, text: 'one TRUE/FALSE value' }, evaluateSheetFormulaNotFunction),
279
+ NOW: getSheetFormulaTopicHandler('NOW', evaluateSheetFormulaDateHandler),
280
+ OR: withSheetFormulaFunctionArity('OR', FILE_CONSTANTS.logicalArity, (values) =>
281
+ evaluateSheetFormulaLogicalCombinator('OR', values),
282
+ ),
283
+ POWER: getSheetFormulaTopicHandler('POWER', evaluateSheetFormulaMathHandler),
284
+ PROPER: withSheetFormulaFunctionArity('PROPER', FILE_CONSTANTS.oneValueArity, (values) =>
285
+ evaluateSheetFormulaTextTransformFunction('PROPER', values),
286
+ ),
287
+ REGEXMATCH: getSheetFormulaTopicHandler('REGEXMATCH', evaluateSheetFormulaLookupHandler),
288
+ REPLACE: withSheetFormulaFunctionArity(
289
+ 'REPLACE',
290
+ {
291
+ max: 4,
292
+ min: 4,
293
+ text: 'a text, a position, a length, and a new text',
294
+ },
295
+ evaluateSheetFormulaReplaceFunction,
296
+ ),
297
+ RIGHT: withSheetFormulaFunctionArity('RIGHT', FILE_CONSTANTS.textCountArity, (values) =>
298
+ evaluateSheetFormulaLeftRightFunction('RIGHT', values),
299
+ ),
300
+ ROUND: getSheetFormulaTopicHandler('ROUND', evaluateSheetFormulaMathHandler),
301
+ ROUNDDOWN: getSheetFormulaTopicHandler('ROUNDDOWN', evaluateSheetFormulaMathHandler),
302
+ ROUNDUP: getSheetFormulaTopicHandler('ROUNDUP', evaluateSheetFormulaMathHandler),
303
+ SEARCH: withSheetFormulaFunctionArity('SEARCH', FILE_CONSTANTS.findArity, (values) =>
304
+ evaluateSheetFormulaFindFunction('SEARCH', values),
305
+ ),
306
+ SIGN: withSheetFormulaFunctionArity('SIGN', FILE_CONSTANTS.oneValueArity, (values) =>
307
+ evaluateSheetFormulaUnaryMathFunction('SIGN', values),
308
+ ),
309
+ SQRT: withSheetFormulaFunctionArity('SQRT', FILE_CONSTANTS.oneValueArity, (values) =>
310
+ evaluateSheetFormulaUnaryMathFunction('SQRT', values),
311
+ ),
312
+ SUBSTITUTE: withSheetFormulaFunctionArity(
313
+ 'SUBSTITUTE',
314
+ {
315
+ max: 4,
316
+ min: 3,
317
+ text: 'a text, a search text, a replacement, and an optional occurrence',
318
+ },
319
+ evaluateSheetFormulaSubstituteFunction,
320
+ ),
321
+ SUM: getSheetFormulaTopicHandler('SUM', evaluateSheetFormulaAggregateHandler),
322
+ SUMIF: withSheetFormulaFunctionArity('SUMIF', FILE_CONSTANTS.singleConditionArity, (values, context) =>
323
+ evaluateSheetFormulaSingleConditionAggregate('SUMIF', values, context.timeZone),
324
+ ),
325
+ SUMIFS: withSheetFormulaFunctionArity('SUMIFS', FILE_CONSTANTS.multiConditionArity, (values, context) =>
326
+ evaluateSheetFormulaMultiConditionAggregate('SUMIFS', values, context.timeZone),
327
+ ),
328
+ SUMPRODUCT: withSheetFormulaFunctionArity(
329
+ 'SUMPRODUCT',
330
+ {
331
+ max: Number.POSITIVE_INFINITY,
332
+ min: 1,
333
+ text: 'at least one range',
334
+ },
335
+ evaluateSheetFormulaSumProductFunction,
336
+ ),
337
+ TEXT: withSheetFormulaFunctionArity('TEXT', { max: 2, min: 2, text: 'a value and a format pattern' }, (values, context) =>
338
+ evaluateSheetFormulaTextFunction(values, context.timeZone),
339
+ ),
340
+ TEXTJOIN: withSheetFormulaFunctionArity(
341
+ 'TEXTJOIN',
342
+ {
343
+ max: Number.POSITIVE_INFINITY,
344
+ min: 3,
345
+ text: 'a delimiter, an ignore_empty flag, and at least one value',
346
+ },
347
+ evaluateSheetFormulaTextJoinFunction,
348
+ ),
349
+ TODAY: getSheetFormulaTopicHandler('TODAY', evaluateSheetFormulaDateHandler),
350
+ TRIM: withSheetFormulaFunctionArity('TRIM', FILE_CONSTANTS.oneValueArity, (values) =>
351
+ evaluateSheetFormulaTextTransformFunction('TRIM', values),
352
+ ),
353
+ TRUNC: withSheetFormulaFunctionArity(
354
+ 'TRUNC',
355
+ { max: 2, min: 1, text: 'a number and optional decimal places' },
356
+ evaluateSheetFormulaTruncFunction,
357
+ ),
358
+ UNIQUE: withSheetFormulaFunctionArity(
359
+ 'UNIQUE',
360
+ {
361
+ max: 3,
362
+ min: 1,
363
+ text: 'a range and optional by_column and exactly_once flags',
364
+ },
365
+ evaluateSheetFormulaUniqueFunction,
366
+ ),
367
+ UPPER: withSheetFormulaFunctionArity('UPPER', FILE_CONSTANTS.oneValueArity, (values) =>
368
+ evaluateSheetFormulaTextTransformFunction('UPPER', values),
369
+ ),
370
+ VALUE: withSheetFormulaFunctionArity('VALUE', FILE_CONSTANTS.oneValueArity, evaluateSheetFormulaValueFunction),
371
+ VLOOKUP: withSheetFormulaFunctionArity('VLOOKUP', FILE_CONSTANTS.tableLookupArity, (values) =>
372
+ evaluateSheetFormulaTableLookupFunction('VLOOKUP', values),
373
+ ),
374
+ WEEKDAY: withSheetFormulaFunctionArity('WEEKDAY', FILE_CONSTANTS.weekArity, (values, context) =>
375
+ evaluateSheetFormulaWeekdayFunction(values, context.timeZone),
376
+ ),
377
+ WEEKNUM: withSheetFormulaFunctionArity('WEEKNUM', FILE_CONSTANTS.weekArity, (values, context) =>
378
+ evaluateSheetFormulaWeekNumberFunction(values, context.timeZone),
379
+ ),
380
+ WORKDAY: withSheetFormulaFunctionArity('WORKDAY', FILE_CONSTANTS.workdayCountArity, (values, context) =>
381
+ evaluateSheetFormulaWorkdayFunction(values, context.timeZone),
382
+ ),
383
+ XOR: withSheetFormulaFunctionArity('XOR', FILE_CONSTANTS.logicalArity, (values) =>
384
+ evaluateSheetFormulaLogicalCombinator('XOR', values),
385
+ ),
386
+ YEAR: getSheetFormulaTopicHandler('YEAR', evaluateSheetFormulaDateHandler),
387
+ };
388
+
389
+ /*
390
+ * Evaluate one eager builtin formula function from already-evaluated argument values.
391
+ */
392
+ export function evaluateSheetFormulaEagerBuiltinFunction(
393
+ name: string,
394
+ values: unknown[],
395
+ timeZone: string,
396
+ now: Date,
397
+ ): SheetFormulaPureEvaluationResult {
398
+ const handler = Object.hasOwn(SHEET_FORMULA_EAGER_FUNCTION_HANDLERS, name)
399
+ ? SHEET_FORMULA_EAGER_FUNCTION_HANDLERS[name as SheetFormulaBuiltinFunctionName]
400
+ : null;
401
+
402
+ return handler ? handler(values, { now, timeZone }) : getSheetFormulaUnknownFunctionResult();
403
+ }
@@ -0,0 +1,116 @@
1
+ import {
2
+ getSheetFormulaLogicalBooleanValue,
3
+ getSheetFormulaPureErrorResult,
4
+ isSheetFormulaBlankValue,
5
+ isSheetFormulaRangeLikeValue,
6
+ type SheetFormulaPureEvaluationResult,
7
+ } from './sheetFormulaEvaluation.ts';
8
+
9
+ /*
10
+ * Return the logical values of AND/OR/XOR arguments: ranges contribute their
11
+ * booleans and numbers (text and blanks inside ranges are skipped), blank
12
+ * scalars are skipped, and any other scalar must read as TRUE or FALSE.
13
+ */
14
+ function getSheetFormulaLogicalArgumentValues(name: string, values: unknown[]): boolean[] | SheetFormulaPureEvaluationResult {
15
+ const logicalValues: boolean[] = [];
16
+ for (const value of values) {
17
+ if (isSheetFormulaRangeLikeValue(value)) {
18
+ for (const rangeValue of value.values) {
19
+ if (typeof rangeValue === 'boolean') {
20
+ logicalValues.push(rangeValue);
21
+ } else if (typeof rangeValue === 'number') {
22
+ logicalValues.push(rangeValue !== 0);
23
+ }
24
+ }
25
+ continue;
26
+ }
27
+
28
+ if (isSheetFormulaBlankValue(value)) {
29
+ continue;
30
+ }
31
+
32
+ const logicalValue = getSheetFormulaLogicalBooleanValue(value);
33
+ if (logicalValue === null) {
34
+ return getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} requires TRUE/FALSE values, not text.`);
35
+ }
36
+
37
+ logicalValues.push(logicalValue);
38
+ }
39
+
40
+ return logicalValues.length
41
+ ? logicalValues
42
+ : getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} found no TRUE/FALSE values in its arguments.`);
43
+ }
44
+
45
+ /*
46
+ * Evaluate AND, OR or XOR over already-evaluated arguments. Arguments are
47
+ * eager (as in Excel, AND(FALSE, 1/0) is #DIV/0!); XOR is TRUE for an odd
48
+ * number of TRUE values.
49
+ */
50
+ export function evaluateSheetFormulaLogicalCombinator(
51
+ name: 'AND' | 'OR' | 'XOR',
52
+ values: unknown[],
53
+ ): SheetFormulaPureEvaluationResult {
54
+ const logicalValues = getSheetFormulaLogicalArgumentValues(name, values);
55
+ if (!Array.isArray(logicalValues)) {
56
+ return logicalValues;
57
+ }
58
+
59
+ if (name === 'AND') {
60
+ return { value: logicalValues.every(Boolean) };
61
+ }
62
+
63
+ return name === 'OR' ? { value: logicalValues.some(Boolean) } : { value: logicalValues.filter(Boolean).length % 2 === 1 };
64
+ }
65
+
66
+ /*
67
+ * Evaluate NOT over one already-evaluated argument; a blank argument is
68
+ * FALSE, so NOT(blank) is TRUE.
69
+ */
70
+ export function evaluateSheetFormulaNotFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
71
+ const [value] = values;
72
+ if (isSheetFormulaRangeLikeValue(value)) {
73
+ return getSheetFormulaPureErrorResult('INVALID_VALUE', 'NOT requires one TRUE/FALSE value, not a range.');
74
+ }
75
+
76
+ const logicalValue = getSheetFormulaLogicalBooleanValue(value);
77
+ return logicalValue === null
78
+ ? getSheetFormulaPureErrorResult('INVALID_VALUE', 'NOT requires a TRUE/FALSE value.')
79
+ : { value: !logicalValue };
80
+ }
81
+
82
+ /*
83
+ * Return the TRUE/FALSE result of one IS* information function from its
84
+ * evaluated argument. ISERROR is TRUE for any error and ISNA for NOT_FOUND
85
+ * (#N/A); ISBLANK, ISNUMBER and ISTEXT are FALSE for any error, so
86
+ * ISNUMBER(SEARCH(...)) tests whether a search found its text. ISBLANK is TRUE
87
+ * for empty cells and empty text; ISTEXT is TRUE for nonempty text, including
88
+ * ISO date text. ISBLANK/ISNUMBER/ISTEXT of a range is INVALID_VALUE.
89
+ */
90
+ export function evaluateSheetFormulaInfoFunction(
91
+ name: 'ISBLANK' | 'ISERROR' | 'ISNA' | 'ISNUMBER' | 'ISTEXT',
92
+ argument: { error?: { code: string } | null; value: unknown },
93
+ ): SheetFormulaPureEvaluationResult {
94
+ if (name === 'ISERROR' || name === 'ISNA') {
95
+ return {
96
+ value: name === 'ISERROR' ? Boolean(argument.error) : argument.error?.code === 'NOT_FOUND',
97
+ };
98
+ }
99
+
100
+ if (argument.error) {
101
+ return { value: false };
102
+ }
103
+
104
+ if (isSheetFormulaRangeLikeValue(argument.value)) {
105
+ return getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} requires a single value, not a range.`);
106
+ }
107
+
108
+ const { value } = argument;
109
+ if (name === 'ISBLANK') {
110
+ return { value: isSheetFormulaBlankValue(value) };
111
+ }
112
+
113
+ return {
114
+ value: name === 'ISNUMBER' ? typeof value === 'number' && Number.isFinite(value) : typeof value === 'string' && value !== '',
115
+ };
116
+ }