-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathheader.js
More file actions
70 lines (60 loc) · 2.35 KB
/
header.js
File metadata and controls
70 lines (60 loc) · 2.35 KB
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
69
70
// Generated by CoffeeScript 1.7.1
(function() {
var Header, fs,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
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) {
return fs.readFile(this.filename, (function(_this) {
return function(err, buffer) {
var i;
if (err) {
throw err;
}
_this.type = (buffer.slice(0, 1)).toString('utf-8');
_this.dateUpdated = _this.parseDate(buffer.slice(1, 4));
_this.numberOfRecords = _this.convertBinaryToInteger(buffer.slice(4, 8));
_this.start = _this.convertBinaryToInteger(buffer.slice(8, 10));
_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));
}
return _results;
}).call(_this)).map(_this.parseFieldSubRecord);
return callback(_this);
};
})(this));
};
Header.prototype.parseDate = function(buffer) {
var day, month, year;
year = 1900 + this.convertBinaryToInteger(buffer.slice(0, 1));
month = (this.convertBinaryToInteger(buffer.slice(1, 2))) - 1;
day = this.convertBinaryToInteger(buffer.slice(2, 3));
return new Date(year, month, day);
};
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'),
displacement: this.convertBinaryToInteger(buffer.slice(12, 16)),
length: this.convertBinaryToInteger(buffer.slice(16, 17)),
decimalPlaces: this.convertBinaryToInteger(buffer.slice(17, 18))
};
};
Header.prototype.convertBinaryToInteger = function(buffer) {
return buffer.readInt32LE(0, true);
};
return Header;
})();
module.exports = Header;
}).call(this);