Skip to content

Commit e89a287

Browse files
LucasLefevrerrahir
authored andcommitted
[REF] helpers: remove duplicated helper
closes #4653 Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent b8b9259 commit e89a287

File tree

4 files changed

+12
-22
lines changed

4 files changed

+12
-22
lines changed

src/formulas/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Token } from ".";
22
import { argTargeting } from "../functions/arguments";
33
import { functionRegistry } from "../functions/index";
4-
import { parseNumber, removeStringQuotes, unquote } from "../helpers";
4+
import { parseNumber, unquote } from "../helpers";
55
import { _t } from "../translation";
66
import { CompiledFormula, DEFAULT_LOCALE, FormulaToExecute } from "../types";
77
import { BadExpressionError, UnknownFunctionError } from "../types/errors";
@@ -279,7 +279,7 @@ function formulaArguments(tokens: Token[]) {
279279
dependencies.push(token.value);
280280
break;
281281
case "STRING":
282-
const value = removeStringQuotes(token.value);
282+
const value = unquote(token.value);
283283
literalValues.strings.push({ value });
284284
break;
285285
case "NUMBER": {

src/formulas/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseNumber, removeStringQuotes, unquote } from "../helpers/index";
1+
import { parseNumber, unquote } from "../helpers/index";
22
import { _t } from "../translation";
33
import { DEFAULT_LOCALE } from "../types";
44
import { BadExpressionError, CellErrorType } from "../types/errors";
@@ -150,7 +150,7 @@ function parseOperand(tokens: TokenList): AST {
150150
case "STRING":
151151
return {
152152
type: "STRING",
153-
value: removeStringQuotes(current.value),
153+
value: unquote(current.value),
154154
tokenStartIndex: current.tokenIndex,
155155
tokenEndIndex: current.tokenIndex,
156156
};

src/helpers/misc.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ import { Cloneable, DebouncedFunction, Style } from "./../types/misc";
88

99
const sanitizeSheetNameRegex = new RegExp(FORBIDDEN_SHEETNAME_CHARS_IN_EXCEL_REGEX, "g");
1010

11-
/**
12-
* Remove quotes from a quoted string
13-
* ```js
14-
* removeStringQuotes('"Hello"')
15-
* > 'Hello'
16-
* ```
17-
*/
18-
export function removeStringQuotes(str: string): string {
19-
if (str[0] === '"') {
20-
str = str.slice(1);
21-
}
22-
if (str[str.length - 1] === '"' && str[str.length - 2] !== "\\") {
23-
return str.slice(0, str.length - 1);
24-
}
25-
return str;
26-
}
27-
2811
function isCloneable<T extends Object>(obj: T | Cloneable<T>): obj is Cloneable<T> {
2912
return "clone" in obj && obj.clone instanceof Function;
3013
}
@@ -97,6 +80,13 @@ export function getUnquotedSheetName(sheetName: string): string {
9780
return unquote(sheetName, "'");
9881
}
9982

83+
/**
84+
* Remove quotes from a quoted string
85+
* ```js
86+
* unquote('"Hello"')
87+
* > 'Hello'
88+
* ```
89+
*/
10090
export function unquote(string: string, quoteChar: "'" | '"' = '"'): string {
10191
if (string.startsWith(quoteChar)) {
10292
string = string.slice(1);

tests/evaluation/parser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ describe("Converting AST to string", () => {
490490
test("Convert strings", () => {
491491
expect(astToFormula(parse('"R"'))).toBe('"R"');
492492
expect(astToFormula(parse('"R'))).toBe('"R"');
493-
expect(astToFormula(parse('"R\\"'))).toBe('"R\\""');
493+
expect(astToFormula(parse('"R\\"'))).toBe('"R\\"');
494494
expect(astToFormula(parse('CONCAT("R", "EM")'))).toBe('CONCAT("R","EM")');
495495
});
496496
test("Convert numbers", () => {

0 commit comments

Comments
 (0)