index.js 507 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var isUtf8 = require('is-utf8');
  3. var stripBom = module.exports = function (arg) {
  4. if (typeof arg === 'string') {
  5. return arg.replace(/^\ufeff/g, '');
  6. }
  7. if (Buffer.isBuffer(arg) && isUtf8(arg) &&
  8. arg[0] === 0xef && arg[1] === 0xbb && arg[2] === 0xbf) {
  9. return arg.slice(3);
  10. }
  11. return arg;
  12. };
  13. stripBom.stream = function () {
  14. var firstChunk = require('first-chunk-stream');
  15. return firstChunk({minSize: 3}, function (chunk, enc, cb) {
  16. this.push(stripBom(chunk));
  17. cb();
  18. });
  19. };