Skip to content

Instantly share code, notes, and snippets.

@chockenberry
Created April 26, 2013 00:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chockenberry/5464387 to your computer and use it in GitHub Desktop.
Save chockenberry/5464387 to your computer and use it in GitHub Desktop.
Unicode trim
// Names: HEAVY BLACK HEART <space> EXTRATERRESTRIAL ALIEN <space> FIRE <space> PILE OF POO
// Unicode: U+2764 U+1F47D U+1F525 U+1F4A9
// UTF-8 E2 9D A4 F0 9F 91 BD F0 9F 94 A5 F0 9F 92 A9
> var str = '❤ 👽 🔥 💩'
> str
'❤ 👽 🔥 💩'
undefined
> var buf = new Buffer(str, 'utf8')
undefined
> buf
<Buffer e2 9d a4 20 f0 9f 91 bd 20 f0 9f 94 a5 20 f0 9f 92 a9>
> buf.toString('utf8')
'❤ 👽 🔥 💩'
> buf.toString('utf8', 0, 5)
'❤ �'
> var result = buf.toString('utf8', 0, 5)
undefined
> result.charCodeAt(result.length - 1).toString(16)
'fffd'
> result.length
3
> result.charCodeAt(0).toString(16)
'2764'
> result.charCodeAt(1).toString(16)
'20'
> result.charCodeAt(2).toString(16)
'fffd'
> var buf2 = new Buffer(str, 'ucs2')
<Buffer 64 27 20 00 3d d8 7d dc 20 00 3d d8 25 dd 20 00 3d d8 a9 dc>
> var result2 = buf2.toString('utf8', 0, 5)
undefined
> result2
'd\' \u0000='
> result2.length
5
> result2.charCodeAt(0).toString(16)
'64'
> result2.charCodeAt(1).toString(16)
'27'
> result2.charCodeAt(2).toString(16)
'20'
> result2.charCodeAt(3).toString(16)
'0'
> result2.charCodeAt(4).toString(16)
'3d'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment