Skip to content

Commit 1c9091e

Browse files
committed
input->text, parseString -> parseText
1 parent b7753ab commit 1c9091e

14 files changed

Lines changed: 294 additions & 294 deletions

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { parseString } from './string'
1+
export { parseText } from './text'
22
export { parseUrl } from './url'
33
export { testEmptyLine } from './utils'

src/options/delimiter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ export function validateDelimiter(delimiter?: string): undefined | string {
2020

2121
/**
2222
* Guess the delimiter
23-
* @param input The input string
23+
* @param text The string
2424
* @param newline The newline character
2525
* @param comments The comment character or boolean to indicate comments
2626
* @param delimitersToGuess The list of delimiters to guess from
2727
* @returns An object indicating whether guessing was successful and the best delimiter found
2828
*/
29-
export function guessDelimiter(input: string, newline?: Newline, comments?: boolean | string, delimitersToGuess?: string[]) {
29+
export function guessDelimiter(text: string, newline?: Newline, comments?: boolean | string, delimitersToGuess?: string[]) {
3030
let bestDelimiter, bestDelta, maxFieldCount
3131

3232
delimitersToGuess = delimitersToGuess || [',', '\t', '|', ';', RECORD_SEP, UNIT_SEP]
@@ -40,7 +40,7 @@ export function guessDelimiter(input: string, newline?: Newline, comments?: bool
4040
let j = 0
4141
const previewLines = 10
4242

43-
for (const { row } of parse(input, {
43+
for (const { row } of parse(text, {
4444
delimiter,
4545
newline,
4646
comments,

src/options/newline.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ export function validateNewline(newline?: Newline): Newline | undefined {
1414

1515
/**
1616
* Guess the line endings
17-
* @param input The input string
17+
* @param text The string
1818
* @param quoteChar The quote character
1919
* @returns The line ending character
2020
*/
21-
export function guessLineEndings(input: string, quoteChar: string): Newline {
22-
input = input.substring(0, 1024 * 1024) // max length 1 MB
21+
export function guessLineEndings(text: string, quoteChar: string): Newline {
22+
text = text.substring(0, 1024 * 1024) // max length 1 MB
2323
// Replace all the text inside quotes
2424
const re = new RegExp(escapeRegExp(quoteChar) + '([^]*?)' + escapeRegExp(quoteChar), 'gm')
25-
input = input.replace(re, '')
25+
text = text.replace(re, '')
2626

27-
const r = input.split('\r')
27+
const r = text.split('\r')
2828

29-
const n = input.split('\n')
29+
const n = text.split('\n')
3030

3131
/* v8 ignore if -- @preserve */
3232
if (!(0 in r && 0 in n)) {

src/options/parseOptions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ export interface DelimiterError {
2727
* @param parseOptions.escapeChar The escape character used in the CSV data. Defaults to the quote character.
2828
* @param parseOptions.comments The comment character or boolean to indicate comments.
2929
* @param params The parameters for validation and guessing.
30-
* @param params.input The input string to use for guessing.
30+
* @param params.text The string to use for guessing.
3131
* @param params.delimitersToGuess The list of delimiters to guess from.
3232
* @returns The validated and guessed parsing options, and the delimiter error (if any).
3333
*/
34-
export function validateAndGuessParseOptions(parseOptions: ParseOptions, { input, delimitersToGuess}: {
35-
input: string
34+
export function validateAndGuessParseOptions(parseOptions: ParseOptions, { text, delimitersToGuess}: {
35+
text: string
3636
delimitersToGuess?: string[]
3737
}): {
3838
parseOptions: ParseOptions
3939
error?: DelimiterError
4040
} {
4141
const quoteChar = validateQuoteChar(parseOptions.quoteChar)
4242
const escapeChar = validateEscapeChar(parseOptions.escapeChar) ?? quoteChar
43-
const newline = validateNewline(parseOptions.newline) ?? guessLineEndings(input, quoteChar)
43+
const newline = validateNewline(parseOptions.newline) ?? guessLineEndings(text, quoteChar)
4444
const comments = parseOptions.comments
4545

4646
let error: DelimiterError | undefined
4747
let delimiter = validateDelimiter(parseOptions.delimiter)
4848
if (!delimiter) {
49-
const delimGuess = guessDelimiter(input, newline, comments, delimitersToGuess)
49+
const delimGuess = guessDelimiter(text, newline, comments, delimitersToGuess)
5050
if (delimGuess.successful)
5151
delimiter = delimGuess.bestDelimiter
5252
else {

src/parser.ts

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import type { ParseError, ParseResult } from './types'
44
import { 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
}

src/string.ts renamed to src/text.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { parse } from './parser'
33
import type { ParseResult } from './types'
44

55
/**
6-
* Parses a string into CSV data.
7-
* @param input The input string to parse.
6+
* Parses a text into CSV data.
7+
* @param text The string to parse.
88
* @param options Options for parsing the string.
99
* @param options.delimiter The delimiter used in the CSV data. Defaults to ','.
1010
* @param options.newline The newline used in the CSV data. Defaults to '\n'.
@@ -13,21 +13,21 @@ import type { ParseResult } from './types'
1313
* @param options.comments The comment character or boolean to indicate comments. Defaults to false (don't strip comments).
1414
* @param options.delimitersToGuess The list of delimiters to guess from
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 Parsed data and metadata.
1818
* @returns A generator yielding parsed data and metadata row by row.
1919
*/
20-
export function* parseString(input: string,
20+
export function* parseText(text: string,
2121
options: ParseOptions & {
2222
delimitersToGuess?: string[]
2323
ignoreLastRow?: boolean
2424
stripBOM?: boolean
2525
} = {}): Generator<ParseResult, void, unknown> {
2626
const { delimitersToGuess, ignoreLastRow, stripBOM } = options
27-
const { parseOptions, error } = validateAndGuessParseOptions(options, { input, delimitersToGuess })
27+
const { parseOptions, error } = validateAndGuessParseOptions(options, { text, delimitersToGuess })
2828

2929
let delimiterError: DelimiterError | undefined = error
30-
for (const result of parse(input, {
30+
for (const result of parse(text, {
3131
...parseOptions,
3232
ignoreLastRow,
3333
stripBOM,

0 commit comments

Comments
 (0)