Last active
October 26, 2015 12:57
-
-
Save RReverser/8e79df1b9df28c0dd94a to your computer and use it in GitHub Desktop.
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 *getSymbols(s) { | |
for (var i = 0; i < s.length - 1; i++) { | |
if (s.charCodeAt(i) >> 10 === 0x36 && s.charCodeAt(i + 1) >> 10 === 0x37) { | |
yield s.substr(i, 2); | |
} else { | |
yield s.charAt(i); | |
} | |
} | |
yield s.charAt(i); | |
} |
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
// Just slightly shorter, you know :) | |
function getSymbols(s) { | |
var output = []; | |
for (var i = 0; i < s.length - 1; i++) { | |
if (s.charCodeAt(i) >> 10 === 0x36 && s.charCodeAt(i + 1) >> 10 === 0x37) { | |
output.push(s.substr(i, 2)); | |
} else { | |
output.push(s.charAt(i)); | |
} | |
} | |
output.push(s.charAt(i)); | |
return output; | |
} |
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 getSymbols(string) { | |
var i, output = []; | |
for (i = 0; i < string.length - 1; i++) { | |
var charCode = string.charCodeAt(i); | |
if (charCode >= 0xD800 && charCode <= 0xDBFF) { | |
charCode = string.charCodeAt(i + 1); | |
if (charCode >= 0xDC00 && charCode <= 0xDFFF) { | |
output.push(string.slice(i, i + 2)); | |
i++; | |
continue; | |
} | |
} | |
output.push(string.charAt(i)); | |
} | |
output.push(string.charAt(i)); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference, my old version was:
I’ve updated https://mathiasbynens.be/notes/javascript-unicode#iterating-over-symbols now. Thanks!