diff --git a/encoding/iso2022.js b/encoding/iso2022.js index 99d4fdb..afff88a 100644 --- a/encoding/iso2022.js +++ b/encoding/iso2022.js @@ -1,4 +1,4 @@ -var util = require('../util'), +var util = require('util'), Match = require ('../match'); diff --git a/encoding/mbcs.js b/encoding/mbcs.js index b6da630..5aa1557 100644 --- a/encoding/mbcs.js +++ b/encoding/mbcs.js @@ -1,4 +1,4 @@ -var util = require('../util'), +var util = require('util'), Match = require ('../match'); /** diff --git a/encoding/sbcs.js b/encoding/sbcs.js index e79c738..80d525e 100644 --- a/encoding/sbcs.js +++ b/encoding/sbcs.js @@ -1,4 +1,4 @@ -var util = require('../util'), +var util = require('util'), Match = require ('../match'); /** diff --git a/encoding/unicode.js b/encoding/unicode.js index 6c5a907..6458d79 100644 --- a/encoding/unicode.js +++ b/encoding/unicode.js @@ -1,5 +1,5 @@ 'use strict'; -var util = require('../util'), +var util = require('util'), Match = require ('../match'); /** diff --git a/fs.js b/fs.js deleted file mode 100644 index 36a48a0..0000000 --- a/fs.js +++ /dev/null @@ -1,46 +0,0 @@ -var fs = require('fs'); -var self = require('./index'); - -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, opts)); - }; - - if (opts && opts.sampleSize) { - fd = fs.openSync(filepath, 'r'), - sample = Buffer.allocUnsafe(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, opts) { - if (opts && opts.sampleSize) { - var fd = fs.openSync(filepath, 'r'), - sample = Buffer.allocUnsafe(opts.sampleSize); - - fs.readSync(fd, sample, 0, opts.sampleSize); - fs.closeSync(fd); - return self.detect(sample, opts); - } - - return self.detect(fs.readFileSync(filepath), opts); - }; - \ No newline at end of file diff --git a/index.js b/index.js index 26bd737..91b2bec 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,6 @@ + +var fs = require('fs'); + var utf8 = require('./encoding/utf8'), unicode = require('./encoding/unicode'), mbcs = require('./encoding/mbcs'), @@ -75,6 +78,49 @@ module.exports.detect = function(buffer, opts) { } }; +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, opts)); + }; + + if (opts && opts.sampleSize) { + fd = fs.openSync(filepath, 'r'), + sample = Buffer.allocUnsafe(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, opts) { + if (opts && opts.sampleSize) { + var fd = fs.openSync(filepath, 'r'), + sample = Buffer.allocUnsafe(opts.sampleSize); + + fs.readSync(fd, sample, 0, opts.sampleSize); + fs.closeSync(fd); + return self.detect(sample, opts); + } + + return self.detect(fs.readFileSync(filepath), opts); +}; + // Wrappers for the previous functions to return all encodings module.exports.detectAll = function(buffer, opts) { if (typeof opts !== 'object') { diff --git a/util.js b/util.js deleted file mode 100644 index 037d986..0000000 --- a/util.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports.inherits = function(a, b) { - for (var key of Object.keys(b.prototype)) { - a.prototype[key] = b.prototype[key]; - } -}