NPM酷库,天天两分钟,相识一个盛行NPM库。·
在NPM酷库039中,我们熟悉到了iconv-lite库能够转换恣意的字符编码到JavaScript内置的Unicode编码,以便于我们的顺序和外部体系友爱对接。然则假如我们不知道外部数据的编码,那末我们也就没法挪用iconv-lite来举行转换。
jschardet
jschardet 能够识别出一个Buffer数据所运用的编码花样,详细支撑的花样包含:
- Big5, GB2312/GB18030, EUC-TW, HZ-GB-2312, and ISO-2022-CN (Traditional and Simplified Chinese)
- EUC-JP, SHIFT_JIS, and ISO-2022-JP (Japanese)
- EUC-KR and ISO-2022-KR (Korean)
- KOI8-R, MacCyrillic, IBM855, IBM866, ISO-8859-5, and windows-1251 (Russian)
- ISO-8859-2 and windows-1250 (Hungarian)
- ISO-8859-5 and windows-1251 (Bulgarian)
- windows-1252
- ISO-8859-7 and windows-1253 (Greek)
- ISO-8859-8 and windows-1255 (Visual and Logical Hebrew)
- TIS-620 (Thai)
- UTF-32 BE, LE, 3412-ordered, or 2143-ordered (with a BOM)
- UTF-16 BE or LE (with a BOM)
- UTF-8 (with or without a BOM)
- ASCII
我们能方便地运用 jschardet 库:
const jschardet = require("jschardet");
// "次经常使用国字标准字体表" in Big5
jschardet.detect("\xa6\xb8\xb1\x60\xa5\xce\xb0\xea\xa6\x72\xbc\xd0\xb7\xc7\xa6\x72\xc5\xe9\xaa\xed");
// { encoding: "Big5", confidence: 0.99 }