@@ -4,20 +4,20 @@ import type { ParseError, ParseResult } from './types'
44import { escapeRegExp } from './utils'
55
66/**
7- * Parses the input string with the given options
8- * @param input The input string to parse
7+ * Parses the text with the given options
8+ * @param text The string to parse
99 * @param options The options for parsing
1010 * @param options.delimiter The delimiter used in the CSV data. Defaults to ','.
1111 * @param options.newline The newline used in the CSV data. Defaults to '\n'.
1212 * @param options.quoteChar The quote character used in the CSV data. Defaults to '"'.
1313 * @param options.escapeChar The escape character used in the CSV data. Defaults to the quote character.
1414 * @param options.comments The comment character or boolean to indicate comments. Defaults to false (don't strip comments).
1515 * @param options.ignoreLastRow Whether to ignore the last row. Defaults to false.
16- * @param options.stripBOM Whether to strip the BOM character at the start of the input . Defaults to true.
16+ * @param options.stripBOM Whether to strip the BOM character at the start of the text . Defaults to true.
1717 * @yields The parse results, one row at a time
1818 * @returns A generator yielding parse results
1919 */
20- export function * parse ( input : string , options : ParseOptions & {
20+ export function * parse ( text : string , options : ParseOptions & {
2121 ignoreLastRow ?: boolean
2222 stripBOM ?: boolean
2323} = { } ) : Generator < ParseResult , void , unknown > {
@@ -33,7 +33,7 @@ export function* parse(input: string, options: ParseOptions & {
3333
3434 // We don't need to compute some of these every time parse() is called,
3535 // but having them in a more local scope seems to perform better
36- const inputLen = input . length ,
36+ const textLen = text . length ,
3737 delimLen = delimiter . length ,
3838 newlineLen = newline . length ,
3939 commentsLen = comments === false ? 0 : comments . length
@@ -45,19 +45,19 @@ export function* parse(input: string, options: ParseOptions & {
4545 let lastCursor = 0
4646 let offset = 0 // Byte offset
4747
48- if ( stripBOM && input . charCodeAt ( 0 ) === 0xfeff ) {
48+ if ( stripBOM && text . charCodeAt ( 0 ) === 0xfeff ) {
4949 cursor = 1
5050 }
5151
52- let nextDelim = input . indexOf ( delimiter , cursor )
53- let nextNewline = input . indexOf ( newline , cursor )
52+ let nextDelim = text . indexOf ( delimiter , cursor )
53+ let nextNewline = text . indexOf ( newline , cursor )
5454 const quoteCharRegex = new RegExp ( escapeRegExp ( escapeChar ) + escapeRegExp ( quoteChar ) , 'g' )
55- let quoteSearch = input . indexOf ( quoteChar , cursor )
55+ let quoteSearch = text . indexOf ( quoteChar , cursor )
5656
5757 // Parser loop
5858 for ( ; ; ) {
5959 // Field has opening quote
60- if ( input [ cursor ] === quoteChar ) {
60+ if ( text [ cursor ] === quoteChar ) {
6161 // Start our search for the closing quote where the cursor is
6262 quoteSearch = cursor
6363
@@ -66,7 +66,7 @@ export function* parse(input: string, options: ParseOptions & {
6666
6767 for ( ; ; ) {
6868 // Find closing quote
69- quoteSearch = input . indexOf ( quoteChar , quoteSearch + 1 )
69+ quoteSearch = text . indexOf ( quoteChar , quoteSearch + 1 )
7070
7171 // No other quotes are found - no other delimiters
7272 if ( quoteSearch === - 1 ) {
@@ -86,8 +86,8 @@ export function* parse(input: string, options: ParseOptions & {
8686 }
8787
8888 // Closing quote at EOF
89- if ( quoteSearch === inputLen - 1 ) {
90- const value = input . substring ( cursor , quoteSearch ) . replace ( quoteCharRegex , quoteChar )
89+ if ( quoteSearch === textLen - 1 ) {
90+ const value = text . substring ( cursor , quoteSearch ) . replace ( quoteCharRegex , quoteChar )
9191 const last = finish ( value )
9292 if ( last ) {
9393 yield last
@@ -97,53 +97,53 @@ export function* parse(input: string, options: ParseOptions & {
9797
9898 // If this quote is escaped, it's part of the data; skip it
9999 // If the quote character is the escape character, then check if the next character is the escape character
100- if ( quoteChar === escapeChar && input [ quoteSearch + 1 ] === escapeChar ) {
100+ if ( quoteChar === escapeChar && text [ quoteSearch + 1 ] === escapeChar ) {
101101 quoteSearch ++
102102 continue
103103 }
104104
105105 // If the quote character is not the escape character, then check if the previous character was the escape character
106- if ( quoteChar !== escapeChar && quoteSearch !== 0 && input [ quoteSearch - 1 ] === escapeChar ) {
106+ if ( quoteChar !== escapeChar && quoteSearch !== 0 && text [ quoteSearch - 1 ] === escapeChar ) {
107107 continue
108108 }
109109
110110 if ( nextDelim !== - 1 && nextDelim < ( quoteSearch + 1 ) ) {
111- nextDelim = input . indexOf ( delimiter , ( quoteSearch + 1 ) )
111+ nextDelim = text . indexOf ( delimiter , ( quoteSearch + 1 ) )
112112 }
113113 if ( nextNewline !== - 1 && nextNewline < ( quoteSearch + 1 ) ) {
114- nextNewline = input . indexOf ( newline , ( quoteSearch + 1 ) )
114+ nextNewline = text . indexOf ( newline , ( quoteSearch + 1 ) )
115115 }
116116 // Check up to nextDelim or nextNewline, whichever is closest
117117 const checkUpTo = nextNewline === - 1 ? nextDelim : Math . min ( nextDelim , nextNewline )
118- const spacesBetweenQuoteAndDelimiter = extraSpaces ( { index : checkUpTo , input , quoteSearch } )
118+ const spacesBetweenQuoteAndDelimiter = extraSpaces ( { index : checkUpTo , text , quoteSearch } )
119119
120120 // Closing quote followed by delimiter or 'unnecessary spaces + delimiter'
121121 const startA = quoteSearch + 1 + spacesBetweenQuoteAndDelimiter
122122 const endA = startA + delimLen
123- if ( input . substring ( startA , endA ) === delimiter ) {
124- row . push ( input . substring ( cursor , quoteSearch ) . replace ( quoteCharRegex , quoteChar ) )
123+ if ( text . substring ( startA , endA ) === delimiter ) {
124+ row . push ( text . substring ( cursor , quoteSearch ) . replace ( quoteCharRegex , quoteChar ) )
125125 cursor = endA
126126
127127 // If char after following delimiter is not quoteChar, we find next quote char position
128- if ( input [ endA ] !== quoteChar ) {
129- quoteSearch = input . indexOf ( quoteChar , cursor )
128+ if ( text [ endA ] !== quoteChar ) {
129+ quoteSearch = text . indexOf ( quoteChar , cursor )
130130 }
131- nextDelim = input . indexOf ( delimiter , cursor )
132- nextNewline = input . indexOf ( newline , cursor )
131+ nextDelim = text . indexOf ( delimiter , cursor )
132+ nextNewline = text . indexOf ( newline , cursor )
133133 break
134134 }
135135
136- const spacesBetweenQuoteAndNewLine = extraSpaces ( { index : nextNewline , input , quoteSearch } )
136+ const spacesBetweenQuoteAndNewLine = extraSpaces ( { index : nextNewline , text , quoteSearch } )
137137
138138 // Closing quote followed by newline or 'unnecessary spaces + newLine'
139139 const startB = quoteSearch + 1 + spacesBetweenQuoteAndNewLine
140140 const endB = startB + newlineLen
141- if ( input . substring ( startB , endB ) === newline ) {
142- row . push ( input . substring ( cursor , quoteSearch ) . replace ( quoteCharRegex , quoteChar ) )
141+ if ( text . substring ( startB , endB ) === newline ) {
142+ row . push ( text . substring ( cursor , quoteSearch ) . replace ( quoteCharRegex , quoteChar ) )
143143 cursor = endB
144- nextNewline = input . indexOf ( newline , cursor )
145- nextDelim = input . indexOf ( delimiter , cursor ) // because we may have skipped the nextDelim in the quoted field
146- quoteSearch = input . indexOf ( quoteChar , cursor ) // we search for first quote in next line
144+ nextNewline = text . indexOf ( newline , cursor )
145+ nextDelim = text . indexOf ( delimiter , cursor ) // because we may have skipped the nextDelim in the quoted field
146+ quoteSearch = text . indexOf ( quoteChar , cursor ) // we search for first quote in next line
147147
148148 /** Yields the row and resets row & errors. */
149149 yield getResult ( )
@@ -170,31 +170,31 @@ export function* parse(input: string, options: ParseOptions & {
170170 }
171171
172172 // Comment found at start of new line
173- if ( comments && row . length === 0 && input . substring ( cursor , cursor + commentsLen ) === comments ) {
173+ if ( comments && row . length === 0 && text . substring ( cursor , cursor + commentsLen ) === comments ) {
174174 if ( nextNewline === - 1 ) {
175175 // Comment ends at EOF
176176 return
177177 }
178178 cursor = nextNewline + newlineLen
179- nextNewline = input . indexOf ( newline , cursor )
180- nextDelim = input . indexOf ( delimiter , cursor )
179+ nextNewline = text . indexOf ( newline , cursor )
180+ nextDelim = text . indexOf ( delimiter , cursor )
181181 continue
182182 }
183183
184184 // Next delimiter comes before next newline, so we've reached end of field
185185 if ( nextDelim !== - 1 && ( nextDelim < nextNewline || nextNewline === - 1 ) ) {
186- row . push ( input . substring ( cursor , nextDelim ) )
186+ row . push ( text . substring ( cursor , nextDelim ) )
187187 cursor = nextDelim + delimLen
188188 // we look for next delimiter char
189- nextDelim = input . indexOf ( delimiter , cursor )
189+ nextDelim = text . indexOf ( delimiter , cursor )
190190 continue
191191 }
192192
193193 // End of row
194194 if ( nextNewline !== - 1 ) {
195- row . push ( input . substring ( cursor , nextNewline ) )
195+ row . push ( text . substring ( cursor , nextNewline ) )
196196 cursor = nextNewline + newlineLen
197- nextNewline = input . indexOf ( newline , cursor )
197+ nextNewline = text . indexOf ( newline , cursor )
198198
199199 yield getResult ( )
200200 row = [ ]
@@ -213,17 +213,17 @@ export function* parse(input: string, options: ParseOptions & {
213213 return
214214
215215 /**
216- * Appends the remaining input from cursor to the end into
216+ * Appends the remaining text from cursor to the end into
217217 * row, saves the row, calls step, and returns the results.
218- * @param value The remaining input to append
218+ * @param value The remaining text to append
219219 * @returns The results object
220220 */
221221 function finish ( value ?: string ) : ParseResult | undefined {
222222 if ( ignoreLastRow )
223223 return
224- value ??= input . substring ( cursor )
224+ value ??= text . substring ( cursor )
225225 row . push ( value )
226- cursor = inputLen
226+ cursor = textLen
227227 // lastCursor = cursor
228228 return getResult ( )
229229 }
@@ -234,7 +234,7 @@ export function* parse(input: string, options: ParseOptions & {
234234 */
235235 function getResult ( ) : ParseResult {
236236 // the row started at lastCursor, ended at cursor
237- const string = input . substring ( lastCursor , cursor )
237+ const string = text . substring ( lastCursor , cursor )
238238 const byteCount = new TextEncoder ( ) . encode ( string ) . length
239239
240240 const result = {
@@ -260,19 +260,19 @@ export function* parse(input: string, options: ParseOptions & {
260260 * Checks if there are extra spaces after closing quote and given index without any text
261261 * if Yes, returns the number of spaces
262262 * @param options Arguments
263- * @param options.input The input string
263+ * @param options.text The text string
264264 * @param options.index The index to check up to
265265 * @param options.quoteSearch The position of the closing quote
266266 * @returns number of spaces
267267 */
268- function extraSpaces ( { input , index, quoteSearch } : {
269- input : string
268+ function extraSpaces ( { text , index, quoteSearch } : {
269+ text : string
270270 index : number
271271 quoteSearch : number
272272} ) {
273273 let spaceLength = 0
274274 if ( index !== - 1 ) {
275- const textBetweenClosingQuoteAndIndex = input . substring ( quoteSearch + 1 , index )
275+ const textBetweenClosingQuoteAndIndex = text . substring ( quoteSearch + 1 , index )
276276 if ( textBetweenClosingQuoteAndIndex && textBetweenClosingQuoteAndIndex . trim ( ) === '' ) {
277277 spaceLength = textBetweenClosingQuoteAndIndex . length
278278 }
0 commit comments