/** * Rules for Enum: * starts with at least one ASCII character * followed by any ASCII character or digits or underscore. * @param {string} enumName String as candidate for Enum name * @param {function} okFn a reference to a unary function, which is called with enumName * in valid case * @param {function} failFn a reference to a unary function, which is called with enumName * in invalid case * */ function testEnumLexicalOk(enumName, okFn, failFn) { // More restrictive than a valid Identifier: Underscore is not allowed at begin // ^ and $ match begin and end of string const VALID_PATTERN = /^([A-Za-z]+)([\w_])*$/g; let isValid = VALID_PATTERN.test(enumName); console.log(isValid); if (isValid) { okFn(enumName); } else { failFn(enumName); } } let enumCandidate = [ //invalid "1_LEVEL", "$", "", "A B", //valid "RechteckStatus", "SensorType", "Red", "Blue", "Green", "rgb", "hsl", "ONE", "TWO", "THREE", "COLOR_1", "COLOR_2", "COLOR_3", ]; for (let e of enumCandidate) { testEnumLexicalOk(e, (e) => {console.log(`'${e}' is a valid enum`)} , (e) => {console.error(`'${e}' is an invalid enum`);} ) } function enumNameOk(enumName) { document.getElementById('enumerationNameSpan').innerHTML = enumName; document.getElementById('enumNameOut').innerHTML = enumName; } function enumNameFailure(enumName) { document.getElementById('enumerationNameSpan').innerHTML = "ungueltiger Name"; document.getElementById('enumNameOut').innerHTML = "ungueltiger Name"; }