Comparing version 1.5.0 to 1.6.0
@@ -15,4 +15,10 @@ import { FieldDescriptor } from './field-descriptor'; | ||
fields: FieldDescriptor[]; | ||
/** Reads a subset of records from this DBF file. */ | ||
readRecords(maxCount?: number): Promise<Record<string, unknown>[]>; | ||
/** | ||
* Reads a subset of records from this DBF file. If the `includeDeletedRecords` option is set, then deleted records | ||
* are included in the results, otherwise they are skipped. Deleted records have the property `[DELETED]: true`, | ||
* using the `DELETED` symbol exported from this library. | ||
*/ | ||
readRecords(maxCount?: number): Promise<(Record<string, unknown> & { | ||
[DELETED]?: true | undefined; | ||
})[]>; | ||
/** Appends the specified records to this DBF file. */ | ||
@@ -22,2 +28,3 @@ appendRecords(records: any[]): Promise<DBFFile>; | ||
_encoding: Encoding; | ||
_includeDeletedRecords: boolean; | ||
_recordsRead: number; | ||
@@ -29,1 +36,3 @@ _headerLength: number; | ||
} | ||
/** Symbol used for detecting deleted records when the `includeDeletedRecords` option is used. */ | ||
export declare const DELETED: unique symbol; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DBFFile = void 0; | ||
exports.DELETED = exports.DBFFile = void 0; | ||
const assert = require("assert"); | ||
@@ -24,2 +24,3 @@ const iconv = require("iconv-lite"); | ||
this._encoding = ''; | ||
this._includeDeletedRecords = false; | ||
this._recordsRead = 0; | ||
@@ -39,3 +40,7 @@ this._headerLength = 0; | ||
} | ||
/** Reads a subset of records from this DBF file. */ | ||
/** | ||
* Reads a subset of records from this DBF file. If the `includeDeletedRecords` option is set, then deleted records | ||
* are included in the results, otherwise they are skipped. Deleted records have the property `[DELETED]: true`, | ||
* using the `DELETED` symbol exported from this library. | ||
*/ | ||
readRecords(maxCount = 10000000) { | ||
@@ -50,2 +55,4 @@ return readRecordsFromDBF(this, maxCount); | ||
exports.DBFFile = DBFFile; | ||
/** Symbol used for detecting deleted records when the `includeDeletedRecords` option is used. */ | ||
exports.DELETED = Symbol(); | ||
//-------------------- Private implementation starts here -------------------- | ||
@@ -102,3 +109,6 @@ async function openDBF(path, opts) { | ||
// Validate the record length. | ||
assert(recordLength === calculateRecordLengthInBytes(fields), 'Invalid DBF: Incorrect record length'); | ||
const computedRecordLength = calculateRecordLengthInBytes(fields); | ||
if (options.readMode === 'loose') | ||
recordLength = computedRecordLength; | ||
assert(recordLength === computedRecordLength, 'Invalid DBF: Incorrect record length'); | ||
// Return a new DBFFile instance. | ||
@@ -111,2 +121,3 @@ let result = new DBFFile(); | ||
result._encoding = options.encoding; | ||
result._includeDeletedRecords = options.includeDeletedRecords; | ||
result._recordsRead = 0; | ||
@@ -248,3 +259,3 @@ result._headerLength = headerLength; | ||
let isDeleted = (buffer[offset++] === 0x2a); | ||
if (isDeleted) { | ||
if (isDeleted && !dbf._includeDeletedRecords) { | ||
offset += recordLength - 1; | ||
@@ -281,7 +292,8 @@ continue; | ||
value = null; | ||
break; | ||
} | ||
const julianDay = buffer.readInt32LE(offset); | ||
const msSinceMidnight = buffer.readInt32LE(offset + 4) + 1; | ||
value = utils_2.parseVfpDateTime({ julianDay, msSinceMidnight }); | ||
else { | ||
const julianDay = buffer.readInt32LE(offset); | ||
const msSinceMidnight = buffer.readInt32LE(offset + 4) + 1; | ||
value = utils_2.parseVfpDateTime({ julianDay, msSinceMidnight }); | ||
} | ||
offset += 8; | ||
@@ -378,3 +390,6 @@ break; | ||
} | ||
//add the record to the result. | ||
// If the record is marked as deleted, add the `[DELETED]` flag. | ||
if (isDeleted) | ||
record[exports.DELETED] = true; | ||
// Add the record to the result. | ||
records.push(record); | ||
@@ -439,8 +454,13 @@ } | ||
case 'L': // Boolean | ||
buffer.writeUInt8(value ? 0x54 /* 'T' */ : 0x46 /* 'F' */, offset++); | ||
buffer.writeUInt8(value === '' ? 0x20 : value ? 0x54 /* 'T' */ : 0x46 /* 'F' */, offset++); | ||
break; | ||
case 'T': // DateTime | ||
const { julianDay, msSinceMidnight } = utils_2.formatVfpDateTime(value); | ||
buffer.writeInt32LE(julianDay, offset); | ||
buffer.writeInt32LE(msSinceMidnight, offset + 4); | ||
if (!value) { | ||
iconv.encode(' ', encoding).copy(buffer, offset, 0, 8); | ||
} | ||
else { | ||
const { julianDay, msSinceMidnight } = utils_2.formatVfpDateTime(value); | ||
buffer.writeInt32LE(julianDay, offset); | ||
buffer.writeInt32LE(msSinceMidnight, offset + 4); | ||
} | ||
offset += 8; | ||
@@ -507,14 +527,18 @@ break; | ||
if (typeof value !== 'string') | ||
throw new Error('Expected a string'); | ||
throw new Error(`${name}: expected a string`); | ||
if (value.length > 255) | ||
throw new Error('Text is too long (maximum length is 255 chars)'); | ||
throw new Error(`${name}: text is too long (maximum length is 255 chars)`); | ||
} | ||
else if (type === 'N' || type === 'F' || type === 'I') { | ||
if (typeof value !== 'number') | ||
throw new Error('Expected a number'); | ||
throw new Error(`${name}: expected a number`); | ||
} | ||
else if (type === 'D') { | ||
if (!(value instanceof Date)) | ||
throw new Error('Expected a date'); | ||
throw new Error(`${name}: expected a date`); | ||
} | ||
else if (type === 'L') { | ||
if (typeof value !== 'boolean') | ||
throw new Error(`${name}: expected a boolean`); | ||
} | ||
} | ||
@@ -521,0 +545,0 @@ } |
@@ -1,3 +0,3 @@ | ||
export { DBFFile, DBFFile as default } from './dbf-file'; | ||
export { DBFFile, DBFFile as default, DELETED } from './dbf-file'; | ||
export { FieldDescriptor } from './field-descriptor'; | ||
export { CreateOptions, OpenOptions } from './options'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = exports.DBFFile = void 0; | ||
exports.DELETED = exports.default = exports.DBFFile = void 0; | ||
var dbf_file_1 = require("./dbf-file"); | ||
Object.defineProperty(exports, "DBFFile", { enumerable: true, get: function () { return dbf_file_1.DBFFile; } }); | ||
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return dbf_file_1.DBFFile; } }); | ||
Object.defineProperty(exports, "DELETED", { enumerable: true, get: function () { return dbf_file_1.DELETED; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -16,2 +16,4 @@ import { FileVersion } from './file-version'; | ||
encoding?: Encoding; | ||
/** Indicates whether deleted records should be included in results when reading records. Defaults to false. */ | ||
includeDeletedRecords?: boolean; | ||
} | ||
@@ -18,0 +20,0 @@ /** Options for creating a DBF file. */ |
@@ -8,3 +8,3 @@ "use strict"; | ||
function normaliseOpenOptions(options) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
// Validate `encoding`. | ||
@@ -18,4 +18,9 @@ let encoding = (_a = options === null || options === void 0 ? void 0 : options.encoding) !== null && _a !== void 0 ? _a : 'ISO-8859-1'; | ||
} | ||
// Validate `includeDeletedRecords`. | ||
let includeDeletedRecords = (_c = options === null || options === void 0 ? void 0 : options.includeDeletedRecords) !== null && _c !== void 0 ? _c : false; | ||
if (typeof includeDeletedRecords !== 'boolean') { | ||
throw new Error(`Invalid value 'includeDeletedRecords' value ${includeDeletedRecords}`); | ||
} | ||
// Return a new normalised options object. | ||
return { encoding, readMode }; | ||
return { encoding, readMode, includeDeletedRecords }; | ||
} | ||
@@ -22,0 +27,0 @@ exports.normaliseOpenOptions = normaliseOpenOptions; |
{ | ||
"name": "dbffile", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Read and write .dbf (dBase III & Visual FoxPro) files in Node.js", | ||
@@ -40,3 +40,3 @@ "main": "dist/index.js", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^8.10.49", | ||
"@types/node": "^12.x", | ||
"@types/rimraf": "^2.0.2", | ||
@@ -47,3 +47,3 @@ "chai": "^4.2.0", | ||
"rimraf": "^2.6.3", | ||
"typescript": "^4.0.2" | ||
"typescript": "^4.3.5" | ||
}, | ||
@@ -50,0 +50,0 @@ "dependencies": { |
@@ -24,2 +24,3 @@ # DBFFile | ||
- Can read records in arbitrary-sized batches | ||
- Can include deleted records in results | ||
- Supports very large files | ||
@@ -51,3 +52,3 @@ - Can create a new .dbf file | ||
let records = await dbf.readRecords(100); | ||
for (let record of records) console.log(records); | ||
for (let record of records) console.log(record); | ||
} | ||
@@ -156,2 +157,8 @@ ``` | ||
encoding?: Encoding; | ||
/** | ||
* Indicates whether deleted records should be included in results when reading records. Defaults to false. | ||
* Deleted records have the property `[DELETED]: true`, using the `DELETED` symbol exported from this library. | ||
*/ | ||
includeDeletedRecords?: boolean; | ||
} | ||
@@ -158,0 +165,0 @@ |
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
180006
848
180