Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add the papaparse options to the button/link #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import { CSVDownloader } from 'svelte-csv';
</CSVDownloader>
```

#### Link
#### Button

```javascript
import { CSVDownloader } from 'svelte-csv';
Expand Down Expand Up @@ -124,6 +124,33 @@ import { CSVDownloader } from 'svelte-csv';
</CSVDownloader>
```

**Option**

It is possible to supply options through `options={optionObj}`. For available options,
see the [papaparse docs](https://www.papaparse.com/docs#unparse-config-default)
```javascript
import { CSVDownloader } from 'svelte-csv';

<CSVDownloader
data={[]}
options={
{
quotes: false, //or array of booleans
quoteChar: '"',
escapeChar: '"',
delimiter: ";",
header: true,
newline: "\r\n",
skipEmptyLines: false, //other option is 'greedy', meaning skip delimiters, quotes, and whitespace.
columns: null //or array of strings
}
}
>
Download
</CSVDownloader>
```


### 🎀 readString

```javascript
Expand Down
1 change: 1 addition & 0 deletions src/CSVDownloader.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ComponentType = 'link' | 'button';
18 changes: 11 additions & 7 deletions src/CSVDownloader.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<script>
<script lang="ts">
import PapaParse from 'papaparse';
import type { ComponentType } from './CSVDownloader';

export let data;
export let filename = 'filename';
export let type = 'link';
export let bom = 2;
export let filename: string = 'filename';
export let type: ComponentType = 'link';
export let bom: number = 2;
export let options: PapaParse.UnparseConfig | undefined = undefined;

function download(data, filename, bom) {
const bomCode = bom ? '\ufeff' : '';
let csvContent = null;
if (typeof data === 'object') {
csvContent = PapaParse.unparse(data);
csvContent = PapaParse.unparse(data, options);
} else {
csvContent = data;
}
Expand All @@ -30,11 +34,11 @@
</script>

{#if type === 'link'}
<span on:click={download(data, filename, bom)} class="link">
<span class={$$props.class + " link"} on:click={() => download(data, filename, bom)}>
<slot />
</span>
{:else}
<button on:click={download(data, filename, bom)}>
<button class={$$props.class} on:click={() => download(data, filename, bom)}>
<slot />
</button>
{/if}
Expand Down