-
Notifications
You must be signed in to change notification settings - Fork 22
/
node-icu-charset-detector.js
68 lines (54 loc) · 1.68 KB
/
node-icu-charset-detector.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
66
67
68
var NativeCharsetMatch = require("./build/Release/node-icu-charset-detector").CharsetMatch;
function CharsetMatch(buffer) {
if (!(buffer instanceof Buffer)) {
throw "Expected an instance of Buffer";
}
this.native = new NativeCharsetMatch(buffer);
}
CharsetMatch.prototype = {
getName: function () {
return this.native.getName.apply(this.native, arguments);
},
getLanguage: function () {
return this.native.getLanguage.apply(this.native, arguments);
},
getConfidence: function () {
return this.native.getConfidence.apply(this.native, arguments);
}
};
function detectCharset(buffer, options) {
var charsetMatch = new CharsetMatch(buffer);
if (charsetMatch.getName() === null) {
return null;
}
var charset = new String(charsetMatch.getName());
charset.language = charsetMatch.getLanguage();
charset.confidence = charsetMatch.getConfidence();
return charset;
}
function detectCharsetStream(stream, onDetectionFinish) {
var buffer = null;
var finished = false;
if (!(stream instanceof require("stream").Stream)) {
throw "Expected an instance of Stream";
}
function onChunkArrives(chunk) {
buffer = buffer ? Buffer.concat(buffer, chunk) : chunk;
var charset = detectCharset(buffer);
if (charset !== null) {
// detection succeed. stop reading chunks.
stream.removeListener("data", onChunkArrives);
finished = true;
onDetectionFinish(charset);
}
}
stream.on("data", onChunkArrives);
stream.on("end", function () {
if (!finished) {
onDetectionFinish(null);
}
});
}
exports.detectCharset = detectCharset;
exports.detectCharsetStream = detectCharsetStream;
exports.CharsetMatch = CharsetMatch;