index.js 407 B

1234567891011121314151617
  1. 'use strict';
  2. var isUtf8 = require('is-utf8');
  3. module.exports = function (x) {
  4. // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
  5. // conversion translates it to FEFF (UTF-16 BOM)
  6. if (typeof x === 'string' && x.charCodeAt(0) === 0xFEFF) {
  7. return x.slice(1);
  8. }
  9. if (Buffer.isBuffer(x) && isUtf8(x) &&
  10. x[0] === 0xEF && x[1] === 0xBB && x[2] === 0xBF) {
  11. return x.slice(3);
  12. }
  13. return x;
  14. };