@cobol-ts/cursor-loader-csv 0.0.0-stage → 0.0.1
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/README.md +245 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/package.json +60 -3
package/README.md
CHANGED
|
@@ -1,3 +1,246 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cobol-ts/cursor-loader-csv
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Byte-backed CSV parsing and lazy CSV projection support for the `@cobol-ts` cursor stack.
|
|
4
|
+
|
|
5
|
+
The CSV parser owns both CSV record recognition and structural field scanning. There is no lower generic line-framing pass.
|
|
6
|
+
|
|
7
|
+
That is essential because CR/LF inside a quoted field is data rather than a record boundary.
|
|
8
|
+
|
|
9
|
+
## Position in the stack
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
@cobol-ts/cursor-file
|
|
13
|
+
↓
|
|
14
|
+
@cobol-ts/cursor-record
|
|
15
|
+
↓
|
|
16
|
+
parser physical record starting at startByte
|
|
17
|
+
↓
|
|
18
|
+
csvParser.prepare() / csvParser.parse()
|
|
19
|
+
├── walkCsvRecord()
|
|
20
|
+
├── calls readMore() as required
|
|
21
|
+
├── recognizes the CSV row boundary
|
|
22
|
+
├── establishes endByte
|
|
23
|
+
└── records field byte ranges
|
|
24
|
+
↓
|
|
25
|
+
CsvRepresentation
|
|
26
|
+
= PhysicalRecordData<CsvRecordState>
|
|
27
|
+
↓
|
|
28
|
+
compiled numeric projection paths
|
|
29
|
+
↓
|
|
30
|
+
lazy field decoding
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Parser configuration
|
|
34
|
+
|
|
35
|
+
The current CSV parser configuration contains only physical CSV syntax bytes:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
interface CsvParserConfig {
|
|
39
|
+
readonly separator?: number;
|
|
40
|
+
readonly quote?: number;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Values are byte numbers, for example:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const config: CsvParserConfig = {
|
|
48
|
+
separator: ",".charCodeAt(0),
|
|
49
|
+
quote: "\"".charCodeAt(0)
|
|
50
|
+
};
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The defaults are comma and double quote.
|
|
54
|
+
|
|
55
|
+
Separator and quote must be distinct byte values in the range 0..255.
|
|
56
|
+
|
|
57
|
+
Logical projected columns are defined by the application projection, not by `CsvParserConfig`.
|
|
58
|
+
|
|
59
|
+
## Header preparation
|
|
60
|
+
|
|
61
|
+
CSV uses parser preparation.
|
|
62
|
+
|
|
63
|
+
The first CSV record is treated as the header. `prepare()` performs the same CSV grammatical scan used for data rows and may call `readMore()` if the header spans physical buffers.
|
|
64
|
+
|
|
65
|
+
Preparation produces:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
interface CsvParserDetails {
|
|
69
|
+
readonly columns: ReadonlyMap<string,number>;
|
|
70
|
+
readonly columnCount: number;
|
|
71
|
+
readonly separator: number;
|
|
72
|
+
readonly quote: number;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`columns` maps the decoded header names to physical column indexes.
|
|
77
|
+
|
|
78
|
+
The bootstrap header record is not projected as application data. `openRecordCursor()` retains only its `{source,recordNumber}` context after closing it.
|
|
79
|
+
|
|
80
|
+
That context is later available while projection paths are compiled.
|
|
81
|
+
|
|
82
|
+
## Projection-path compilation
|
|
83
|
+
|
|
84
|
+
CSV canonical projection paths must contain exactly one logical header name:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
["age"]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
During source setup:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
compileProjectionPath(
|
|
94
|
+
details,
|
|
95
|
+
preparationContext,
|
|
96
|
+
["age"]
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
looks up the prepared header once and returns the numeric column index.
|
|
101
|
+
|
|
102
|
+
Runtime access therefore does not perform a map lookup by column name.
|
|
103
|
+
|
|
104
|
+
Missing or malformed projected paths are setup/configuration failures and throw before data-row processing. Diagnostics include:
|
|
105
|
+
|
|
106
|
+
- header source;
|
|
107
|
+
- header record number;
|
|
108
|
+
- requested column/path;
|
|
109
|
+
- available columns.
|
|
110
|
+
|
|
111
|
+
For example:
|
|
112
|
+
|
|
113
|
+
```text
|
|
114
|
+
customers.csv: record 1: CSV column "postcode" does not exist. Variable columns: "id", "name", "age".
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Data-row representation
|
|
118
|
+
|
|
119
|
+
CSV does not construct a `CsvRow` object containing materialised values.
|
|
120
|
+
|
|
121
|
+
Its representation is the physical record data itself:
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
type CsvRepresentation =
|
|
125
|
+
PhysicalRecordData<CsvRecordState>;
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
with reusable structural state:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
interface CsvRecordState {
|
|
132
|
+
starts: Int32Array;
|
|
133
|
+
ends: Int32Array;
|
|
134
|
+
fieldCount: number;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The parser scans each row and records field start/end byte offsets. The structural arrays remain attached to the pooled physical-record holder and are reused.
|
|
139
|
+
|
|
140
|
+
## Parser-controlled record recognition
|
|
141
|
+
|
|
142
|
+
`walkCsvRecord()` starts at `record.startByte` and recognizes LF, CRLF or EOF as the end of the current CSV row **only when outside a quoted field**.
|
|
143
|
+
|
|
144
|
+
It also handles:
|
|
145
|
+
|
|
146
|
+
- separators;
|
|
147
|
+
- quoted fields;
|
|
148
|
+
- doubled quotes;
|
|
149
|
+
- CR/LF inside quoted fields;
|
|
150
|
+
- records spanning arbitrary physical buffer boundaries;
|
|
151
|
+
- carried records starting partway through an already-read buffer;
|
|
152
|
+
- final records without a line terminator.
|
|
153
|
+
|
|
154
|
+
When more bytes are required, it calls `record.readMore()` and continues in the same invocation.
|
|
155
|
+
|
|
156
|
+
This fuses boundary recognition and structural parsing into one scan.
|
|
157
|
+
|
|
158
|
+
## Lazy value materialisation
|
|
159
|
+
|
|
160
|
+
The parser must scan the complete CSV row to validate grammar and identify field boundaries.
|
|
161
|
+
|
|
162
|
+
It does **not** decode every field to a JavaScript string.
|
|
163
|
+
|
|
164
|
+
A runtime projection accessor already has a compiled numeric column index, so it reads the corresponding start/end byte range and decodes only that field when requested.
|
|
165
|
+
|
|
166
|
+
Quoted string materialisation collapses doubled quote characters as part of decoding.
|
|
167
|
+
|
|
168
|
+
Unused fields therefore avoid string allocation and value parsing.
|
|
169
|
+
|
|
170
|
+
## Row width errors
|
|
171
|
+
|
|
172
|
+
After a data row is scanned, `csvParser.parse()` compares its physical field count with the prepared header count.
|
|
173
|
+
|
|
174
|
+
A mismatch is returned as recoverable `Errors`:
|
|
175
|
+
|
|
176
|
+
```text
|
|
177
|
+
CSV record has 9 columns; header has 8
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`cursor-file` then applies the selected row-error policy and can continue with following records.
|
|
181
|
+
|
|
182
|
+
## CSV syntax errors
|
|
183
|
+
|
|
184
|
+
Structural syntax failures discovered by the walker throw an exception with physical provenance and logical byte position.
|
|
185
|
+
|
|
186
|
+
Examples include:
|
|
187
|
+
|
|
188
|
+
- quote inside an unquoted field;
|
|
189
|
+
- unexpected data after a closing quote;
|
|
190
|
+
- malformed CRLF after a closing quote;
|
|
191
|
+
- an unterminated quoted field at EOF.
|
|
192
|
+
|
|
193
|
+
A representative diagnostic is:
|
|
194
|
+
|
|
195
|
+
```text
|
|
196
|
+
customers.csv: record 3: CSV column 4: unexpected byte after closing quote at logical byte 80
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Because quoted newlines are not treated as boundaries, an error may legitimately be reported later than the first physical newline encountered inside the malformed quoted field.
|
|
200
|
+
|
|
201
|
+
## Runtime projection errors
|
|
202
|
+
|
|
203
|
+
Typed accessors validate the requested value lazily.
|
|
204
|
+
|
|
205
|
+
Examples:
|
|
206
|
+
|
|
207
|
+
```text
|
|
208
|
+
CSV value is empty; expected an integer
|
|
209
|
+
CSV value "twelve" is not an integer
|
|
210
|
+
CSV value "AB" is not a character
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The CSV support returns only the parser-specific reason. The generic projection layer adds:
|
|
214
|
+
|
|
215
|
+
- source;
|
|
216
|
+
- current data-record number;
|
|
217
|
+
- logical projection path;
|
|
218
|
+
- the rendered physical CSV record.
|
|
219
|
+
|
|
220
|
+
## Lifetime
|
|
221
|
+
|
|
222
|
+
`CsvRepresentation` borrows both the physical byte buffers and the `CsvRecordState` attached to that physical record.
|
|
223
|
+
|
|
224
|
+
A successful application projection is therefore valid only until its `FileCursorResult` is closed.
|
|
225
|
+
|
|
226
|
+
`cursor-file` keeps the physical record alive for that lease and clears the projection before releasing it.
|
|
227
|
+
|
|
228
|
+
## Performance intent
|
|
229
|
+
|
|
230
|
+
CSV is the first parser in this stack which exercises the late-materialisation design directly:
|
|
231
|
+
|
|
232
|
+
```text
|
|
233
|
+
scan row once
|
|
234
|
+
↓
|
|
235
|
+
retain field byte ranges in reusable arrays
|
|
236
|
+
↓
|
|
237
|
+
compile logical column names once per opened source
|
|
238
|
+
↓
|
|
239
|
+
decode only fields the application asks for
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
This reduces per-row materialisation without duplicating a generic line-framing scan.
|
|
243
|
+
|
|
244
|
+
## Projection lifetime and recent generic changes
|
|
245
|
+
|
|
246
|
+
CSV still uses one structural scan per row and decodes selected column values lazily. The generic projection layer now avoids reselection of already-bound objects and arrays, and may pass optional retained selection/provider values when rebinding. CSV's scalar path does not require ODO caches or parser-specific cleanup. For a malformed lazy projection value, the generic exception formatter reports the current file and record, plus an indexed application path when relevant selection context exists; this path is built only for failures.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./src/csv.walker"), exports);
|
|
18
|
+
__exportStar(require("./src/csv.parser"), exports);
|
|
19
|
+
__exportStar(require("./src/csv.projection.support"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobol-ts/cursor-loader-csv",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "parser for csv to support the cursor loader package",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"types": "dist/index",
|
|
7
|
+
"browserslist": {
|
|
8
|
+
"development": [
|
|
9
|
+
"last 1 chrome version",
|
|
10
|
+
"last 1 firefox version",
|
|
11
|
+
"last 1 safari version"
|
|
12
|
+
],
|
|
13
|
+
"production": [
|
|
14
|
+
">0.2%",
|
|
15
|
+
"not dead",
|
|
16
|
+
"not op_mini all"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@cobol-ts/cursor-loader-types": "0.0.1",
|
|
21
|
+
"@cobol-ts/errors": "0.0.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@testing-library/jest-dom": "^6.1.4",
|
|
25
|
+
"@types/jest": "^29.0.0",
|
|
26
|
+
"@types/node": "^14.0.0",
|
|
27
|
+
"jest": "^29.0.0",
|
|
28
|
+
"ts-jest": "^29.0.0",
|
|
29
|
+
"typescript": "^5.2.2"
|
|
30
|
+
},
|
|
31
|
+
"eslintConfig": {
|
|
32
|
+
"extends": [
|
|
33
|
+
"react-app",
|
|
34
|
+
"react-app/jest"
|
|
35
|
+
],
|
|
36
|
+
"overrides": [
|
|
37
|
+
{
|
|
38
|
+
"files": [
|
|
39
|
+
"**/*.stories.*"
|
|
40
|
+
],
|
|
41
|
+
"rules": {
|
|
42
|
+
"import/no-anonymous-default-export": "off"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"dist/index.js",
|
|
49
|
+
"dist/index.d.ts",
|
|
50
|
+
"dist/src/",
|
|
51
|
+
"build/"
|
|
52
|
+
],
|
|
53
|
+
"keywords": [
|
|
54
|
+
"indexing"
|
|
55
|
+
],
|
|
56
|
+
"license": "MIT",
|
|
57
|
+
"repository": "git@github.com:intellimaintain/intellimaintain.git",
|
|
58
|
+
"scripts": {
|
|
59
|
+
"test": "jest --config jest.config.json --env node",
|
|
60
|
+
"test:watch": "npm run test -- --watch",
|
|
61
|
+
"start": "react-scripts start"
|
|
62
|
+
}
|
|
6
63
|
}
|