etherpad-lite/src/node/hooks/express/isValidJSONPName.js

84 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-03-23 11:17:39 +00:00
const RESERVED_WORDS = [
'abstract',
'arguments',
'await',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'double',
'else',
'enum',
'eval',
'export',
'extends',
'false',
'final',
'finally',
'float',
'for',
'function',
'goto',
'if',
'implements',
'import',
'in',
'instanceof',
'int',
'interface',
'let',
'long',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'true',
'try',
'typeof',
'var',
'void',
'volatile',
'while',
'with',
2020-11-23 13:24:19 -05:00
'yield',
2018-03-23 11:17:39 +00:00
];
const regex = /^[a-zA-Z_$][0-9a-zA-Z_$]*(?:\[(?:".+"|\'.+\'|\d+)\])*?$/;
2020-11-23 13:24:19 -05:00
module.exports.check = function (inputStr) {
let isValid = true;
inputStr.split('.').forEach((part) => {
2018-03-23 11:17:39 +00:00
if (!regex.test(part)) {
isValid = false;
}
if (RESERVED_WORDS.indexOf(part) !== -1) {
isValid = false;
}
});
return isValid;
2020-11-23 13:24:19 -05:00
};