-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathTextPlugin.ts
More file actions
446 lines (402 loc) · 14.4 KB
/
Copy pathTextPlugin.ts
File metadata and controls
446 lines (402 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {CellError, ErrorType} from '../../Cell'
import {ErrorMessage} from '../../error-message'
import {Maybe} from '../../Maybe'
import {ProcedureAst} from '../../parser'
import {InterpreterState} from '../InterpreterState'
import {SimpleRangeValue} from '../../SimpleRangeValue'
import {ExtendedNumber, InterpreterValue, isExtendedNumber, RawScalarValue, InternalScalarValue} from '../InterpreterValue'
import {FunctionArgumentType, FunctionPlugin, FunctionPluginTypecheck, ImplementedFunctions} from './FunctionPlugin'
/**
* Interpreter plugin containing text-specific functions
*/
export class TextPlugin extends FunctionPlugin implements FunctionPluginTypecheck<TextPlugin> {
public static implementedFunctions: ImplementedFunctions = {
'CONCATENATE': {
method: 'concatenate',
parameters: [
{argumentType: FunctionArgumentType.STRING}
],
repeatLastArgs: 1,
expandRanges: true,
},
'EXACT': {
method: 'exact',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.STRING}
]
},
'SPLIT': {
method: 'split',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER},
]
},
'LEN': {
method: 'len',
parameters: [
{argumentType: FunctionArgumentType.STRING}
]
},
'LOWER': {
method: 'lower',
parameters: [
{argumentType: FunctionArgumentType.STRING}
]
},
'MID': {
method: 'mid',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER},
{argumentType: FunctionArgumentType.NUMBER},
]
},
'TRIM': {
method: 'trim',
parameters: [
{argumentType: FunctionArgumentType.STRING}
]
},
'T': {
method: 't',
parameters: [
{argumentType: FunctionArgumentType.SCALAR}
]
},
'N': {
method: 'n',
parameters: [
{argumentType: FunctionArgumentType.ANY}
]
},
'PROPER': {
method: 'proper',
parameters: [
{argumentType: FunctionArgumentType.STRING}
]
},
'CLEAN': {
method: 'clean',
parameters: [
{argumentType: FunctionArgumentType.STRING}
]
},
'REPT': {
method: 'rept',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER},
]
},
'RIGHT': {
method: 'right',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER, defaultValue: 1},
]
},
'LEFT': {
method: 'left',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER, defaultValue: 1},
]
},
'REPLACE': {
method: 'replace',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER},
{argumentType: FunctionArgumentType.NUMBER},
{argumentType: FunctionArgumentType.STRING}
]
},
'SEARCH': {
method: 'search',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER, defaultValue: 1},
]
},
'SUBSTITUTE': {
method: 'substitute',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER, optionalArg: true}
]
},
'FIND': {
method: 'find',
parameters: [
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.STRING},
{argumentType: FunctionArgumentType.NUMBER, defaultValue: 1},
]
},
'UPPER': {
method: 'upper',
parameters: [
{argumentType: FunctionArgumentType.STRING}
]
},
'VALUE': {
method: 'value',
parameters: [
{argumentType: FunctionArgumentType.SCALAR}
]
},
}
/**
* Corresponds to CONCATENATE(value1, [value2, ...])
*
* Concatenates provided arguments to one string.
*
* @param {ProcedureAst} ast - The procedure AST node
* @param {InterpreterState} state - The interpreter state
*/
public concatenate(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('CONCATENATE'), (...args) => {
return ''.concat(...args)
})
}
/**
* Corresponds to SPLIT(string, index)
*
* Splits provided string using space separator and returns chunk at zero-based position specified by second argument
*
* @param {ProcedureAst} ast - The procedure AST node
* @param {InterpreterState} state - The interpreter state
*/
public split(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('SPLIT'), (stringToSplit: string, indexToUse: number) => {
const splittedString = stringToSplit.split(' ')
if (indexToUse >= splittedString.length || indexToUse < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.IndexBounds)
}
return splittedString[indexToUse]
})
}
public len(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('LEN'), (arg: string) => {
return arg.length
})
}
public lower(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('LOWER'), (arg: string) => {
return arg.toLowerCase()
})
}
public trim(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('TRIM'), (arg: string) => {
return arg
.replace(/^ +/g, '')
.replace(/ +$/g, '')
.replace(/ +/g, ' ')
})
}
public proper(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('PROPER'), (arg: string) => {
return arg.replace(/\p{L}+/gu, word => word.charAt(0).toUpperCase() + word.substring(1).toLowerCase())
})
}
public clean(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('CLEAN'), (arg: string) => {
// eslint-disable-next-line no-control-regex
return arg.replace(/[\u0000-\u001F]/g, '')
})
}
public exact(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('EXACT'), (left: string, right: string) => {
return left === right
})
}
public rept(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('REPT'), (text: string, count: number) => {
if (count < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeCount)
}
return text.repeat(count)
})
}
public right(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('RIGHT'), (text: string, length: number) => {
if (length < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength)
} else if (length === 0) {
return ''
}
return text.slice(-length)
})
}
public left(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('LEFT'), (text: string, length: number) => {
if (length < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength)
}
return text.slice(0, length)
})
}
public mid(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('MID'), (text: string, startPosition: number, numberOfChars: number) => {
if (startPosition < 1) {
return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne)
}
if (numberOfChars < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength)
}
return text.substring(startPosition - 1, startPosition + numberOfChars - 1)
})
}
public replace(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('REPLACE'), (text: string, startPosition: number, numberOfChars: number, newText: string) => {
if (startPosition < 1) {
return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne)
}
if (numberOfChars < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength)
}
return text.substring(0, startPosition - 1) + newText + text.substring(startPosition + numberOfChars - 1)
})
}
public search(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('SEARCH'), (pattern: string, text: string, startIndex: number) => {
if (startIndex < 1 || startIndex > text.length) {
return new CellError(ErrorType.VALUE, ErrorMessage.LengthBounds)
}
const normalizedPattern = pattern.toLowerCase()
const normalizedText = text.substring(startIndex - 1).toLowerCase()
const index = this.arithmeticHelper.requiresRegex(normalizedPattern)
? this.arithmeticHelper.searchString(normalizedPattern, normalizedText)
: normalizedText.indexOf(normalizedPattern)
return index > -1 ? index + startIndex : new CellError(ErrorType.VALUE, ErrorMessage.PatternNotFound)
})
}
public substitute(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('SUBSTITUTE'), (text: string, searchString: string, replacementString: string, occurrenceNum: number | undefined) => {
const escapedSearchString = this.escapeRegExpSpecialCharacters(searchString)
const searchRegExp = new RegExp(escapedSearchString, 'g')
if (occurrenceNum === undefined) {
return text.replace(searchRegExp, replacementString)
}
if (occurrenceNum < 1) {
return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne)
}
let match: RegExpExecArray | null
let i = 0
while ((match = searchRegExp.exec(text)) !== null) {
if (occurrenceNum === ++i) {
return text.substring(0, match.index) + replacementString + text.substring(searchRegExp.lastIndex)
}
}
return text
})
}
public find(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('FIND'), (pattern, text: string, startIndex: number) => {
if (startIndex < 1 || startIndex > text.length) {
return new CellError(ErrorType.VALUE, ErrorMessage.IndexBounds)
}
const shiftedText = text.substring(startIndex - 1)
const index = shiftedText.indexOf(pattern) + startIndex
return index > 0 ? index : new CellError(ErrorType.VALUE, ErrorMessage.PatternNotFound)
})
}
public t(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('T'), (arg: RawScalarValue) => {
if (arg instanceof CellError) {
return arg
}
return typeof arg === 'string' ? arg : ''
})
}
/**
* Corresponds to N(value)
*
* Converts a value to a number according to Excel specification:
* - Numbers return themselves
* - Dates return their serial number (stored as numbers internally)
* - TRUE returns 1, FALSE returns 0
* - Error values propagate
* - Anything else (text, empty) returns 0
* - For ranges, uses the first cell value
*/
public n(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('N'), (arg: InternalScalarValue | SimpleRangeValue) => {
const value = arg instanceof SimpleRangeValue ? arg.data[0]?.[0] : arg
if (value instanceof CellError) {
return value
}
if (typeof value === 'number') {
return value
}
if (typeof value === 'boolean') {
return value ? 1 : 0
}
return 0
})
}
public upper(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('UPPER'), (arg: string) => {
return arg.toUpperCase()
})
}
/**
* Corresponds to VALUE(text)
*
* Converts a text string that represents a number to a number.
*
* @param {ProcedureAst} ast - The procedure AST node
* @param {InterpreterState} state - The interpreter state
*/
public value(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('VALUE'), (arg: RawScalarValue): ExtendedNumber | CellError => {
if (arg instanceof CellError) {
return arg
}
if (isExtendedNumber(arg)) {
return arg
}
if (typeof arg !== 'string') {
return new CellError(ErrorType.VALUE, ErrorMessage.NumberCoercion)
}
const trimmedArg = arg.trim()
const parenthesesMatch = /^\(([^()]+)\)$/.exec(trimmedArg)
if (parenthesesMatch) {
const innerValue = this.parseStringToNumber(parenthesesMatch[1])
if (innerValue !== undefined) {
return -innerValue
}
}
const parsedValue = this.parseStringToNumber(trimmedArg)
if (parsedValue !== undefined) {
return parsedValue
}
return new CellError(ErrorType.VALUE, ErrorMessage.NumberCoercion)
})
}
/**
* Parses a string to a numeric value, handling whitespace trimming and empty string validation.
*
* @param {string} input - The string to parse
* @returns {Maybe<ExtendedNumber>} The parsed number or undefined if parsing fails or input is empty
*/
private parseStringToNumber(input: string): Maybe<ExtendedNumber> {
const trimmedInput = input.trim()
if (trimmedInput === '') {
return undefined
}
return this.arithmeticHelper.coerceToMaybeNumber(trimmedInput)
}
private escapeRegExpSpecialCharacters(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
}