Skip to content

Commit b172d07

Browse files
committed
move options related code to options/ dir
1 parent 0af412f commit b172d07

20 files changed

Lines changed: 108 additions & 115 deletions
File renamed without changes.
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { parse } from '../parser'
2+
import { testEmptyLine } from '../utils'
13
import { BAD_DELIMITERS, RECORD_SEP, UNIT_SEP } from './constants'
2-
import { parse } from './parser'
3-
import { testEmptyLine } from './utils'
44

55
/**
66
* Validates the delimiter

src/newline.ts renamed to src/options/newline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { escapeRegExp } from './utils'
1+
import { escapeRegExp } from '../utils'
22

33
type Newline = '\n' | '\r' | '\r\n'
44

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { validateComments } from './comments'
12
import { DefaultDelimiter } from './constants'
23
import { guessDelimiter, validateDelimiter } from './delimiter'
34
import { validateEscapeChar } from './escapeChar'
@@ -64,3 +65,47 @@ export function validateAndGuessParseOptions(parseOptions: ParseOptions, { input
6465
error,
6566
}
6667
}
68+
69+
export interface ValidParseOptions {
70+
delimiter: string
71+
newline: '\n' | '\r' | '\r\n'
72+
comments: string | false
73+
quoteChar: string
74+
escapeChar: string
75+
}
76+
77+
/**
78+
* Validates and fills in default options
79+
* @param parseOptions The parsing options to validate and guess.
80+
* @param parseOptions.delimiter The delimiter used in the CSV data. Defaults to ','.
81+
* @param parseOptions.newline The newline used in the CSV data. Defaults to '\n'.
82+
* @param parseOptions.quoteChar The quote character used in the CSV data. Defaults to '"'.
83+
* @param parseOptions.escapeChar The escape character used in the CSV data. Defaults to the quote character.
84+
* @param parseOptions.comments The comment character or boolean to indicate comments.
85+
* @returns The validated options
86+
*/
87+
export function validateAndSetDefaultParseOptions(parseOptions: ParseOptions): ValidParseOptions {
88+
const quoteChar = validateQuoteChar(parseOptions.quoteChar)
89+
const escapeChar = validateEscapeChar(parseOptions.escapeChar) ?? quoteChar
90+
91+
// Delimiter must be valid
92+
// TODO(SL): it now throws if invalid delimiter is provided instead of defaulting to ,
93+
const delimiter = validateDelimiter(parseOptions.delimiter) ?? ','
94+
95+
// Comment character must be valid
96+
// TODO(SL): it now throws if invalid comment character is provided instead of defaulting to false
97+
const comments = validateComments(parseOptions.comments, delimiter) ?? false
98+
99+
// Newline must be valid: \r, \n, or \r\n
100+
// TODO(SL): it now throws if invalid newline is provided instead of defaulting to \n
101+
// TODO(SL): force newline to have the correct type?
102+
const newline = validateNewline(parseOptions.newline) ?? '\n'
103+
104+
return {
105+
delimiter,
106+
newline,
107+
comments,
108+
quoteChar,
109+
escapeChar,
110+
}
111+
}

src/parser.ts

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,27 @@
11
// 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'
73
import type { ParseError, ParseResult } from './types'
84
import { escapeRegExp } from './utils'
95

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-
}
606
/**
617
* Parses the input string with the given options
628
* @param input The input string to parse
639
* @param options The options for parsing
6410
* @yields The parse results, one row at a time
6511
* @returns A generator yielding parse results
6612
*/
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> {
6816
const {
6917
delimiter,
7018
newline,
7119
comments,
7220
quoteChar,
7321
escapeChar,
74-
ignoreLastRow,
75-
} = validateOptions(options)
22+
} = validateAndSetDefaultParseOptions(options)
7623
// TODO(SL): allow passing no options?
24+
const ignoreLastRow = options.ignoreLastRow ?? false
7725

7826
// We don't need to compute some of these every time parse() is called,
7927
// but having them in a more local scope seems to perform better

src/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type DelimiterError, type ParseOptions, validateAndGuessParseOptions } from './parseOptions'
1+
import { type DelimiterError, type ParseOptions, validateAndGuessParseOptions } from './options/parseOptions'
22
import { parse } from './parser'
33
import type { ParseResult } from './types'
44
import { testEmptyLine } from './utils'

0 commit comments

Comments
 (0)