本文整理匯總了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);
}