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.

50 lines
1.2 KiB

4 years ago
  1. # utf-8-validate
  2. [![Version npm](https://img.shields.io/npm/v/utf-8-validate.svg)](https://www.npmjs.com/package/utf-8-validate)
  3. [![Build Status](https://travis-ci.org/websockets/utf-8-validate.svg?branch=master)](https://travis-ci.org/websockets/utf-8-validate)
  4. [![Windows Build](https://ci.appveyor.com/api/projects/status/github/websockets/utf-8-validate?branch=master&svg=true)](https://ci.appveyor.com/project/lpinca/utf-8-validate)
  5. Check if a buffer contains valid UTF-8 encoded text.
  6. ## Installation
  7. ```
  8. npm install utf-8-validate --save-optional
  9. ```
  10. The `--save-optional` flag tells npm to save the package in your package.json
  11. under the [`optionalDependencies`](https://docs.npmjs.com/files/package.json#optionaldependencies)
  12. key.
  13. ## API
  14. The module exports a single function which takes one argument.
  15. ### `isValidUTF8(buffer)`
  16. Checks whether a buffer contains valid UTF-8.
  17. #### Arguments
  18. - `buffer` - The buffer to check.
  19. #### Return value
  20. `true` if the buffer contains only correct UTF-8, else `false`.
  21. #### Example
  22. ```js
  23. 'use strict';
  24. const isValidUTF8 = require('utf-8-validate');
  25. const buf = Buffer.from([0xf0, 0x90, 0x80, 0x80]);
  26. console.log(isValidUTF8(buf));
  27. // => true
  28. ```
  29. ## License
  30. [MIT](LICENSE)