Created
March 29, 2014 14:15
-
-
Save panta82/9855254 to your computer and use it in GitHub Desktop.
Test which characters in javascript are OK in variable names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Easier than looking up in documentation... :-) | |
function test(chars) { | |
for (var i = 0; i < chars.length; i++) { | |
testSole(chars[i]); | |
testStart(chars[i]); | |
testMiddle(chars[i]); | |
} | |
function testSole(c) { | |
doTest(c, c, "as sole character"); | |
} | |
function testStart(c) { | |
doTest(c, c + "good", "as starting character"); | |
} | |
function testMiddle(c) { | |
doTest(c, "good" + c + "good", "as middle character"); | |
} | |
function doTest(char, key, description) { | |
var ob = makeTestOb(key); | |
var ok = false; | |
try { | |
ok = eval("ob." + key + "();"); | |
} | |
catch (err) { | |
} | |
console.log(char + " is " + (ok ? "OK" : "broken") + " " + description); | |
} | |
function makeTestOb(key) { | |
var res = {}; | |
res[key] = testFn; | |
return res; | |
} | |
function testFn() { | |
return true; | |
} | |
} | |
test("$|~_-"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment