index.js 605 B

123456789101112131415161718192021222324252627
  1. /*!
  2. * remove-bom-buffer <https://github.com/jonschlinkert/remove-bom-buffer>
  3. *
  4. * Copyright (c) 2015-2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var isUTF8 = require('is-utf8');
  9. var isBuffer = require('is-buffer');
  10. function matchBOM(buf) {
  11. return buf[0] === 0xEF && buf[1] === 0xBB && buf[2] === 0xBF;
  12. }
  13. function maybeUTF8(buf) {
  14. // Only "maybe" because we aren't sniffing the whole buffer
  15. return isUTF8(buf.slice(3, 7));
  16. }
  17. module.exports = function(buf) {
  18. if (isBuffer(buf) && matchBOM(buf) && maybeUTF8(buf)) {
  19. return buf.slice(3);
  20. }
  21. return buf;
  22. };