removeBOM.js 585 B

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