(buffer: Buffer, autoGuessEncoding: boolean)
| 37 | * Guess the encoding from the buffer. |
| 38 | */ |
| 39 | export const guessEncoding = (buffer: Buffer, autoGuessEncoding: boolean): Encoding => { |
| 40 | const isBom = false |
| 41 | let encoding = 'utf8' |
| 42 | |
| 43 | // Detect UTF8- and UTF16-BOM encodings. |
| 44 | for (const [key, value] of Object.entries(BOM_ENCODINGS)) { |
| 45 | if (checkSequence(buffer, value)) { |
| 46 | return { encoding: key, isBom: true } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Auto guess encoding, otherwise use UTF-8. |
| 51 | if (autoGuessEncoding) { |
| 52 | encoding = ced(buffer) |
| 53 | if (CED_ICONV_ENCODINGS[encoding]) { |
| 54 | encoding = CED_ICONV_ENCODINGS[encoding] |
| 55 | } else { |
| 56 | encoding = encoding.toLowerCase().replace(/-_/g, '') |
| 57 | } |
| 58 | } |
| 59 | return { encoding, isBom } |
| 60 | } |
no test coverage detected