Comparing version 0.0.0 to 0.0.1
{ | ||
"name": "dbf", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "generate dbf files", | ||
"main": "index.js", | ||
"dependencies": { | ||
"jdataview": "~2.4.4" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -10,0 +8,0 @@ "expect.js": "~0.2.0", |
# dbf | ||
based on https://code.google.com/p/js2shapefile/ but aiming for a full rewrite. | ||
Create [DBF](http://en.wikipedia.org/wiki/DBASE) database files in pure | ||
JavaScript. | ||
DBF is a zombie file format kept alive by inclusion in the [Shapefile](http://en.wikipedia.org/wiki/Shapefile) | ||
file format. | ||
## usage | ||
@@ -10,2 +14,20 @@ | ||
### api | ||
`arraybuffer = dbf.writer(fields, data)` | ||
Fields as objects with | ||
* type: one of | ||
* C: String | ||
* L: Bool | ||
* D: Date | ||
* N, M, F, B: Number | ||
* name: column name | ||
And data is an array of objects that represent rows. | ||
Returns an [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBuffer) | ||
of binary data. | ||
### example | ||
@@ -12,0 +34,0 @@ |
@@ -28,3 +28,5 @@ var fieldTypes = { | ||
attributes.forEach(function(attr, i) { | ||
attributes.forEach(writeAttribute); | ||
function writeAttribute(attr, i) { | ||
var fieldLength = fieldTypes[attr.type](attr); | ||
@@ -34,7 +36,8 @@ | ||
view.setInt8(i * 32 + 11, attr.type.charCodeAt(0)); | ||
view.setInt8(i * 32 + 16, fieldLength); | ||
var i32 = i * 32; | ||
view.setInt8(i32 + 11, attr.type.charCodeAt(0)); | ||
view.setInt8(i32 + 16, fieldLength); | ||
if (attr.type == 'N') { | ||
view.setInt8(i * 32 + 17, attr.scale || 0); | ||
view.setInt8(i32 + 17, attr.scale || 0); | ||
} | ||
@@ -44,5 +47,5 @@ | ||
bytesPerRecord += fieldLength; | ||
}); | ||
} | ||
// mark end of header | ||
// end of header | ||
view.setInt8(fieldDescLength - 1, 13); | ||
@@ -58,10 +61,6 @@ | ||
// var dbfHeaderBlob = new BlobBuilder(); | ||
// dbfHeaderBlob.append(dbfHeaderView.getBuffer()); | ||
// dbfHeaderBlob.append(view.getBuffer()); | ||
return { | ||
recordLength: bytesPerRecord, | ||
header: headerBuffer, | ||
field: buffer | ||
header: headerView, | ||
field: view | ||
}; | ||
@@ -68,0 +67,0 @@ }; |
@@ -5,3 +5,3 @@ var lib = require('./lib'); | ||
var dataLength = (recordLength) * rows.length + 1, | ||
var dataLength = recordLength * rows.length + 1, | ||
buffer = new ArrayBuffer(dataLength), | ||
@@ -11,9 +11,8 @@ view = new DataView(buffer), | ||
rows.forEach(function(row) { | ||
rows.forEach(writeRow); | ||
function writeRow(row) { | ||
view.setUint8(offset, 32); | ||
offset += 1; | ||
offset = writeRow(row, offset); | ||
}); | ||
function writeRow(row, offset) { | ||
offset++; | ||
attributes.forEach(writeAttribute); | ||
@@ -24,4 +23,3 @@ | ||
fieldLength = attribute.length || 0, | ||
val = row[attribute.name] || rownum.toString(), | ||
str; | ||
val = row[attribute.name] || rownum.toString(); | ||
@@ -31,3 +29,3 @@ switch (dataType) { | ||
view.setUint8(offset, val ? 84 : 70); | ||
offset += 1; | ||
offset++; | ||
break; | ||
@@ -53,4 +51,2 @@ | ||
} | ||
return offset; | ||
} | ||
@@ -60,3 +56,3 @@ | ||
return buffer; | ||
return view; | ||
}; |
@@ -1,23 +0,21 @@ | ||
var fs = require('fs'), | ||
record = require('./record'), | ||
var record = require('./record'), | ||
header = require('./header'); | ||
module.exports = function write(fields, data) { | ||
var headData = header(fields); | ||
var rowData = record(fields, data, headData.recordLength); | ||
var out = fs.createWriteStream('foo.dbf'); | ||
fs.writeFileSync('foo.dbf', Buffer.concat([ | ||
toBuffer(headData.header), | ||
toBuffer(headData.field), | ||
toBuffer(rowData) | ||
])); | ||
var head = header(fields), | ||
row = record(fields, data, head.recordLength); | ||
return combine(combine(head.header, head.field), row); | ||
}; | ||
function toBuffer(ab) { | ||
var buffer = new Buffer(ab.byteLength); | ||
var view = new Uint8Array(ab); | ||
for (var i = 0; i < buffer.length; ++i) { | ||
buffer[i] = view[i]; | ||
function combine(a, b) { | ||
var c = new ArrayBuffer(a.byteLength + b.byteLength), | ||
d = new DataView(c); | ||
for (var i = 0; i < a.byteLength; i++) { | ||
d.setUint8(i, a.getUint8(i)); | ||
} | ||
return buffer; | ||
for (; i < a.byteLength + b.byteLength; i++) { | ||
d.setUint8(i, b.getUint8(i - a.byteLength)); | ||
} | ||
return d; | ||
} |
Sorry, the diff of this file is not supported yet
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
8422
0
207
50
- Removedjdataview@~2.4.4
- Removedjdataview@2.4.6(transitive)