Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support of dbf files encoded in utf-8 #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var TextDecoder = require('text-encoding').TextDecoder;

function dbfHeader(buffer) {
var data = new DataView(buffer);
var out = {};
Expand Down Expand Up @@ -28,9 +30,9 @@ function dbfRowHeader(buffer, headerLen) {
return out;
}

function rowFuncs(buffer, offset, len, type) {
function rowFuncs(buffer, offset, len, type, textDecoder) {
var data = (new Uint8Array(buffer, offset, len));
var textData = String.fromCharCode.apply(this, data).replace(/\0|\s+$/g, '');
var textData = textDecoder.decode(data).replace(/\0|\s+$/g, '');
switch (type) {
case 'N':
case 'F':
Expand All @@ -45,15 +47,15 @@ function rowFuncs(buffer, offset, len, type) {
}
}

function parseRow(buffer, offset, rowHeaders) {
function parseRow(buffer, offset, rowHeaders, textDecoder) {
var out = {};
var i = 0;
var len = rowHeaders.length;
var field;
var header;
while (i < len) {
header = rowHeaders[i];
field = rowFuncs(buffer, offset, header.len, header.dataType);
field = rowFuncs(buffer, offset, header.len, header.dataType, textDecoder);
offset += header.len;
if (typeof field !== 'undefined') {
out[header.name] = field;
Expand All @@ -62,16 +64,19 @@ function parseRow(buffer, offset, rowHeaders) {
}
return out;
}
module.exports = function(buffer) {
module.exports = function(buffer, encoding) {
var header = dbfHeader(buffer);
var rowHeaders = dbfRowHeader(buffer, header.headerLen - 1);

var offset = ((rowHeaders.length + 1) << 5) + 2;
var recLen = header.recLen;
var records = header.records;
var out = [];

var textDecoder = new TextDecoder(encoding || 'utf-8');

while (records) {
out.push(parseRow(buffer, offset, rowHeaders));
out.push(parseRow(buffer, offset, rowHeaders, textDecoder));
offset += recLen;
records--;
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"chai": "^3.3.0",
"jshint": "^2.8.0",
"mocha": "^2.3.3"
},
"dependencies": {
"text-encoding": "^0.6.4"
}
}