diff --git a/lib/header.js b/lib/header.js index af17954..5690715 100644 --- a/lib/header.js +++ b/lib/header.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.3.3 +// Generated by CoffeeScript 1.6.2 (function() { var Header, fs, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; @@ -6,20 +6,19 @@ fs = require('fs'); Header = (function() { - function Header(filename) { this.filename = filename; this.parseFieldSubRecord = __bind(this.parseFieldSubRecord, this); - this.parseDate = __bind(this.parseDate, this); - return this; } Header.prototype.parse = function(callback) { var _this = this; + return fs.readFile(this.filename, function(err, buffer) { var i; + if (err) { throw err; } @@ -30,6 +29,7 @@ _this.recordLength = _this.convertBinaryToInteger(buffer.slice(10, 12)); _this.fields = ((function() { var _i, _ref, _results; + _results = []; for (i = _i = 32, _ref = this.start - 32; _i <= _ref; i = _i += 32) { _results.push(buffer.slice(i, i + 32)); @@ -42,6 +42,7 @@ Header.prototype.parseDate = function(buffer) { var day, month, year; + console.log(this.convertBinaryToInteger(buffer.slice(0, 1))); year = 1900 + this.convertBinaryToInteger(buffer.slice(0, 1)); month = (this.convertBinaryToInteger(buffer.slice(1, 2))) - 1; @@ -51,6 +52,7 @@ Header.prototype.parseFieldSubRecord = function(buffer) { var header; + return header = { name: ((buffer.slice(0, 11)).toString('utf-8')).replace(/[\u0000]+$/, ''), type: (buffer.slice(11, 12)).toString('utf-8'), diff --git a/lib/parser.js b/lib/parser.js index 28edbbe..dedd705 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.3.3 +// Generated by CoffeeScript 1.6.2 (function() { var EventEmitter, Header, Parser, fs, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, @@ -12,29 +12,28 @@ fs = require('fs'); Parser = (function(_super) { - __extends(Parser, _super); function Parser(filename) { this.filename = filename; this.parseField = __bind(this.parseField, this); - this.parseRecord = __bind(this.parseRecord, this); - this.parse = __bind(this.parse, this); - } Parser.prototype.parse = function() { var _this = this; + this.emit('start', this); this.header = new Header(this.filename); this.header.parse(function(err) { var sequenceNumber; + _this.emit('header', _this.header); sequenceNumber = 0; return fs.readFile(_this.filename, function(err, buffer) { var loc; + if (err) { throw err; } @@ -51,6 +50,7 @@ Parser.prototype.parseRecord = function(sequenceNumber, buffer) { var field, loc, record, _fn, _i, _len, _ref, _this = this; + record = { '@sequenceNumber': sequenceNumber, '@deleted': (buffer.slice(0, 1))[0] !== 32 @@ -69,9 +69,10 @@ Parser.prototype.parseField = function(field, buffer) { var value; - value = (buffer.toString('utf-8')).replace(/^\x20+|\x20+$/g, ''); + + value = buffer.toString().replace(/^\x20+|\x20+$/g, ''); if (field.type === 'N') { - value = parseInt(value, 10); + value = parseFloat(value); } return value; }; diff --git a/src/parser.coffee b/src/parser.coffee index 61ec67d..5fc0239 100644 --- a/src/parser.coffee +++ b/src/parser.coffee @@ -41,10 +41,10 @@ class Parser extends EventEmitter return record parseField: (field, buffer) => - value = (buffer.toString 'utf-8').replace /^\x20+|\x20+$/g, '' + value = buffer.toString().replace /^\x20+|\x20+$/g, '' - if field.type is 'N' then value = parseInt value, 10 + if field.type is 'N' then value = parseFloat value return value -module.exports = Parser \ No newline at end of file +module.exports = Parser