Skip to content

Commit c2b148c

Browse files
author
majid.akbaristerabadi
committed
Added "variableSpecialCharacters" property to support any character for variable name, Close #3
1 parent fdefd8f commit c2b148c

File tree

5 files changed

+68
-47
lines changed

5 files changed

+68
-47
lines changed

demo/index.js

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/index.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ let funcs = {
1414
throw new Error("The 'Substr' function accepts a string for the first parameter which is not a string!");
1515

1616
return str.substr(from);
17-
},
18-
majid: function () {
19-
return "majid";
20-
},
21-
test: function (x) {
22-
return x * 2;
2317
}
2418
};
2519

@@ -35,10 +29,6 @@ $(function () {
3529
{
3630
variableId: 2,
3731
name: 'Gender'
38-
},
39-
{
40-
variableId: 3,
41-
name: '1TEST'
4232
}
4333
]
4434
});

src/expression-builder-v2.js

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/expression-builder-v2.ts

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ interface ExpressionBuilderOption {
1818
expression?: string,
1919
variables?: Array<ExpressionBuilderVariable>,
2020
functions?: any,
21-
preventWrongInput?: boolean
21+
preventWrongInput?: boolean,
22+
variableSpecialCharacters?: string[]
2223
}
2324

2425
interface ExpressionBuilder {
@@ -41,7 +42,8 @@ jQuery.fn.extend({
4142

4243
interface ParserOption {
4344
funcs?: any,
44-
variables?: Array<ExpressionBuilderVariable>
45+
variables?: Array<ExpressionBuilderVariable>,
46+
variableSpecialCharacters?: string[]
4547
}
4648

4749
let parser = function (expression: string, options?: ParserOption) {
@@ -212,14 +214,16 @@ jQuery.fn.extend({
212214
return "(" + this.left.toString(parseVariables) + this.op + this.right.toString(parseVariables) + ")";
213215
}
214216
}
215-
217+
216218
function parse(str): GraphNode {
217219
function extractTokens(exp: string): Array<any> {
218220

221+
var languageCharsString = options.variableSpecialCharacters.map(c => `\\${c}`).join('');
222+
219223
//dynamically build my parsing regex:
220-
var tokenParser = new RegExp([
224+
var regExpStr = [
221225
//properties
222-
/\[[a-zA-Z0-9$_]*\]+/.source,
226+
`\[[a-zA-Z0-9$_${languageCharsString}]*\]+`,
223227

224228
//numbers
225229
/\d+(?:\.\d*)?|\.\d+/.source,
@@ -242,7 +246,8 @@ jQuery.fn.extend({
242246
//remaining (non-whitespace-)chars, just in case
243247
//has to be at the end
244248
/\S/.source
245-
].map(s => "(" + s + ")").join("|"), "g");
249+
].map(s => "(" + s + ")").join("|");
250+
var tokenParser = new RegExp(regExpStr, "g");
246251

247252
var _tokens: Array<any> = [];
248253

@@ -258,14 +263,14 @@ jQuery.fn.extend({
258263
str = str.replace(/"/g, '\'');
259264
t = new ValueNode(JSON.parse('"' + str + '"'));
260265
}
261-
else if (property)
262-
t = new PropertyNode(property);
263-
else if (prop)
264-
t = new PropertyNode(prop.substring(1, prop.length - 1), true);
265-
else if (token == ',')
266-
t = new CommaNode();
267-
else if (!op)
268-
throw new Error("unexpected token '" + token + "'");
266+
else if (property)
267+
t = new PropertyNode(property);
268+
else if (prop)
269+
t = new PropertyNode(prop.substring(1, prop.length - 1), true);
270+
else if (token == ',')
271+
t = new CommaNode();
272+
else if (!op)
273+
throw new Error("unexpected token '" + token + "'");
269274

270275
_tokens.push(t);
271276

@@ -367,7 +372,7 @@ jQuery.fn.extend({
367372

368373
var expTokens = extractTokens(str);
369374

370-
expTokens = handleNegativenumbers(expTokens);
375+
expTokens = handleNegativenumbers(expTokens);
371376

372377
return wrapParenteses(expTokens);
373378
}
@@ -422,7 +427,8 @@ jQuery.fn.extend({
422427
isPaste = false,
423428
parserOptions: ParserOption = {
424429
funcs: {},
425-
variables: []
430+
variables: [],
431+
variableSpecialCharacters: []
426432
};
427433

428434
//Initial for the first time
@@ -448,6 +454,9 @@ jQuery.fn.extend({
448454
options.functions = options.functions || [];
449455
expressionInput.data('funcs', options.functions);
450456

457+
options.variableSpecialCharacters = options.variableSpecialCharacters || [];
458+
expressionInput.data('variableSpecialCharacters', options.variableSpecialCharacters);
459+
451460
let parent = $("<div class='exp-container' exp-id='" + id + "'></div>");
452461

453462
expressionInput.parent().append(parent);
@@ -480,11 +489,15 @@ jQuery.fn.extend({
480489

481490
if (!options.functions)
482491
options.functions = expressionInput.data('funcs');
492+
493+
if (!options.variableSpecialCharacters)
494+
options.variableSpecialCharacters = expressionInput.data('variableSpecialCharacters');
483495
}
484496

485497
parserOptions = {
486498
funcs: options.functions,
487-
variables: options.variables
499+
variables: options.variables,
500+
variableSpecialCharacters: options.variableSpecialCharacters
488501
};
489502
}
490503

@@ -558,7 +571,7 @@ jQuery.fn.extend({
558571
validation();
559572
}
560573

561-
//if the expression is ready to accpet new entry (variable, date or string)
574+
//if the expression is ready to accept new entry (variable, date or string)
562575
function acceptNewEntry(lastChar) {
563576
if (lastText == '')
564577
return true;
@@ -885,6 +898,9 @@ jQuery.fn.extend({
885898
if (char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z')
886899
return true;
887900

901+
if (options.variableSpecialCharacters.indexOf(char) >= 0)
902+
return true;
903+
888904
return false;
889905
}
890906

@@ -1009,8 +1025,16 @@ jQuery.fn.extend({
10091025
//}
10101026

10111027
let selectedText = $(div).text();
1028+
let hasSpecialCharacter = false;
1029+
1030+
for (var i = 0; i < selectedText.length; i++) {
1031+
if (options.variableSpecialCharacters.indexOf(selectedText[i]) >= 0) {
1032+
hasSpecialCharacter = true;
1033+
break;
1034+
}
1035+
}
10121036

1013-
if (isNumber(selectedText[0])) {
1037+
if (isNumber(selectedText[0]) || hasSpecialCharacter) {
10141038
if (text[start - 1] != '[')
10151039
text = text.substr(0, start) + "[" + selectedText + "]" + tail.trim();
10161040
else

0 commit comments

Comments
 (0)