|
5 | 5 |
|
6 | 6 | import {CellError, ErrorType} from '../../Cell' |
7 | 7 | import {ErrorMessage} from '../../error-message' |
| 8 | +import { Maybe } from '../../Maybe' |
8 | 9 | import {ProcedureAst} from '../../parser' |
9 | 10 | import {InterpreterState} from '../InterpreterState' |
10 | 11 | import {SimpleRangeValue} from '../../SimpleRangeValue' |
@@ -400,39 +401,39 @@ export class TextPlugin extends FunctionPlugin implements FunctionPluginTypechec |
400 | 401 | return arg |
401 | 402 | } |
402 | 403 |
|
403 | | - if (typeof arg === 'boolean') { |
| 404 | + if (typeof arg !== 'string') { |
404 | 405 | return new CellError(ErrorType.VALUE, ErrorMessage.NumberCoercion) |
405 | 406 | } |
406 | 407 |
|
407 | | - if (typeof arg === 'string') { |
408 | | - if (arg === '') { |
409 | | - return new CellError(ErrorType.VALUE, ErrorMessage.NumberCoercion) |
410 | | - } |
411 | | - |
412 | | - const trimmedArg = arg.trim() |
| 408 | + const trimmedArg = arg.trim() |
413 | 409 |
|
414 | | - // Try parsing parentheses notation for negative numbers: "(123)" -> -123 |
415 | | - const parenthesesMatch = /^\(([^()]+)\)$/.exec(trimmedArg) |
416 | | - if (parenthesesMatch) { |
417 | | - const innerValue = this.arithmeticHelper.parseStringToNumber(parenthesesMatch[1]) |
418 | | - if (innerValue !== undefined) { |
419 | | - return -innerValue |
420 | | - } |
421 | | - } |
422 | | - |
423 | | - // Try standard parsing |
424 | | - const parsedValue = this.arithmeticHelper.parseStringToNumber(trimmedArg) |
425 | | - if (parsedValue !== undefined) { |
426 | | - return parsedValue |
| 410 | + const parenthesesMatch = /^\(([^()]+)\)$/.exec(trimmedArg) |
| 411 | + if (parenthesesMatch) { |
| 412 | + const innerValue = this.parseStringToNumber(parenthesesMatch[1]) |
| 413 | + if (innerValue !== undefined) { |
| 414 | + return -innerValue |
427 | 415 | } |
| 416 | + } |
428 | 417 |
|
429 | | - return new CellError(ErrorType.VALUE, ErrorMessage.NumberCoercion) |
| 418 | + const parsedValue = this.parseStringToNumber(trimmedArg) |
| 419 | + if (parsedValue !== undefined) { |
| 420 | + return parsedValue |
430 | 421 | } |
431 | 422 |
|
432 | 423 | return new CellError(ErrorType.VALUE, ErrorMessage.NumberCoercion) |
433 | 424 | }) |
434 | 425 | } |
435 | 426 |
|
| 427 | + private parseStringToNumber(input: string): Maybe<ExtendedNumber> { |
| 428 | + const trimmedInput = input.trim() |
| 429 | + |
| 430 | + if (trimmedInput === '') { |
| 431 | + return undefined |
| 432 | + } |
| 433 | + |
| 434 | + return this.arithmeticHelper.coerceToMaybeNumber(trimmedInput) |
| 435 | + } |
| 436 | + |
436 | 437 | private escapeRegExpSpecialCharacters(text: string): string { |
437 | 438 | return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
438 | 439 | } |
|
0 commit comments