Skip to content

Commit

Permalink
Add text library helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Irene Alvarado committed Apr 23, 2021
1 parent 41dc8c3 commit 52b59a6
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ You can import and use these helper functions directly, or treat them as a start

TBD API

### TXT

TBD API

### JSON

TBD API
Expand All @@ -82,7 +86,7 @@ TBD API

Run all the tests:

`deno test -A tests/*`
`deno test -A --unstable tests/*`

Run separate tests

Expand Down
1 change: 1 addition & 0 deletions examples/txt/text-write.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Writing test
1 change: 1 addition & 0 deletions examples/txt/text-write2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Write to a file
1 change: 1 addition & 0 deletions examples/txt/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test file to read
13 changes: 13 additions & 0 deletions examples/txt/txt-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readTXT, writeTXT } from '../../src/txt.ts' // replace with latest library https://deno.land/x/[email protected]/mod.ts

// Path to a csv file
const readTXTPath = './examples/txt/text.txt'
const writeTXTPath = './examples/txt/text-write.txt'
const content = 'Writing test'

// read from a text file
const text = await readTXT(readTXTPath)
console.log(text)

// write a text file
await writeTXT(writeTXTPath, content)
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from './src/csv.ts'
export * from './src/image.ts'
export * from './src/xlsx.ts'
export * from './src/zip.ts'
export * from './src/remove.ts'
export * from './src/remove.ts'
export * from './src/txt.ts'
8 changes: 8 additions & 0 deletions src/txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function readTXT(path: string) {
const text = await Deno.readTextFile(path)
return text
}

export async function writeTXT(path: string, text: string) {
await Deno.writeTextFile(path, text)
}
20 changes: 20 additions & 0 deletions tests/txt-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { assertStringIncludes } from "https://deno.land/[email protected]/testing/asserts.ts"
import { readTXT, writeTXT } from '../src/txt.ts'

const txtReadPath = './examples/txt/text.txt'
const jsonWritePath = './examples/txt/text-write2.txt'

Deno.test("reads a txt file", async () => {
const txt = await readTXT(txtReadPath)
console.log(txt)

assertStringIncludes(txt, 'Test file to read');
})

Deno.test("writes a txt file", async () => {
const content = 'Write to a file'
await writeTXT(jsonWritePath, content)
const txt = await readTXT(jsonWritePath)

assertStringIncludes(txt, content);
})

0 comments on commit 52b59a6

Please sign in to comment.