I think you need to filter out NaN here, since NaN will convert to a string. Can you add a test case that NaN and 'NaN' don't conflict? It wouldn't hurt to add a bunch of similar test cases that foo and String(foo) don't conflict :-)