@trebco/treb 29.7.5 → 29.8.3
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.
- package/dist/treb-spreadsheet-light.mjs +10 -10
- package/dist/treb-spreadsheet.mjs +10 -10
- package/dist/treb.d.ts +1 -1
- package/package.json +1 -1
- package/treb-calculator/src/calculator.ts +2 -1
- package/treb-calculator/src/functions/base-functions.ts +85 -0
- package/treb-calculator/src/functions/regex-functions.ts +205 -0
- package/treb-grid/src/types/grid.ts +61 -2
- package/tsproject.json +1 -1
package/dist/treb.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -43,6 +43,7 @@ import { InformationFunctionLibrary } from './functions/information-functions';
|
|
|
43
43
|
import { StatisticsFunctionLibrary, StatisticsFunctionAliases } from './functions/statistics-functions';
|
|
44
44
|
import { ComplexFunctionLibrary } from './functions/complex-functions';
|
|
45
45
|
import { MatrixFunctionLibrary } from './functions/matrix-functions';
|
|
46
|
+
import { RegexFunctionLibrary } from './functions/regex-functions';
|
|
46
47
|
|
|
47
48
|
import { Variance } from './functions/statistics-functions';
|
|
48
49
|
|
|
@@ -222,7 +223,7 @@ export class Calculator extends Graph {
|
|
|
222
223
|
InformationFunctionLibrary, // etc
|
|
223
224
|
ComplexFunctionLibrary,
|
|
224
225
|
MatrixFunctionLibrary,
|
|
225
|
-
|
|
226
|
+
RegexFunctionLibrary,
|
|
226
227
|
);
|
|
227
228
|
|
|
228
229
|
// aliases
|
|
@@ -923,6 +923,91 @@ export const BaseFunctionLibrary: FunctionMap = {
|
|
|
923
923
|
|
|
924
924
|
},
|
|
925
925
|
|
|
926
|
+
/**
|
|
927
|
+
*
|
|
928
|
+
*/
|
|
929
|
+
Filter: {
|
|
930
|
+
description: "Filter an array using a second array.",
|
|
931
|
+
arguments: [
|
|
932
|
+
{ name: 'source', description: 'Source array' },
|
|
933
|
+
{ name: 'filter', description: 'Filter array' },
|
|
934
|
+
// if_empty
|
|
935
|
+
],
|
|
936
|
+
|
|
937
|
+
fn: (source: CellValue|CellValue[][], filter: CellValue|CellValue[][]) => {
|
|
938
|
+
|
|
939
|
+
if (typeof source === 'undefined' || typeof filter === 'undefined') {
|
|
940
|
+
return ArgumentError();
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
if (!Array.isArray(source)) {
|
|
944
|
+
source = [[source]];
|
|
945
|
+
}
|
|
946
|
+
if (!Array.isArray(filter)) {
|
|
947
|
+
filter = [[filter]];
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
const source_cols = source.length;
|
|
951
|
+
const source_rows = source[0].length;
|
|
952
|
+
|
|
953
|
+
const filter_cols = filter.length;
|
|
954
|
+
const filter_rows = filter[0].length;
|
|
955
|
+
|
|
956
|
+
// prefer rows
|
|
957
|
+
|
|
958
|
+
if (source_rows === filter_rows) {
|
|
959
|
+
|
|
960
|
+
const result: UnionValue[][] = [];
|
|
961
|
+
|
|
962
|
+
for (let i = 0; i < source_cols; i++) {
|
|
963
|
+
result.push([]);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
for (const [index, entry] of filter[0].entries()) {
|
|
967
|
+
|
|
968
|
+
// FIXME: don't allow strings? errors? (...)
|
|
969
|
+
|
|
970
|
+
if (entry) {
|
|
971
|
+
for (let i = 0; i < source_cols; i++) {
|
|
972
|
+
result[i].push(Box(source[i][index]));
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
return {
|
|
979
|
+
type: ValueType.array,
|
|
980
|
+
value: result,
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
}
|
|
984
|
+
else if (source_cols === filter_cols) {
|
|
985
|
+
|
|
986
|
+
const result: UnionValue[][] = [];
|
|
987
|
+
|
|
988
|
+
for (const [index, [entry]] of filter.entries()) {
|
|
989
|
+
|
|
990
|
+
// FIXME: don't allow strings? errors? (...)
|
|
991
|
+
|
|
992
|
+
if (entry) {
|
|
993
|
+
result.push(source[index].map(value => Box(value)));
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
return {
|
|
999
|
+
type: ValueType.array,
|
|
1000
|
+
value: result,
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
return ArgumentError();
|
|
1006
|
+
|
|
1007
|
+
},
|
|
1008
|
+
|
|
1009
|
+
},
|
|
1010
|
+
|
|
926
1011
|
/**
|
|
927
1012
|
* sort arguments, but ensure we return empty strings to
|
|
928
1013
|
* fill up the result array
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of TREB.
|
|
3
|
+
*
|
|
4
|
+
* TREB is free software: you can redistribute it and/or modify it under the
|
|
5
|
+
* terms of the GNU General Public License as published by the Free Software
|
|
6
|
+
* Foundation, either version 3 of the License, or (at your option) any
|
|
7
|
+
* later version.
|
|
8
|
+
*
|
|
9
|
+
* TREB is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
10
|
+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
11
|
+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
12
|
+
* details.
|
|
13
|
+
*
|
|
14
|
+
* You should have received a copy of the GNU General Public License along
|
|
15
|
+
* with TREB. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*
|
|
17
|
+
* Copyright 2022-2024 trebco, llc.
|
|
18
|
+
* info@treb.app
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { FunctionMap } from '../descriptors';
|
|
23
|
+
import { ValueType } from 'treb-base-types';
|
|
24
|
+
import { ArgumentError } from '../function-error';
|
|
25
|
+
|
|
26
|
+
export const RegexFunctionLibrary: FunctionMap = {
|
|
27
|
+
|
|
28
|
+
RegexExtract: {
|
|
29
|
+
description: 'Extract text using a regular expression',
|
|
30
|
+
arguments: [
|
|
31
|
+
{
|
|
32
|
+
name: 'text',
|
|
33
|
+
unroll: true,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'pattern',
|
|
37
|
+
unroll: true,
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'return mode',
|
|
41
|
+
unroll: true,
|
|
42
|
+
default: 0,
|
|
43
|
+
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'case insensitive',
|
|
47
|
+
unroll: true,
|
|
48
|
+
default: false,
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
fn: (text: string, pattern: string, return_mode = 0, icase = false) => {
|
|
53
|
+
|
|
54
|
+
const args: string[] = [];
|
|
55
|
+
if (icase) {
|
|
56
|
+
args.push('i');
|
|
57
|
+
}
|
|
58
|
+
if (return_mode === 1) {
|
|
59
|
+
args.push('g');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const rex = new RegExp(pattern, args.length ? args.join('') : undefined);
|
|
63
|
+
|
|
64
|
+
switch (return_mode) {
|
|
65
|
+
case 0:
|
|
66
|
+
{
|
|
67
|
+
const result = text.match(rex);
|
|
68
|
+
return {
|
|
69
|
+
type: ValueType.string,
|
|
70
|
+
value: (result === null) ? '' : result[0] ?? '',
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
case 1:
|
|
75
|
+
{
|
|
76
|
+
const result = Array.from(text.matchAll(rex));
|
|
77
|
+
console.info({result});
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
type: ValueType.array,
|
|
81
|
+
value: [result.map(entry => ({
|
|
82
|
+
type: ValueType.string,
|
|
83
|
+
value: entry[0] || '',
|
|
84
|
+
}))],
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
case 2:
|
|
88
|
+
{
|
|
89
|
+
const result = text.match(rex);
|
|
90
|
+
if (result === null) {
|
|
91
|
+
return {
|
|
92
|
+
type: ValueType.string,
|
|
93
|
+
value: '',
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const arr = Array.from(result).slice(1);
|
|
98
|
+
return {
|
|
99
|
+
type: ValueType.array,
|
|
100
|
+
value: [
|
|
101
|
+
arr.map(value => ({
|
|
102
|
+
type: ValueType.string,
|
|
103
|
+
value,
|
|
104
|
+
}))
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
default:
|
|
110
|
+
return ArgumentError();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
RegexReplace: {
|
|
117
|
+
arguments: [
|
|
118
|
+
{
|
|
119
|
+
name: 'text',
|
|
120
|
+
unroll: true,
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'pattern',
|
|
124
|
+
unroll: true,
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'replacement',
|
|
128
|
+
unroll: true,
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: 'occurrence',
|
|
132
|
+
unroll: true,
|
|
133
|
+
default: 0,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: 'case insensitive',
|
|
137
|
+
unroll: true,
|
|
138
|
+
default: false,
|
|
139
|
+
}
|
|
140
|
+
],
|
|
141
|
+
description: 'Replace text in a string using a regex',
|
|
142
|
+
fn: (text: string, pattern: string, replacement: string, occurrence = 0, icase = false) => {
|
|
143
|
+
|
|
144
|
+
const args: string[] = ['g'];
|
|
145
|
+
if (icase) {
|
|
146
|
+
args.push('i');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const rex = new RegExp(pattern, args.length ? args.join('') : undefined);
|
|
150
|
+
|
|
151
|
+
if (occurrence === 0) {
|
|
152
|
+
return {
|
|
153
|
+
type: ValueType.string,
|
|
154
|
+
value: (text as any).replaceAll(rex, replacement), // huh?
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (occurrence < 0) {
|
|
159
|
+
const count = Array.from(text.matchAll(rex)).length;
|
|
160
|
+
occurrence += (count + 1);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const value = text.replace(rex, (match) => {
|
|
164
|
+
if (--occurrence === 0) {
|
|
165
|
+
return replacement;
|
|
166
|
+
}
|
|
167
|
+
return match;
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
type: ValueType.string,
|
|
172
|
+
value,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
RegexTest: {
|
|
179
|
+
arguments: [
|
|
180
|
+
{
|
|
181
|
+
name: 'text',
|
|
182
|
+
unroll: true,
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
name: 'pattern',
|
|
186
|
+
unroll: true,
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
name: 'case insensitive',
|
|
190
|
+
unroll: true,
|
|
191
|
+
default: false,
|
|
192
|
+
|
|
193
|
+
}
|
|
194
|
+
],
|
|
195
|
+
description: 'Match text against a regular expression',
|
|
196
|
+
fn: (text: string, pattern: string, icase = false) => {
|
|
197
|
+
const rex = new RegExp(pattern, icase ? 'i' : undefined);
|
|
198
|
+
return {
|
|
199
|
+
type: ValueType.boolean,
|
|
200
|
+
value: rex.test(text),
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
};
|
|
@@ -6885,6 +6885,27 @@ export class Grid extends GridBase {
|
|
|
6885
6885
|
else {
|
|
6886
6886
|
|
|
6887
6887
|
const area = this.active_sheet.RealArea(this.primary_selection.area);
|
|
6888
|
+
|
|
6889
|
+
const column_width: number[] = [];
|
|
6890
|
+
const row_height: number[] = [];
|
|
6891
|
+
|
|
6892
|
+
if (this.primary_selection.area.entire_column) {
|
|
6893
|
+
for (let c = area.start.column; c <= area.end.column; c++) {
|
|
6894
|
+
const width = this.active_sheet.GetColumnWidth(c);
|
|
6895
|
+
if (width !== this.active_sheet.default_column_width) {
|
|
6896
|
+
column_width[c - area.start.column] = width;
|
|
6897
|
+
}
|
|
6898
|
+
}
|
|
6899
|
+
}
|
|
6900
|
+
if (this.primary_selection.area.entire_row) {
|
|
6901
|
+
for (let r = area.start.row; r <= area.end.row; r++) {
|
|
6902
|
+
const height = this.active_sheet.GetRowHeight(r);
|
|
6903
|
+
if (height !== this.active_sheet.default_row_height) {
|
|
6904
|
+
row_height[r - area.start.row] = height;
|
|
6905
|
+
}
|
|
6906
|
+
}
|
|
6907
|
+
}
|
|
6908
|
+
|
|
6888
6909
|
const columns = area.columns;
|
|
6889
6910
|
const rows = area.rows;
|
|
6890
6911
|
|
|
@@ -6961,7 +6982,12 @@ export class Grid extends GridBase {
|
|
|
6961
6982
|
if (event.clipboardData) {
|
|
6962
6983
|
event.clipboardData.clearData();
|
|
6963
6984
|
event.clipboardData.setData('text/plain', tsv);
|
|
6964
|
-
event.clipboardData.setData('text/x-treb', JSON.stringify({
|
|
6985
|
+
event.clipboardData.setData('text/x-treb', JSON.stringify({
|
|
6986
|
+
source: area,
|
|
6987
|
+
data: treb_data,
|
|
6988
|
+
column_width,
|
|
6989
|
+
row_height,
|
|
6990
|
+
}));
|
|
6965
6991
|
}
|
|
6966
6992
|
}
|
|
6967
6993
|
|
|
@@ -7114,7 +7140,14 @@ export class Grid extends GridBase {
|
|
|
7114
7140
|
if (treb_data) {
|
|
7115
7141
|
|
|
7116
7142
|
try {
|
|
7117
|
-
|
|
7143
|
+
|
|
7144
|
+
const object_data: {
|
|
7145
|
+
source: Area,
|
|
7146
|
+
data: ClipboardCellData[],
|
|
7147
|
+
column_width?: number[],
|
|
7148
|
+
row_height?: number[],
|
|
7149
|
+
} = JSON.parse(treb_data);
|
|
7150
|
+
|
|
7118
7151
|
const source_area = new Area(object_data.source.start, object_data.source.end);
|
|
7119
7152
|
|
|
7120
7153
|
// recycle...
|
|
@@ -7220,6 +7253,32 @@ export class Grid extends GridBase {
|
|
|
7220
7253
|
|
|
7221
7254
|
});
|
|
7222
7255
|
|
|
7256
|
+
if (object_data.column_width?.length) {
|
|
7257
|
+
for (const [index, width] of object_data.column_width.entries()) {
|
|
7258
|
+
if (typeof width === 'number') {
|
|
7259
|
+
const column = index + paste_area.start.column;
|
|
7260
|
+
commands.push({
|
|
7261
|
+
key: CommandKey.ResizeColumns,
|
|
7262
|
+
column,
|
|
7263
|
+
width,
|
|
7264
|
+
});
|
|
7265
|
+
}
|
|
7266
|
+
}
|
|
7267
|
+
}
|
|
7268
|
+
|
|
7269
|
+
if (object_data.row_height?.length) {
|
|
7270
|
+
for (const [index, height] of object_data.row_height.entries()) {
|
|
7271
|
+
if (typeof height === 'number') {
|
|
7272
|
+
const row = index + paste_area.start.row;
|
|
7273
|
+
commands.push({
|
|
7274
|
+
key: CommandKey.ResizeRows,
|
|
7275
|
+
row,
|
|
7276
|
+
height,
|
|
7277
|
+
});
|
|
7278
|
+
}
|
|
7279
|
+
}
|
|
7280
|
+
}
|
|
7281
|
+
|
|
7223
7282
|
}
|
|
7224
7283
|
|
|
7225
7284
|
}
|