@trebco/treb 30.2.4 → 30.2.14

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 (32) hide show
  1. package/dist/treb-spreadsheet-light.mjs +11 -11
  2. package/dist/treb-spreadsheet.mjs +11 -11
  3. package/dist/treb.d.ts +1 -1
  4. package/esbuild-custom-element.mjs +2 -0
  5. package/esbuild-utils.mjs +2 -0
  6. package/package.json +6 -7
  7. package/treb-calculator/src/calculator.ts +41 -0
  8. package/treb-calculator/src/expression-calculator.ts +30 -1
  9. package/treb-calculator/src/functions/base-functions.ts +164 -1
  10. package/treb-calculator/src/functions/statistics-functions.ts +134 -0
  11. package/treb-calculator/src/functions/text-functions.ts +46 -1
  12. package/treb-calculator/src/primitives.ts +3 -1
  13. package/treb-calculator/src/utilities.ts +5 -1
  14. package/treb-data-model/src/data_model.ts +227 -2
  15. package/treb-data-model/src/index.ts +1 -0
  16. package/{treb-embed → treb-data-model}/src/language-model.ts +3 -0
  17. package/treb-data-model/src/sheet.ts +0 -13
  18. package/treb-embed/src/embedded-spreadsheet.ts +22 -39
  19. package/treb-embed/style/autocomplete.scss +2 -18
  20. package/treb-embed/style/dialog.scss +1 -1
  21. package/treb-embed/style/dropdown-select.scss +5 -6
  22. package/treb-embed/style/formula-bar.scss +5 -3
  23. package/treb-embed/style/grid.scss +7 -4
  24. package/treb-embed/style/layout.scss +23 -18
  25. package/treb-embed/style/tab-bar.scss +2 -2
  26. package/treb-embed/style/theme-defaults.scss +4 -4
  27. package/treb-embed/style/toolbar.scss +1 -1
  28. package/treb-grid/src/render/tile_renderer.ts +14 -0
  29. package/treb-grid/src/types/grid.ts +25 -139
  30. package/treb-parser/src/parser-types.ts +12 -0
  31. package/treb-parser/src/parser.ts +43 -2
  32. package/tsproject.json +1 -1
package/dist/treb.d.ts CHANGED
@@ -1310,7 +1310,7 @@ export type Color = ThemeColor | HTMLColor | NullColor;
1310
1310
  export declare const ThemeColorIndex: (color: ThemeColor) => number;
1311
1311
  export declare const IsHTMLColor: (color?: Color) => color is HTMLColor;
1312
1312
  export declare const IsThemeColor: (color?: Color) => color is ThemeColor;
1313
- export declare const IsDefinedColor: (color?: Color) => color is ThemeColor | HTMLColor;
1313
+ export declare const IsDefinedColor: (color?: Color) => color is (ThemeColor | HTMLColor);
1314
1314
  export type CellValue = undefined | string | number | boolean | Complex | DimensionedQuantity;
1315
1315
 
1316
1316
  /**
@@ -1,5 +1,7 @@
1
1
  // @ts-check
2
2
 
3
+ /* global process */
4
+
3
5
  import * as esbuild from 'esbuild';
4
6
 
5
7
  import { SassPlugin, WorkerPlugin, NotifyPlugin, HTMLPlugin } from './esbuild-utils.mjs';
package/esbuild-utils.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
2
  // @ts-check
3
3
 
4
+ /* global console */
5
+
4
6
  import * as esbuild from 'esbuild';
5
7
  import { promises as fs } from 'fs';
6
8
  import { minify } from 'html-minifier';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trebco/treb",
3
- "version": "30.2.4",
3
+ "version": "30.2.14",
4
4
  "license": "LGPL-3.0-or-later",
5
5
  "homepage": "https://treb.app",
6
6
  "repository": {
@@ -12,13 +12,12 @@
12
12
  "type": "module",
13
13
  "devDependencies": {
14
14
  "@types/html-minifier": "^4.0.2",
15
- "@types/node": "^20.8.5",
15
+ "@types/node": "^22.1.0",
16
16
  "@types/uzip": "^0.20201231.0",
17
- "archiver": "^6.0.1",
18
17
  "base64-js": "^1.5.1",
19
- "cssnano": "^6.0.0",
20
- "esbuild": "^0.20.1",
21
- "eslint": "^8.56.0",
18
+ "cssnano": "^7.0.4",
19
+ "esbuild": "^0.23.0",
20
+ "eslint": "^9.8.0",
22
21
  "fast-xml-parser": "^4.0.7",
23
22
  "html-minifier": "^4.0.0",
24
23
  "sass": "^1.69.3",
@@ -34,7 +33,7 @@
34
33
  "ts-node-dev": "^2.0.0",
35
34
  "tslib": "^2.2.0",
36
35
  "typescript": "^5.3.3",
37
- "typescript-eslint": "^7.0.2",
36
+ "typescript-eslint": "^8.0.0",
38
37
  "uzip": "^0.20201231.0"
39
38
  },
40
39
  "scripts": {
@@ -33,6 +33,7 @@ import type { CalculationResult } from './dag/spreadsheet_vertex_base';
33
33
 
34
34
  import { ExpressionCalculator, UnionIsMetadata } from './expression-calculator';
35
35
  import * as Utilities from './utilities';
36
+ import { StringUnion } from './utilities';
36
37
 
37
38
  import { FunctionLibrary } from './function-library';
38
39
  import type { FunctionMap } from './descriptors';
@@ -567,6 +568,46 @@ export class Calculator extends Graph {
567
568
  },
568
569
  },
569
570
 
571
+ Address: {
572
+ arguments: [
573
+ { name: 'row' },
574
+ { name: 'column' },
575
+ { name: 'absolute' },
576
+ { name: 'a1' },
577
+ { name: 'sheet name'}
578
+ ],
579
+ fn: (row = 1, column = 1, absolute = 1, a1 = true, sheet_name?: string): UnionValue => {
580
+
581
+ const address: UnitAddress = {
582
+ type: 'address',
583
+ id: 0, position: 0, label: '',
584
+ row: row-1,
585
+ column: column-1,
586
+ };
587
+
588
+ switch (absolute) {
589
+ case 2:
590
+ address.absolute_row = true;
591
+ break;
592
+ case 3:
593
+ address.absolute_column = true;
594
+ break;
595
+ case 4:
596
+ break;
597
+ default:
598
+ address.absolute_column = true;
599
+ address.absolute_row = true;
600
+ }
601
+
602
+ if (sheet_name) {
603
+ address.sheet = sheet_name;
604
+ }
605
+
606
+ return StringUnion(this.parser.Render(address, { r1c1: !a1 }));
607
+
608
+ },
609
+ },
610
+
570
611
  /**
571
612
  * anything I said about COUNTIF applies here, but worse.
572
613
  * COUNTIFS is an AND operation across separate COUNTIFs.
@@ -782,7 +782,36 @@ export class ExpressionCalculator {
782
782
  // unless the expression changes, which will discard the generated
783
783
  // function (along with the expression itself).
784
784
 
785
- const fn = Primitives.MapOperator(x.operator);
785
+ // pulling out concat so we can bind the model for language values
786
+
787
+ const fn = x.operator === '&' ? (a: UnionValue, b: UnionValue): UnionValue => {
788
+
789
+ // this works, but it's not volatile so it doesn't update on
790
+ // a language change; maybe language change should force a recalc? (...)
791
+
792
+ if (a.type === ValueType.error) { return a; }
793
+ if (b.type === ValueType.error) { return b; }
794
+
795
+ const strings = [a, b].map(x => {
796
+ if (x.type === ValueType.undefined) { return ''; }
797
+ if (x.type === ValueType.boolean) {
798
+ if (x.value) {
799
+ return this.data_model.language_model?.boolean_true || 'TRUE';
800
+ }
801
+ else {
802
+ return this.data_model.language_model?.boolean_false || 'FALSE';
803
+ }
804
+ }
805
+ return x.value;
806
+ });
807
+
808
+ return {
809
+ type: ValueType.string,
810
+ value: `${strings[0]}${strings[1]}`,
811
+ };
812
+
813
+
814
+ } : Primitives.MapOperator(x.operator);
786
815
 
787
816
  if (!fn) {
788
817
 
@@ -21,9 +21,12 @@
21
21
 
22
22
  import type { FunctionMap, IntrinsicValue } from '../descriptors';
23
23
  import * as Utils from '../utilities';
24
+ // import { StringUnion, NumberUnion } from '../utilities';
24
25
  import { ReferenceError, NotImplError, NAError, ArgumentError, DivideByZeroError, ValueError } from '../function-error';
25
26
  import type { UnionValue,
26
- RenderFunctionResult, RenderFunctionOptions, Complex, CellValue } from 'treb-base-types';
27
+ RenderFunctionResult, RenderFunctionOptions, Complex, CellValue,
28
+ // ICellAddress
29
+ } from 'treb-base-types';
27
30
  import { Box, ValueType, GetValueType, ComplexOrReal, IsComplex, Area } from 'treb-base-types';
28
31
  import { Sparkline } from './sparkline';
29
32
  import { LotusDate, UnlotusDate } from 'treb-format';
@@ -318,6 +321,15 @@ export const AltFunctionLibrary: FunctionMap = {
318
321
 
319
322
  export const BaseFunctionLibrary: FunctionMap = {
320
323
 
324
+ // not sure why there are functions for booleans, but there are
325
+ True: {
326
+ fn: () => ({ type: ValueType.boolean, value: true }),
327
+ },
328
+
329
+ False: {
330
+ fn: () => ({ type: ValueType.boolean, value: false }),
331
+ },
332
+
321
333
  Int: {
322
334
  fn: (value: number) => {
323
335
  return {type: ValueType.number, value: Math.floor(value) };
@@ -585,6 +597,37 @@ export const BaseFunctionLibrary: FunctionMap = {
585
597
  },
586
598
  },
587
599
 
600
+ IsErr: {
601
+ description: 'Checks if another cell contains an error',
602
+ arguments: [{ name: 'reference', allow_error: true, boxed: true }],
603
+ fn: (...args: UnionValue[]): UnionValue => {
604
+
605
+ const values = Utils.FlattenBoxed(args);
606
+ for (const value of values) {
607
+ if (value.type === ValueType.error && value.value !== 'N/A') {
608
+ return { type: ValueType.boolean, value: true };
609
+ }
610
+ }
611
+
612
+ /*
613
+ if (Array.isArray(ref)) {
614
+ const values = Utils.Flatten(ref) as UnionValue[];
615
+ for (const value of values) {
616
+ if (value.type === ValueType.error) {
617
+ return { type: ValueType.boolean, value: true };
618
+ }
619
+ }
620
+ }
621
+ else if (ref) {
622
+ return { type: ValueType.boolean, value: ref.type === ValueType.error };
623
+ }
624
+ */
625
+
626
+ return { type: ValueType.boolean, value: false };
627
+
628
+ },
629
+ },
630
+
588
631
  IsError: {
589
632
  description: 'Checks if another cell contains an error',
590
633
  arguments: [{ name: 'reference', allow_error: true, boxed: true }],
@@ -1182,6 +1225,126 @@ export const BaseFunctionLibrary: FunctionMap = {
1182
1225
  */
1183
1226
 
1184
1227
 
1228
+ Row: {
1229
+ arguments: [{ name: 'reference', metadata: true }],
1230
+ fn: (ref: UnionValue): UnionValue => {
1231
+ if (ref.type === ValueType.array) {
1232
+ const arr = ref.value;
1233
+ const first = arr[0][0];
1234
+
1235
+ if (UnionIsMetadata(first)) {
1236
+ return {
1237
+ type: ValueType.array,
1238
+ value: [arr[0].map((row, index) => ({
1239
+ type: ValueType.number,
1240
+ value: index + first.value.address.row + 1
1241
+ }))],
1242
+ };
1243
+ }
1244
+
1245
+ }
1246
+ else if (UnionIsMetadata(ref)) {
1247
+ return {
1248
+ type: ValueType.number, value: ref.value.address.row + 1,
1249
+ }
1250
+ }
1251
+ return ArgumentError();
1252
+ },
1253
+ },
1254
+
1255
+ Column: {
1256
+ arguments: [{ name: 'reference', metadata: true }],
1257
+ fn: (ref: UnionValue): UnionValue => {
1258
+ if (ref.type === ValueType.array) {
1259
+ const arr = ref.value;
1260
+ const first = arr[0][0];
1261
+
1262
+ if (UnionIsMetadata(first)) {
1263
+ return {
1264
+ type: ValueType.array,
1265
+ value: arr.map((row, index) => [{
1266
+ type: ValueType.number,
1267
+ value: index + first.value.address.column + 1
1268
+ }]),
1269
+ };
1270
+ }
1271
+
1272
+ }
1273
+ else if (UnionIsMetadata(ref)) {
1274
+ return {
1275
+ type: ValueType.number, value: ref.value.address.row + 1,
1276
+ }
1277
+ }
1278
+ return ArgumentError();
1279
+ },
1280
+ },
1281
+
1282
+ Choose: {
1283
+ arguments: [
1284
+ { name: 'Selected index', },
1285
+ { name: 'Choice 1...', metadata: true },
1286
+ ],
1287
+ return_type: 'reference',
1288
+ description: 'Returns one of a list of choices',
1289
+ fn: (selected: number, ...choices: UnionValue[]): UnionValue => {
1290
+
1291
+ if (selected < 1 || selected > choices.length) {
1292
+ return ValueError();
1293
+ }
1294
+
1295
+ const value = choices[selected - 1];
1296
+
1297
+ // this should be metadata. is there a different object we
1298
+ // might run into? maybe we should refactor how metadata works
1299
+
1300
+ if (UnionIsMetadata(value)) {
1301
+ return {
1302
+ type: ValueType.object,
1303
+ value: value.value.address,
1304
+ }
1305
+ }
1306
+
1307
+ // check if array is metadata. if it's a literal array
1308
+ // we just want to return it.
1309
+
1310
+ if (value.type === ValueType.array) {
1311
+ const arr = value.value;
1312
+ const rows = arr.length;
1313
+ const cols = arr[0].length;
1314
+ const first = arr[0][0];
1315
+ const last = arr[rows - 1][cols - 1];
1316
+
1317
+ if (rows === 1 && cols === 1) {
1318
+ if (UnionIsMetadata(first)) {
1319
+ return {
1320
+ type: ValueType.object,
1321
+ value: first.value.address,
1322
+ };
1323
+ }
1324
+ }
1325
+ else {
1326
+ if (UnionIsMetadata(first) && UnionIsMetadata(last)) {
1327
+ return {
1328
+ type: ValueType.object,
1329
+ value: {
1330
+ type: 'range',
1331
+ position: 0, id: 0, label: '',
1332
+ start: first.value.address,
1333
+ end: last.value.address,
1334
+ }
1335
+ }
1336
+ }
1337
+
1338
+ }
1339
+ }
1340
+
1341
+ return {
1342
+ ...value, // should we deep-copy in case of an array?
1343
+ };
1344
+
1345
+ },
1346
+ },
1347
+
1185
1348
  /*
1186
1349
  * rewrite of xlookup to return a reference. better compatibility.
1187
1350
  * ---
@@ -260,6 +260,140 @@ export const StatisticsFunctionLibrary: FunctionMap = {
260
260
  },
261
261
  },
262
262
 
263
+ LCM: {
264
+ description: 'Retuns the least common mulitple of the arguments',
265
+ arguments: [{ boxed: true }],
266
+ fn: (...args: UnionValue[]): UnionValue => {
267
+ args = Utils.FlattenBoxed(args);
268
+ const list: number[] = [];
269
+
270
+ for (const ref of args as UnionValue[]) {
271
+ if (ref.type === ValueType.error) {
272
+ return ref;
273
+ }
274
+ if (ref.type === ValueType.number) {
275
+ list.push(ref.value);
276
+ }
277
+ else if (ref.type !== ValueType.undefined ) {
278
+ console.info("RT", ref.type);
279
+ return ArgumentError();
280
+ }
281
+ }
282
+
283
+ const gcd = (x: number, y: number): number => {
284
+ if (x % y == 0) {
285
+ return y; // Base case
286
+ }
287
+ else {
288
+ return gcd(y, x % y); // Recusrsive case
289
+ }
290
+ };
291
+
292
+ if (list.length === 0) {
293
+ return { type: ValueType.number, value: 0 };
294
+ }
295
+
296
+ let lcm = list[0];
297
+
298
+ for (let i = 1; i < list.length; i++) {
299
+ lcm = (lcm * list[i]) / gcd(lcm, list[i]);
300
+ }
301
+
302
+ return {
303
+ type: ValueType.number,
304
+ value: lcm,
305
+ }
306
+
307
+ },
308
+ },
309
+
310
+ GCD: {
311
+ description: 'Finds the greatest common divisor of the arguments',
312
+ arguments: [{ boxed: true }],
313
+ fn: (...args: UnionValue[]): UnionValue => {
314
+ args = Utils.FlattenBoxed(args);
315
+ const list: number[] = [];
316
+
317
+ for (const ref of args as UnionValue[]) {
318
+ if (ref.type === ValueType.error) {
319
+ return ref;
320
+ }
321
+ if (ref.type === ValueType.number) {
322
+ list.push(ref.value);
323
+ }
324
+ else if (ref.type !== ValueType.undefined ) {
325
+ console.info("RT", ref.type);
326
+ return ArgumentError();
327
+ }
328
+ }
329
+
330
+ const gcd = (x: number, y: number): number => {
331
+ if (x % y == 0) {
332
+ return y; // Base case
333
+ }
334
+ else {
335
+ return gcd(y, x % y); // Recusrsive case
336
+ }
337
+ };
338
+
339
+ if (list.length === 0) {
340
+ return ValueError();
341
+ }
342
+
343
+ if (list.length === 1) {
344
+ return {
345
+ type: ValueType.number,
346
+ value: list[0],
347
+ };
348
+ }
349
+
350
+ let value = list[0];
351
+ for (let i = 1; i < list.length; i++) {
352
+ value = gcd(value, list[i]);
353
+ }
354
+
355
+ return {
356
+ type: ValueType.number,
357
+ value,
358
+ };
359
+
360
+
361
+ }
362
+ },
363
+
364
+ HarMean: {
365
+ description: 'Returns the harmonic mean of the arguments',
366
+ arguments: [{ boxed: true }],
367
+ fn: (...args: UnionValue[]): UnionValue => {
368
+ args = Utils.FlattenBoxed(args);
369
+ const result = { real: 0, imaginary: 0 };
370
+ let count = 0;
371
+
372
+ for (const ref of args as UnionValue[]) {
373
+ if (ref.type === ValueType.error) {
374
+ return ref;
375
+ }
376
+ if (ref.type === ValueType.number) {
377
+ result.real += (1/ref.value);
378
+ count++;
379
+ }
380
+ if (ref.type === ValueType.complex) {
381
+ const reciprocal = ComplexMath.Divide({real: 1, imaginary: 0}, ref.value);
382
+ result.real += reciprocal.real;
383
+ result.imaginary += reciprocal.imaginary;
384
+ count++;
385
+ }
386
+ }
387
+
388
+ if (result.imaginary) {
389
+ return { type: ValueType.complex, value: ComplexMath.Divide({real: count, imaginary: 0}, result), };
390
+ }
391
+
392
+ return { type: ValueType.number, value: count/result.real };
393
+
394
+ },
395
+ },
396
+
263
397
  Average: {
264
398
 
265
399
  description: 'Returns the arithmetic mean of all numeric arguments',
@@ -120,6 +120,51 @@ export const TextFunctionLibrary: FunctionMap = {
120
120
  },
121
121
  },
122
122
 
123
+ Len: {
124
+ arguments: [
125
+ { name: 'string', unroll: true },
126
+ ],
127
+ fn: (str: string): UnionValue => {
128
+ return {
129
+ type: ValueType.number,
130
+ value: str.toString().length,
131
+ }
132
+ },
133
+ },
134
+
135
+ Substitute: {
136
+ arguments: [
137
+ { name: 'text' },
138
+ { name: 'search' },
139
+ { name: 'replacement' },
140
+ { name: 'index' },
141
+ ],
142
+ fn: (text: string, search: string, replacement: string, index?: number): UnionValue => {
143
+ if (typeof index === 'number') {
144
+ if (index < 1) {
145
+ return ValueError();
146
+ }
147
+
148
+ let counter = 1;
149
+ return {
150
+ type: ValueType.string,
151
+ value: text.replaceAll(search, (...args) => {
152
+ console.info(args);
153
+ return (counter++) === index ? replacement : search;
154
+ }),
155
+ };
156
+
157
+ }
158
+ else {
159
+ return {
160
+ type: ValueType.string,
161
+ value: text.replaceAll(search, replacement),
162
+ };
163
+ }
164
+
165
+ },
166
+ },
167
+
123
168
  Left: {
124
169
  arguments: [
125
170
  { name: 'string' },
@@ -256,7 +301,7 @@ export const TextFunctionLibrary: FunctionMap = {
256
301
  },
257
302
  },
258
303
 
259
- /** canonical should be CONCAT; concatenate can be an alias */
304
+ /** canonical should be CONCAT; concatenate can be an alias */
260
305
  Concat: {
261
306
  description: 'Pastes strings together',
262
307
  fn: (...args: CellValue[]): UnionValue => {
@@ -266,6 +266,7 @@ export const Modulo = (a: UnionValue, b: UnionValue): UnionValue => {
266
266
  return { value: x % y, type: ValueType.number };
267
267
  };
268
268
 
269
+ /*
269
270
  export const Concatenate = (a: UnionValue, b: UnionValue): UnionValue => {
270
271
  if (a.type === ValueType.error) { return a; }
271
272
  if (b.type === ValueType.error) { return b; }
@@ -276,6 +277,7 @@ export const Concatenate = (a: UnionValue, b: UnionValue): UnionValue => {
276
277
  };
277
278
 
278
279
  };
280
+ */
279
281
 
280
282
  export const Equals = (a: UnionValue, b: UnionValue): UnionValue => {
281
283
  if (a.type === ValueType.error) { return a; }
@@ -430,7 +432,7 @@ export const UseComplex = () => {
430
432
 
431
433
  export const MapOperator = (operator: string) => {
432
434
  switch(operator) {
433
- case '&': return Concatenate;
435
+ // case '&': return Concatenate;
434
436
  case '+': return Add;
435
437
  case '-': return Subtract;
436
438
  case '*': return Multiply;
@@ -352,4 +352,8 @@ export const ParseWildcards = (text: string): string => {
352
352
 
353
353
  return result.join('');
354
354
 
355
- };
355
+ };
356
+
357
+ export const StringUnion = (value: string): UnionValue => ({ type: ValueType.string, value });
358
+ export const NumberUnion = (value: number): UnionValue => ({ type: ValueType.number, value });
359
+