@borgar/fx 4.12.0 → 5.0.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.
Files changed (139) hide show
  1. package/dist/index-BMr6cTgc.d.cts +1444 -0
  2. package/dist/index-BMr6cTgc.d.ts +1444 -0
  3. package/dist/index.cjs +3054 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +1 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +2984 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/xlsx/index.cjs +3120 -0
  10. package/dist/xlsx/index.cjs.map +1 -0
  11. package/dist/xlsx/index.d.cts +55 -0
  12. package/dist/xlsx/index.d.ts +55 -0
  13. package/dist/xlsx/index.js +3049 -0
  14. package/dist/xlsx/index.js.map +1 -0
  15. package/docs/API.md +2959 -718
  16. package/docs/AST_format.md +2 -2
  17. package/eslint.config.mjs +40 -0
  18. package/lib/a1.spec.ts +32 -0
  19. package/lib/a1.ts +26 -0
  20. package/lib/addA1RangeBounds.ts +50 -0
  21. package/lib/addTokenMeta.spec.ts +166 -0
  22. package/lib/{addTokenMeta.js → addTokenMeta.ts} +53 -33
  23. package/lib/astTypes.ts +211 -0
  24. package/lib/cloneToken.ts +29 -0
  25. package/lib/{constants.js → constants.ts} +6 -3
  26. package/lib/fixRanges.spec.ts +220 -0
  27. package/lib/fixRanges.ts +260 -0
  28. package/lib/fromCol.spec.ts +15 -0
  29. package/lib/{fromCol.js → fromCol.ts} +1 -1
  30. package/lib/index.spec.ts +119 -0
  31. package/lib/index.ts +76 -0
  32. package/lib/isNodeType.ts +151 -0
  33. package/lib/isType.spec.ts +208 -0
  34. package/lib/{isType.js → isType.ts} +26 -25
  35. package/lib/lexers/advRangeOp.ts +18 -0
  36. package/lib/lexers/canEndRange.ts +25 -0
  37. package/lib/lexers/lexBoolean.ts +55 -0
  38. package/lib/lexers/lexContext.ts +104 -0
  39. package/lib/lexers/lexError.ts +15 -0
  40. package/lib/lexers/lexFunction.ts +37 -0
  41. package/lib/lexers/lexNameFuncCntx.ts +112 -0
  42. package/lib/lexers/lexNamed.ts +60 -0
  43. package/lib/lexers/lexNewLine.ts +12 -0
  44. package/lib/lexers/lexNumber.ts +48 -0
  45. package/lib/lexers/lexOperator.ts +26 -0
  46. package/lib/lexers/lexRange.ts +15 -0
  47. package/lib/lexers/lexRangeA1.ts +134 -0
  48. package/lib/lexers/lexRangeR1C1.ts +146 -0
  49. package/lib/lexers/lexRangeTrim.ts +26 -0
  50. package/lib/lexers/lexRefOp.ts +19 -0
  51. package/lib/lexers/lexString.ts +22 -0
  52. package/lib/lexers/lexStructured.ts +25 -0
  53. package/lib/lexers/lexWhitespace.ts +31 -0
  54. package/lib/lexers/sets.ts +51 -0
  55. package/lib/mergeRefTokens.spec.ts +141 -0
  56. package/lib/{mergeRefTokens.js → mergeRefTokens.ts} +47 -32
  57. package/lib/nodeTypes.ts +54 -0
  58. package/lib/parse.spec.ts +1410 -0
  59. package/lib/{parser.js → parse.ts} +81 -63
  60. package/lib/parseA1Range.spec.ts +233 -0
  61. package/lib/parseA1Range.ts +206 -0
  62. package/lib/parseA1Ref.spec.ts +337 -0
  63. package/lib/parseA1Ref.ts +115 -0
  64. package/lib/parseR1C1Range.ts +191 -0
  65. package/lib/parseR1C1Ref.spec.ts +323 -0
  66. package/lib/parseR1C1Ref.ts +127 -0
  67. package/lib/parseRef.spec.ts +90 -0
  68. package/lib/parseRef.ts +240 -0
  69. package/lib/parseSRange.ts +240 -0
  70. package/lib/parseStructRef.spec.ts +168 -0
  71. package/lib/parseStructRef.ts +76 -0
  72. package/lib/stringifyA1Range.spec.ts +72 -0
  73. package/lib/stringifyA1Range.ts +72 -0
  74. package/lib/stringifyA1Ref.spec.ts +64 -0
  75. package/lib/stringifyA1Ref.ts +59 -0
  76. package/lib/{stringifyPrefix.js → stringifyPrefix.ts} +17 -2
  77. package/lib/stringifyR1C1Range.spec.ts +92 -0
  78. package/lib/stringifyR1C1Range.ts +73 -0
  79. package/lib/stringifyR1C1Ref.spec.ts +63 -0
  80. package/lib/stringifyR1C1Ref.ts +67 -0
  81. package/lib/stringifyStructRef.spec.ts +124 -0
  82. package/lib/stringifyStructRef.ts +113 -0
  83. package/lib/stringifyTokens.ts +15 -0
  84. package/lib/toCol.spec.ts +11 -0
  85. package/lib/{toCol.js → toCol.ts} +4 -4
  86. package/lib/tokenTypes.ts +76 -0
  87. package/lib/tokenize-srefs.spec.ts +429 -0
  88. package/lib/tokenize.spec.ts +2103 -0
  89. package/lib/tokenize.ts +346 -0
  90. package/lib/translate.spec.ts +35 -0
  91. package/lib/translateToA1.spec.ts +247 -0
  92. package/lib/translateToA1.ts +231 -0
  93. package/lib/translateToR1C1.spec.ts +227 -0
  94. package/lib/translateToR1C1.ts +145 -0
  95. package/lib/types.ts +179 -0
  96. package/lib/xlsx/index.spec.ts +27 -0
  97. package/lib/xlsx/index.ts +32 -0
  98. package/package.json +46 -30
  99. package/tsconfig.json +28 -0
  100. package/typedoc-ignore-links.ts +17 -0
  101. package/typedoc.json +41 -0
  102. package/.eslintrc +0 -22
  103. package/dist/fx.d.ts +0 -823
  104. package/dist/fx.js +0 -2
  105. package/dist/package.json +0 -1
  106. package/lib/a1.js +0 -348
  107. package/lib/a1.spec.js +0 -458
  108. package/lib/addTokenMeta.spec.js +0 -153
  109. package/lib/astTypes.js +0 -96
  110. package/lib/extraTypes.js +0 -74
  111. package/lib/fixRanges.js +0 -104
  112. package/lib/fixRanges.spec.js +0 -170
  113. package/lib/fromCol.spec.js +0 -11
  114. package/lib/index.js +0 -134
  115. package/lib/index.spec.js +0 -67
  116. package/lib/isType.spec.js +0 -168
  117. package/lib/lexer-srefs.spec.js +0 -324
  118. package/lib/lexer.js +0 -283
  119. package/lib/lexer.spec.js +0 -1953
  120. package/lib/lexerParts.js +0 -228
  121. package/lib/mergeRefTokens.spec.js +0 -121
  122. package/lib/package.json +0 -1
  123. package/lib/parseRef.js +0 -157
  124. package/lib/parseRef.spec.js +0 -71
  125. package/lib/parseSRange.js +0 -167
  126. package/lib/parseStructRef.js +0 -48
  127. package/lib/parseStructRef.spec.js +0 -164
  128. package/lib/parser.spec.js +0 -1208
  129. package/lib/rc.js +0 -341
  130. package/lib/rc.spec.js +0 -403
  131. package/lib/stringifyStructRef.js +0 -80
  132. package/lib/stringifyStructRef.spec.js +0 -182
  133. package/lib/toCol.spec.js +0 -11
  134. package/lib/translate-toA1.spec.js +0 -214
  135. package/lib/translate-toRC.spec.js +0 -197
  136. package/lib/translate.js +0 -239
  137. package/lib/translate.spec.js +0 -21
  138. package/rollup.config.mjs +0 -22
  139. package/tsd.json +0 -12
package/lib/rc.js DELETED
@@ -1,341 +0,0 @@
1
- /*
2
- ** RC notation works differently from A1 in that we can't merge static
3
- ** references joined by `:`. Merging can only work between references
4
- ** that are relative/absolute on the same axes, so:
5
- ** - R1C1:R2C2 will work,
6
- ** - R[1]C1:R[2]C2 will also work, but
7
- ** - R[1]C[1]:R2C2 doesn't have a direct rectangle represention without context.
8
- */
9
- import { rangeOperator, trimDirection } from './a1.js';
10
- import { MAX_ROWS, MAX_COLS } from './constants.js';
11
- import { parseRef } from './parseRef.js';
12
- import { stringifyPrefix, stringifyPrefixAlt } from './stringifyPrefix.js';
13
-
14
- const clamp = (min, val, max) => Math.min(Math.max(val, min), max);
15
-
16
- function toCoord (value, isAbs) {
17
- if (isAbs) {
18
- return String(value + 1);
19
- }
20
- return value ? '[' + value + ']' : '';
21
- }
22
-
23
- /**
24
- * Stringify a range object into R1C1 syntax.
25
- *
26
- * @ignore
27
- * @private
28
- * @see parseR1C1Ref
29
- * @param {RangeR1C1} range A range object
30
- * @returns {string} An R1C1-style string represenation of a range
31
- */
32
- export function toR1C1 (range) {
33
- let { r0, c0, r1, c1 } = range;
34
- const { $c0, $c1, $r0, $r1 } = range;
35
- const nullR0 = r0 == null;
36
- const nullC0 = c0 == null;
37
- let nullR1 = r1 == null;
38
- let nullC1 = c1 == null;
39
- const op = rangeOperator(range.trim);
40
- const hasTrim = !!range.trim;
41
- r0 = clamp($r0 ? 0 : -MAX_ROWS, r0 | 0, MAX_ROWS);
42
- c0 = clamp($c0 ? 0 : -MAX_COLS, c0 | 0, MAX_COLS);
43
- if (!nullR0 && nullR1 && !nullC0 && nullC1) {
44
- r1 = r0;
45
- nullR1 = false;
46
- c1 = c0;
47
- nullC1 = false;
48
- }
49
- else {
50
- r1 = clamp($r1 ? 0 : -MAX_ROWS, r1 | 0, MAX_ROWS);
51
- c1 = clamp($c1 ? 0 : -MAX_COLS, c1 | 0, MAX_COLS);
52
- }
53
- // C:C
54
- const allRows = r0 === 0 && r1 >= MAX_ROWS;
55
- if ((allRows && !nullC0 && !nullC1) || (nullR0 && nullR1)) {
56
- const a = toCoord(c0, $c0);
57
- const b = toCoord(c1, $c1);
58
- return 'C' + (a === b && !hasTrim ? a : a + op + 'C' + b);
59
- }
60
- // R:R
61
- const allCols = c0 === 0 && c1 >= MAX_COLS;
62
- if ((allCols && !nullR0 && !nullR1) || (nullC0 && nullC1)) {
63
- const a = toCoord(r0, $r0);
64
- const b = toCoord(r1, $r1);
65
- return 'R' + (a === b && !hasTrim ? a : a + op + 'R' + b);
66
- }
67
- const s_r0 = toCoord(r0, $r0);
68
- const s_r1 = toCoord(r1, $r1);
69
- const s_c0 = toCoord(c0, $c0);
70
- const s_c1 = toCoord(c1, $c1);
71
- // RC:R, RC:C
72
- if (nullR0 || nullR1 || nullC0 || nullC1) {
73
- return (
74
- (nullR0 ? '' : 'R' + s_r0) +
75
- (nullC0 ? '' : 'C' + s_c0) +
76
- op +
77
- (nullR1 ? '' : 'R' + s_r1) +
78
- (nullC1 ? '' : 'C' + s_c1)
79
- );
80
- }
81
- // RC:RC
82
- if (s_r0 !== s_r1 || s_c0 !== s_c1) {
83
- return 'R' + s_r0 + 'C' + s_c0 + op + 'R' + s_r1 + 'C' + s_c1;
84
- }
85
- // RC
86
- return 'R' + s_r0 + 'C' + s_c0;
87
- }
88
-
89
- function parseR1C1Part (ref) {
90
- let r0 = null;
91
- let c0 = null;
92
- let $r0 = null;
93
- let $c0 = null;
94
- // R part
95
- const rm = /^R(?:\[([+-]?\d+)\]|(\d+))?/.exec(ref);
96
- if (rm) {
97
- if (rm[1]) {
98
- r0 = parseInt(rm[1], 10);
99
- $r0 = false;
100
- }
101
- else if (rm[2]) {
102
- r0 = parseInt(rm[2], 10) - 1;
103
- $r0 = true;
104
- }
105
- else {
106
- r0 = 0;
107
- $r0 = false;
108
- }
109
- ref = ref.slice(rm[0].length);
110
- }
111
- // C part
112
- const cm = /^C(?:\[([+-]?\d+)\]|(\d+))?/.exec(ref);
113
- if (cm) {
114
- if (cm[1]) {
115
- c0 = parseInt(cm[1], 10);
116
- $c0 = false;
117
- }
118
- else if (cm[2]) {
119
- c0 = parseInt(cm[2], 10) - 1;
120
- $c0 = true;
121
- }
122
- else {
123
- c0 = 0;
124
- $c0 = false;
125
- }
126
- ref = ref.slice(cm[0].length);
127
- }
128
- // must have at least one part (and nothing more)
129
- if ((!rm && !cm) || ref.length) {
130
- return null;
131
- }
132
- return [ r0, c0, $r0, $c0 ];
133
- }
134
-
135
- /**
136
- * Parse a simple string reference to an R1C1 range into a range object.
137
- *
138
- * @private
139
- * @ignore
140
- * @see parseA1Ref
141
- * @param {string} rangeString A range string
142
- * @returns {(RangeR1C1|null)} An object representing a valid reference or null if it is invalid.
143
- */
144
- export function fromR1C1 (rangeString) {
145
- let final = null;
146
- const [ part1, op, part2, overflow ] = rangeString.split(/(\.?:\.?)/);
147
- if (overflow) {
148
- return null;
149
- }
150
- const range = parseR1C1Part(part1);
151
- const trim = trimDirection(!!op && op[0] === '.', !!op && op[op.length - 1] === '.');
152
- if (range) {
153
- const [ r0, c0, $r0, $c0 ] = range;
154
- if (part2) {
155
- const extendTo = parseR1C1Part(part2);
156
- if (extendTo) {
157
- final = {};
158
- const [ r1, c1, $r1, $c1 ] = extendTo;
159
- // rows
160
- if (r0 != null && r1 != null) {
161
- final.r0 = $r0 === $r1 ? Math.min(r0, r1) : r0;
162
- final.$r0 = $r0;
163
- final.r1 = $r0 === $r1 ? Math.max(r0, r1) : r1;
164
- final.$r1 = $r1;
165
- }
166
- else if (r0 != null && r1 == null) {
167
- // partial RC:C
168
- final.r0 = r0;
169
- final.$r0 = $r0;
170
- final.r1 = null;
171
- final.$r1 = $r0;
172
- }
173
- else if (r0 == null && r1 != null) {
174
- // partial C:RC
175
- final.r0 = r1;
176
- final.$r0 = $r1;
177
- final.r1 = null;
178
- final.$r1 = $r1;
179
- }
180
- else if (r0 == null && r1 == null) {
181
- // C:C
182
- final.r0 = null;
183
- final.$r0 = false;
184
- final.r1 = null;
185
- final.$r1 = false;
186
- }
187
- // columns
188
- if (c0 != null && c1 != null) {
189
- final.c0 = $c0 === $c1 ? Math.min(c0, c1) : c0;
190
- final.$c0 = $c0;
191
- final.c1 = $c0 === $c1 ? Math.max(c0, c1) : c1;
192
- final.$c1 = $c1;
193
- }
194
- else if (c0 != null && c1 == null) {
195
- final.c0 = c0;
196
- final.$c0 = $c0;
197
- final.c1 = null;
198
- final.$c1 = $c0;
199
- }
200
- else if (c0 == null && c1 != null) {
201
- final.c0 = c1;
202
- final.$c0 = $c1;
203
- final.c1 = null;
204
- final.$c1 = $c1;
205
- }
206
- else if (c0 == null && c1 == null) {
207
- final.c0 = null;
208
- final.$c0 = false;
209
- final.c1 = null;
210
- final.$c1 = false;
211
- }
212
- }
213
- else {
214
- return null;
215
- }
216
- }
217
- // range only - no second part
218
- else if (r0 != null && c0 == null) {
219
- final = {
220
- r0: r0,
221
- c0: null,
222
- r1: r0,
223
- c1: null,
224
- $r0: $r0,
225
- $c0: false,
226
- $r1: $r0,
227
- $c1: false
228
- };
229
- }
230
- else if (r0 == null && c0 != null) {
231
- final = {
232
- r0: null,
233
- c0: c0,
234
- r1: null,
235
- c1: c0,
236
- $r0: false,
237
- $c0: $c0,
238
- $r1: false,
239
- $c1: $c0
240
- };
241
- }
242
- else {
243
- final = {
244
- r0: r0 || 0,
245
- c0: c0 || 0,
246
- r1: r0 || 0,
247
- c1: c0 || 0,
248
- $r0: $r0 || false,
249
- $c0: $c0 || false,
250
- $r1: $r0 || false,
251
- $c1: $c0 || false
252
- };
253
- }
254
- }
255
- if (final && trim) {
256
- final.trim = trim;
257
- }
258
- return final;
259
- }
260
-
261
- /**
262
- * Parse a string reference into an object representing it.
263
- *
264
- * ```js
265
- * parseR1C1Ref('Sheet1!R[9]C9:R[9]C9');
266
- * // => {
267
- * // context: [ 'Sheet1' ],
268
- * // range: {
269
- * // r0: 9,
270
- * // c0: 8,
271
- * // r1: 9,
272
- * // c1: 8,
273
- * // $c0: true,
274
- * // $c1: true
275
- * // $r0: false,
276
- * // $r1: false
277
- * // }
278
- * // }
279
- * ```
280
- *
281
- * @param {string} refString An R1C1-style reference string
282
- * @param {object} [options={}] Options
283
- * @param {boolean} [options.allowNamed=true] Enable parsing names as well as ranges.
284
- * @param {boolean} [options.allowTernary=false] Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md.
285
- * @param {boolean} [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
286
- * @returns {(ReferenceR1C1|null)} An object representing a valid reference or null if it is invalid.
287
- */
288
- export function parseR1C1Ref (refString, { allowNamed = true, allowTernary = false, xlsx = false } = {}) {
289
- const d = parseRef(refString, { allowNamed, allowTernary, xlsx, r1c1: true });
290
- if (d && (d.r0 || d.name)) {
291
- const range = d.r1
292
- ? fromR1C1(d.r0 + d.operator + d.r1)
293
- : fromR1C1(d.r0);
294
- if (range) {
295
- return xlsx
296
- ? { workbookName: d.workbookName, sheetName: d.sheetName, range }
297
- : { context: d.context, range };
298
- }
299
- if (d.name) {
300
- return xlsx
301
- ? { workbookName: d.workbookName, sheetName: d.sheetName, name: d.name }
302
- : { context: d.context, name: d.name };
303
- }
304
- return null;
305
- }
306
- return null;
307
- }
308
-
309
- /**
310
- * Get an R1C1-style string representation of a reference object.
311
- *
312
- * ```js
313
- * stringifyR1C1Ref({
314
- * context: [ 'Sheet1' ],
315
- * range: {
316
- * r0: 9,
317
- * c0: 8,
318
- * r1: 9,
319
- * c1: 8,
320
- * $c0: true,
321
- * $c1: true
322
- * $r0: false,
323
- * $r1: false
324
- * }
325
- * });
326
- * // => 'Sheet1!R[9]C9:R[9]C9'
327
- * ```
328
- *
329
- * @param {ReferenceR1C1} refObject A reference object
330
- * @param {object} [options={}] Options
331
- * @param {boolean} [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
332
- * @returns {string} The reference in R1C1-style string format
333
- */
334
- export function stringifyR1C1Ref (refObject, { xlsx = false } = {}) {
335
- const prefix = xlsx
336
- ? stringifyPrefixAlt(refObject)
337
- : stringifyPrefix(refObject);
338
- return prefix + (
339
- refObject.name ? refObject.name : toR1C1(refObject.range)
340
- );
341
- }