-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
65 lines (59 loc) · 1.82 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
var iconv = require('iconv-lite');
var jconv = require('jconv');
var encodings = require('./encodings.js');
var mappings = {
'euc-jp': 'eucjp',
'shift_jis': 'sjis',
'utf-16le': 'utf16le',
'ucs-2': 'ucs2'
};
var jconvEncodings = [
'iso-2022-jp',
'iso-2022-jp-1',
'iso2022jp',
'jis',
'unicode'
];
exports.labels = encodings.map(function (enc) {
return enc.labels;
}).concat(Object.keys(require('iconv-lite/encodings'))).reduce(function (acc, next) {
return acc.concat(next);
}, []).filter(function (enc) {
return enc[0] !== '_';
}).concat(jconvEncodings).concat(Object.keys(mappings)).sort();
exports.labels = exports.labels.filter(function (enc, i) {
return exports.labels[i - 1] != enc;
});
exports.decode = decode;
function decode(source, encoding, options) {
if (encoding in mappings) encoding = mappings[encoding];
if (iconv.encodingExists(encoding)) {
return iconv.decode(source, encoding);
}
if (jconv.encodingExists(encoding)) {
return jconv.decode(source, encoding);
}
for (var i = 0; i < encodings.length; i++) {
if (encodings[i].labels.indexOf(encoding) !== -1) {
return encodings[i].decode(source.toString('binary'), options);
}
}
throw new TypeError('Unknown encoding: ' + JSON.stringify(encoding));
}
exports.encode = encode;
function encode(source, encoding, options) {
if (encoding in mappings) encoding = mappings[encoding];
if (iconv.encodingExists(encoding)) {
return iconv.encode(source, encoding);
}
if (jconv.encodingExists(encoding)) {
return jconv.encode(source, encoding);
}
for (var i = 0; i < encodings.length; i++) {
if (encodings[i].labels.indexOf(encoding) !== -1) {
return new Buffer(encodings[i].encode(source, options), 'binary');
}
}
throw new TypeError('Unknown encoding: ' + JSON.stringify(encoding));
}