|
1 | 1 | // Adapted from PapaParse (https://www.papaparse.com/) |
2 | | -import { validateComments } from './comments' |
3 | | -import { validateDelimiter } from './delimiter' |
4 | | -import { validateEscapeChar } from './escapeChar' |
5 | | -import { validateNewline } from './newline' |
6 | | -import { validateQuoteChar } from './quoteChar' |
| 2 | +import { type ParseOptions, validateAndSetDefaultParseOptions } from './options/parseOptions' |
7 | 3 | import type { ParseError, ParseResult } from './types' |
8 | 4 | import { escapeRegExp } from './utils' |
9 | 5 |
|
10 | | -export interface ParseOptions { |
11 | | - delimiter?: string |
12 | | - newline?: string |
13 | | - comments?: string | boolean |
14 | | - quoteChar?: string |
15 | | - escapeChar?: string |
16 | | - ignoreLastRow?: boolean |
17 | | -} |
18 | | -export interface ValidParseOptions { |
19 | | - delimiter: string |
20 | | - newline: '\n' | '\r' | '\r\n' |
21 | | - comments: string | false |
22 | | - quoteChar: string |
23 | | - escapeChar: string |
24 | | - ignoreLastRow: boolean |
25 | | -} |
26 | | - |
27 | | -/** |
28 | | - * Validates and fills in default options |
29 | | - * @param options The options to validate |
30 | | - * @returns The validated options |
31 | | - */ |
32 | | -export function validateOptions(options: ParseOptions): ValidParseOptions { |
33 | | - const quoteChar = validateQuoteChar(options.quoteChar) |
34 | | - const escapeChar = validateEscapeChar(options.escapeChar) ?? quoteChar |
35 | | - |
36 | | - // Delimiter must be valid |
37 | | - // TODO(SL): it now throws if invalid delimiter is provided instead of defaulting to , |
38 | | - const delimiter = validateDelimiter(options.delimiter) ?? ',' |
39 | | - |
40 | | - // Comment character must be valid |
41 | | - // TODO(SL): it now throws if invalid comment character is provided instead of defaulting to false |
42 | | - const comments = validateComments(options.comments, delimiter) ?? false |
43 | | - |
44 | | - // Newline must be valid: \r, \n, or \r\n |
45 | | - // TODO(SL): it now throws if invalid newline is provided instead of defaulting to \n |
46 | | - // TODO(SL): force newline to have the correct type? |
47 | | - const newline = validateNewline(options.newline) ?? '\n' |
48 | | - |
49 | | - const ignoreLastRow = options.ignoreLastRow ?? false |
50 | | - |
51 | | - return { |
52 | | - delimiter, |
53 | | - newline, |
54 | | - comments, |
55 | | - quoteChar, |
56 | | - escapeChar, |
57 | | - ignoreLastRow, |
58 | | - } |
59 | | -} |
60 | 6 | /** |
61 | 7 | * Parses the input string with the given options |
62 | 8 | * @param input The input string to parse |
63 | 9 | * @param options The options for parsing |
64 | 10 | * @yields The parse results, one row at a time |
65 | 11 | * @returns A generator yielding parse results |
66 | 12 | */ |
67 | | -export function* parse(input: string, options: ParseOptions): Generator<ParseResult, void, unknown> { |
| 13 | +export function* parse(input: string, options: ParseOptions & { |
| 14 | + ignoreLastRow?: boolean |
| 15 | +}): Generator<ParseResult, void, unknown> { |
68 | 16 | const { |
69 | 17 | delimiter, |
70 | 18 | newline, |
71 | 19 | comments, |
72 | 20 | quoteChar, |
73 | 21 | escapeChar, |
74 | | - ignoreLastRow, |
75 | | - } = validateOptions(options) |
| 22 | + } = validateAndSetDefaultParseOptions(options) |
76 | 23 | // TODO(SL): allow passing no options? |
| 24 | + const ignoreLastRow = options.ignoreLastRow ?? false |
77 | 25 |
|
78 | 26 | // We don't need to compute some of these every time parse() is called, |
79 | 27 | // but having them in a more local scope seems to perform better |
|
0 commit comments