Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const parse = (filePath: string): string[] => {
try {
const baseDir = path.parse(filePath).dir;
const encoding = chardet.detectFileSync(filePath);
const content = fs.readFileSync(filePath);
if (typeof encoding !== 'string') {
throw (new Error(`could not guess the file encoding (${filePath})`));
}
const decodedContent = iconv.decode(content, encoding);
const files = decodedContent
.split('\n')
.reduce((acc, line) => {
if (line.length === 0) {
return acc;
}
// If absolute path
get encoding() {
// When data is huge, we want to optimize performace (in tradeoff of less accuracy):
// So we are using sample of first 100K bytes here:
if (this.size > 1000000) {
return chardet.detectFileSync(this.path, {
sampleSize: 1000000
});
}
return chardet.detectFileSync(this.path);
}
get encoding() {
// When data is huge, we want to optimize performace (in tradeoff of less accuracy):
// So we are using sample of first 100K bytes here:
if (this.size > 1000000) {
return chardet.detectFileSync(this.path, {sampleSize: 1000000})
}
return chardet.detectFileSync(this.path)
}
}
get encoding() {
return chardet.detectFileSync(this.path);
}
}
get encoding() {
return chardet.detectFileSync(this.path)
}
}
const detectEncoding = (path) => {
const enc = chardet.detectFileSync(path);
switch (enc) {
case 'ISO-8859-1':
case 'ISO-8859-2':
return 'latin1';
default:
return 'utf8';
}
};