Skip to content

Commit a1cdaca

Browse files
authored
feat: Expose EncodingName type (#108)
1 parent 2472f28 commit a1cdaca

File tree

11 files changed

+90
-46
lines changed

11 files changed

+90
-46
lines changed

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"singleQuote": true,
3-
"maxLineLength": 80
3+
"printWidth": 80
44
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"format:check": "prettier --list-different ./src/**/*.ts",
1919
"test": "jest",
2020
"prepublish": "npm run build",
21-
"semantic-release": "semantic-release"
21+
"semantic-release": "semantic-release",
22+
"typecheck": "tsc"
2223
},
2324
"files": [
2425
"lib"

src/encoding/ascii.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Context, Recogniser } from '.';
2-
import match, { Match } from '../match';
1+
import type { Context, Recogniser } from '.';
2+
import match, { type EncodingName, type Match } from '../match';
33

44
export default class Ascii implements Recogniser {
5-
name() {
5+
name(): EncodingName {
66
return 'ASCII';
77
}
88

src/encoding/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Match } from '../match';
1+
import type { EncodingName, Match } from '../match';
22

33
export interface Recogniser {
44
match(input: Context): Match | null;
5-
name(input?: Context): string;
5+
name(input?: Context): EncodingName;
66
language?(): string | undefined;
77
}
88

src/encoding/iso2022.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Context, Recogniser } from '.';
2-
import match, { Match } from '../match';
1+
import type { Context, Recogniser } from '.';
2+
import match, { type Match, type EncodingName } from '../match';
33

44
/**
55
* This is a superclass for the individual detectors for
@@ -10,7 +10,7 @@ import match, { Match } from '../match';
1010
class ISO_2022 implements Recogniser {
1111
escapeSequences: number[][] = [];
1212

13-
name() {
13+
name(): EncodingName {
1414
return 'ISO_2022';
1515
}
1616

@@ -85,7 +85,7 @@ class ISO_2022 implements Recogniser {
8585
}
8686

8787
export class ISO_2022_JP extends ISO_2022 {
88-
name() {
88+
name(): EncodingName {
8989
return 'ISO-2022-JP';
9090
}
9191

@@ -110,7 +110,7 @@ export class ISO_2022_JP extends ISO_2022 {
110110
}
111111

112112
export class ISO_2022_KR extends ISO_2022 {
113-
name() {
113+
name(): EncodingName {
114114
return 'ISO-2022-KR';
115115
}
116116
language() {
@@ -120,7 +120,7 @@ export class ISO_2022_KR extends ISO_2022 {
120120
}
121121

122122
export class ISO_2022_CN extends ISO_2022 {
123-
name() {
123+
name(): EncodingName {
124124
return 'ISO-2022-CN';
125125
}
126126
language() {

src/encoding/mbcs.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Context, Recogniser } from '.';
2-
import match, { Match } from '../match';
1+
import type { Context, Recogniser } from '.';
2+
import match, { type Match, type EncodingName } from '../match';
33

44
/**
55
* Binary search implementation (recursive)
@@ -83,7 +83,7 @@ class IteratedChar {
8383
class mbcs implements Recogniser {
8484
commonChars: number[] = [];
8585

86-
name() {
86+
name(): EncodingName {
8787
return 'mbcs';
8888
}
8989

@@ -198,9 +198,10 @@ class mbcs implements Recogniser {
198198
* Shift_JIS charset recognizer.
199199
*/
200200
export class sjis extends mbcs {
201-
name() {
201+
name(): EncodingName {
202202
return 'Shift_JIS';
203203
}
204+
204205
language() {
205206
return 'ja';
206207
}
@@ -249,9 +250,10 @@ export class sjis extends mbcs {
249250
* Big5 charset recognizer.
250251
*/
251252
export class big5 extends mbcs {
252-
name() {
253+
name(): EncodingName {
253254
return 'Big5';
254255
}
256+
255257
language() {
256258
return 'zh';
257259
}
@@ -362,9 +364,10 @@ function eucNextChar(iter: IteratedChar, det: Context) {
362364
* is created and kept by the public CharsetDetector class
363365
*/
364366
export class euc_jp extends mbcs {
365-
name() {
367+
name(): EncodingName {
366368
return 'EUC-JP';
367369
}
370+
368371
language() {
369372
return 'ja';
370373
}
@@ -395,7 +398,7 @@ export class euc_jp extends mbcs {
395398
* is created and kept by the public CharsetDetector class
396399
*/
397400
export class euc_kr extends mbcs {
398-
name() {
401+
name(): EncodingName {
399402
return 'EUC-KR';
400403
}
401404

@@ -428,7 +431,7 @@ export class euc_kr extends mbcs {
428431
* GB-18030 recognizer. Uses simplified Chinese statistics.
429432
*/
430433
export class gb_18030 extends mbcs {
431-
name() {
434+
name(): EncodingName {
432435
return 'GB18030';
433436
}
434437

src/encoding/sbcs.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Context, Recogniser } from '../encoding/index';
2-
import match, { Match } from '../match';
1+
import type { Context, Recogniser } from '.';
2+
import match, { type EncodingName, type Match } from '../match';
33

44
/**
55
* This class recognizes single-byte encodings. Because the encoding scheme is so
@@ -120,7 +120,7 @@ class sbcs implements Recogniser {
120120
return [];
121121
}
122122

123-
name(_input: Context): string {
123+
name(_input: Context): EncodingName {
124124
return 'sbcs';
125125
}
126126

@@ -342,7 +342,7 @@ export class ISO_8859_1 extends sbcs {
342342
];
343343
}
344344

345-
name(input: Context): string {
345+
name(input: Context): EncodingName {
346346
return input && input.c1Bytes ? 'windows-1252' : 'ISO-8859-1';
347347
}
348348
}
@@ -440,7 +440,7 @@ export class ISO_8859_2 extends sbcs {
440440
];
441441
}
442442

443-
name(det: Context): string {
443+
name(det: Context): EncodingName {
444444
return det && det.c1Bytes ? 'windows-1250' : 'ISO-8859-2';
445445
}
446446
}
@@ -488,7 +488,7 @@ export class ISO_8859_5 extends sbcs {
488488
];
489489
}
490490

491-
name() {
491+
name(): EncodingName {
492492
return 'ISO-8859-5';
493493
}
494494

@@ -540,7 +540,7 @@ export class ISO_8859_6 extends sbcs {
540540
];
541541
}
542542

543-
name() {
543+
name(): EncodingName {
544544
return 'ISO-8859-6';
545545
}
546546

@@ -592,7 +592,7 @@ export class ISO_8859_7 extends sbcs {
592592
];
593593
}
594594

595-
name(det: Context): string {
595+
name(det: Context): EncodingName {
596596
return det && det.c1Bytes ? 'windows-1253' : 'ISO-8859-7';
597597
}
598598

@@ -664,7 +664,7 @@ export class ISO_8859_8 extends sbcs {
664664
];
665665
}
666666

667-
name(det: Context): string {
667+
name(det: Context): EncodingName {
668668
return det && det.c1Bytes ? 'windows-1255' : 'ISO-8859-8';
669669
}
670670

@@ -716,7 +716,7 @@ export class ISO_8859_9 extends sbcs {
716716
];
717717
}
718718

719-
name(det: Context): string {
719+
name(det: Context): EncodingName {
720720
return det && det.c1Bytes ? 'windows-1254' : 'ISO-8859-9';
721721
}
722722

@@ -768,7 +768,7 @@ export class windows_1251 extends sbcs {
768768
];
769769
}
770770

771-
name() {
771+
name(): EncodingName {
772772
return 'windows-1251';
773773
}
774774

@@ -820,7 +820,7 @@ export class windows_1256 extends sbcs {
820820
];
821821
}
822822

823-
name() {
823+
name(): EncodingName {
824824
return 'windows-1256';
825825
}
826826

@@ -872,7 +872,7 @@ export class KOI8_R extends sbcs {
872872
];
873873
}
874874

875-
name() {
875+
name(): EncodingName {
876876
return 'KOI8-R';
877877
}
878878

src/encoding/unicode.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Context, Recogniser } from '.';
2-
import match, { Match } from '../match';
1+
import type { Context, Recogniser } from '.';
2+
import match, { type Match, type EncodingName } from '../match';
33

44
/**
55
* This class matches UTF-16 and UTF-32, both big- and little-endian. The
66
* BOM will be used if it is present.
77
*/
88
export class UTF_16BE implements Recogniser {
9-
name() {
9+
name(): EncodingName {
1010
return 'UTF-16BE';
1111
}
1212

@@ -27,9 +27,10 @@ export class UTF_16BE implements Recogniser {
2727
}
2828

2929
export class UTF_16LE implements Recogniser {
30-
name() {
30+
name(): EncodingName {
3131
return 'UTF-16LE';
3232
}
33+
3334
match(det: Context): Match | null {
3435
const input = det.rawInput;
3536

@@ -56,7 +57,7 @@ interface WithGetChar {
5657
}
5758

5859
class UTF_32 implements Recogniser, WithGetChar {
59-
name() {
60+
name(): EncodingName {
6061
return 'UTF-32';
6162
}
6263

@@ -111,7 +112,7 @@ class UTF_32 implements Recogniser, WithGetChar {
111112
}
112113

113114
export class UTF_32BE extends UTF_32 {
114-
name() {
115+
name(): EncodingName {
115116
return 'UTF-32BE';
116117
}
117118
getChar(input: Uint8Array, index: number) {
@@ -125,7 +126,7 @@ export class UTF_32BE extends UTF_32 {
125126
}
126127

127128
export class UTF_32LE extends UTF_32 {
128-
name() {
129+
name(): EncodingName {
129130
return 'UTF-32LE';
130131
}
131132

src/encoding/utf8.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Context, Recogniser } from '.';
2-
import match, { Match } from '../match';
1+
import type { Context, Recogniser } from '.';
2+
import match, { type EncodingName, type Match } from '../match';
33

44
export default class Utf8 implements Recogniser {
5-
name() {
5+
name(): EncodingName {
66
return 'UTF-8';
77
}
88

@@ -57,7 +57,7 @@ export default class Utf8 implements Recogniser {
5757
}
5858
}
5959

60-
// Cook up some sort of confidence score, based on presense of a BOM
60+
// Cook up some sort of confidence score, based on presence of a BOM
6161
// and the existence of valid and/or invalid multi-byte sequences.
6262
confidence = 0;
6363
if (hasBOM && numInvalid == 0) confidence = 100;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,5 @@ export default {
152152
detectFileSync,
153153
detectFile,
154154
};
155+
156+
export { Match, EncodingName } from './match';

0 commit comments

Comments
 (0)