Function normalizeJsName

  • Validates that the string name conforms to the JS IdentifierName specification and if not normalizes the name so that it would. This method does not identify or change any keywords meaning that if you pass in a known keyword the same value will be returned.

    Parameters

    • jsName: string

      The string value to validate

    • OptionalcamelCase: boolean

      Optionally (see [1]) convert into CamelCase with the leading character either

      • true => lowercase
      • 'false' => uppercase
      • undefined => not converted

    Returns string

    The original string name, if it conforms to the JS naming convention otherwise an encoded version.

    [1]: Camel casing the name will remove all non-word characters from the result so you will NOT end up with any leading, embedded or trailing _ characters which may cause duplicate results for different string values.

    0.9.0

    normalizeJsName("HelloDarkness"); // "HelloDarkness"
    normalizeJsName("Hello Darkness"); // "Hello_Darkness"
    normalizeJsName("hello Darkness"); // "hello_Darkness"
    normalizeJsName("hello Darkness"); // "hello_Darkness"
    normalizeJsName("hello.Darkness"); // "hello_Darkness"
    normalizeJsName("hello-Darkness"); // "hello_Darkness"
    normalizeJsName("hello_Darkness"); // "hello_Darkness"
    normalizeJsName("abc-123"); // "abc_123"
    normalizeJsName("0abc0"); // "0abc0"
    normalizeJsName("\"HelloDarkness\""); // "_HelloDarkness_"
    normalizeJsName("\"Hello Darkness\""); // "_Hello_Darkness_"
    normalizeJsName("\"hello Darkness\""); // "_hello_Darkness_"
    normalizeJsName("\"hello Darkness\""); // "_hello_Darkness_"
    normalizeJsName("\"hello .,#[]Darkness\""); // "_hello______Darkness_"

    normalizeJsName("HelloDarkness", true); // "helloDarkness"
    normalizeJsName("Hello Darkness", true); // "helloDarkness"
    normalizeJsName("hello Darkness", true); // "helloDarkness"
    normalizeJsName("hello Darkness", true); // "helloDarkness"
    normalizeJsName("hello.Darkness", true); // "helloDarkness"
    normalizeJsName("hello-Darkness", true); // "helloDarkness"
    normalizeJsName("hello_Darkness", true); // "helloDarkness"
    normalizeJsName("abc-123", true); // "abc123"
    normalizeJsName("0abc0", true); // "0abc0"
    normalizeJsName("\"HelloDarkness\"", true); // "helloDarkness"
    normalizeJsName("\"Hello Darkness\"", true); // "helloDarkness"
    normalizeJsName("hello \"Darkness\"", true); // "helloDarkness"
    normalizeJsName("hello \"Darkness\"", true); // "helloDarkness"
    normalizeJsName("\"hello .,#[]Darkness\"", true); // "helloDarkness"