Comparing version 0.3.0 to 0.4.0
44
index.js
@@ -76,11 +76,43 @@ | ||
module.exports.detectFile = function(filepath, fn) { | ||
fs.readFile(filepath, function(err, res) { | ||
if (err) return fn(err, null); | ||
fn(null, self.detect(res)); | ||
}); | ||
module.exports.detectFile = function(filepath, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = undefined; | ||
} | ||
var fd; | ||
var handler = function(err, buffer) { | ||
if (fd) { | ||
fs.closeSync(fd); | ||
} | ||
if (err) return cb(err, null); | ||
cb(null, self.detect(buffer)); | ||
}; | ||
if (opts && opts.sampleSize) { | ||
fd = fs.openSync(filepath, 'r'), | ||
sample = new Buffer(opts.sampleSize); | ||
fs.read(fd, sample, 0, opts.sampleSize, null, function(err) { | ||
handler(err, sample); | ||
}); | ||
return; | ||
} | ||
fs.readFile(filepath, handler); | ||
}; | ||
module.exports.detectFileSync = function(filepath) { | ||
module.exports.detectFileSync = function(filepath, opts) { | ||
if (opts && opts.sampleSize) { | ||
var fd = fs.openSync(filepath, 'r'), | ||
sample = new Buffer(opts.sampleSize); | ||
fs.readSync(fd, sample, 0, opts.sampleSize); | ||
fs.closeSync(fd); | ||
return self.detect(sample); | ||
} | ||
return self.detect(fs.readFileSync(filepath)); | ||
}; |
{ | ||
"name": "chardet", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/runk/node-chardet", | ||
@@ -5,0 +5,0 @@ "description": "Character detector", |
@@ -26,2 +26,11 @@ | ||
## Working with large data sets | ||
Sometimes, when data set is huge and you want to optimize performace (in tradeoff of less accuracy), | ||
you can sample only first N bytes of the buffer: | ||
```javascript | ||
chardet.detectFile('/path/to/file', { sampleSize: 32 }, function(err, encoding) {}); | ||
``` | ||
## Supported Encodings: | ||
@@ -28,0 +37,0 @@ |
@@ -23,2 +23,10 @@ var assert = require('assert'), | ||
}); | ||
it('should detect encoding with smaller sample size', function(done) { | ||
chardet.detectFile(path, { sampleSize: 32 }, function(err, res) { | ||
assert.equal(err, null); | ||
assert.equal(res, 'UTF-8'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -30,3 +38,7 @@ | ||
}); | ||
it('should detect encoding with smaller sample size', function() { | ||
assert.equal(chardet.detectFileSync(path, { sampleSize: 32 }), 'UTF-8'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1543387
1868
67