typed-csv 1.0.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 +190 -0
- package/dist/csv-loader/esbuild.d.mts +45 -0
- package/dist/csv-loader/esbuild.d.ts +45 -0
- package/dist/csv-loader/esbuild.js +2020 -0
- package/dist/csv-loader/esbuild.mjs +1985 -0
- package/dist/csv-loader/loader.d.mts +154 -0
- package/dist/csv-loader/loader.d.ts +154 -0
- package/dist/csv-loader/loader.js +1736 -0
- package/dist/csv-loader/loader.mjs +1701 -0
- package/dist/csv-loader/rollup.d.mts +60 -0
- package/dist/csv-loader/rollup.d.ts +60 -0
- package/dist/csv-loader/rollup.js +2006 -0
- package/dist/csv-loader/rollup.mjs +1971 -0
- package/dist/csv-loader/webpack.d.mts +42 -0
- package/dist/csv-loader/webpack.d.ts +42 -0
- package/dist/csv-loader/webpack.js +1979 -0
- package/dist/csv-loader/webpack.mjs +1948 -0
- package/dist/index.d.mts +65 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +877 -0
- package/dist/index.mjs +845 -0
- package/package.json +76 -0
package/README.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# typed-csv
|
|
2
|
+
|
|
3
|
+
A TypeScript library for typed CSV data with inline schema validation using a TypeScript-like syntax with `;` instead of `,`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install typed-csv
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic Example
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { defineSchema } from 'typed-csv';
|
|
17
|
+
|
|
18
|
+
// Define a schema
|
|
19
|
+
const stringSchema = defineSchema('string');
|
|
20
|
+
const numberSchema = defineSchema('number');
|
|
21
|
+
const booleanSchema = defineSchema('boolean');
|
|
22
|
+
|
|
23
|
+
// Parse values
|
|
24
|
+
const name = stringSchema.parse('hello'); // "hello"
|
|
25
|
+
const age = numberSchema.parse('42'); // 42
|
|
26
|
+
const active = booleanSchema.parse('true'); // true
|
|
27
|
+
|
|
28
|
+
// Validate parsed values
|
|
29
|
+
stringSchema.validator(name); // true
|
|
30
|
+
numberSchema.validator(name); // false
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Tuples
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
const tupleSchema = defineSchema('[string; number; boolean]');
|
|
37
|
+
|
|
38
|
+
// With brackets
|
|
39
|
+
const value1 = tupleSchema.parse('[hello; 42; true]');
|
|
40
|
+
// ["hello", 42, true]
|
|
41
|
+
|
|
42
|
+
// Without brackets (outermost brackets are optional)
|
|
43
|
+
const value2 = tupleSchema.parse('hello; 42; true');
|
|
44
|
+
// ["hello", 42, true]
|
|
45
|
+
|
|
46
|
+
tupleSchema.validator(value1); // true
|
|
47
|
+
tupleSchema.validator(['a', 'b', true]); // false (second element should be number)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Arrays
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
// Array syntax: Type[] or [Type][]
|
|
54
|
+
const stringArray = defineSchema('string[]');
|
|
55
|
+
const numberArray = defineSchema('[number][]');
|
|
56
|
+
|
|
57
|
+
// With brackets
|
|
58
|
+
const names1 = stringArray.parse('[alice; bob; charlie]');
|
|
59
|
+
// ["alice", "bob", "charlie"]
|
|
60
|
+
|
|
61
|
+
// Without brackets (outermost brackets are optional)
|
|
62
|
+
const names2 = stringArray.parse('alice; bob; charlie');
|
|
63
|
+
// ["alice", "bob", "charlie"]
|
|
64
|
+
|
|
65
|
+
const numbers = numberArray.parse('[1; 2; 3; 4; 5]');
|
|
66
|
+
// [1, 2, 3, 4, 5]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Array of Tuples
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const schema = defineSchema('[string; number][]');
|
|
73
|
+
|
|
74
|
+
// With outer brackets
|
|
75
|
+
const data1 = schema.parse('[[a; 1]; [b; 2]; [c; 3]]');
|
|
76
|
+
// [["a", 1], ["b", 2], ["c", 3]]
|
|
77
|
+
|
|
78
|
+
// Without outer brackets
|
|
79
|
+
const data2 = schema.parse('[a; 1]; [b; 2]; [c; 3]');
|
|
80
|
+
// [["a", 1], ["b", 2], ["c", 3]]
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Escaping Special Characters
|
|
84
|
+
|
|
85
|
+
Use `\` to escape special characters `;`, `[`, `]`, and `\` in string values:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const schema = defineSchema('string');
|
|
89
|
+
|
|
90
|
+
const value1 = schema.parse('hello\\;world'); // "hello;world"
|
|
91
|
+
const value2 = schema.parse('hello\\[world'); // "hello[world"
|
|
92
|
+
const value3 = schema.parse('hello\\\\world'); // "hello\\world"
|
|
93
|
+
|
|
94
|
+
// In tuples
|
|
95
|
+
const tupleSchema = defineSchema('[string; string]');
|
|
96
|
+
const tuple = tupleSchema.parse('hello\\;world; test');
|
|
97
|
+
// ["hello;world", "test"]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### String Identifiers
|
|
101
|
+
|
|
102
|
+
Any identifier (including hyphens) is treated as a string schema:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const schema = defineSchema('word-smith');
|
|
106
|
+
const value = schema.parse('word-smith');
|
|
107
|
+
// "word-smith"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## API
|
|
111
|
+
|
|
112
|
+
### `defineSchema(schemaString: string): ParsedSchema`
|
|
113
|
+
|
|
114
|
+
Parses a schema string and returns an object with:
|
|
115
|
+
- `schema`: The parsed schema AST
|
|
116
|
+
- `validator`: A function to validate values against the schema
|
|
117
|
+
- `parse`: A function to parse value strings
|
|
118
|
+
|
|
119
|
+
### `parseSchema(schemaString: string): Schema`
|
|
120
|
+
|
|
121
|
+
Parses a schema string and returns the schema AST.
|
|
122
|
+
|
|
123
|
+
### `parseValue(schema: Schema, valueString: string): unknown`
|
|
124
|
+
|
|
125
|
+
Parses a value string according to the given schema.
|
|
126
|
+
|
|
127
|
+
### `createValidator(schema: Schema): (value: unknown) => boolean`
|
|
128
|
+
|
|
129
|
+
Creates a validation function for the given schema.
|
|
130
|
+
|
|
131
|
+
### `schemaToTypeString(schema: Schema): string`
|
|
132
|
+
|
|
133
|
+
Converts a schema AST back to a human-readable type string.
|
|
134
|
+
|
|
135
|
+
### `ParseError`
|
|
136
|
+
|
|
137
|
+
Error class thrown for invalid schema syntax.
|
|
138
|
+
|
|
139
|
+
### Types
|
|
140
|
+
|
|
141
|
+
The following TypeScript types are exported:
|
|
142
|
+
|
|
143
|
+
- `Schema` — union of all schema AST node types
|
|
144
|
+
- `ParsedSchema` — the return type of `defineSchema()`
|
|
145
|
+
- `PrimitiveSchema`, `TupleSchema`, `ArraySchema` — AST node types
|
|
146
|
+
- `ReferenceSchema`, `ReverseReferenceSchema` — reference AST node types
|
|
147
|
+
- `StringLiteralSchema`, `UnionSchema` — literal and union AST node types
|
|
148
|
+
|
|
149
|
+
## Schema Syntax
|
|
150
|
+
|
|
151
|
+
| Type | Schema | Example Value |
|
|
152
|
+
|------|--------|---------------|
|
|
153
|
+
| String | `string` or `identifier` | `hello` |
|
|
154
|
+
| Int | `int` | `42` |
|
|
155
|
+
| Float | `float` | `3.14` |
|
|
156
|
+
| Number | `number` | `42` or `3.14` |
|
|
157
|
+
| Boolean | `boolean` | `true` or `false` |
|
|
158
|
+
| Tuple | `[Type1; Type2; ...]` | `[hello; 42; true]` or `hello; 42; true` |
|
|
159
|
+
| Array | `Type[]` or `[Type][]` | `[1; 2; 3]` or `1; 2; 3` |
|
|
160
|
+
| Array of Tuples | `[Type1; Type2][]` | `[[a; 1]; [b; 2]]` or `[a; 1]; [b; 2]` |
|
|
161
|
+
| Union | `Type1 \| Type2` | `hello` or `42` (matches first valid member) |
|
|
162
|
+
| String Literal | `'on' \| 'off'` or `"red"` | `on` or `off` |
|
|
163
|
+
| Reference | `@tablename` or `@tablename[]` | (resolved at CSV load time) |
|
|
164
|
+
| Reverse Reference | `~tablename(fk)` | (resolved at CSV load time) |
|
|
165
|
+
|
|
166
|
+
## Notes
|
|
167
|
+
|
|
168
|
+
- Semicolons `;` are used as separators instead of commas `,`
|
|
169
|
+
- Outermost brackets `[]` are optional for tuple and array values
|
|
170
|
+
- Special characters can be escaped with backslash: `\;`, `\[`, `\]`, `\\`
|
|
171
|
+
- Empty arrays/tuples are not allowed
|
|
172
|
+
- For CSV loading with reference resolution, see [csv-loader.md](./csv-loader.md)
|
|
173
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
// rspack.config.js
|
|
176
|
+
module.exports = {
|
|
177
|
+
module: {
|
|
178
|
+
rules: [
|
|
179
|
+
{
|
|
180
|
+
test: /\.schema\.csv$/,
|
|
181
|
+
use: 'typed-csv/csv-loader',
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
ISC
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Plugin } from 'esbuild';
|
|
2
|
+
|
|
3
|
+
interface CsvLoaderOptions {
|
|
4
|
+
delimiter?: string;
|
|
5
|
+
quote?: string;
|
|
6
|
+
escape?: string;
|
|
7
|
+
bom?: boolean;
|
|
8
|
+
comment?: string | false;
|
|
9
|
+
trim?: boolean;
|
|
10
|
+
/** Generate TypeScript declaration file (.d.ts) */
|
|
11
|
+
emitTypes?: boolean;
|
|
12
|
+
/** Output directory for generated type files (relative to output path) */
|
|
13
|
+
typesOutputDir?: string;
|
|
14
|
+
/** Write .d.ts files to disk (useful for dev server) */
|
|
15
|
+
writeToDisk?: boolean;
|
|
16
|
+
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
17
|
+
refBaseDir?: string;
|
|
18
|
+
/** Primary key field name for referenced tables (default: 'id') */
|
|
19
|
+
defaultPrimaryKey?: string;
|
|
20
|
+
/** Current file path (used to resolve relative references) */
|
|
21
|
+
currentFilePath?: string;
|
|
22
|
+
/**
|
|
23
|
+
* When false, reference fields store parsed IDs instead of resolved objects.
|
|
24
|
+
* Used by csvToModule to emit accessor-based code with lazy resolution.
|
|
25
|
+
* Default: true (resolves references eagerly by loading referenced CSV files).
|
|
26
|
+
*/
|
|
27
|
+
resolveReferences?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface CsvEsbuildOptions extends CsvLoaderOptions {
|
|
31
|
+
/** Include pattern for CSV files (default: /\.csv$/) */
|
|
32
|
+
include?: RegExp | string | Array<RegExp | string>;
|
|
33
|
+
/** Exclude pattern for CSV files */
|
|
34
|
+
exclude?: RegExp | string | Array<RegExp | string>;
|
|
35
|
+
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
36
|
+
refBaseDir?: string;
|
|
37
|
+
/** Primary key field name for referenced tables (default: 'id') */
|
|
38
|
+
defaultPrimaryKey?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Esbuild plugin for loading CSV files with typed-csv validation.
|
|
42
|
+
*/
|
|
43
|
+
declare function csvLoader(options?: CsvEsbuildOptions): Plugin;
|
|
44
|
+
|
|
45
|
+
export { type CsvEsbuildOptions, csvLoader, csvLoader as default };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Plugin } from 'esbuild';
|
|
2
|
+
|
|
3
|
+
interface CsvLoaderOptions {
|
|
4
|
+
delimiter?: string;
|
|
5
|
+
quote?: string;
|
|
6
|
+
escape?: string;
|
|
7
|
+
bom?: boolean;
|
|
8
|
+
comment?: string | false;
|
|
9
|
+
trim?: boolean;
|
|
10
|
+
/** Generate TypeScript declaration file (.d.ts) */
|
|
11
|
+
emitTypes?: boolean;
|
|
12
|
+
/** Output directory for generated type files (relative to output path) */
|
|
13
|
+
typesOutputDir?: string;
|
|
14
|
+
/** Write .d.ts files to disk (useful for dev server) */
|
|
15
|
+
writeToDisk?: boolean;
|
|
16
|
+
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
17
|
+
refBaseDir?: string;
|
|
18
|
+
/** Primary key field name for referenced tables (default: 'id') */
|
|
19
|
+
defaultPrimaryKey?: string;
|
|
20
|
+
/** Current file path (used to resolve relative references) */
|
|
21
|
+
currentFilePath?: string;
|
|
22
|
+
/**
|
|
23
|
+
* When false, reference fields store parsed IDs instead of resolved objects.
|
|
24
|
+
* Used by csvToModule to emit accessor-based code with lazy resolution.
|
|
25
|
+
* Default: true (resolves references eagerly by loading referenced CSV files).
|
|
26
|
+
*/
|
|
27
|
+
resolveReferences?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface CsvEsbuildOptions extends CsvLoaderOptions {
|
|
31
|
+
/** Include pattern for CSV files (default: /\.csv$/) */
|
|
32
|
+
include?: RegExp | string | Array<RegExp | string>;
|
|
33
|
+
/** Exclude pattern for CSV files */
|
|
34
|
+
exclude?: RegExp | string | Array<RegExp | string>;
|
|
35
|
+
/** Base directory for resolving referenced CSV files (default: directory of current file) */
|
|
36
|
+
refBaseDir?: string;
|
|
37
|
+
/** Primary key field name for referenced tables (default: 'id') */
|
|
38
|
+
defaultPrimaryKey?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Esbuild plugin for loading CSV files with typed-csv validation.
|
|
42
|
+
*/
|
|
43
|
+
declare function csvLoader(options?: CsvEsbuildOptions): Plugin;
|
|
44
|
+
|
|
45
|
+
export { type CsvEsbuildOptions, csvLoader, csvLoader as default };
|