Skip to content

Commit f80d78f

Browse files
committed
Add charOffset and charCount to meta
1 parent 7a90f58 commit f80d78f

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,11 @@ export function* parse(text: string, options: ParseOptions & {
243243
meta: {
244244
delimiter,
245245
newline,
246-
// cursor: lastCursor,
247-
cursor,
248246
byteOffset,
249247
byteCount,
248+
charOffset: lastCursor,
249+
charCount: string.length,
250+
cursor, // it's the next charOffset
250251
},
251252
}
252253
lastCursor = cursor

src/types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ export interface ParseMeta {
2929
/** Number of bytes parsed, including line breaks, BOM, spaces, etc. */
3030
// numBytes: number
3131

32-
/** Byte offset */
32+
/** Byte offset at the start of the row */
3333
byteOffset: number
3434
/** Number of bytes consumed in this row */
3535
byteCount: number
36+
/** Character offset at the start of the row */
37+
charOffset: number
38+
/** Number of characters consumed in this row */
39+
charCount: number
3640
}
3741

3842
/**

tests/text.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ describe('parseText', () => {
1717
errors: [],
1818
meta: {
1919
cursor: text.length,
20-
byteOffset: 0,
2120
delimiter: ',',
2221
newline: '\n' as const,
22+
byteOffset: 0,
2323
byteCount: bytes.length,
24+
charOffset: 0,
25+
charCount: text.length,
2426
},
2527
})
2628
}

tests/url.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ function* parseMock(text: string): Generator<ParseResult, void, unknown> {
1414
meta: {
1515
byteOffset: 0,
1616
byteCount: bytes.length,
17+
charOffset: 0,
18+
charCount: text.length,
1719
newline: '\n',
1820
// quote: '"',
1921
delimiter: ',',
@@ -107,8 +109,10 @@ describe('parseURL, while mocking parse, ', () => {
107109
row: [],
108110
errors: [],
109111
meta: {
110-
byteCount: 2 * text.length,
111112
byteOffset: 0,
113+
byteCount: 2 * text.length,
114+
charOffset: 0,
115+
charCount: 2 * text.length,
112116
newline: '\n',
113117
// quote: '"',
114118
delimiter: ',',

0 commit comments

Comments
 (0)