web-csv-toolbox 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/LICENSE +21 -0
- package/README.md +191 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Yuki Yamazaki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# web-csv-toolbox
|
|
2
|
+
|
|
3
|
+
A CSV Toolbox utilizing Web Standard APIs.
|
|
4
|
+
|
|
5
|
+
## Key concepts
|
|
6
|
+
|
|
7
|
+
- Web Standards first.
|
|
8
|
+
- Using the [Web Streams API](https://streams.spec.whatwg.org/).
|
|
9
|
+
- TypeScript friendly & User friendly.
|
|
10
|
+
- Fully typed and documented.
|
|
11
|
+
- Zero dependencies.
|
|
12
|
+
- Using only Web Standards APIs.
|
|
13
|
+
- Property-based testing.
|
|
14
|
+
- Using [fast-check](https://fast-check.dev/) and [vitest](https://vitest.dev).
|
|
15
|
+
- **To Be Tested** Cross platform.
|
|
16
|
+
- Works on browsers and Node.js, Deno
|
|
17
|
+
- Only web standard APIs are used, so it should work with these Runtimes.
|
|
18
|
+
|
|
19
|
+
## Key features
|
|
20
|
+
|
|
21
|
+
- Parses CSV files using the [WHATWG Streams API](https://streams.spec.whatwg.org/).
|
|
22
|
+
- Supports parsing CSV files from strings, `ReadableStream`s, and `Response` objects.
|
|
23
|
+
- Supports parsing CSV files with different delimiters and quotation characters.
|
|
24
|
+
- Defaults to `,` and `"` respectively.
|
|
25
|
+
- Supports parsing TSV files by setting `delimiter` to `\t`.
|
|
26
|
+
- Supports parsing with multi-character/multi-byte delimiters and quotation characters.
|
|
27
|
+
- Supports parsing binary CSV files.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install web-csv-toolbox
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Parsing CSV files from strings
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { parse } from 'web-csv-toolbox';
|
|
41
|
+
|
|
42
|
+
const csv = `name,age
|
|
43
|
+
Alice,42
|
|
44
|
+
Bob,69`;
|
|
45
|
+
|
|
46
|
+
for await (const record of parse(csv)) {
|
|
47
|
+
console.log(record);
|
|
48
|
+
}
|
|
49
|
+
// Prints:
|
|
50
|
+
// { name: 'Alice', age: '42' }
|
|
51
|
+
// { name: 'Bob', age: '69' }
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Parsing CSV files from `ReadableStream`s
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
import { parse } from 'web-csv-toolbox';
|
|
58
|
+
|
|
59
|
+
const csv = `name,age
|
|
60
|
+
Alice,42
|
|
61
|
+
Bob,69`;
|
|
62
|
+
|
|
63
|
+
const stream = new ReadableStream({
|
|
64
|
+
start(controller) {
|
|
65
|
+
controller.enqueue(csv);
|
|
66
|
+
controller.close();
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
for await (const record of parse(stream)) {
|
|
71
|
+
console.log(record);
|
|
72
|
+
}
|
|
73
|
+
// Prints:
|
|
74
|
+
// { name: 'Alice', age: '42' }
|
|
75
|
+
// { name: 'Bob', age: '69' }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Parsing CSV files from `Response` objects
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { parse } from 'web-csv-toolbox';
|
|
82
|
+
|
|
83
|
+
const response = await fetch('https://example.com/data.csv');
|
|
84
|
+
|
|
85
|
+
for await (const record of parse(response)) {
|
|
86
|
+
console.log(record);
|
|
87
|
+
}
|
|
88
|
+
// Prints:
|
|
89
|
+
// { name: 'Alice', age: '42' }
|
|
90
|
+
// { name: 'Bob', age: '69' }
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Parsing CSV files with different delimiters and quotation characters
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
import { parse } from 'web-csv-toolbox';
|
|
97
|
+
|
|
98
|
+
const csv = `name\tage
|
|
99
|
+
Alice\t42
|
|
100
|
+
Bob\t69`;
|
|
101
|
+
|
|
102
|
+
for await (const record of parse(csv, { delimiter: '\t' })) {
|
|
103
|
+
console.log(record);
|
|
104
|
+
}
|
|
105
|
+
// Prints:
|
|
106
|
+
// { name: 'Alice', age: '42' }
|
|
107
|
+
// { name: 'Bob', age: '69' }
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Parsing CSV files with different headers
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
import { parse } from 'web-csv-toolbox';
|
|
114
|
+
|
|
115
|
+
const csv = `Alice,42
|
|
116
|
+
Bob,69`;
|
|
117
|
+
|
|
118
|
+
for await (const record of parse(csv, { headers: ['name', 'age'] })) {
|
|
119
|
+
console.log(record);
|
|
120
|
+
}
|
|
121
|
+
// Prints:
|
|
122
|
+
// { name: 'Alice', age: '42' }
|
|
123
|
+
// { name: 'Bob', age: '69' }
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## API
|
|
127
|
+
|
|
128
|
+
### High-level APIs
|
|
129
|
+
|
|
130
|
+
#### `parse(input[, options]): AsyncIterableIterator<Record>` function
|
|
131
|
+
|
|
132
|
+
Returns an asynchronous iterable of records.
|
|
133
|
+
|
|
134
|
+
##### `input` parameter
|
|
135
|
+
|
|
136
|
+
The input to parse. This can be a `string`, a `ReadableStream` of `string`s or `Uint8Array`s, or a `Response` object.
|
|
137
|
+
|
|
138
|
+
##### `options` parameter
|
|
139
|
+
|
|
140
|
+
An optional object with the following properties:
|
|
141
|
+
|
|
142
|
+
###### `options.delimiter`
|
|
143
|
+
|
|
144
|
+
The character used to separate fields in the CSV input. Defaults to `,`.
|
|
145
|
+
|
|
146
|
+
###### `options.quotation`
|
|
147
|
+
|
|
148
|
+
The character used to quote fields in the CSV input. Defaults to `"`.
|
|
149
|
+
|
|
150
|
+
###### `options.headers`
|
|
151
|
+
|
|
152
|
+
An optional array of strings to use as the headers for the parsed records.
|
|
153
|
+
|
|
154
|
+
If not provided, the first record will be used as the headers.
|
|
155
|
+
|
|
156
|
+
###### `options.decompression`
|
|
157
|
+
|
|
158
|
+
The decompression format to use when parsing the binary CSV input.
|
|
159
|
+
|
|
160
|
+
If not provided, the input will be treated as text.
|
|
161
|
+
|
|
162
|
+
Possible values are:
|
|
163
|
+
|
|
164
|
+
- `gzip`
|
|
165
|
+
- `deflate`
|
|
166
|
+
- `deflate-raw`
|
|
167
|
+
- Note: This format is supported in Node.js v21.2.0 and above.
|
|
168
|
+
|
|
169
|
+
###### `options.charset`
|
|
170
|
+
|
|
171
|
+
The character set to use when parsing the binary CSV input.
|
|
172
|
+
|
|
173
|
+
Defaults to `utf-8`.
|
|
174
|
+
|
|
175
|
+
###### `options.ignoreBOM`
|
|
176
|
+
|
|
177
|
+
Whether to ignore a leading BOM in the CSV input.
|
|
178
|
+
Defaults to `false`.
|
|
179
|
+
|
|
180
|
+
###### `options.fatal`
|
|
181
|
+
|
|
182
|
+
Whether to throw an error if the CSV input is invalid.
|
|
183
|
+
Defaults to `false`.
|
|
184
|
+
|
|
185
|
+
### Low-level APIs
|
|
186
|
+
|
|
187
|
+
For low-level API details, please refer to [source code](https://github.com/kamiazya/web-csv-toolbox).
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
[MIT](./LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "web-csv-toolbox",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A CSV Toolbox utilizing Web Standard APIs.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"module": "lib/index.js",
|
|
8
|
+
"types": "lib/index.d.ts",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18.0.0"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"default": "./lib/index.js",
|
|
15
|
+
"types": "./lib/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "vitest",
|
|
25
|
+
"format": "biome format . --write",
|
|
26
|
+
"lint": "biome lint .",
|
|
27
|
+
"check": "biome check src --apply",
|
|
28
|
+
"check:no-apply": "biome check src",
|
|
29
|
+
"build": "rollup -c rollup.config.ts --configPlugin rollup-plugin-typescript2 && biome format lib --write",
|
|
30
|
+
"prepare": "husky install"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/kamiazya/web-csv-toolbox.git"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"csv",
|
|
38
|
+
"parser",
|
|
39
|
+
"web-streams",
|
|
40
|
+
"web-streams-api"
|
|
41
|
+
],
|
|
42
|
+
"author": "Yuki Yamazaki <yuki@kamiazya.tech>",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/kamiazya/web-csv-toolbox/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/kamiazya/web-csv-toolbox#readme",
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@biomejs/biome": "1.4.1",
|
|
50
|
+
"@changesets/cli": "^2.27.1",
|
|
51
|
+
"@fast-check/vitest": "^0.0.8",
|
|
52
|
+
"husky": "^8.0.0",
|
|
53
|
+
"jsdom": "^23.0.1",
|
|
54
|
+
"lint-staged": "^15.2.0",
|
|
55
|
+
"rollup": "^4.9.1",
|
|
56
|
+
"rollup-plugin-delete": "^2.0.0",
|
|
57
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
58
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
59
|
+
"typescript": "^5.3.2",
|
|
60
|
+
"vitest": "^0.34.6"
|
|
61
|
+
}
|
|
62
|
+
}
|