Skip to content

Commit 5057522

Browse files
severoCopilot
andauthored
rename testEmptyLine to isEmptyLine and change the signature + add wa… (#21)
* rename testEmptyLine to isEmptyLine and change the signature + add warning in README about stability * Update src/utils.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent f2090d5 commit 5057522

9 files changed

Lines changed: 42 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## [0.0.6]
4+
5+
- rename testEmptyLine to isEmptyLine, change its signature to accept options object with greedy property
6+
37
## [0.0.5]
48

59
- export Newline type

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99

1010
Fetch and parse ranges of CSV file.
1111

12+
## Early version
13+
14+
This is an early version. The API may change completely:
15+
16+
- until version 0.1.0, breaking changes may be introduced at any time.
17+
- from version 0.1.0 to 1.0.0, breaking changes will be introduced only in minor versions.
18+
- from version 1.0.0, breaking changes will be introduced only in major versions.
19+
1220
## Install
1321

1422
```bash

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "csv-range",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Parse ranges of a CSV file.",
55
"type": "module",
66
"license": "MIT",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { parseText } from './text'
22
export type { Newline, ParseError, ParseMeta, ParseResult } from './types'
33
export { parseURL } from './url'
4-
export { testEmptyLine } from './utils'
4+
export { isEmptyLine } from './utils'

src/options/delimiter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse } from '../parser'
22
import type { Newline } from '../types'
3-
import { testEmptyLine } from '../utils'
3+
import { isEmptyLine } from '../utils'
44
import { BAD_DELIMITERS, RECORD_SEP, UNIT_SEP } from './constants'
55

66
/**
@@ -50,7 +50,7 @@ export function guessDelimiter(text: string, newline?: Newline, comments?: boole
5050
break
5151
}
5252
// always remove empty lines from consideration
53-
if (testEmptyLine(row, true)) {
53+
if (isEmptyLine(row)) {
5454
continue
5555
}
5656
nonEmptyLinesCount++

src/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ export function escapeRegExp(string: string) {
7373
}
7474

7575
/**
76-
* Test if the string array is an empty line
76+
* Check if the string array is an empty line
7777
* @param s The string array
78-
* @param skipEmptyLines If 'greedy', trims all spaces to test for emptiness
78+
* @param options Options
79+
* @param options.greedy If true, trims all spaces to test for emptiness. Defaults to false.
7980
* @returns Whether the line is empty
8081
*/
81-
export function testEmptyLine(s: string[], skipEmptyLines?: 'greedy' | boolean) {
82-
return skipEmptyLines === 'greedy' ? s.join('').trim() === '' : 0 in s && s.length === 1 && s[0].length === 0
82+
export function isEmptyLine(s: string[], options: { greedy?: boolean } = {}) {
83+
return options.greedy ? s.join('').trim() === '' : 0 in s && s.length === 1 && s[0].length === 0
8384
}

tests/text.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from 'vitest'
22

33
import { parseText } from '../src/text'
4-
import { testEmptyLine } from '../src/utils'
4+
import { isEmptyLine } from '../src/utils'
55
import { PARSE_TESTS } from './cases'
66

77
describe('parseText', () => {
@@ -34,17 +34,17 @@ describe('parseText', () => {
3434
{ description: ', with empty text', text: '', expected: [] },
3535
{ description: ', with first line only whitespace', text: ' \na,b,c', expected: [[' '], ['a', 'b', 'c']] },
3636
{ description: ', with comments', text: '#comment line 1\n#comment line 2\na,b,c\n#comment line 3\nd,e,f\n', expected: [['a', 'b', 'c'], ['d', 'e', 'f']] },
37-
])('works together with testEmptyLine to skip empty lines$description', ({ text, expected }) => {
38-
const results = [...parseText(text, { comments: '#' })].filter(({ row }) => !testEmptyLine(row, true))
37+
])('works together with isEmptyLine to skip empty lines$description', ({ text, expected }) => {
38+
const results = [...parseText(text, { comments: '#' })].filter(({ row }) => !isEmptyLine(row))
3939

4040
expect(results.map(({ row }) => row)).toEqual(expected)
4141
})
4242

4343
it.for([
4444
{ description: '', text: 'a,b\n\n,\nc,d\n , \n""," "\n\t,\t\n,,,,\n', expected: [['a', 'b'], ['c', 'd']] },
4545
{ description: ', with quotes and delimiters as content', text: 'a,b\n\n,\nc,d\n" , ",","\n""" """,""""""\n\n\n', expected: [['a', 'b'], ['c', 'd'], [' , ', ','], ['" "', '""']] },
46-
])('works together with testEmptyLine to skip empty lines, in greedy mode$description', ({ text, expected }) => {
47-
const results = [...parseText(text)].filter(({ row }) => !testEmptyLine(row, 'greedy'))
46+
])('works together with isEmptyLine to skip empty lines, in greedy mode$description', ({ text, expected }) => {
47+
const results = [...parseText(text)].filter(({ row }) => !isEmptyLine(row, { greedy: true }))
4848

4949
expect(results.map(({ row }) => row)).toEqual(expected)
5050
})

tests/utils.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22

3-
import { decode, escapeRegExp, testEmptyLine, toUrl } from '../src/utils'
3+
import { decode, escapeRegExp, isEmptyLine, toUrl } from '../src/utils'
44

55
describe('toUrl', () => {
66
it('creates a valid blob URL and revokes it', async () => {
@@ -59,20 +59,21 @@ describe('escapeRegExp', () => {
5959
})
6060
})
6161

62-
describe('testEmptyLine', () => {
63-
it.for([true, false, undefined])('detects empty lines correctly with skipEmptyLines being: %s (same behavior!)', (skipEmptyLines) => {
64-
expect(testEmptyLine([''], skipEmptyLines)).toBe(true)
65-
expect(testEmptyLine(['\t'], skipEmptyLines)).toBe(false)
66-
expect(testEmptyLine([' '], skipEmptyLines)).toBe(false)
67-
expect(testEmptyLine(['', ''], skipEmptyLines)).toBe(false)
68-
expect(testEmptyLine(['data'], skipEmptyLines)).toBe(false)
62+
describe('isEmptyLine', () => {
63+
it.for([undefined, {}, { greedy: false }])('detects empty lines correctly with options being: %s (same behavior!)', (options) => {
64+
expect(isEmptyLine([''], options)).toBe(true)
65+
expect(isEmptyLine(['\t'], options)).toBe(false)
66+
expect(isEmptyLine([' '], options)).toBe(false)
67+
expect(isEmptyLine(['', ''], options)).toBe(false)
68+
expect(isEmptyLine(['data'], options)).toBe(false)
6969
})
7070

71-
it('detects empty lines correctly with skipEmptyLines as "greedy"', () => {
72-
expect(testEmptyLine([''], 'greedy')).toBe(true)
73-
expect(testEmptyLine([' '], 'greedy')).toBe(true)
74-
expect(testEmptyLine([' \t', ' '], 'greedy')).toBe(true)
75-
expect(testEmptyLine(['data'], 'greedy')).toBe(false)
71+
it('detects empty lines correctly with options {greedy: true}', () => {
72+
const options = { greedy: true }
73+
expect(isEmptyLine([''], options)).toBe(true)
74+
expect(isEmptyLine([' '], options)).toBe(true)
75+
expect(isEmptyLine([' \t', ' '], options)).toBe(true)
76+
expect(isEmptyLine(['data'], options)).toBe(false)
7677
})
7778
})
7879

0 commit comments

Comments
 (0)