Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/formulas/compiler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/formulas/parser.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
};
Expand Down
24 changes: 7 additions & 17 deletions src/helpers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Object>(obj: T | Cloneable<T>): obj is Cloneable<T> {
return "clone" in obj && obj.clone instanceof Function;
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/evaluation/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down