How to use chardet - 10 common examples

To help you get started, we’ve selected a few chardet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github martpie / museeks / src / ui / utils / utils-m3u.ts View on Github external
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
github AirenSoft / OvenPlayer / src / js / utils / downloader.js View on Github external
res.on('end', function() {
            var result = '';

            // Buffer
            if (opts.isBuffer) {
                result =  Buffer.concat(body, size);
            } else {
                var buffer = new Buffer(size);
                for (var i = 0, pos = 0, l = body.length; i < l; i++) {
                    var chunk = body[i];
                    chunk.copy(buffer, pos);
                    pos += chunk.length;
                }
                result = Iconv.decode(buffer, Chardet.detect(buffer)).toString(); //buffer.toString(opts.encoding);

                if (opts.json) {
                    result = JSON.parse(result);
                }
            }

            callback(null, res, result);
        });
    });
github mrkmg / node-external-editor / main / index.js View on Github external
ExternalEditor.prototype.readTemporaryFile = function () {
        try {
            var tempFileBuffer = fs_1.readFileSync(this.tempFile);
            if (tempFileBuffer.length === 0) {
                this.text = "";
            }
            else {
                var encoding = chardet_1.detect(tempFileBuffer).toString();
                if (!iconv_lite_1.encodingExists(encoding)) {
                    // Probably a bad idea, but will at least prevent crashing
                    encoding = "utf8";
                }
                this.text = iconv_lite_1.decode(tempFileBuffer, encoding);
            }
        }
        catch (readFileError) {
            throw new ReadFileError_1.ReadFileError(readFileError);
        }
    };
    ExternalEditor.prototype.removeTemporaryFile = function () {
github synzen / Discord.RSS / src / structs / DecodedFeedParser.js View on Github external
_transform (chunk, encoding, done) {
    if (!config.feeds.decode || !config.feeds.decode[this.url]) this.stream.write(chunk)
    else {
      const encoding = config.feeds.decode[this.url]
      this.stream.write(iconv.decode(chunk, encoding === 'auto' ? require('chardet').detect(chunk) : encoding)) // Assumes that the encoding specified is valid, and will not check via iconv.encodingExists()
    }
    done()
  }
}
github faressoft / subtitle-fetch / bin / encoding.js View on Github external
function converToUTF8(buffer) {

  // Detect the encoding
  var detectedEncoding = chardet.detect(buffer);

  // Already UTF8
  if (!detectedEncoding || detectedEncoding.toLowerCase() == 'utf-8' || detectedEncoding.toLowerCase() == 'ascii') {
    return buffer.toString();
  }

  return iconvLite.decode(buffer, detectedEncoding);

}
github datopian / data.js / lib / index.js View on Github external
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);
  }
github datopian / data.js / src / index.js View on Github external
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)
  }
}
github justlaputa / cue-parser / lib / cue.ts View on Github external
export function parse(filename: string): ICueSheet {
  const cuesheet = new CueSheet();

  if (!filename) {
    console.log('no file name specified for parse');
    return;
  }

  if (!fs.existsSync(filename)) {
    throw new Error('file ' + filename + ' does not exist');
  }

  cuesheet.encoding = chardet.detect(fs.readFileSync(filename));
  let encoding = cuesheet.encoding;

  switch (cuesheet.encoding) {
    case 'ISO-8859-1':
      encoding = 'binary';
      break;
  }

  const lines = (fs.readFileSync(filename, {encoding, flag: 'r'}) as any)
    .replace(/\r\n/, '\n').split('\n');

  lines.forEach(line => {
    if (!line.match(/^\s*$/)) {
      const lineParser = parseCommand(line);
      commandMap[lineParser.command](lineParser.params, cuesheet);
    }
github unbug / logproxy / index.js View on Github external
function autoDecodeCharset(data){
    if(data){
        var buffer = new Buffer(data),
            charset = chardet.detect(buffer);
        console.log(('Data charset is '+charset ).magenta.bold);
        try {
            data = buffer.toString(charset);
          } catch (e) {
            if(Iconv.encodingExists(charset)){
                data = Iconv.decode(buffer,charset);
            }
          }
        return data;
    }
}//end autoDecodeCharset
/**
github jsayol / vscode-firebase-explorer / src / extension.ts View on Github external
async provideTextDocumentContent(uri: vscode.Uri): Promise {
        try {
          const buffer = await readFile(uri.path);
          const charset = chardet.detect(buffer, {
            returnAllMatches: false
          }) as string | undefined;
          return iconv.decode(buffer, charset || 'utf8');
        } catch (err) {
          console.log(err);
          throw err;
        }
      }
    }

chardet

Character encoding detector

MIT
Latest version published 7 months ago

Package Health Score

76 / 100
Full package analysis

Popular chardet functions