Last active
October 5, 2016 21:07
-
-
Save mk-pmb/191436f9963fb82d7c9025324c76accb 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
/*jslint indent: 2, maxlen: 80, node: true */ | |
/* -*- tab-width: 2 -*- */ | |
'use strict'; | |
var jsStringify = require('js-stringify'), | |
animals = { dog: '\uD83D\uDC15', cow: '\uD83D\uDC04' }; | |
function charCodes(s) { | |
if (s.length === 1) { return '[' + s.charCodeAt(0).toString(16) + ']'; } | |
return s.replace(/[~-\uFFFF]/g, charCodes); | |
} | |
animals.halfbreed = animals.dog[1] + animals.cow[0]; | |
animals = JSON.stringify(animals, null, 2).replace(/\s+/g, ' '); | |
if ((typeof animals) !== 'string') { throw new Error('JSON fail'); } | |
console.log(['\uFEFF<!DOCTYPE html><html><head>', | |
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', | |
'</head><body><p></p><script>', | |
String(charCodes), | |
'console.log(document.characterSet);', | |
'console.log(charCodes(' + jsStringify(animals) + '));', | |
'console.log(' + charCodes(jsStringify(animals)) + ');', | |
'</script></body></html>'].join('\n')); |
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
<!DOCTYPE html><html><head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
</head><body><p></p><script> | |
function charCodes(s) { | |
if (s.length === 1) { return '[' + s.charCodeAt(0).toString(16) + ']'; } | |
return s.replace(/[~-\uFFFF]/g, charCodes); | |
} | |
console.log(document.characterSet); | |
console.log(charCodes("{ \"dog\": \"🐕\", \"cow\": \"🐄\", \"halfbreed\": \"��\" }")); | |
console.log("{ \"dog\": \"[d83d][dc15]\", \"cow\": \"[d83d][dc04]\", \"halfbreed\": \"[dc15][d83d]\" }"); | |
</script></body></html> |
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
UTF-8 | |
{ "dog": "[d83d][dc15]", "cow": "[d83d][dc04]", "halfbreed": "[fffd][fffd]" } | |
{ "dog": "[d83d][dc15]", "cow": "[d83d][dc04]", "halfbreed": "[dc15][d83d]" } |
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
/*jslint indent: 2, maxlen: 80, node: true */ | |
'use strict'; | |
var dog = '\uD83D\uDC15', cow = '\uD83D\uDC04', halves = dog[1] + cow[0], | |
jsStringify = require('js-stringify'); | |
console.log('orig: <' + halves + '>'); | |
console.log('JSON: ' + JSON.stringify(halves)); | |
console.log('js-str:' + jsStringify(halves)); |
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
00000000 6f 72 69 67 3a 20 20 3c ef bf bd ef bf bd 3e 0a |orig: <......>.| | |
00000010 4a 53 4f 4e 3a 20 20 22 ef bf bd ef bf bd 22 0a |JSON: "......".| | |
00000020 6a 73 2d 73 74 72 3a 22 ef bf bd ef bf bd 22 0a |js-str:"......".| | |
00000030 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment