web-csv-toolbox 0.7.5 → 0.9.0
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 +2 -3
- package/dist/cjs/Lexer.cjs +1 -1
- package/dist/cjs/Lexer.cjs.map +1 -1
- package/dist/cjs/RecordAssembler.cjs +1 -1
- package/dist/cjs/RecordAssembler.cjs.map +1 -1
- package/dist/cjs/_virtual/web_csv_toolbox_wasm_bg.wasm.cjs +1 -1
- package/dist/cjs/assertCommonOptions.cjs +1 -1
- package/dist/cjs/assertCommonOptions.cjs.map +1 -1
- package/dist/es/Lexer.js +145 -77
- package/dist/es/Lexer.js.map +1 -1
- package/dist/es/RecordAssembler.js +1 -1
- package/dist/es/RecordAssembler.js.map +1 -1
- package/dist/es/_virtual/web_csv_toolbox_wasm_bg.wasm.js +1 -1
- package/dist/es/assertCommonOptions.js +20 -14
- package/dist/es/assertCommonOptions.js.map +1 -1
- package/dist/types/Lexer.d.ts +19 -0
- package/dist/types/Lexer.test.d.ts +1 -0
- package/dist/types/assertCommonOptions.d.ts +16 -3
- package/dist/types/common/types.d.ts +70 -6
- package/dist/web-csv-toolbox.umd.cjs +1 -1
- package/dist/web-csv-toolbox.umd.cjs.map +1 -1
- package/dist/web_csv_toolbox_wasm_bg.wasm +0 -0
- package/package.json +4 -4
package/dist/types/Lexer.d.ts
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
import type { CommonOptions, Token } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* CSV Lexer.
|
|
4
|
+
*
|
|
5
|
+
* Lexter tokenizes CSV data into fields and records.
|
|
6
|
+
*/
|
|
2
7
|
export declare class Lexer {
|
|
3
8
|
#private;
|
|
9
|
+
/**
|
|
10
|
+
* Constructs a new Lexer instance.
|
|
11
|
+
* @param options - The common options for the lexer.
|
|
12
|
+
*/
|
|
4
13
|
constructor({ delimiter, quotation, }?: CommonOptions);
|
|
14
|
+
/**
|
|
15
|
+
* Lexes the given chunk of CSV data.
|
|
16
|
+
* @param chunk - The chunk of CSV data to be lexed.
|
|
17
|
+
* @param buffering - Indicates whether the lexer is buffering or not.
|
|
18
|
+
* @returns An iterable iterator of tokens.
|
|
19
|
+
*/
|
|
5
20
|
lex(chunk: string | null, buffering?: boolean): IterableIterator<Token>;
|
|
21
|
+
/**
|
|
22
|
+
* Flushes the lexer and returns any remaining tokens.
|
|
23
|
+
* @returns An array of tokens.
|
|
24
|
+
*/
|
|
6
25
|
flush(): Token[];
|
|
7
26
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import type { CommonOptions } from "./common/types.ts";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Asserts that the provided options object contains all the required properties.
|
|
4
|
+
* Throws an error if any required property is missing
|
|
5
|
+
* or if the delimiter and quotation length is not 1 byte character,
|
|
6
|
+
* or if the delimiter is the same as the quotation.
|
|
4
7
|
*
|
|
5
|
-
* @
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* assertCommonOptions({
|
|
12
|
+
* quotation: '"',
|
|
13
|
+
* delimiter: ',',
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param options - The options object to be validated.
|
|
18
|
+
* @throws {Error} If any required property is missing or if the delimiter is the same as the quotation.
|
|
6
19
|
*/
|
|
7
|
-
export declare function assertCommonOptions(options: Required<CommonOptions>):
|
|
20
|
+
export declare function assertCommonOptions(options: Required<CommonOptions>): asserts options is Required<CommonOptions>;
|
|
@@ -1,4 +1,46 @@
|
|
|
1
1
|
import type { Field, FieldDelimiter, RecordDelimiter } from "./constants.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Position object.
|
|
4
|
+
*/
|
|
5
|
+
export interface Position {
|
|
6
|
+
/**
|
|
7
|
+
* Line number.
|
|
8
|
+
* Starts from 1.
|
|
9
|
+
*/
|
|
10
|
+
line: number;
|
|
11
|
+
/**
|
|
12
|
+
* Column number.
|
|
13
|
+
* Starts from 1.
|
|
14
|
+
*/
|
|
15
|
+
column: number;
|
|
16
|
+
/**
|
|
17
|
+
* Character offset.
|
|
18
|
+
* Starts from 0.
|
|
19
|
+
*/
|
|
20
|
+
offset: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Token location object.
|
|
24
|
+
*/
|
|
25
|
+
export interface TokenLocation {
|
|
26
|
+
/**
|
|
27
|
+
* Start location.
|
|
28
|
+
*/
|
|
29
|
+
start: Position;
|
|
30
|
+
/**
|
|
31
|
+
* End location.
|
|
32
|
+
*/
|
|
33
|
+
end: Position;
|
|
34
|
+
/**
|
|
35
|
+
* Row number.
|
|
36
|
+
* Starts from 1.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* This represents the logical row number in the CSV,
|
|
40
|
+
* counting from 1 for the first row, whether it is a header or not.
|
|
41
|
+
*/
|
|
42
|
+
rowNumber: number;
|
|
43
|
+
}
|
|
2
44
|
/**
|
|
3
45
|
* Field token type.
|
|
4
46
|
* @category Types
|
|
@@ -6,13 +48,32 @@ import type { Field, FieldDelimiter, RecordDelimiter } from "./constants.ts";
|
|
|
6
48
|
export interface FieldToken {
|
|
7
49
|
type: typeof Field;
|
|
8
50
|
value: string;
|
|
51
|
+
location: TokenLocation;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Field delimiter token type.
|
|
55
|
+
* @category Types
|
|
56
|
+
*/
|
|
57
|
+
export interface FieldDelimiterToken {
|
|
58
|
+
type: typeof FieldDelimiter;
|
|
59
|
+
value: string;
|
|
60
|
+
location: TokenLocation;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Record delimiter token type.
|
|
64
|
+
* @category Types
|
|
65
|
+
*/
|
|
66
|
+
export interface RecordDelimiterToken {
|
|
67
|
+
type: typeof RecordDelimiter;
|
|
68
|
+
value: string;
|
|
69
|
+
location: TokenLocation;
|
|
9
70
|
}
|
|
10
71
|
/**
|
|
11
72
|
* Token is a atomic unit of a CSV file.
|
|
12
73
|
* It can be a field, field delimiter, or record delimiter.
|
|
13
74
|
* @category Types
|
|
14
75
|
*/
|
|
15
|
-
export type Token = FieldToken |
|
|
76
|
+
export type Token = FieldToken | FieldDelimiterToken | RecordDelimiterToken;
|
|
16
77
|
/**
|
|
17
78
|
* CSV Common Options.
|
|
18
79
|
* @category Types
|
|
@@ -20,20 +81,23 @@ export type Token = FieldToken | typeof FieldDelimiter | typeof RecordDelimiter;
|
|
|
20
81
|
export interface CommonOptions {
|
|
21
82
|
/**
|
|
22
83
|
* CSV field delimiter.
|
|
84
|
+
* If you want to parse TSV, specify `'\t'`.
|
|
23
85
|
*
|
|
24
86
|
* @remarks
|
|
25
|
-
*
|
|
87
|
+
* Detail restrictions are as follows:
|
|
88
|
+
*
|
|
89
|
+
* - Must not be empty
|
|
90
|
+
* - Must be a single character
|
|
91
|
+
* - Multi-byte characters are not supported
|
|
92
|
+
* - Must not include CR or LF
|
|
93
|
+
* - Must not be the same as the quotation
|
|
26
94
|
*
|
|
27
|
-
* This library supports multi-character delimiters.
|
|
28
95
|
* @default ','
|
|
29
96
|
*/
|
|
30
97
|
delimiter?: string;
|
|
31
98
|
/**
|
|
32
99
|
* CSV field quotation.
|
|
33
100
|
*
|
|
34
|
-
* @remarks
|
|
35
|
-
* This library supports multi-character quotations.
|
|
36
|
-
*
|
|
37
101
|
* @default '"'
|
|
38
102
|
*/
|
|
39
103
|
quotation?: string;
|