Created
March 30, 2016 23:44
-
-
Save jacargentina/be454c13fa19003cf9f48175e82304d5 to your computer and use it in GitHub Desktop.
NodeJS: Convert any text file on disk encoding to UTF-8
This file contains hidden or 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
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