refactor: default csv-parse params
This commit is contained in:
parent
3a6f57ef6f
commit
9a8cdcaaea
|
|
@ -38,6 +38,9 @@ module.exports = {
|
||||||
delimiter: ',',
|
delimiter: ',',
|
||||||
quote: '"',
|
quote: '"',
|
||||||
escape: '\\',
|
escape: '\\',
|
||||||
|
bom: true, // 处理 BOM (默认 true)
|
||||||
|
comment: '#', // 忽略 # 开头的注释行 (默认 '#')
|
||||||
|
trim: true, // 修剪表头和值的前后空格 (默认 true)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -64,6 +67,9 @@ import data from './data.schema.csv';
|
||||||
| `delimiter` | string | `,` | Column delimiter |
|
| `delimiter` | string | `,` | Column delimiter |
|
||||||
| `quote` | string | `"` | Quote character |
|
| `quote` | string | `"` | Quote character |
|
||||||
| `escape` | string | `\` | Escape character |
|
| `escape` | string | `\` | Escape character |
|
||||||
|
| `bom` | boolean | `true` | Handle byte order mark |
|
||||||
|
| `comment` | string \| `false` | `#` | Comment character (set `false` to disable) |
|
||||||
|
| `trim` | boolean | `true` | Trim headers and values |
|
||||||
|
|
||||||
## Schema Syntax
|
## Schema Syntax
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ export interface CsvLoaderOptions {
|
||||||
delimiter?: string;
|
delimiter?: string;
|
||||||
quote?: string;
|
quote?: string;
|
||||||
escape?: string;
|
escape?: string;
|
||||||
|
bom?: boolean;
|
||||||
|
comment?: string | false;
|
||||||
|
trim?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PropertyConfig {
|
interface PropertyConfig {
|
||||||
|
|
@ -19,15 +22,21 @@ export default function csvLoader(
|
||||||
this: LoaderContext<CsvLoaderOptions>,
|
this: LoaderContext<CsvLoaderOptions>,
|
||||||
content: string
|
content: string
|
||||||
): string | Buffer {
|
): string | Buffer {
|
||||||
const options = this.getOptions() || {};
|
const options = this.getOptions() as CsvLoaderOptions | undefined;
|
||||||
const delimiter = options.delimiter ?? ',';
|
const delimiter = options?.delimiter ?? ',';
|
||||||
const quote = options.quote ?? '"';
|
const quote = options?.quote ?? '"';
|
||||||
const escape = options.escape ?? '\\';
|
const escape = options?.escape ?? '\\';
|
||||||
|
const bom = options?.bom ?? true;
|
||||||
|
const comment = options?.comment === false ? undefined : (options?.comment ?? '#');
|
||||||
|
const trim = options?.trim ?? true;
|
||||||
|
|
||||||
const records = parse(content, {
|
const records = parse(content, {
|
||||||
delimiter,
|
delimiter,
|
||||||
quote,
|
quote,
|
||||||
escape,
|
escape,
|
||||||
|
bom,
|
||||||
|
comment,
|
||||||
|
trim,
|
||||||
relax_column_count: true,
|
relax_column_count: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue