Skip to content

Commit 8e8bdec

Browse files
authored
Merge pull request #8 from cyberdelia/optionally-optional
Allow to pass write options
2 parents 59569f9 + 2062087 commit 8e8bdec

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ Usage:
8282
#### writeCSV
8383

8484
```ts
85-
writeCSV(path: string, data: Record<string, unknown>[] | string)
85+
writeCSV(path: string, data: Record<string, unknown>[] | string, options?: Deno.WriteFileOptions)
8686
```
8787

8888
Args:
8989

9090
* **path**: path to a local CSV file
9191
* **data**: string or object array to store
92+
* **options**: [options](https://doc.deno.land/builtin/stable#Deno.WriteFileOptions) for writing the CSV file
9293

9394
Usage:
9495

@@ -122,14 +123,14 @@ const text = await readTXT('./path/to/file.txt')
122123
#### writeTXT
123124

124125
```ts
125-
writeTXT(path: string, text: string): void
126+
writeTXT(path: string, text: string, options?: Deno.WriteFileOptions): void
126127
```
127128

128129
Args:
129130

130131
* **path**: path to a local TXT file
131132
* **text**: text to write to file
132-
133+
* **options**: [options](https://doc.deno.land/builtin/stable#Deno.WriteFileOptions) for writing the TXT file
133134

134135
Usage:
135136

src/csv.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ export async function readCSV(path: string, options?: ParseOptions): Promise<Rec
1515
return content as Record<string, unknown>[]
1616
}
1717

18-
export async function writeCSV(path: string, data: Record<string, unknown>[] | string) {
18+
export async function writeCSV(path: string, data: Record<string, unknown>[] | string, options?: Deno.WriteFileOptions) {
1919
if (typeof data === 'string') {
20-
await Deno.writeTextFile(path, data);
20+
await Deno.writeTextFile(path, data, options);
2121
return
2222
}
2323

2424
const headers = Object.keys(data[0])
2525
// we have to stringify the data with a row header
2626
const dataString = await stringify(data as DataItem[], headers)
2727

28-
await Deno.writeTextFile(path, dataString);
29-
}
28+
await Deno.writeTextFile(path, dataString, options);
29+
}

src/txt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ export async function readTXT(path: string) {
33
return text
44
}
55

6-
export async function writeTXT(path: string, text: string) {
7-
await Deno.writeTextFile(path, text)
6+
export async function writeTXT(path: string, text: string, options?: Deno.WriteFileOptions) {
7+
await Deno.writeTextFile(path, text, options)
88
}

tests/csv-test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,28 @@ Deno.test("writes a csv file", async () => {
3333
const csv = await readCSV(csvWritePath)
3434

3535
assertArrayIncludes(csv, [{ age: "70", name: "Rick" }]);
36+
})
37+
38+
Deno.test("appends to a csv file", async () => {
39+
const data = [
40+
{
41+
age: 70,
42+
name: 'Rick'
43+
},
44+
{
45+
age: 14,
46+
name: 'Smith'
47+
}
48+
]
49+
await writeCSV(csvWritePath, data)
50+
const data2 = [
51+
{
52+
age: 42,
53+
name: 'Jodi'
54+
}
55+
]
56+
await writeCSV(csvWritePath, data2, { append: true })
57+
const csv = await readCSV(csvWritePath)
58+
assertArrayIncludes(csv, [{ age: "70", name: "Rick" }])
59+
assertArrayIncludes(csv, [{ age: "42", name: "Jodi" }])
3660
})

tests/txt-test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,15 @@ Deno.test("writes a txt file", async () => {
1717
const txt = await readTXT(jsonWritePath)
1818

1919
assertStringIncludes(txt, content);
20+
})
21+
22+
Deno.test("appends to a txt file", async () => {
23+
const content = 'Write to a file'
24+
const content2 = 'Append to a file'
25+
await writeTXT(jsonWritePath, content)
26+
await writeTXT(jsonWritePath, content2, { append: true })
27+
const txt = await readTXT(jsonWritePath)
28+
29+
assertStringIncludes(txt, content);
30+
assertStringIncludes(txt, content2);
2031
})

0 commit comments

Comments
 (0)