Last active
February 22, 2023 09:56
-
-
Save 0xlkda/e019ccdbda3f9b6a552e3e2bda12e660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Detects encoding of a buffer | |
exports.detectEncoding = function (buffer) { | |
for (var i = 0; i < buffer.length; i++) { | |
if (buffer[i] === 65533 || buffer[i] <= 8) { | |
// The character code 65533 is often used as a replacement character for an unknown or unprintable character in a text file. | |
// The check for values less than or equal to 8 is intended to exclude control characters which are typically not part of human-readable text. | |
return 'binary' | |
} | |
} | |
return 'utf8' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment