136
return buffer.toString('utf16le', 2);
137
}
139
>
// 0x00. Sample the first 16 bytes (skipping byte 0 in case it's a BOM
140
>
// remnant). If a clear majority of odd-indexed bytes are zero, treat as
141
>
// UTF-16LE.
142
>
const sampleLen = Math.min(buffer.length, 16);
143
>
if (sampleLen >= 4) {
144
>
let zeros = 0;
145
>
let total = 0;
146
>
for (let i = 1; i < sampleLen; i += 2) {
147
>
total++;
148
>
if (buffer[i] === 0) {
149
zeros++;
150
}
152
>
if (total > 0 && zeros / total >= 0.75) {
153
let text = buffer.toString('utf16le');
154
if (text.charCodeAt(0) === 0xfeff) {