You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.5 KiB

8 years ago
  1. # wtf-8 [![Build status](https://travis-ci.org/mathiasbynens/wtf-8.svg?branch=master)](https://travis-ci.org/mathiasbynens/wtf-8) [![Dependency status](https://gemnasium.com/mathiasbynens/wtf-8.svg)](https://gemnasium.com/mathiasbynens/wtf-8)
  2. _wtf-8_ is a well-tested [WTF-8](https://simonsapin.github.io/wtf-8/) encoder/decoder written in JavaScript. WTF-8 is a superset of UTF-8: it can encode/decode any given Unicode code point, including those of (unpaired) surrogates. [Here’s an online demo.](https://mothereff.in/wtf-8)
  3. Feel free to fork if you see possible improvements!
  4. ## Installation
  5. Via [npm](http://npmjs.org/):
  6. ```bash
  7. npm install wtf-8
  8. ```
  9. Via [Bower](http://bower.io/):
  10. ```bash
  11. bower install wtf-8
  12. ```
  13. Via [Component](https://github.com/component/component):
  14. ```bash
  15. component install mathiasbynens/wtf-8
  16. ```
  17. In a browser:
  18. ```html
  19. <script src="wtf-8.js"></script>
  20. ```
  21. In [Narwhal](http://narwhaljs.org/), [Node.js](http://nodejs.org/), and [RingoJS ≥ v0.8.0](http://ringojs.org/):
  22. ```js
  23. var wtf8 = require('wtf-8');
  24. ```
  25. In [Rhino](http://www.mozilla.org/rhino/):
  26. ```js
  27. load('wtf-8.js');
  28. ```
  29. Using an AMD loader like [RequireJS](http://requirejs.org/):
  30. ```js
  31. require(
  32. {
  33. 'paths': {
  34. 'wtf-8': 'path/to/wtf-8'
  35. }
  36. },
  37. ['wtf-8'],
  38. function(wtf8) {
  39. console.log(wtf8);
  40. }
  41. );
  42. ```
  43. ## API
  44. ### `wtf8.encode(string)`
  45. Encodes any given JavaScript string (`string`) as WTF-8, and returns the WTF-8-encoded version of the string.
  46. ```js
  47. // U+00A9 COPYRIGHT SIGN; see http://codepoints.net/U+00A9
  48. wtf8.encode('\xA9');
  49. // → '\xC2\xA9'
  50. // U+10001 LINEAR B SYLLABLE B038 E; see http://codepoints.net/U+10001
  51. wtf8.encode('\uD800\uDC01');
  52. // → '\xF0\x90\x80\x81'
  53. ```
  54. ### `wtf8.decode(byteString)`
  55. Decodes any given WTF-8-encoded string (`byteString`) as WTF-8, and returns the WTF-8-decoded version of the string. It throws an error when malformed WTF-8 is detected.
  56. ```js
  57. wtf8.decode('\xC2\xA9');
  58. // → '\xA9'
  59. wtf8.decode('\xF0\x90\x80\x81');
  60. // → '\uD800\uDC01'
  61. // → U+10001 LINEAR B SYLLABLE B038 E
  62. ```
  63. ### `wtf8.version`
  64. A string representing the semantic version number.
  65. ## Support
  66. _wtf-8_ has been tested in (at least) the latest versions of Chrome, Opera, Firefox, Safari, IE, Node.js, Narwhal, RingoJS, PhantomJS, and Rhino.
  67. ## Author
  68. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  69. |---|
  70. | [Mathias Bynens](https://mathiasbynens.be/) |
  71. ## License
  72. _wtf-8_ is available under the [MIT](https://mths.be/mit) license.