本文整理汇总了TypeScript中jschardet.detect函数的典型用法代码示例。如果您正苦于以下问题:TypeScript detect函数的具体用法?TypeScript detect怎么用?TypeScript detect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了detect函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: detect
export function detect(filename: string=null, buffer: Buffer=null): DetectionResult | null {
if (filename !== null) {
const mimeType = filenameToTextMimetype(filename);
if (mimeType !== null) {
return {mimeType, charset: null };
}
const imageMimeType = filenameToImageMimetype(filename);
if (imageMimeType !== null) {
return { mimeType: imageMimeType, charset: null };
}
}
// Check the data directly.
if (buffer !== null) {
const mimeType = magicToMimeType(buffer);
if (mimeType !== null) {
return {mimeType, charset: null };
}
if ( ! isNotText(buffer)) {
const result = jschardet.detect(buffer.slice(0, SAMPLE_SIZE).toString());
if (result.encoding !== null && result.confidence > 0.8) {
return { mimeType: "text/plain", charset: result.encoding };
}
}
}
return { mimeType: "application/octet-stream", charset: null };
}
示例2: detectEncoding
export function detectEncoding(buffer: Buffer): string | null {
let result = detectEncodingByBOM(buffer);
if (result) {
return result;
}
const detected = jschardet.detect(buffer);
if (!detected || !detected.encoding) {
return null;
}
const encoding = detected.encoding;
// Ignore encodings that cannot guess correctly
// (http://chardet.readthedocs.io/en/latest/supported-encodings.html)
if (0 <= IGNORE_ENCODINGS.indexOf(encoding.toLowerCase())) {
return null;
}
const normalizedEncodingName = encoding.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
const mapped = JSCHARDET_TO_ICONV_ENCODINGS[normalizedEncodingName];
return mapped || normalizedEncodingName;
}
示例3: readFile
readFile(editor.document.fileName, (err, data) => {
if (err) {
console.error(err);
} else {
let encoding = jschardet.detect(data).encoding;
vscode.window.setStatusBarMessage(`may be ${encoding}`, 4000);
}
});
示例4: decode
private decode(data) {
var buffer = new Buffer(data);
var detectResult = jschardet.detect(buffer).encoding;
return iconv.decode(buffer, detectResult);
}