From 2225b86170aa0477c21ac089006854fe05c1758e Mon Sep 17 00:00:00 2001 From: Jeff ten Have Date: Mon, 6 Nov 2017 08:50:57 -0800 Subject: [PATCH 1/5] Added support for automatic detection of Date objects. --- json2dbf.js | 2 +- src/fields.js | 3 ++- src/lib.js | 5 +++++ src/structure.js | 4 ++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/json2dbf.js b/json2dbf.js index 758d2a5..a0ed2e4 100644 --- a/json2dbf.js +++ b/json2dbf.js @@ -2,7 +2,7 @@ var dbf = require('./'), fs = require('fs'); var buf = dbf.structure([ - {foo:'bar',noo:10}, + {foo:'bar',noo:new Date()}, {foo:'louie'} ]); diff --git a/src/fields.js b/src/fields.js index 5961b76..27c9285 100644 --- a/src/fields.js +++ b/src/fields.js @@ -4,6 +4,7 @@ var types = { string: 'C', number: 'N', boolean: 'L', + date: 'D', // type to use if all values of a field are null null: 'C' }; @@ -36,7 +37,7 @@ function inherit(a, b) { function obj(_) { var fields = {}, o = []; - for (var p in _) fields[p] = _[p] === null ? 'null' : typeof _[p]; + for (var p in _) fields[p] = _[p] === null ? 'null' : _[p] instanceof Date ? 'date' : typeof _[p]; for (var n in fields) { var t = types[fields[n]]; if(t){ diff --git a/src/lib.js b/src/lib.js index 0c7ff80..5b60ed0 100644 --- a/src/lib.js +++ b/src/lib.js @@ -31,3 +31,8 @@ module.exports.writeField = function writeField(view, fieldLength, str, offset) } return offset; }; + +module.exports.writeDate = function(date) { + if(!date || isNaN(date.getTime())) return " "; + return ("0000"+date.getFullYear()).slice(-4) + ("00"+(date.getMonth()+1)).slice(-2) + ("00"+date.getDate()).slice(-2); +}; diff --git a/src/structure.js b/src/structure.js index 4b02783..9bc286d 100644 --- a/src/structure.js +++ b/src/structure.js @@ -29,7 +29,7 @@ module.exports = function structure(data, meta) { view.setUint8(0, 0x03); // date of last update view.setUint8(1, now.getFullYear() - 1900); - view.setUint8(2, now.getMonth()); + view.setUint8(2, now.getMonth()+1); view.setUint8(3, now.getDate()); // number of records view.setUint32(4, data.length, true); @@ -75,7 +75,7 @@ module.exports = function structure(data, meta) { // date case 'D': offset = lib.writeField(view, 8, - lib.lpad(val.toString(), 8, ' '), offset); + lib.writeDate(val), offset); break; // number From a17636c9f29ca1719fc08c68a7834a5cf2567810 Mon Sep 17 00:00:00 2001 From: SeanR104 Date: Tue, 11 Jun 2024 14:44:58 -0500 Subject: [PATCH 2/5] added to gitignore and fixed depricated buffer in json2dbf.js --- .gitignore | 3 +++ json2dbf.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) mode change 100644 => 100755 json2dbf.js diff --git a/.gitignore b/.gitignore index 9917066..3037437 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ dbf.js +node_modules +foo.dbf +package-lock.json \ No newline at end of file diff --git a/json2dbf.js b/json2dbf.js old mode 100644 new mode 100755 index a0ed2e4..e6da01d --- a/json2dbf.js +++ b/json2dbf.js @@ -9,7 +9,7 @@ var buf = dbf.structure([ fs.writeFileSync('foo.dbf', toBuffer(buf.buffer)); function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength); + var buffer = Buffer.alloc(ab.byteLength); var view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; From 68cf432d38383c8f1b52b6550cf7f522d70b87c9 Mon Sep 17 00:00:00 2001 From: SeanR104 Date: Tue, 11 Jun 2024 18:24:33 -0500 Subject: [PATCH 3/5] deleted package-lock.json and node_modules, updated .gitignore --- .gitignore | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 3037437..ba84763 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1 @@ -dbf.js -node_modules -foo.dbf -package-lock.json \ No newline at end of file +dbf.js \ No newline at end of file From 0dcbbca3961da7bdc5275a825400ea959c54ec70 Mon Sep 17 00:00:00 2001 From: SeanR104 Date: Thu, 13 Jun 2024 14:56:36 -0500 Subject: [PATCH 4/5] created new record in test for dates and updated not a function error in lib.js from TypeScript --- json2dbf.js | 5 +++-- src/lib.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/json2dbf.js b/json2dbf.js index e6da01d..10eaa52 100755 --- a/json2dbf.js +++ b/json2dbf.js @@ -2,8 +2,9 @@ var dbf = require('./'), fs = require('fs'); var buf = dbf.structure([ - {foo:'bar',noo:new Date()}, - {foo:'louie'} + {foo:'bar',noo:10}, + {foo:'louie'}, + {baz:new Date()} ]); fs.writeFileSync('foo.dbf', toBuffer(buf.buffer)); diff --git a/src/lib.js b/src/lib.js index 5b60ed0..fbeeaa7 100644 --- a/src/lib.js +++ b/src/lib.js @@ -33,6 +33,6 @@ module.exports.writeField = function writeField(view, fieldLength, str, offset) }; module.exports.writeDate = function(date) { - if(!date || isNaN(date.getTime())) return " "; - return ("0000"+date.getFullYear()).slice(-4) + ("00"+(date.getMonth()+1)).slice(-2) + ("00"+date.getDate()).slice(-2); + if(!date || isNaN(new Date(date).getTime())) return " "; + return ("0000"+new Date(date).getFullYear()).slice(-4) + ("00"+(new Date(date).getMonth()+1)).slice(-2) + ("00"+new Date(date).getDate()).slice(-2); }; From c583b999b568b06ee1c2b9ebbb82806b776d8dd5 Mon Sep 17 00:00:00 2001 From: SeanR104 Date: Thu, 13 Jun 2024 15:00:42 -0500 Subject: [PATCH 5/5] updated dbf to version 0.3.0 in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 059c808..d1d282f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dbf", - "version": "0.2.0", + "version": "0.3.0", "description": "generate dbf files", "main": "index.js", "dependencies": {