/
utils.js
63 lines (57 loc) · 1.7 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Check if provided expression is valid formula expression.
*
* @param {*} expression Expression to check.
* @returns {boolean}
*/
export function isFormulaExpression(expression) {
return typeof expression === 'string' && expression.length >= 2 && expression.charAt(0) === '=';
}
/**
* Check if provided formula expression is escaped.
*
* @param {*} expression Expression to check.
* @returns {boolean}
*/
export function isFormulaExpressionEscaped(expression) {
return typeof expression === 'string' && expression.charAt(0) === '\'' && expression.charAt(1) === '=';
}
/**
* Replace escaped formula expression into valid string.
*
* @param {string} expression Expression to process.
* @returns {string}
*/
export function unescapeFormulaExpression(expression) {
return isFormulaExpressionEscaped(expression) ? expression.substr(1) : expression;
}
/**
* Upper case formula expression.
*
* @param {string} expression Formula expression.
* @returns {string}
*/
export function toUpperCaseFormula(expression) {
const PATTERN = /(\\"|"(?:\\"|[^"])*"|(\+))|(\\'|'(?:\\'|[^'])*'|(\+))/g;
const strings = expression.match(PATTERN) || [];
let index = -1;
return expression.toUpperCase().replace(PATTERN, () => {
index += 1;
return strings[index];
});
}
/**
* Cell coordinates function factory.
*
* @param {string} axis An axis name (`row` or `column`) which default index will be applied to.
* @param {number} defaultIndex Default index.
* @returns {Function}
*/
export function cellCoordFactory(axis, defaultIndex) {
return function(cell) {
return {
row: axis === 'row' ? defaultIndex : cell.row,
column: axis === 'column' ? defaultIndex : cell.column,
};
};
}