@trebco/treb 29.7.5 → 29.8.1
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.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
|
|
@@ -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
|
+
};
|