diff --git a/src/formulas/compiler.ts b/src/formulas/compiler.ts index 43019f907e..b42b5ae2dc 100644 --- a/src/formulas/compiler.ts +++ b/src/formulas/compiler.ts @@ -1,7 +1,7 @@ import { Token } from "."; import { argTargeting } from "../functions/arguments"; import { functionRegistry } from "../functions/index"; -import { parseNumber, removeStringQuotes, unquote } from "../helpers"; +import { parseNumber, unquote } from "../helpers"; import { _t } from "../translation"; import { CompiledFormula, DEFAULT_LOCALE, FormulaToExecute } from "../types"; import { BadExpressionError, UnknownFunctionError } from "../types/errors"; @@ -279,7 +279,7 @@ function formulaArguments(tokens: Token[]) { dependencies.push(token.value); break; case "STRING": - const value = removeStringQuotes(token.value); + const value = unquote(token.value); literalValues.strings.push({ value }); break; case "NUMBER": { diff --git a/src/formulas/parser.ts b/src/formulas/parser.ts index 54eb7f1aea..059b649c9d 100644 --- a/src/formulas/parser.ts +++ b/src/formulas/parser.ts @@ -1,4 +1,4 @@ -import { parseNumber, removeStringQuotes, unquote } from "../helpers/index"; +import { parseNumber, unquote } from "../helpers/index"; import { _t } from "../translation"; import { DEFAULT_LOCALE } from "../types"; import { BadExpressionError, CellErrorType } from "../types/errors"; @@ -150,7 +150,7 @@ function parseOperand(tokens: TokenList): AST { case "STRING": return { type: "STRING", - value: removeStringQuotes(current.value), + value: unquote(current.value), tokenStartIndex: current.tokenIndex, tokenEndIndex: current.tokenIndex, }; diff --git a/src/helpers/misc.ts b/src/helpers/misc.ts index 5f4deb7dcf..67992f71a7 100644 --- a/src/helpers/misc.ts +++ b/src/helpers/misc.ts @@ -8,23 +8,6 @@ import { Cloneable, DebouncedFunction, Style } from "./../types/misc"; const sanitizeSheetNameRegex = new RegExp(FORBIDDEN_SHEETNAME_CHARS_IN_EXCEL_REGEX, "g"); -/** - * Remove quotes from a quoted string - * ```js - * removeStringQuotes('"Hello"') - * > 'Hello' - * ``` - */ -export function removeStringQuotes(str: string): string { - if (str[0] === '"') { - str = str.slice(1); - } - if (str[str.length - 1] === '"' && str[str.length - 2] !== "\\") { - return str.slice(0, str.length - 1); - } - return str; -} - function isCloneable(obj: T | Cloneable): obj is Cloneable { return "clone" in obj && obj.clone instanceof Function; } @@ -97,6 +80,13 @@ export function getUnquotedSheetName(sheetName: string): string { return unquote(sheetName, "'"); } +/** + * Remove quotes from a quoted string + * ```js + * unquote('"Hello"') + * > 'Hello' + * ``` + */ export function unquote(string: string, quoteChar: "'" | '"' = '"'): string { if (string.startsWith(quoteChar)) { string = string.slice(1); diff --git a/tests/evaluation/parser.test.ts b/tests/evaluation/parser.test.ts index 71f000624f..c116a89b26 100644 --- a/tests/evaluation/parser.test.ts +++ b/tests/evaluation/parser.test.ts @@ -490,7 +490,7 @@ describe("Converting AST to string", () => { test("Convert strings", () => { expect(astToFormula(parse('"R"'))).toBe('"R"'); expect(astToFormula(parse('"R'))).toBe('"R"'); - expect(astToFormula(parse('"R\\"'))).toBe('"R\\""'); + expect(astToFormula(parse('"R\\"'))).toBe('"R\\"'); expect(astToFormula(parse('CONCAT("R", "EM")'))).toBe('CONCAT("R","EM")'); }); test("Convert numbers", () => {