Skip to content

Instantly share code, notes, and snippets.

@jacargentina
Created March 30, 2016 23:44
Show Gist options
  • Save jacargentina/be454c13fa19003cf9f48175e82304d5 to your computer and use it in GitHub Desktop.
Save jacargentina/be454c13fa19003cf9f48175e82304d5 to your computer and use it in GitHub Desktop.
NodeJS: Convert any text file on disk encoding to UTF-8
var
fs = require('fs'),
charsetDetector = require('node-icu-charset-detector'),
iconvlite = require('iconv-lite');
/* Having different encodings
* on text files in a git repo
* but need to serve always on
* standard 'utf-8'
*/
function getFileContentsInUTF8(file_path) {
var content = fs.readFileSync(file_path);
var original_charset = charsetDetector.detectCharset(content);
var jsString = iconvlite.decode(content, original_charset.toString());
return jsString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment