@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/a1.js DELETED
@@ -1,348 +0,0 @@
1
- import { MAX_ROWS, MAX_COLS } from './constants.js';
2
- import { parseRef } from './parseRef.js';
3
- import { toCol } from './toCol.js';
4
- import { fromCol } from './fromCol.js';
5
- import { stringifyPrefix, stringifyPrefixAlt } from './stringifyPrefix.js';
6
-
7
- const clamp = (min, val, max) => Math.min(Math.max(val, min), max);
8
- const toColStr = (c, a) => (a ? '$' : '') + toCol(c);
9
- const toRowStr = (r, a) => (a ? '$' : '') + toRow(r);
10
-
11
- export function fromRow (rowStr) {
12
- return +rowStr - 1;
13
- }
14
-
15
- export function toRow (top) {
16
- return String(top + 1);
17
- }
18
-
19
- export function toRelative (range) {
20
- return Object.assign({}, range, { $left: false, $right: false, $top: false, $bottom: false });
21
- }
22
-
23
- export function toAbsolute (range) {
24
- return Object.assign({}, range, { $left: true, $right: true, $top: true, $bottom: true });
25
- }
26
-
27
- /**
28
- * @ignore
29
- * @param {'head' | 'tail' | 'both' | null | undefined} trim Does the range have trimming?
30
- * @returns {string} The appropriate range join operator
31
- */
32
- export function rangeOperator (trim) {
33
- if (trim === 'both') {
34
- return '.:.';
35
- }
36
- else if (trim === 'head') {
37
- return '.:';
38
- }
39
- else if (trim === 'tail') {
40
- return ':.';
41
- }
42
- return ':';
43
- }
44
-
45
- export function trimDirection (head, tail) {
46
- if (head && tail) {
47
- return 'both';
48
- }
49
- if (head) {
50
- return 'head';
51
- }
52
- if (tail) {
53
- return 'tail';
54
- }
55
- }
56
-
57
- /**
58
- * Stringify a range object into A1 syntax.
59
- *
60
- * @private
61
- * @ignore
62
- * @see parseA1Ref
63
- * @param {RangeA1} range A range object
64
- * @returns {string} An A1-style string represenation of a range
65
- */
66
- export function toA1 (range) {
67
- // eslint-disable-next-line prefer-const
68
- let { top, left, bottom, right, trim } = range;
69
- const { $left, $right, $top, $bottom } = range;
70
- const noLeft = left == null;
71
- const noRight = right == null;
72
- const noTop = top == null;
73
- const noBottom = bottom == null;
74
- // allow skipping right and bottom to define a cell
75
- top = clamp(0, top | 0, MAX_ROWS);
76
- left = clamp(0, left | 0, MAX_COLS);
77
- if (!noLeft && !noTop && noRight && noBottom) {
78
- bottom = top;
79
- right = left;
80
- }
81
- else {
82
- bottom = clamp(0, bottom | 0, MAX_ROWS);
83
- right = clamp(0, right | 0, MAX_COLS);
84
- }
85
- const op = rangeOperator(trim);
86
- // A:A
87
- const allRows = top === 0 && bottom >= MAX_ROWS;
88
- const haveAbsCol = ($left && !noLeft) || ($right && !noRight);
89
- if ((allRows && !noLeft && !noRight && (!haveAbsCol || left === right)) || (noTop && noBottom)) {
90
- return toColStr(left, $left) + op + toColStr(right, $right);
91
- }
92
- // 1:1
93
- const allCols = left === 0 && right >= MAX_COLS;
94
- const haveAbsRow = ($top && !noTop) || ($bottom && !noBottom);
95
- if ((allCols && !noTop && !noBottom && (!haveAbsRow || top === bottom)) || (noLeft && noRight)) {
96
- return toRowStr(top, $top) + op + toRowStr(bottom, $bottom);
97
- }
98
- // A1:1
99
- if (!noLeft && !noTop && !noRight && noBottom) {
100
- return toColStr(left, $left) + toRowStr(top, $top) + op + toColStr(right, $right);
101
- }
102
- // A:A1 => A1:1
103
- if (!noLeft && noTop && !noRight && !noBottom) {
104
- return toColStr(left, $left) + toRowStr(bottom, $bottom) + op + toColStr(right, $right);
105
- }
106
- // A1:A
107
- if (!noLeft && !noTop && noRight && !noBottom) {
108
- return toColStr(left, $left) + toRowStr(top, $top) + op + toRowStr(bottom, $bottom);
109
- }
110
- // A:A1 => A1:A
111
- if (noLeft && !noTop && !noRight && !noBottom) {
112
- return toColStr(right, $right) + toRowStr(top, $top) + op + toRowStr(bottom, $bottom);
113
- }
114
- // A1:A1
115
- if (right !== left || bottom !== top || $right !== $left || $bottom !== $top) {
116
- return toColStr(left, $left) + toRowStr(top, $top) + op +
117
- toColStr(right, $right) + toRowStr(bottom, $bottom);
118
- }
119
- // A1
120
- return toColStr(left, $left) + toRowStr(top, $top);
121
- }
122
-
123
- function splitA1 (str) {
124
- const m = /^(?=.)(\$(?=\D))?([A-Za-z]{0,3})?(\$)?([1-9][0-9]{0,6})?$/.exec(str);
125
- if (!m || (!m[2] && !m[4])) {
126
- return null;
127
- }
128
- return [
129
- m[4] ? fromRow(m[4]) : null, // row index or null
130
- m[2] ? fromCol(m[2]) : null, // col index or null
131
- !!m[3], // is row absolute?
132
- !!m[1] // is col absolute?
133
- ];
134
- }
135
-
136
- /**
137
- * Parse a simple string reference to an A1 range into a range object.
138
- * Will accept `A1`, `A2`, `A:A`, or `1:1`.
139
- *
140
- * @private
141
- * @ignore
142
- * @see parseA1Ref
143
- * @param {string} rangeString A range string
144
- * @returns {(RangeA1|null)} An object representing a valid range or null if it is invalid.
145
- */
146
- export function fromA1 (rangeString) {
147
- let top = null;
148
- let left = null;
149
- let bottom = null;
150
- let right = null;
151
- let $top = false;
152
- let $left = false;
153
- let $bottom = false;
154
- let $right = false;
155
- const [ part1, op1, part2, op2, part3 ] = rangeString.split(/(\.?:\.?)/);
156
- if (op2 || part3) {
157
- return null;
158
- }
159
- const trim = trimDirection(!!op1 && op1[0] === '.', !!op1 && op1[op1.length - 1] === '.');
160
- const p1 = splitA1(part1);
161
- const p2 = part2 ? splitA1(part2) : null;
162
- if (!p1 || (part2 && !p2)) {
163
- // invalid section
164
- return null;
165
- }
166
- // part 1 bits
167
- if (p1[0] != null && p1[1] != null) {
168
- [ top, left, $top, $left ] = p1;
169
- }
170
- else if (p1[0] == null && p1[1] != null) {
171
- [ , left, , $left ] = p1;
172
- }
173
- else if (p1[0] != null && p1[1] == null) {
174
- [ top, , $top ] = p1;
175
- }
176
- // part 2 bits
177
- if (!part2) {
178
- // part 2 must exist if either top or left is null:
179
- // this disallows a single num or col patterns
180
- if (top == null || left == null) {
181
- return null;
182
- }
183
- bottom = top;
184
- right = left;
185
- $bottom = $top;
186
- $right = $left;
187
- }
188
- else if (p2[0] != null && p2[1] != null) {
189
- [ bottom, right, $bottom, $right ] = p2;
190
- }
191
- else if (p2[0] == null && p2[1] != null) {
192
- [ , right, , $right ] = p2;
193
- }
194
- else if (p2[0] != null && p2[1] == null) {
195
- [ bottom, , $bottom ] = p2;
196
- }
197
- // flip left/right and top/bottom as needed
198
- // for partial ranges we perfer the coord on the left-side of the :
199
- if (right != null && (left == null || (left != null && right < left))) {
200
- [ left, right, $left, $right ] = [ right, left, $right, $left ];
201
- }
202
- if (bottom != null && (top == null || (top != null && bottom < top))) {
203
- [ top, bottom, $top, $bottom ] = [ bottom, top, $bottom, $top ];
204
- }
205
- const r = { top, left, bottom, right, $top, $left, $bottom, $right };
206
- if (trim) { r.trim = trim; }
207
- return r;
208
- }
209
-
210
- /**
211
- * Parse a string reference into an object representing it.
212
- *
213
- * ```js
214
- * parseA1Ref('Sheet1!A$1:$B2');
215
- * // => {
216
- * // context: [ 'Sheet1' ],
217
- * // range: {
218
- * // top: 0,
219
- * // left: 0,
220
- * // bottom: 1,
221
- * // right: 1
222
- * // $top: true,
223
- * // $left: false,
224
- * // $bottom: false,
225
- * // $right: true
226
- * // }
227
- * // }
228
- * ```
229
- *
230
- * For A:A or A1:A style ranges, `null` will be used for any dimensions that the
231
- * syntax does not specify:
232
- *
233
- * @param {string} refString An A1-style reference string
234
- * @param {object} [options={}] Options
235
- * @param {boolean} [options.allowNamed=true] Enable parsing names as well as ranges.
236
- * @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.
237
- * @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)
238
- * @returns {(ReferenceA1|null)} An object representing a valid reference or null if it is invalid.
239
- */
240
- export function parseA1Ref (refString, { allowNamed = true, allowTernary = false, xlsx = false } = {}) {
241
- const d = parseRef(refString, { allowNamed, allowTernary, xlsx, r1c1: false });
242
- if (d && (d.r0 || d.name)) {
243
- let range = null;
244
- if (d.r0) {
245
- range = fromA1(d.r1 ? d.r0 + d.operator + d.r1 : d.r0);
246
- }
247
- if (range) {
248
- return xlsx
249
- ? { workbookName: d.workbookName, sheetName: d.sheetName, range }
250
- : { context: d.context, range };
251
- }
252
- if (d.name) {
253
- return xlsx
254
- ? { workbookName: d.workbookName, sheetName: d.sheetName, name: d.name }
255
- : { context: d.context, name: d.name };
256
- }
257
- return null;
258
- }
259
- return null;
260
- }
261
-
262
- /**
263
- * Get an A1-style string representation of a reference object.
264
- *
265
- * ```js
266
- * stringifyA1Ref({
267
- * context: [ 'Sheet1' ],
268
- * range: {
269
- * top: 0,
270
- * left: 0,
271
- * bottom: 1,
272
- * right: 1,
273
- * $top: true,
274
- * $left: false,
275
- * $bottom: false,
276
- * $right: true
277
- * }
278
- * });
279
- * // => 'Sheet1!A$1:$B2'
280
- * ```
281
- *
282
- * @param {ReferenceA1} refObject A reference object
283
- * @param {object} [options={}] Options
284
- * @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)
285
- * @returns {string} The reference in A1-style string format
286
- */
287
- export function stringifyA1Ref (refObject, { xlsx = false } = {}) {
288
- const prefix = xlsx
289
- ? stringifyPrefixAlt(refObject)
290
- : stringifyPrefix(refObject);
291
- return prefix + (
292
- refObject.name ? refObject.name : toA1(refObject.range)
293
- );
294
- }
295
-
296
- /**
297
- * Fill the any missing bounds in range objects. Top will be set to 0, bottom to
298
- * 1048575, left to 0, and right to 16383, if they are `null` or `undefined`.
299
- *
300
- * ```js
301
- * addA1RangeBounds({
302
- * context: [ 'Sheet1' ],
303
- * range: {
304
- * top: 0,
305
- * left: 0,
306
- * bottom: 1,
307
- * $top: true,
308
- * $left: false,
309
- * $bottom: false,
310
- * }
311
- * });
312
- * // => {
313
- * // context: [ 'Sheet1' ],
314
- * // range: {
315
- * // top: 0,
316
- * // left: 0,
317
- * // bottom: 1,
318
- * // right: 16383,
319
- * // $top: true,
320
- * // $left: false,
321
- * // $bottom: false,
322
- * // $right: false
323
- * // }
324
- * // }
325
- * ```
326
- *
327
- * @param {RangeA1} range The range part of a reference object.
328
- * @returns {RangeA1} same range with missing bounds filled in.
329
- */
330
- export function addA1RangeBounds (range) {
331
- if (range.top == null) {
332
- range.top = 0;
333
- range.$top = false;
334
- }
335
- if (range.bottom == null) {
336
- range.bottom = MAX_ROWS;
337
- range.$bottom = false;
338
- }
339
- if (range.left == null) {
340
- range.left = 0;
341
- range.$left = false;
342
- }
343
- if (range.right == null) {
344
- range.right = MAX_COLS;
345
- range.$right = false;
346
- }
347
- return range;
348
- }