blob: 5695c5c79bcc59e0fbbafe0fa55b3e0c1f036f29 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2var isUtf8 = require('is-utf8');
3
4module.exports = function (x) {
5 // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
6 // conversion translates it to FEFF (UTF-16 BOM)
7 if (typeof x === 'string' && x.charCodeAt(0) === 0xFEFF) {
8 return x.slice(1);
9 }
10
11 if (Buffer.isBuffer(x) && isUtf8(x) &&
12 x[0] === 0xEF && x[1] === 0xBB && x[2] === 0xBF) {
13 return x.slice(3);
14 }
15
16 return x;
17};