You can do predefined tests so that your function produces the correct result in different situations:
function test_getFileExtension() { test("foo.png", "png"); test("bar.py", "py"); test("the_really_long_name.txt", "txt"); test("playlist.xspf", "xspf"); test("%20.txt", "txt"); test("file_without_extension", ""); // actually gives an error function test(input,output) { var o=getFileExtension(input); if (o === output) { console.log("OK: "+input+" --> "+o); } else { console.log("ERROR: "+input+" --> "+o); } } }
Then, as soon as you update your function, you call test_getFileExtension() again and make sure that it still works as intended.
source share