Skip to content

Instantly share code, notes, and snippets.

@caferrari
Last active April 28, 2016 18:57
Show Gist options
  • Save caferrari/1f9d2a751dccd8ddff66464f561daf23 to your computer and use it in GitHub Desktop.
Save caferrari/1f9d2a751dccd8ddff66464f561daf23 to your computer and use it in GitHub Desktop.
Detect and convert file charsets
const exec = require('child_process').exec;
const fs = require('fs');
const iconv = (from, to, encoding, isFallback) => {
return new Promise((resolve, reject) => {
exec(`iconv -f ${encoding.trim()} -t utf-8 ${from} > ${to}`, (err) => {
if (err && !isFallback) {
return resolve(iconv(from, to, 'ISO_8859-1', true));
}
if (err) return reject(err);
resolve();
});
});
};
const detect = (file) => {
return new Promise((resolve, reject) => {
if (!fs.existsSync(file)) {
reject(new Error(`file ${file} not found`)) ;
}
exec(`uchardet ${file}`, (err, encoding) => {
if (err) reject(err);
resolve(encoding.trim());
});
});
};
const convert = (from, to) => {
return new Promise((resolve, reject) => {
detect(from).then((encoding) => {
resolve(iconv(from, to, encoding));
}).catch(reject);
});
};
module.exports = {
convert,
detect,
iconv
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment