Skip to content

Commit d8ab451

Browse files
committed
[REF] helpers: remove duplicated helper
1 parent 9094cdc commit d8ab451

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

src/formulas/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Token } from ".";
22
import { functionRegistry } from "../functions/index";
3-
import { concat, parseNumber, removeStringQuotes } from "../helpers";
3+
import { concat, parseNumber, unquote } from "../helpers";
44
import { _t } from "../translation";
55
import { CompiledFormula, DEFAULT_LOCALE, FormulaToExecute } from "../types";
66
import { BadExpressionError, UnknownFunctionError } from "../types/errors";
@@ -217,7 +217,7 @@ function compilationCacheKey(
217217
tokens.map((token) => {
218218
switch (token.type) {
219219
case "STRING":
220-
const value = removeStringQuotes(token.value);
220+
const value = unquote(token.value);
221221
return `|S${constantValues.strings.indexOf(value)}|`;
222222
case "NUMBER":
223223
return `|N${constantValues.numbers.indexOf(parseNumber(token.value, DEFAULT_LOCALE))}|`;
@@ -252,7 +252,7 @@ function formulaArguments(tokens: Token[]) {
252252
dependencies.push(token.value);
253253
break;
254254
case "STRING":
255-
const value = removeStringQuotes(token.value);
255+
const value = unquote(token.value);
256256
if (!constantValues.strings.includes(value)) {
257257
constantValues.strings.push(value);
258258
}

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 } from "../helpers/index";
1+
import { parseNumber, unquote } from "../helpers/index";
22
import { _t } from "../translation";
33
import { DEFAULT_LOCALE } from "../types";
44
import { BadExpressionError, InvalidReferenceError } from "../types/errors";
@@ -110,7 +110,7 @@ function parseOperand(tokens: Token[]): AST {
110110
case "NUMBER":
111111
return { type: "NUMBER", value: parseNumber(current.value, DEFAULT_LOCALE) };
112112
case "STRING":
113-
return { type: "STRING", value: removeStringQuotes(current.value) };
113+
return { type: "STRING", value: unquote(current.value) };
114114
case "INVALID_REFERENCE":
115115
throw new InvalidReferenceError();
116116
case "REFERENCE":

src/helpers/misc.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@ import { ConsecutiveIndexes, Lazy, UID } from "../types";
66
import { SearchOptions } from "../types/find_and_replace";
77
import { Cloneable, DebouncedFunction } from "./../types/misc";
88

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

73+
/**
74+
* Remove quotes from a quoted string
75+
* ```js
76+
* unquote('"Hello"')
77+
* > 'Hello'
78+
* ```
79+
*/
9080
export function unquote(string: string, quoteChar: "'" | '"' = '"'): string {
9181
if (string.startsWith(quoteChar)) {
9282
string = string.slice(1);

0 commit comments

Comments
 (0)