Skip to content

Commit 52b59a6

Browse files
author
Irene Alvarado
committed
Add text library helpers
1 parent 41dc8c3 commit 52b59a6

File tree

8 files changed

+51
-2
lines changed

8 files changed

+51
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ You can import and use these helper functions directly, or treat them as a start
5858

5959
TBD API
6060

61+
### TXT
62+
63+
TBD API
64+
6165
### JSON
6266

6367
TBD API
@@ -82,7 +86,7 @@ TBD API
8286

8387
Run all the tests:
8488

85-
`deno test -A tests/*`
89+
`deno test -A --unstable tests/*`
8690

8791
Run separate tests
8892

examples/txt/text-write.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Writing test

examples/txt/text-write2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Write to a file

examples/txt/text.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test file to read

examples/txt/txt-example.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readTXT, writeTXT } from '../../src/txt.ts' // replace with latest library https://deno.land/x/flat@0.0.x/mod.ts
2+
3+
// Path to a csv file
4+
const readTXTPath = './examples/txt/text.txt'
5+
const writeTXTPath = './examples/txt/text-write.txt'
6+
const content = 'Writing test'
7+
8+
// read from a text file
9+
const text = await readTXT(readTXTPath)
10+
console.log(text)
11+
12+
// write a text file
13+
await writeTXT(writeTXTPath, content)

mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './src/csv.ts'
33
export * from './src/image.ts'
44
export * from './src/xlsx.ts'
55
export * from './src/zip.ts'
6-
export * from './src/remove.ts'
6+
export * from './src/remove.ts'
7+
export * from './src/txt.ts'

src/txt.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function readTXT(path: string) {
2+
const text = await Deno.readTextFile(path)
3+
return text
4+
}
5+
6+
export async function writeTXT(path: string, text: string) {
7+
await Deno.writeTextFile(path, text)
8+
}

tests/txt-test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { assertStringIncludes } from "https://deno.land/std@0.92.0/testing/asserts.ts"
2+
import { readTXT, writeTXT } from '../src/txt.ts'
3+
4+
const txtReadPath = './examples/txt/text.txt'
5+
const jsonWritePath = './examples/txt/text-write2.txt'
6+
7+
Deno.test("reads a txt file", async () => {
8+
const txt = await readTXT(txtReadPath)
9+
console.log(txt)
10+
11+
assertStringIncludes(txt, 'Test file to read');
12+
})
13+
14+
Deno.test("writes a txt file", async () => {
15+
const content = 'Write to a file'
16+
await writeTXT(jsonWritePath, content)
17+
const txt = await readTXT(jsonWritePath)
18+
19+
assertStringIncludes(txt, content);
20+
})

0 commit comments

Comments
 (0)