typedsv
Version:
Parse and map delimiter-separated values (csv, tsv, etc.) to TypeScript/ES6+ classes.
260 lines • 11.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var stream_1 = require("stream");
var DefaultReaderOptions = Object.freeze({
strict: false,
headers: false,
quote: '"',
escape: '"',
delimiter: ',',
newline: '\n',
comment: '#',
range: { start: 1 }
});
var Reader = /** @class */ (function () {
function Reader(options) {
this.cr = '\r'.charCodeAt(0);
this.space = ' '.charCodeAt(0);
var opts = Object.assign({}, DefaultReaderOptions);
if (options) {
Object.entries(options)
.filter(function (_a) {
var _ = _a[0], value = _a[1];
return value;
})
.forEach(function (_a) {
var key = _a[0], value = _a[1];
return (opts[key] = value);
});
// If the quote character is not the default but the escape character is
// then the escape character is set to the custom quote character.
if (options.quote && options.quote !== DefaultReaderOptions.quote && !options.escape) {
opts.escape = opts.quote;
}
}
this.strict = opts.strict;
if (opts.range instanceof Array) {
opts.range = { start: opts.range[0], end: opts.range[1] };
}
this.range = opts.range;
this.headers = opts.headers;
this.quote = opts.quote.charCodeAt(0);
this.escape = opts.escape.charCodeAt(0);
this.delimiter = opts.delimiter.charCodeAt(0);
this.newline = opts.newline.charCodeAt(0);
this.comment = opts.comment.charCodeAt(0);
this.mapHeaders = opts.mapHeaders;
this.onHeaders = opts.onHeaders;
this.onRow = opts.onRow;
}
Reader.prototype.read = function (input) {
this.reset();
if (typeof input === 'string') {
var readable = new stream_1.Readable();
readable.push(input.trim());
readable.push(String.fromCharCode(this.newline));
readable.push(null);
input = readable;
}
return this.readReadable(input);
};
Reader.prototype.reset = function () {
this.escaped = false;
this.quoted = false;
this.commented = false;
this.lineNumber = 0;
this.actualLine = 0;
this.maxColumns = 0;
this.result = { headers: null, rows: [] };
if (this.headers instanceof Array) {
this.result.headers = this.headers;
}
else {
this.result.headers = this.headers ? [] : null;
}
};
Reader.prototype.readReadable = function (input) {
var _this = this;
var end = this.range.end;
var buffer = Buffer.alloc(0);
return new Promise(function (resolve, reject) {
input
.on('error', function (err) { return reject(err); })
.on('data', function (chunk) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
try {
buffer = Buffer.concat([buffer, chunk], buffer.length + chunk.length);
buffer = this.readBuffer(buffer);
if (end && this.lineNumber >= end) {
input.destroy();
input.emit('end');
}
}
catch (e) {
reject(e);
}
return [2 /*return*/];
});
}); })
.on('end', function () { return resolve(_this.result); });
});
};
Reader.prototype.readBuffer = function (input) {
var _a = this.range, start = _a.start, end = _a.end;
var row = [];
var cell = [];
this.escaped = false;
this.quoted = false;
this.commented = false;
var read = 0;
for (var i = 0; i < input.length; i++) {
// Return early if an explicit end has already been reached.
if (end && this.lineNumber >= end) {
return input.slice(0, 0);
}
var c = input[i];
var prev = i > 0 ? input[i - 1] : null;
var next = i < input.length - 1 ? input[i + 1] : null;
var eol = (c === this.cr && next === this.newline) || c === this.newline;
var isNextEol = next === this.newline || (next === this.cr && input[i + 2] === this.newline);
if (c === this.comment && !this.commented && !this.quoted) {
this.commented = true;
continue;
}
if (this.commented) {
if (eol) {
this.commented = false;
}
else {
continue;
}
}
if (c === this.quote) {
if (!this.quoted && cell.length === 0) {
this.quoted = true;
continue;
}
// Escape the next iteration if currently quoted and the escape character is the same
// as the quote character.
if (this.quoted && c === this.escape && next === this.quote && !this.escaped) {
this.escaped = true;
continue;
}
if (this.quoted && !this.escaped) {
this.quoted = false;
// Properly quoted but empty at EOL is OK.
if ((eol || isNextEol) && cell.length === 0) {
row.push('');
}
continue;
}
}
if (c === this.space && !this.quoted && (prev === this.quote || next === this.quote || cell.length === 0)) {
continue;
}
if (c === this.escape && this.quoted && next === this.quote && !this.escaped) {
this.escaped = true;
continue;
}
if (!this.quoted) {
if (c === this.delimiter) {
if (this.strict && (next === this.cr || next === this.newline)) {
throw new Error("Trailing delimiter found at the end of line " + (this.lineNumber + 1));
}
row.push(String.fromCharCode.apply(String, cell));
cell = [];
continue;
}
if (eol) {
this.lineNumber++;
if (row.length === 0 && cell.length === 0) {
continue;
}
this.actualLine++;
if (cell.length > 0) {
row.push(String.fromCharCode.apply(String, cell));
cell = [];
}
if ((!start || this.actualLine >= start) &&
(!end || this.actualLine + (this.actualLine % this.lineNumber) < end)) {
this.rowCallback(row);
read = read + ((c === this.cr ? i + 2 : i + 1) - read);
}
else {
read = i + 1;
}
row = [];
continue;
}
}
cell.push(c);
this.escaped = false;
}
return input.slice(read, input.length);
};
Reader.prototype.rowCallback = function (row) {
var _a, _b, _c, _d, _e;
if (this.actualLine === 1) {
this.maxColumns = row.length;
}
else if (this.strict && row.length !== this.maxColumns) {
throw new Error("Line " + this.lineNumber + " has " + row.length + " columns but " + this.maxColumns + " were expected");
}
if (this.headers) {
if (this.actualLine === 1 && typeof this.headers === 'boolean') {
this.result.headers = (_b = (_a = this.mapHeaders) === null || _a === void 0 ? void 0 : _a.call(this, row)) !== null && _b !== void 0 ? _b : row;
(_c = this.onHeaders) === null || _c === void 0 ? void 0 : _c.call(this, this.result.headers);
}
else {
var rows = this.result.rows;
var objectRow_1 = {};
this.result.headers.forEach(function (header, index) { return (objectRow_1[header] = row[index]); });
rows.push(objectRow_1);
(_d = this.onRow) === null || _d === void 0 ? void 0 : _d.call(this, objectRow_1, this.lineNumber);
}
}
else {
this.result.rows.push(row);
(_e = this.onRow) === null || _e === void 0 ? void 0 : _e.call(this, row, this.lineNumber);
}
};
return Reader;
}());
exports.Reader = Reader;
//# sourceMappingURL=Reader.js.map