Feel free to fork if you see possible improvements!
npm install utf8
bower install utf8
component install mathiasbynens/utf8.js
In a browser:
var utf8 = require'utf8';
Using an AMD loader like RequireJS:
string) as UTF-8, and returns the UTF-8-encoded version of the string. It throws an error if the input string contains a non-scalar value, i.e. a lone surrogate. (If you need to be able to encode non-scalar values as well, use WTF-8 instead.)
// U+00A9 COPYRIGHT SIGN; seeutf8encode'\xA9';// → '\xC2\xA9'// U+10001 LINEAR B SYLLABLE B038 E; seeutf8encode'\uD800\uDC01';// → '\xF0\x90\x80\x81'
Decodes any given UTF-8-encoded string (
byteString) as UTF-8, and returns the UTF-8-decoded version of the string. It throws an error when malformed UTF-8 is detected. (If you need to be able to decode encoded non-scalar values as well, use WTF-8 instead.)
utf8decode'\xC2\xA9';// → '\xA9'utf8decode'\xF0\x90\x80\x81';// → '\uD800\uDC01'// → U+10001 LINEAR B SYLLABLE B038 E
A string representing the semantic version number.
utf8.js has been tested in at least Chrome 27-39, Firefox 3-34, Safari 4-8, Opera 10-28, IE 6-11, Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.11, PhantomJS 1.9.0, and Rhino 1.7RC4.
After cloning this repository, run
npm install to install the dependencies needed for development and testing. You may want to install Istanbul globally using
npm install istanbul -g.
Once that’s done, you can run the unit tests in Node using
npm test or
node tests/tests.js. To run the tests in Rhino, Ringo, Narwhal, PhantomJS, and web browsers as well, use
To generate the code coverage report, use
Long before utf8.js was created, the
utf8 module on npm was registered and used by another (slightly buggy) library. @ryanmcgrath was kind enough to give me access to the
utf8 package on npm when I told him about utf8.js. Since there has already been a v1.0.0 release of the old library, and to avoid breaking backwards compatibility with projects that rely on the
utf8 npm package, I decided the tag the first release of utf8.js as v2.0.0 and take it from there.
utf8.js is available under the MIT license.