removeBOM.js 617 B

1234567891011121314151617181920212223242526
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. /**
  7. * Returns result without BOM.
  8. * @param {string | Buffer} strOrBuffer string or buffer
  9. * @returns {string | Buffer} result without BOM
  10. */
  11. module.exports = (strOrBuffer) => {
  12. if (typeof strOrBuffer === "string" && strOrBuffer.charCodeAt(0) === 0xfeff) {
  13. return strOrBuffer.slice(1);
  14. } else if (
  15. Buffer.isBuffer(strOrBuffer) &&
  16. strOrBuffer[0] === 0xef &&
  17. strOrBuffer[1] === 0xbb &&
  18. strOrBuffer[2] === 0xbf
  19. ) {
  20. return strOrBuffer.subarray(3);
  21. }
  22. return strOrBuffer;
  23. };