Skip to content

Commit f3d6c7d

Browse files
committed
function to validate ds2 variable names
Signed-off-by: David Weik <[email protected]>
1 parent 9242a17 commit f3d6c7d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<script src="./js/utility/get-URL-search-parameters.js"></script>
109109
<script src="./js/utility/get-model-variables.js"></script>
110110
<script src="./js/utility/delete-model-variable.js"></script>
111+
<script src="./js/utility/validate-ds2-variable-name.js"></script>
111112

112113
<!-- Import Object Builder -->
113114
<script src="./js/objects/add-text-objects.js"></script>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* A function that checks a string for being a valid variable name in DS2 -
3+
* @param {String} name - Potential name of the variable
4+
* @returns {Boolean} - True if a valid name, False if an invalid name
5+
*/
6+
function isValidDS2VariableName(name) {
7+
// Check the general pattern: starts with a letter or underscore, followed by letters, digits, or underscores
8+
const identifierPattern = /^[A-Za-z_][A-Za-z0-9_]*$/;
9+
if (!identifierPattern.test(name)) {
10+
return false;
11+
}
12+
13+
// Check the length of the pattern
14+
if (name.length > 255) {
15+
return false;
16+
}
17+
18+
// Reserved keywords (converted to lowercase for case-insensitive check)
19+
const reservedKeywords = new Set([
20+
"___kplist", "_all_", "_hostname_", "_new_", "_localnthreads_", "_localthreadid_", "_nthreads_",
21+
"_null_", "_rc_", "_rowset_", "_temporary_", "_threadid_", "abort", "and", "as", "asm",
22+
"bigint", "binary", "by", "call", "catalog", "char", "character", "commit", "continue",
23+
"data", "date", "dcl", "decimal", "declare", "delete", "descending", "dim", "do", "double",
24+
"drop", "ds2_options", "elif", "else", "end", "enddata", "endmodule", "endpackage",
25+
"endstage", "endtable", "endthread", "eq", "error", "escape", "float", "format", "fortran",
26+
"forward", "from", "function", "ge", "global", "goto", "group", "gt", "having", "identity",
27+
"if", "in", "indsname", "indsnum", "informat", "inline", "input", "int", "integer", "in_out",
28+
"keep", "label", "le", "leave", "like", "list", "lt", "merge", "method", "missing", "modify",
29+
"module", "national", "nchar", "ne", "ng", "nl", "not", "null", "numeric", "nvarchar", "ods",
30+
"of", "or", "order", "other", "otherwise", "output", "overwrite", "package", "partition",
31+
"precision", "private", "program", "put", "real", "remove", "rename", "replace", "require",
32+
"retain", "return", "returns", "rollback", "select", "set", "smallint", "sqlsub", "stage",
33+
"stop", "stored", "substr", "system", "table", "then", "this", "thread", "threads", "time",
34+
"timestamp", "tinyint", "to", "transaction", "t_udf", "tspl_options", "until", "update",
35+
"vararray", "varbinary", "varchar", "varlist", "varying", "when", "where", "while"
36+
]);
37+
return !reservedKeywords.has(name.toLowerCase());
38+
}

0 commit comments

Comments
 (0)