-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow pasting from other sheet sources (e.g. Google Sheets) (#18)
- Loading branch information
1 parent
5b18e38
commit aafe55c
Showing
5 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { parseTableFromTabbedText } from "./table"; | ||
|
||
describe('parseTableFromTabbedText', () => { | ||
test('invalid case: undefined', () => { | ||
expect(parseTableFromTabbedText(undefined)).toBe(undefined) | ||
}) | ||
|
||
test('invalid case: empty string', () => { | ||
expect(parseTableFromTabbedText('')).toBe(undefined) | ||
}) | ||
|
||
test('invalid case: no columns', () => { | ||
expect(parseTableFromTabbedText('test')).toBe(undefined) | ||
}) | ||
|
||
test('invalid case: different number of columns', () => { | ||
const str = ` | ||
hello\tworld | ||
this is an invalid table | ||
`.trim() | ||
|
||
expect(parseTableFromTabbedText(str)).toBe(undefined) | ||
}) | ||
|
||
test('valid case', () => { | ||
const str = ` | ||
hello\tworld | ||
this is a valid table\they! | ||
`.trim() | ||
|
||
expect(parseTableFromTabbedText(str)).toBe(` | ||
|hello|world| | ||
|this is a valid table|hey!| | ||
`.trim()) | ||
}) | ||
|
||
test('valid case, with pipe characters', () => { | ||
const str = ` | ||
hello\tworld | ||
this is a | valid table\they! | ||
`.trim() | ||
|
||
expect(parseTableFromTabbedText(str)).toBe(` | ||
|hello|world| | ||
|this is a \\| valid table|hey!| | ||
`.trim()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function parseTableFromTabbedText(text: string | undefined): string | undefined { | ||
if (!text) return undefined | ||
|
||
const lines = text.split('\n') | ||
const tabCountPerLine = lines.map(line => line.match(/\t/g)?.length || 0) | ||
const allLinesHaveEqualTabCount = tabCountPerLine.every(tabCount => tabCount === tabCountPerLine[0] && tabCount > 0) | ||
|
||
if (!allLinesHaveEqualTabCount) return undefined | ||
return lines.map(line => `|${line.replace(/\|/g, '\\|').replace(/\t/g, '|')}|`).join('\n') | ||
} |