@@ -46,6 +46,10 @@ const JSCHARDET_TO_ICONV_ENCODINGS: { [name: string]: string } = {
46
46
big5 : "cp950"
47
47
} ;
48
48
49
+ function normaliseEncodingName ( name : string ) : string {
50
+ return name . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "" ) . toLowerCase ( ) ;
51
+ }
52
+
49
53
export function detectEncoding ( buffer : Buffer ) : string | null {
50
54
const result = detectEncodingByBOM ( buffer ) ;
51
55
@@ -58,9 +62,22 @@ export function detectEncoding(buffer: Buffer): string | null {
58
62
false
59
63
) ;
60
64
if ( experimental ) {
61
- const detected = chardet . detect ( buffer ) ;
62
- if ( detected ) {
63
- return detected . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "" ) . toLocaleLowerCase ( ) ;
65
+ const detected = chardet . detectAll ( buffer ) ;
66
+ const encodingPriorities = configuration . get < string [ ] > (
67
+ "experimental.encoding_priority" ,
68
+ [ ]
69
+ ) ;
70
+
71
+ if ( ! detected ) {
72
+ return null ;
73
+ }
74
+
75
+ for ( const pri of encodingPriorities ) {
76
+ for ( const det of detected ) {
77
+ if ( normaliseEncodingName ( pri ) === normaliseEncodingName ( det . name ) ) {
78
+ return normaliseEncodingName ( det . name ) ;
79
+ }
80
+ }
64
81
}
65
82
66
83
return null ;
@@ -80,9 +97,7 @@ export function detectEncoding(buffer: Buffer): string | null {
80
97
return null ;
81
98
}
82
99
83
- const normalizedEncodingName = encoding
84
- . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "" )
85
- . toLowerCase ( ) ;
100
+ const normalizedEncodingName = normaliseEncodingName ( encoding ) ;
86
101
const mapped = JSCHARDET_TO_ICONV_ENCODINGS [ normalizedEncodingName ] ;
87
102
88
103
return mapped || normalizedEncodingName ;
0 commit comments