Created
August 16, 2012 09:37
-
-
Save ForbesLindesay/3368836 to your computer and use it in GitHub Desktop.
Compare two strings really carefully to work out what's different
This file contains 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
function longCompare(actual, expected) { | |
function format(str) { | |
if (typeof str === 'string') { | |
return JSON.stringify(str).replace(/^\"/g, '\'').replace(/\"$/g, '\''); | |
} else { | |
return str; | |
} | |
} | |
for (var i = 0; i < actual.length || i < expected.length; i++) { | |
if (actual.charAt(i) !== expected.charAt(i)) { | |
console.log(format(actual.charAt(i)) + '!=' + format(expected.charAt(i))); | |
} else { | |
console.log(format(actual.charAt(i)) + '=' + format(expected.charAt(i))); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment