@cobol-ts/cursor-loader-csv 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js.map +1 -0
- package/dist/src/csv.int.test.d.ts +1 -0
- package/dist/src/csv.int.test.js +92 -0
- package/dist/src/csv.int.test.js.map +1 -0
- package/dist/src/csv.parser.d.ts +34 -0
- package/dist/src/csv.parser.js +125 -0
- package/dist/src/csv.parser.js.map +1 -0
- package/dist/src/csv.projection.support.d.ts +16 -0
- package/dist/src/csv.projection.support.js +70 -0
- package/dist/src/csv.projection.support.js.map +1 -0
- package/dist/src/csv.projection.support.spec.d.ts +1 -0
- package/dist/src/csv.projection.support.spec.js +160 -0
- package/dist/src/csv.projection.support.spec.js.map +1 -0
- package/dist/src/csv.walker.d.ts +26 -0
- package/dist/src/csv.walker.js +200 -0
- package/dist/src/csv.walker.js.map +1 -0
- package/dist/src/csv.walker.spec.d.ts +1 -0
- package/dist/src/csv.walker.spec.js +412 -0
- package/dist/src/csv.walker.spec.js.map +1 -0
- package/dist/src/utf8.string.d.ts +12 -0
- package/dist/src/utf8.string.js +55 -0
- package/dist/src/utf8.string.js.map +1 -0
- package/dist/src/utf8.string.spec.d.ts +1 -0
- package/dist/src/utf8.string.spec.js +85 -0
- package/dist/src/utf8.string.spec.js.map +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAgC;AAChC,mDAAgC;AAChC,+DAA4C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const promises_1 = require("node:fs/promises");
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const cursor_projection_1 = require("@cobol-ts/cursor-projection");
|
|
6
|
+
const csv_parser_1 = require("./csv.parser");
|
|
7
|
+
const rowProjection = (0, cursor_projection_1.defineProjection)({
|
|
8
|
+
note: {
|
|
9
|
+
type: "string"
|
|
10
|
+
},
|
|
11
|
+
id: {
|
|
12
|
+
type: "integer"
|
|
13
|
+
},
|
|
14
|
+
name: {
|
|
15
|
+
type: "string"
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
test("parses and projects a CSV file", async () => {
|
|
19
|
+
const buffer = await (0, promises_1.readFile)((0, node_path_1.join)(__dirname, "..", "csv.walker.test.csv"));
|
|
20
|
+
const header = record(buffer, (0, csv_parser_1.createCsvRecordState)(), 0, 1);
|
|
21
|
+
const prepared = await csv_parser_1.csvParser.prepare(header, {});
|
|
22
|
+
if ("errors" in prepared) {
|
|
23
|
+
throw new Error(prepared.errors.join("; "));
|
|
24
|
+
}
|
|
25
|
+
const projection = (0, cursor_projection_1.createProjection)(csv_parser_1.csvParser, prepared, header, rowProjection);
|
|
26
|
+
const row = projection.create();
|
|
27
|
+
const state = (0, csv_parser_1.createCsvRecordState)();
|
|
28
|
+
const rows = [];
|
|
29
|
+
let startByte = header.endByte;
|
|
30
|
+
let recordNumber = 2;
|
|
31
|
+
while (startByte < buffer.length) {
|
|
32
|
+
const physicalRecord = record(buffer, state, startByte, recordNumber);
|
|
33
|
+
const representation = await csv_parser_1.csvParser.parse(physicalRecord, prepared);
|
|
34
|
+
if ("errors" in representation) {
|
|
35
|
+
throw new Error(representation.errors.join("; "));
|
|
36
|
+
}
|
|
37
|
+
projection.bind(row, representation, physicalRecord);
|
|
38
|
+
try {
|
|
39
|
+
rows.push({
|
|
40
|
+
id: row.id(),
|
|
41
|
+
name: row.name(),
|
|
42
|
+
note: normaliseLineEndings(row.note())
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
projection.clear(row);
|
|
47
|
+
}
|
|
48
|
+
startByte = physicalRecord.endByte;
|
|
49
|
+
recordNumber++;
|
|
50
|
+
}
|
|
51
|
+
expect(rows).toEqual([
|
|
52
|
+
{
|
|
53
|
+
id: 1,
|
|
54
|
+
name: "Alice",
|
|
55
|
+
note: "hello, world"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: 2,
|
|
59
|
+
name: "Bob",
|
|
60
|
+
note: "said \"hello\""
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 3,
|
|
64
|
+
name: "Chloë",
|
|
65
|
+
note: "line one\nline two"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: 4,
|
|
69
|
+
name: "Dan",
|
|
70
|
+
note: ""
|
|
71
|
+
}
|
|
72
|
+
]);
|
|
73
|
+
});
|
|
74
|
+
function normaliseLineEndings(value) {
|
|
75
|
+
return value.replace(/\r\n/g, "\n");
|
|
76
|
+
}
|
|
77
|
+
function record(buffer, recordState, startByte, recordNumber) {
|
|
78
|
+
return {
|
|
79
|
+
buffers: [buffer],
|
|
80
|
+
recordState,
|
|
81
|
+
startByte,
|
|
82
|
+
endByte: startByte,
|
|
83
|
+
lastValidByte: buffer.length,
|
|
84
|
+
fileFinished: true,
|
|
85
|
+
source: "csv.walker.test.csv",
|
|
86
|
+
recordNumber,
|
|
87
|
+
async readMore() {
|
|
88
|
+
throw new Error("Unexpected readMore");
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=csv.int.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv.int.test.js","sourceRoot":"","sources":["../../src/csv.int.test.ts"],"names":[],"mappings":";;AAAA,+CAA0C;AAC1C,yCAA+B;AAM/B,mEAGqC;AAErC,6CAIsB;AAUtB,MAAM,aAAa,GAAG,IAAA,oCAAgB,EAAS;IAC3C,IAAI,EAAE;QACF,IAAI,EAAE,QAAQ;KACjB;IACD,EAAE,EAAE;QACA,IAAI,EAAE,SAAS;KAClB;IACD,IAAI,EAAE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;QAC7B,YAAY;QAEZ,KAAK,CAAC,QAAQ;YACV,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QAC3C,CAAC;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Errors } from "@cobol-ts/errors";
|
|
2
|
+
import { type Parser, type ParserPhysicalRecordContent, type PhysicalRecordData, utf8RangeToString } from "@cobol-ts/cursor-loader-types";
|
|
3
|
+
import { type CsvProjectionPath } from "./csv.projection.support";
|
|
4
|
+
export interface CsvParserConfig {
|
|
5
|
+
readonly separator?: number;
|
|
6
|
+
readonly quote?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface CsvParserDetails {
|
|
9
|
+
readonly columns: ReadonlyMap<string, number>;
|
|
10
|
+
readonly columnCount: number;
|
|
11
|
+
readonly separator: number;
|
|
12
|
+
readonly quote: number;
|
|
13
|
+
}
|
|
14
|
+
export interface CsvRecordState {
|
|
15
|
+
starts: Int32Array;
|
|
16
|
+
ends: Int32Array;
|
|
17
|
+
fieldCount: number;
|
|
18
|
+
}
|
|
19
|
+
export type CsvRepresentation = PhysicalRecordData<CsvRecordState>;
|
|
20
|
+
export type CsvParser = Parser<CsvRepresentation, CsvParserConfig, CsvParserDetails, CsvRecordState, CsvProjectionPath>;
|
|
21
|
+
export declare function createCsvRecordState(): CsvRecordState;
|
|
22
|
+
export declare const csvParser: {
|
|
23
|
+
prepare: typeof prepare;
|
|
24
|
+
parse: typeof parse;
|
|
25
|
+
toDebugString: typeof utf8RangeToString;
|
|
26
|
+
compileProjectionPath: (details: CsvParserDetails, context: import("@cobol-ts/cursor-loader-types").PhysicalRecordContext | undefined, path: readonly string[]) => CsvProjectionPath;
|
|
27
|
+
integer: (representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath) => number;
|
|
28
|
+
float: (representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath) => number;
|
|
29
|
+
character: (representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath) => string;
|
|
30
|
+
string: (representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath) => string;
|
|
31
|
+
};
|
|
32
|
+
declare function prepare(record: ParserPhysicalRecordContent<CsvRecordState>, config: CsvParserConfig): Promise<CsvParserDetails | Errors>;
|
|
33
|
+
declare function parse(record: ParserPhysicalRecordContent<CsvRecordState>, details: CsvParserDetails): Promise<CsvRepresentation | Errors>;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.csvParser = void 0;
|
|
4
|
+
exports.createCsvRecordState = createCsvRecordState;
|
|
5
|
+
const cursor_loader_types_1 = require("@cobol-ts/cursor-loader-types");
|
|
6
|
+
const csv_projection_support_1 = require("./csv.projection.support");
|
|
7
|
+
const csv_walker_1 = require("./csv.walker");
|
|
8
|
+
const utf8_string_1 = require("./utf8.string");
|
|
9
|
+
const DEFAULT_SEPARATOR = ",".charCodeAt(0);
|
|
10
|
+
const DEFAULT_QUOTE = "\"".charCodeAt(0);
|
|
11
|
+
function createCsvRecordState() {
|
|
12
|
+
return {
|
|
13
|
+
starts: new Int32Array(0),
|
|
14
|
+
ends: new Int32Array(0),
|
|
15
|
+
fieldCount: 0
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
exports.csvParser = {
|
|
19
|
+
...csv_projection_support_1.csvProjectionSupport,
|
|
20
|
+
prepare,
|
|
21
|
+
parse,
|
|
22
|
+
toDebugString: cursor_loader_types_1.utf8RangeToString
|
|
23
|
+
};
|
|
24
|
+
async function prepare(record, config) {
|
|
25
|
+
const separator = config.separator ?? DEFAULT_SEPARATOR;
|
|
26
|
+
const quote = config.quote ?? DEFAULT_QUOTE;
|
|
27
|
+
validateByte("separator", separator);
|
|
28
|
+
validateByte("quote", quote);
|
|
29
|
+
if (separator === quote) {
|
|
30
|
+
throw new Error("CSV separator and quote must be different");
|
|
31
|
+
}
|
|
32
|
+
const context = {
|
|
33
|
+
record,
|
|
34
|
+
columns: new Map(),
|
|
35
|
+
quote,
|
|
36
|
+
fieldCount: 0,
|
|
37
|
+
duplicate: undefined,
|
|
38
|
+
decodeError: undefined
|
|
39
|
+
};
|
|
40
|
+
await (0, csv_walker_1.walkCsvRecord)(record, collectHeaderField, context, { separator, quote });
|
|
41
|
+
if (context.decodeError !== undefined) {
|
|
42
|
+
return {
|
|
43
|
+
errors: [
|
|
44
|
+
context.decodeError
|
|
45
|
+
]
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (context.duplicate !== undefined) {
|
|
49
|
+
return {
|
|
50
|
+
errors: [
|
|
51
|
+
`CSV header contains duplicate column ${JSON.stringify(context.duplicate)}`
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
columns: context.columns,
|
|
57
|
+
columnCount: context.fieldCount,
|
|
58
|
+
separator,
|
|
59
|
+
quote
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function collectHeaderField(context, column, startByte, endByte) {
|
|
63
|
+
context.fieldCount = column + 1;
|
|
64
|
+
if (context.decodeError !== undefined) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
let name;
|
|
68
|
+
try {
|
|
69
|
+
name = (0, utf8_string_1.utf8String)(context.record, startByte, endByte, true, context.quote);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
context.decodeError =
|
|
73
|
+
`CSV header column ${column + 1} is not valid UTF-8: ${errorMessage(error)}`;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (context.columns.has(name)) {
|
|
77
|
+
context.duplicate ??= name;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
context.columns.set(name, column);
|
|
81
|
+
}
|
|
82
|
+
async function parse(record, details) {
|
|
83
|
+
const state = record.recordState;
|
|
84
|
+
prepareState(state, details.columnCount);
|
|
85
|
+
state.fieldCount = 0;
|
|
86
|
+
await (0, csv_walker_1.walkCsvRecord)(record, collectField, state, {
|
|
87
|
+
separator: details.separator,
|
|
88
|
+
quote: details.quote
|
|
89
|
+
});
|
|
90
|
+
if (state.fieldCount !== details.columnCount) {
|
|
91
|
+
return {
|
|
92
|
+
errors: [
|
|
93
|
+
`CSV record has ${state.fieldCount} columns; header has ${details.columnCount}`
|
|
94
|
+
]
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return record;
|
|
98
|
+
}
|
|
99
|
+
function prepareState(state, columnCount) {
|
|
100
|
+
if (state.starts.length !== 0) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
state.starts = new Int32Array(columnCount);
|
|
104
|
+
state.ends = new Int32Array(columnCount);
|
|
105
|
+
}
|
|
106
|
+
function collectField(state, column, startByte, endByte) {
|
|
107
|
+
if (column < state.starts.length) {
|
|
108
|
+
state.starts[column] = startByte;
|
|
109
|
+
state.ends[column] = endByte;
|
|
110
|
+
}
|
|
111
|
+
state.fieldCount = column + 1;
|
|
112
|
+
}
|
|
113
|
+
function validateByte(name, value) {
|
|
114
|
+
if (!Number.isInteger(value)
|
|
115
|
+
|| value < 0
|
|
116
|
+
|| value > 255) {
|
|
117
|
+
throw new Error(`CSV ${name} must be a byte value from 0 to 255`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function errorMessage(error) {
|
|
121
|
+
return error instanceof Error
|
|
122
|
+
? error.message
|
|
123
|
+
: String(error);
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=csv.parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv.parser.js","sourceRoot":"","sources":["../../src/csv.parser.ts"],"names":[],"mappings":";;;AAwDA,oDAMC;AA5DD,uEAKuC;AAEvC,qEAGkC;AAElC,6CAA2C;AAC3C,+CAAyC;AAGzC,MAAM,iBAAiB,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC5C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AC,CAAC,KAAK,CAAC,OAAO;QACf,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type PhysicalRecordContext } from "@cobol-ts/cursor-loader-types";
|
|
2
|
+
import { type CsvParserDetails, type CsvRepresentation } from "./csv.parser";
|
|
3
|
+
export type CsvProjectionPath = number;
|
|
4
|
+
export declare const csvProjectionSupport: {
|
|
5
|
+
compileProjectionPath: typeof compileProjectionPath;
|
|
6
|
+
integer: typeof integer;
|
|
7
|
+
float: typeof float;
|
|
8
|
+
character: typeof character;
|
|
9
|
+
string: typeof string;
|
|
10
|
+
};
|
|
11
|
+
declare function compileProjectionPath(details: CsvParserDetails, context: PhysicalRecordContext | undefined, path: readonly string[]): CsvProjectionPath;
|
|
12
|
+
declare function integer(representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath): number;
|
|
13
|
+
declare function float(representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath): number;
|
|
14
|
+
declare function character(representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath): string;
|
|
15
|
+
declare function string(representation: CsvRepresentation, details: CsvParserDetails, column: CsvProjectionPath): string;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.csvProjectionSupport = void 0;
|
|
4
|
+
const utf8_string_1 = require("./utf8.string");
|
|
5
|
+
exports.csvProjectionSupport = {
|
|
6
|
+
compileProjectionPath,
|
|
7
|
+
integer,
|
|
8
|
+
float,
|
|
9
|
+
character,
|
|
10
|
+
string
|
|
11
|
+
};
|
|
12
|
+
function compileProjectionPath(details, context, path) {
|
|
13
|
+
if (context === undefined) {
|
|
14
|
+
throw new Error("CSV projection compilation requires preparation record context");
|
|
15
|
+
}
|
|
16
|
+
if (path.length !== 1) {
|
|
17
|
+
throw new Error(`${context.source}: record ${context.recordNumber}: `
|
|
18
|
+
+ `CSV projection path ${JSON.stringify(path)} must contain exactly one column name. `
|
|
19
|
+
+ `Available columns: ${formatColumns(details)}.`);
|
|
20
|
+
}
|
|
21
|
+
const name = path[0];
|
|
22
|
+
const column = details.columns.get(name);
|
|
23
|
+
if (column === undefined) {
|
|
24
|
+
throw new Error(`${context.source}: record ${context.recordNumber}: `
|
|
25
|
+
+ `CSV column ${JSON.stringify(name)} does not exist. `
|
|
26
|
+
+ `Available columns: ${formatColumns(details)}.`);
|
|
27
|
+
}
|
|
28
|
+
return column;
|
|
29
|
+
}
|
|
30
|
+
function integer(representation, details, column) {
|
|
31
|
+
const value = fieldString(representation, details, column);
|
|
32
|
+
if (value.length === 0) {
|
|
33
|
+
throw new Error("CSV value is empty; expected an integer");
|
|
34
|
+
}
|
|
35
|
+
const result = Number(value);
|
|
36
|
+
if (!Number.isInteger(result)) {
|
|
37
|
+
throw new Error(`CSV value ${JSON.stringify(value)} is not an integer`);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
function float(representation, details, column) {
|
|
42
|
+
const value = fieldString(representation, details, column);
|
|
43
|
+
if (value.length === 0) {
|
|
44
|
+
throw new Error("CSV value is empty; expected a number");
|
|
45
|
+
}
|
|
46
|
+
const result = Number(value);
|
|
47
|
+
if (!Number.isFinite(result)) {
|
|
48
|
+
throw new Error(`CSV value ${JSON.stringify(value)} is not a number`);
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
function character(representation, details, column) {
|
|
53
|
+
const value = fieldString(representation, details, column);
|
|
54
|
+
if (value.length !== 1) {
|
|
55
|
+
throw new Error(`CSV value ${JSON.stringify(value)} is not a character`);
|
|
56
|
+
}
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
function string(representation, details, column) {
|
|
60
|
+
return fieldString(representation, details, column);
|
|
61
|
+
}
|
|
62
|
+
function fieldString(representation, details, column) {
|
|
63
|
+
const state = representation.recordState;
|
|
64
|
+
return (0, utf8_string_1.utf8String)(representation, state.starts[column], state.ends[column], true, details.quote);
|
|
65
|
+
}
|
|
66
|
+
function formatColumns(details) {
|
|
67
|
+
const columns = [...details.columns.keys()].map(column => JSON.stringify(column));
|
|
68
|
+
return columns.length === 0 ? "<none>" : columns.join(", ");
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=csv.projection.support.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv.projection.support.js","sourceRoot":"","sources":["../../src/csv.projection.support.ts"],"names":[],"mappings":";;;AAUA,+CAAyC;AAM5B,QAAA,oBAAoB,GAAG;IAChC,qBAAqB;IACrB,OAAO;IACP,KAAK;IACL,SAAS;IACT,MAAM;CACyE,CAAC;AAGpF,SAAS,qBAAqB,CAC1B,OAAyB,EACzB,OAA0C,EAC1C,IAAuB;IAEvB,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,gEAAgE,CAAC,CAAC;IACtF,CAAC;IAED,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CACX,GAAG,OAAO,CAAC,MAAM,YAAY,OAAO,CAAC,YAAY,IAAI;cACnD,uBAAuB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,yCAAyC;cACpF,sBAAsB,aAAa,CAAC,OAAO,CAAC,GAAG,CACpD,CAAC;IACN,CAAC;IAED,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACrB,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAEzC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACX,GAAG,OAAO,CAAC,MAAM,YAAY,OAAO,CAAC,YAAY,IAAI;cACnD,cAAc,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,mBAAmB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C,cAAc,EAAC,OAAO,EAAC,MAAM,CAAC,CAAC;IAEzD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAGD,SAAS,MAAM,CACX,cAAiC,EACjC,OAAyB,EACzB,MAAyB;IAEzB,OAAO,WAAW,CAAC,cAAc,EAAC,OAAO,EAAC,MAAM,CAAC,CAAC;AACtD,CAAC;AAGD,SAAS,WAAW,CAChB,cAAiC,EACjC,OAAyB,EACzB,MAAyB;IAEzB,MAAM,KAAK,GAAG,cAAc,CAAC,WAAW,CAAC;IAEzC,OAAO,IAAA,wBAAU,EACb,cAAc,EACd,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EACpB,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAClB,IAAI,EACJ,OAAO,CAAC,KAAK,CAChB,CAAC;AACN,CAAC;AAGD,SAAS,aAAa,CAAC,OAAyB;IAC5C,MAAM,OAAO,GAAG,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IAClF,OAAO,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const csv_projection_support_1 = require("./csv.projection.support");
|
|
4
|
+
const ENCODER = new TextEncoder();
|
|
5
|
+
const SEPARATOR = ",".charCodeAt(0);
|
|
6
|
+
const QUOTE = "\"".charCodeAt(0);
|
|
7
|
+
const CONTEXT = {
|
|
8
|
+
source: "customers.csv",
|
|
9
|
+
recordNumber: 1
|
|
10
|
+
};
|
|
11
|
+
describe("csvProjectionSupport", () => {
|
|
12
|
+
test("compiles a column name to its column index", () => {
|
|
13
|
+
const details = csvDetails("id", "name", "age");
|
|
14
|
+
expect(csv_projection_support_1.csvProjectionSupport.compileProjectionPath(details, CONTEXT, ["age"])).toBe(2);
|
|
15
|
+
});
|
|
16
|
+
test("requires preparation record context", () => {
|
|
17
|
+
const details = csvDetails("id");
|
|
18
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.compileProjectionPath(details, undefined, ["id"]), "CSV projection compilation requires preparation record context");
|
|
19
|
+
});
|
|
20
|
+
test("rejects an empty projection path", () => {
|
|
21
|
+
const details = csvDetails("id");
|
|
22
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.compileProjectionPath(details, CONTEXT, []), 'customers.csv: record 1: CSV projection path [] must contain exactly one column name. Available columns: "id".');
|
|
23
|
+
});
|
|
24
|
+
test("rejects a nested projection path", () => {
|
|
25
|
+
const details = csvDetails("name");
|
|
26
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.compileProjectionPath(details, CONTEXT, ["customer", "name"]), 'customers.csv: record 1: CSV projection path ["customer","name"] must contain exactly one column name. Available columns: "name".');
|
|
27
|
+
});
|
|
28
|
+
test("rejects an unknown column", () => {
|
|
29
|
+
const details = csvDetails("id", "name");
|
|
30
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.compileProjectionPath(details, CONTEXT, ["age"]), 'customers.csv: record 1: CSV column "age" does not exist. Available columns: "id", "name".');
|
|
31
|
+
});
|
|
32
|
+
test("reads a string using a compiled column index", () => {
|
|
33
|
+
const representation = csvRepresentation("1001", "Alice", "42");
|
|
34
|
+
const details = csvDetails("id", "name", "age");
|
|
35
|
+
expect(csv_projection_support_1.csvProjectionSupport.string(representation, details, 1)).toBe("Alice");
|
|
36
|
+
});
|
|
37
|
+
test("uses byte offsets rather than character offsets", () => {
|
|
38
|
+
const representation = csvRepresentation("Chloë", "42");
|
|
39
|
+
const details = csvDetails("name", "age");
|
|
40
|
+
expect(csv_projection_support_1.csvProjectionSupport.string(representation, details, 1)).toBe("42");
|
|
41
|
+
});
|
|
42
|
+
test("reads a field spanning physical buffers", () => {
|
|
43
|
+
const representation = {
|
|
44
|
+
buffers: [
|
|
45
|
+
ENCODER.encode("Ali"),
|
|
46
|
+
ENCODER.encode("ce,42")
|
|
47
|
+
],
|
|
48
|
+
recordState: {
|
|
49
|
+
starts: new Int32Array([
|
|
50
|
+
0,
|
|
51
|
+
6
|
|
52
|
+
]),
|
|
53
|
+
ends: new Int32Array([
|
|
54
|
+
5,
|
|
55
|
+
8
|
|
56
|
+
]),
|
|
57
|
+
fieldCount: 2
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const details = csvDetails("name", "age");
|
|
61
|
+
expect(csv_projection_support_1.csvProjectionSupport.string(representation, details, 0)).toBe("Alice");
|
|
62
|
+
expect(csv_projection_support_1.csvProjectionSupport.integer(representation, details, 1)).toBe(42);
|
|
63
|
+
});
|
|
64
|
+
test("collapses doubled quotes when materialising a string", () => {
|
|
65
|
+
const representation = csvRepresentation("said \"\"hello\"\"");
|
|
66
|
+
const details = csvDetails("note");
|
|
67
|
+
expect(csv_projection_support_1.csvProjectionSupport.string(representation, details, 0)).toBe("said \"hello\"");
|
|
68
|
+
});
|
|
69
|
+
test("reads an integer", () => {
|
|
70
|
+
const representation = csvRepresentation("-42");
|
|
71
|
+
const details = csvDetails("value");
|
|
72
|
+
expect(csv_projection_support_1.csvProjectionSupport.integer(representation, details, 0)).toBe(-42);
|
|
73
|
+
});
|
|
74
|
+
test("rejects a non-integer", () => {
|
|
75
|
+
const representation = csvRepresentation("42.5");
|
|
76
|
+
const details = csvDetails("value");
|
|
77
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.integer(representation, details, 0), 'CSV value "42.5" is not an integer');
|
|
78
|
+
});
|
|
79
|
+
test("rejects an empty integer", () => {
|
|
80
|
+
const representation = csvRepresentation("");
|
|
81
|
+
const details = csvDetails("value");
|
|
82
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.integer(representation, details, 0), "CSV value is empty; expected an integer");
|
|
83
|
+
});
|
|
84
|
+
test("reads a float", () => {
|
|
85
|
+
const representation = csvRepresentation("1234.56");
|
|
86
|
+
const details = csvDetails("value");
|
|
87
|
+
expect(csv_projection_support_1.csvProjectionSupport.float(representation, details, 0)).toBe(1234.56);
|
|
88
|
+
});
|
|
89
|
+
test("rejects a non-number", () => {
|
|
90
|
+
const representation = csvRepresentation("banana");
|
|
91
|
+
const details = csvDetails("value");
|
|
92
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.float(representation, details, 0), 'CSV value "banana" is not a number');
|
|
93
|
+
});
|
|
94
|
+
test("rejects an empty float", () => {
|
|
95
|
+
const representation = csvRepresentation("");
|
|
96
|
+
const details = csvDetails("value");
|
|
97
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.float(representation, details, 0), "CSV value is empty; expected a number");
|
|
98
|
+
});
|
|
99
|
+
test("reads a character", () => {
|
|
100
|
+
const representation = csvRepresentation("A");
|
|
101
|
+
const details = csvDetails("status");
|
|
102
|
+
expect(csv_projection_support_1.csvProjectionSupport.character(representation, details, 0)).toBe("A");
|
|
103
|
+
});
|
|
104
|
+
test("rejects more than one character", () => {
|
|
105
|
+
const representation = csvRepresentation("AB");
|
|
106
|
+
const details = csvDetails("status");
|
|
107
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.character(representation, details, 0), 'CSV value "AB" is not a character');
|
|
108
|
+
});
|
|
109
|
+
test("rejects an empty character", () => {
|
|
110
|
+
const representation = csvRepresentation("");
|
|
111
|
+
const details = csvDetails("status");
|
|
112
|
+
expectError(() => csv_projection_support_1.csvProjectionSupport.character(representation, details, 0), 'CSV value "" is not a character');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
function expectError(action, message) {
|
|
116
|
+
try {
|
|
117
|
+
action();
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
expect(error).toEqual(new Error(message));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
throw new Error("Expected error");
|
|
124
|
+
}
|
|
125
|
+
function csvDetails(...columns) {
|
|
126
|
+
return {
|
|
127
|
+
columns: new Map(columns.map((name, index) => [
|
|
128
|
+
name,
|
|
129
|
+
index
|
|
130
|
+
])),
|
|
131
|
+
columnCount: columns.length,
|
|
132
|
+
separator: SEPARATOR,
|
|
133
|
+
quote: QUOTE
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function csvRepresentation(...fields) {
|
|
137
|
+
const starts = new Int32Array(fields.length);
|
|
138
|
+
const ends = new Int32Array(fields.length);
|
|
139
|
+
let byte = 0;
|
|
140
|
+
for (let index = 0; index < fields.length; index++) {
|
|
141
|
+
const field = ENCODER.encode(fields[index]);
|
|
142
|
+
starts[index] = byte;
|
|
143
|
+
byte += field.length;
|
|
144
|
+
ends[index] = byte;
|
|
145
|
+
if (index + 1 < fields.length) {
|
|
146
|
+
byte++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
buffers: [
|
|
151
|
+
ENCODER.encode(fields.join(","))
|
|
152
|
+
],
|
|
153
|
+
recordState: {
|
|
154
|
+
starts,
|
|
155
|
+
ends,
|
|
156
|
+
fieldCount: fields.length
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=csv.projection.support.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv.projection.support.spec.js","sourceRoot":"","sources":["../../src/csv.projection.support.spec.ts"],"names":[],"mappings":";;AAUA,qEAA8D;AAG9D,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,SAAS,GAAG,GAAG,CAAC,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 @@
|
|
|
1
|
+
import { type ParserPhysicalRecordContent } from "@cobol-ts/cursor-loader-types";
|
|
2
|
+
export declare const CSV_LINE_ENDING_LF: readonly number[];
|
|
3
|
+
export declare const CSV_LINE_ENDING_CRLF: readonly number[];
|
|
4
|
+
export interface CsvWalkOptions {
|
|
5
|
+
readonly separator?: number;
|
|
6
|
+
readonly quote?: number;
|
|
7
|
+
/**
|
|
8
|
+
* undefined:
|
|
9
|
+
* line ending is not yet known; recognise LF or CRLF.
|
|
10
|
+
*
|
|
11
|
+
* defined:
|
|
12
|
+
* recognise only this line ending.
|
|
13
|
+
*/
|
|
14
|
+
readonly lineEnding?: readonly number[];
|
|
15
|
+
}
|
|
16
|
+
export interface CsvWalkResult {
|
|
17
|
+
/**
|
|
18
|
+
* The line ending which terminated this record.
|
|
19
|
+
*
|
|
20
|
+
* undefined means the record ended at EOF without a line ending.
|
|
21
|
+
*/
|
|
22
|
+
readonly lineEnding: readonly number[] | undefined;
|
|
23
|
+
}
|
|
24
|
+
export type CsvFieldVisitor<TContext> = (context: TContext, column: number, startByte: number, endByte: number) => void;
|
|
25
|
+
export declare function csvLineEndingLength(byte: number, nextByte: number | undefined, lineEnding: readonly number[] | undefined): number;
|
|
26
|
+
export declare function walkCsvRecord<TRecordState, TContext>(record: ParserPhysicalRecordContent<TRecordState>, visitor: CsvFieldVisitor<TContext>, context: TContext, options?: CsvWalkOptions): Promise<CsvWalkResult>;
|