feat: Add default export that mimics named exports

This commit is contained in:
Dmitry Shirokov 2020-05-08 09:06:28 +10:00
parent a6fca620c8
commit 10284683a8
3 changed files with 48 additions and 32 deletions

View File

@ -1,9 +1,11 @@
import * as chardet from '.'; import * as chardet from '.';
import defaultChardet from '.';
import fs from 'fs'; import fs from 'fs';
describe('chardet', () => { describe('chardet', () => {
var path = __dirname + '/test/data/encodings/utf8';
var expectedEncodingsFromPath = [ const path = __dirname + '/test/data/encodings/utf8';
const expectedEncodingsFromPath = [
{ 'confidence': 100, 'name': 'UTF-8', 'lang': undefined }, { 'confidence': 100, 'name': 'UTF-8', 'lang': undefined },
{ 'confidence': 32, 'name': 'windows-1252', 'lang': 'fr' }, { 'confidence': 32, 'name': 'windows-1252', 'lang': 'fr' },
{ 'confidence': 19, 'name': 'KOI8-R', 'lang': undefined }, { 'confidence': 19, 'name': 'KOI8-R', 'lang': undefined },
@ -12,9 +14,16 @@ describe('chardet', () => {
{ 'confidence': 10, 'name': 'windows-1253', 'lang': undefined }, { 'confidence': 10, 'name': 'windows-1253', 'lang': undefined },
{ 'confidence': 6, 'name': 'windows-1250', 'lang': 'pl' }, { 'confidence': 6, 'name': 'windows-1250', 'lang': 'pl' },
{ 'confidence': 4, 'name': 'windows-1254', 'lang': undefined }, { 'confidence': 4, 'name': 'windows-1254', 'lang': undefined },
{ 'confidence': 2, 'name': 'windows-1251', 'lang': undefined } { 'confidence': 2, 'name': 'windows-1251', 'lang': undefined },
]; ];
it('has both named and default exports', () => {
expect(defaultChardet.analyse).toBe(chardet.analyse);
expect(defaultChardet.detect).toBe(chardet.detect);
expect(defaultChardet.detectFile).toBe(chardet.detectFile);
expect(defaultChardet.detectFileSync).toBe(chardet.detectFileSync);
});
describe('#detect', () => { describe('#detect', () => {
it('should detect encoding', () => { it('should detect encoding', () => {
expect(chardet.detect(fs.readFileSync(path))).toBe('UTF-8'); expect(chardet.detect(fs.readFileSync(path))).toBe('UTF-8');

View File

@ -1,17 +1,17 @@
import { Match } from "./match"; import { Match } from './match';
import { Recogniser, Context } from './encoding'; import { Recogniser, Context } from './encoding';
import Utf8 from "./encoding/utf8"; import Utf8 from './encoding/utf8';
import * as unicode from "./encoding/unicode"; import * as unicode from './encoding/unicode';
import * as mbcs from "./encoding/mbcs"; import * as mbcs from './encoding/mbcs';
import * as sbcs from "./encoding/sbcs"; import * as sbcs from './encoding/sbcs';
import * as iso2022 from "./encoding/iso2022"; import * as iso2022 from './encoding/iso2022';
let fs: any; let fsModule: any;
const loadFs = () => { const loadFs = () => {
if (typeof module === 'object' && typeof module.exports === 'object') { if (typeof module === 'object' && typeof module.exports === 'object') {
fs = fs ? fs : require('fs'); fsModule = fsModule ? fsModule : require('fs');
return fs; return fsModule;
} }
throw new Error('File system is not available'); throw new Error('File system is not available');
} }
@ -22,7 +22,7 @@ interface FullOptions {
type Options = Partial<FullOptions> type Options = Partial<FullOptions>
var recognisers: Recogniser[] = [ const recognisers: Recogniser[] = [
new Utf8(), new Utf8(),
new unicode.UTF_16BE(), new unicode.UTF_16BE(),
new unicode.UTF_16LE(), new unicode.UTF_16LE(),
@ -45,7 +45,7 @@ var recognisers: Recogniser[] = [
new sbcs.ISO_8859_9(), new sbcs.ISO_8859_9(),
new sbcs.windows_1251(), new sbcs.windows_1251(),
new sbcs.windows_1256(), new sbcs.windows_1256(),
new sbcs.KOI8_R() new sbcs.KOI8_R(),
]; ];
type DetectResult = Match[] | string | null; type DetectResult = Match[] | string | null;
@ -57,29 +57,29 @@ export const detect = (buffer: Buffer): string | null => {
export const analyse = (buffer: Buffer): Match[] => { export const analyse = (buffer: Buffer): Match[] => {
// Tally up the byte occurrence statistics. // Tally up the byte occurrence statistics.
var fByteStats = []; const fByteStats = [];
for (var i = 0; i < 256; i++) fByteStats[i] = 0; for (let i = 0; i < 256; i++) fByteStats[i] = 0;
for (var i = buffer.length - 1; i >= 0; i--) fByteStats[buffer[i] & 0x00ff]++; for (let i = buffer.length - 1; i >= 0; i--) fByteStats[buffer[i] & 0x00ff]++;
var fC1Bytes = false; let fC1Bytes = false;
for (var i = 0x80; i <= 0x9f; i += 1) { for (let i = 0x80; i <= 0x9f; i += 1) {
if (fByteStats[i] != 0) { if (fByteStats[i] !== 0) {
fC1Bytes = true; fC1Bytes = true;
break; break;
} }
} }
var context: Context = { const context: Context = {
fByteStats: fByteStats, fByteStats,
fC1Bytes: fC1Bytes, fC1Bytes,
fRawInput: buffer, fRawInput: buffer,
fRawLength: buffer.length, fRawLength: buffer.length,
fInputBytes: buffer, fInputBytes: buffer,
fInputLen: buffer.length fInputLen: buffer.length,
}; };
var matches = recognisers const matches = recognisers
.map((rec) => { .map((rec) => {
return rec.match(context); return rec.match(context);
}) })
@ -95,10 +95,10 @@ export const analyse = (buffer: Buffer): Match[] => {
export const detectFile = (filepath: string, opts: Options = {}): Promise<DetectResult> => export const detectFile = (filepath: string, opts: Options = {}): Promise<DetectResult> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
var fd: any; let fd: any;
const fs = loadFs(); const fs = loadFs();
var handler = (err: Error | null | undefined, buffer: Buffer) => { const handler = (err: Error | null | undefined, buffer: Buffer) => {
if (fd) { if (fd) {
fs.closeSync(fd); fs.closeSync(fd);
} }
@ -111,7 +111,7 @@ export const detectFile = (filepath: string, opts: Options = {}): Promise<Detect
}; };
if (opts && opts.sampleSize) { if (opts && opts.sampleSize) {
fd = fs.openSync(filepath, "r"); fd = fs.openSync(filepath, 'r');
const sample: Buffer = Buffer.allocUnsafe(opts.sampleSize); const sample: Buffer = Buffer.allocUnsafe(opts.sampleSize);
fs.read(fd, sample, 0, opts.sampleSize, null, (err?: Error) => { fs.read(fd, sample, 0, opts.sampleSize, null, (err?: Error) => {
@ -127,8 +127,8 @@ export const detectFileSync = (filepath: string, opts: Options = {}): DetectResu
const fs = loadFs(); const fs = loadFs();
if (opts && opts.sampleSize) { if (opts && opts.sampleSize) {
var fd = fs.openSync(filepath, "r"), const fd = fs.openSync(filepath, 'r');
sample = Buffer.allocUnsafe(opts.sampleSize); const sample = Buffer.allocUnsafe(opts.sampleSize);
fs.readSync(fd, sample, 0, opts.sampleSize); fs.readSync(fd, sample, 0, opts.sampleSize);
fs.closeSync(fd); fs.closeSync(fd);
@ -137,3 +137,10 @@ export const detectFileSync = (filepath: string, opts: Options = {}): DetectResu
return detect(fs.readFileSync(filepath)); return detect(fs.readFileSync(filepath));
}; };
export default {
analyse,
detect,
detectFileSync,
detectFile,
};

View File

@ -7,7 +7,7 @@ export interface Match {
// @ts-ignore // @ts-ignore
export default (det, rec, confidence, name, lang): Match => ({ export default (det, rec, confidence, name, lang): Match => ({
confidence: confidence, confidence,
name: name || rec.name(det), name: name || rec.name(det),
lang: lang lang,
}); });