-
Notifications
You must be signed in to change notification settings - Fork 2
/
charsets.js
33 lines (30 loc) · 1.19 KB
/
charsets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* SEE
* https://nodejs.org/docs/latest/api/buffer.html#buffer_buffers_and_character_encodings
*/
const CHARSETS = [
'ascii' , // For 7-bit ASCII data only. This encoding is fast and will strip the high bit if set.
'utf8' , // Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.
'utf-8' ,
'utf16le', // 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.
'ucs2' , // Alias of 'utf16le'.
'base64' , // Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC4648, Section 5.
'latin1' , // A way of encoding the Buffer into a one-byte encoded string (as defined by the IANA in RFC1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes).
'binary' , // Alias for 'latin1'.
'hex' , // Encode each byte as two hexadecimal characters.
];
const ALIAS = {
'iso-8859-1': 'latin1',
};
module.exports = function(charset) {
charset = charset.toLowerCase();
if (ALIAS[charset]) {
return ALIAS[charset];
}
else if (CHARSETS.includes(charset)) {
return charset;
}
else {
return null;
}
};