chardet
Advanced tools
Comparing version 0.4.2 to 0.5.0
21
index.js
@@ -38,3 +38,3 @@ | ||
module.exports.detect = function(buffer) { | ||
module.exports.detect = function(buffer, opts) { | ||
@@ -66,3 +66,3 @@ // Tally up the byte occurence statistics. | ||
var match = recognisers.map(function(rec) { | ||
var matches = recognisers.map(function(rec) { | ||
return rec.match(context); | ||
@@ -72,6 +72,11 @@ }).filter(function(match) { | ||
}).sort(function(a, b) { | ||
return a.confidence - b.confidence; | ||
}).pop(); | ||
return b.confidence - a.confidence; | ||
}); | ||
return match ? match.name : null; | ||
if (opts && opts.returnAllMatches === true) { | ||
return matches; | ||
} | ||
else { | ||
return matches.length > 0 ? matches[0].name : null; | ||
} | ||
}; | ||
@@ -93,3 +98,3 @@ | ||
if (err) return cb(err, null); | ||
cb(null, self.detect(buffer)); | ||
cb(null, self.detect(buffer, opts)); | ||
}; | ||
@@ -117,6 +122,6 @@ | ||
fs.closeSync(fd); | ||
return self.detect(sample); | ||
return self.detect(sample, opts); | ||
} | ||
return self.detect(fs.readFileSync(filepath)); | ||
return self.detect(fs.readFileSync(filepath), opts); | ||
}; |
{ | ||
"name": "chardet", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"homepage": "https://github.com/runk/node-chardet", | ||
@@ -16,7 +16,11 @@ "description": "Character detector", | ||
"contributors": [ | ||
"@spikying" | ||
"@spikying", | ||
"@wtgtybhertgeghgtwtg", | ||
"@suisho", | ||
"@seangarner", | ||
"@zevanty" | ||
], | ||
"devDependencies": { | ||
"github-publish-release": "^4.0.0", | ||
"mocha": "^4.0.1" | ||
"github-publish-release": "^4.2.0", | ||
"mocha": "^5.1.1" | ||
}, | ||
@@ -32,3 +36,3 @@ "repository": { | ||
"scripts": { | ||
"test": "mocha -R spec --recursive", | ||
"test": "mocha -R spec --recursive --bail", | ||
"release": "scripts/release" | ||
@@ -35,0 +39,0 @@ }, |
@@ -35,2 +35,12 @@ | ||
## Returning more detailed results | ||
If you wish to see the full list of possible encodings: | ||
```javascript | ||
chardet.detectFile('/path/to/file', { returnAllMatches: true }, function(err, encodings) { | ||
//encodings is an array of objects sorted by confidence value in decending order | ||
//e.g. [{ confidence: 90, name: 'UTF-8'}, {confidence: 20, name: 'windows-1252', lang: 'fr'}] | ||
}); | ||
``` | ||
## Supported Encodings: | ||
@@ -37,0 +47,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1652
77
74025
12