Hong-Phuc Bui
2023-01-10 91edbe5682e70847a684ab649651696f54a7124f
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
/**
 * 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";
}