Skip to content

Commit 7f95705

Browse files
authored
feat: Add ASCII encoding support (#78)
1 parent 29cb821 commit 7f95705

File tree

7 files changed

+36
-0
lines changed

7 files changed

+36
-0
lines changed

.github/workflows/test-build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ assert(typeof chardet.detectFile, 'function');
88
assert(typeof chardet.detectFileSync, 'function');
99

1010
assert.deepStrictEqual(chardet.analyse(Buffer.from('This is a test')), [
11+
{ confidence: 100, name: 'ASCII', lang: undefined },
1112
{ confidence: 98, name: 'ISO-8859-1', lang: 'en' },
1213
{ confidence: 98, name: 'ISO-8859-2', lang: 'hu' },
1314
{ confidence: 10, name: 'UTF-8', lang: undefined },

.github/workflows/test-build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const main = async () => {
99
assert(typeof chardet.detectFileSync, 'function');
1010

1111
assert.deepStrictEqual(chardet.analyse(Buffer.from('This is a test')), [
12+
{ confidence: 100, name: 'ASCII', lang: undefined },
1213
{ confidence: 98, name: 'ISO-8859-1', lang: 'en' },
1314
{ confidence: 98, name: 'ISO-8859-2', lang: 'hu' },
1415
{ confidence: 10, name: 'UTF-8', lang: undefined },

src/encoding/ascii.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as chardet from '..';
2+
3+
describe('ASCII', () => {
4+
it('should return ASCII', () => {
5+
expect(
6+
chardet.detectFileSync(__dirname + '/../test/data/encodings/ascii')
7+
).toBe('ASCII');
8+
});
9+
});

src/encoding/ascii.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Context, Recogniser } from '.';
2+
import match, { Match } from '../match';
3+
4+
export default class Ascii implements Recogniser {
5+
name() {
6+
return 'ASCII';
7+
}
8+
9+
match(det: Context): Match | null {
10+
const input = det.rawInput;
11+
12+
for (let i = 0; i < det.rawLen; i++) {
13+
const b = input[i];
14+
if (b < 32 || b > 126) {
15+
return match(det, this, 0);
16+
}
17+
}
18+
19+
return match(det, this, 100);
20+
}
21+
}

src/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('chardet', () => {
1515
{ 'confidence': 6, 'name': 'windows-1250', 'lang': 'pl' },
1616
{ 'confidence': 4, 'name': 'windows-1254', 'lang': 'tr' },
1717
{ 'confidence': 2, 'name': 'windows-1251', 'lang': 'ru' },
18+
{ 'confidence': 0, 'name': 'ASCII', 'lang': undefined },
1819
];
1920

2021
it('has both named and default exports', () => {

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Recogniser, Context } from './encoding';
33

44
import loadFs from './fs/node';
55

6+
import Ascii from './encoding/ascii';
67
import Utf8 from './encoding/utf8';
78
import * as unicode from './encoding/unicode';
89
import * as mbcs from './encoding/mbcs';
@@ -40,6 +41,7 @@ const recognisers: Recogniser[] = [
4041
new sbcs.windows_1251(),
4142
new sbcs.windows_1256(),
4243
new sbcs.KOI8_R(),
44+
new Ascii(),
4345
];
4446

4547
export type AnalyseResult = Match[];

src/test/data/encodings/ascii

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

0 commit comments

Comments
 (0)