diff --git a/README.md b/README.md index 8a0bde8..54b361c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -82,7 +86,7 @@ TBD API Run all the tests: -`deno test -A tests/*` +`deno test -A --unstable tests/*` Run separate tests diff --git a/examples/txt/text-write.txt b/examples/txt/text-write.txt new file mode 100644 index 0000000..a5906a7 --- /dev/null +++ b/examples/txt/text-write.txt @@ -0,0 +1 @@ +Writing test \ No newline at end of file diff --git a/examples/txt/text-write2.txt b/examples/txt/text-write2.txt new file mode 100644 index 0000000..247bc30 --- /dev/null +++ b/examples/txt/text-write2.txt @@ -0,0 +1 @@ +Write to a file \ No newline at end of file diff --git a/examples/txt/text.txt b/examples/txt/text.txt new file mode 100644 index 0000000..4a5f094 --- /dev/null +++ b/examples/txt/text.txt @@ -0,0 +1 @@ +Test file to read \ No newline at end of file diff --git a/examples/txt/txt-example.ts b/examples/txt/txt-example.ts new file mode 100644 index 0000000..ad17350 --- /dev/null +++ b/examples/txt/txt-example.ts @@ -0,0 +1,13 @@ +import { readTXT, writeTXT } from '../../src/txt.ts' // replace with latest library https://deno.land/x/flat@0.0.x/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) \ No newline at end of file diff --git a/mod.ts b/mod.ts index 20f39b4..09abca1 100644 --- a/mod.ts +++ b/mod.ts @@ -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' \ No newline at end of file +export * from './src/remove.ts' +export * from './src/txt.ts' \ No newline at end of file diff --git a/src/txt.ts b/src/txt.ts new file mode 100644 index 0000000..cea51cd --- /dev/null +++ b/src/txt.ts @@ -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) +} diff --git a/tests/txt-test.ts b/tests/txt-test.ts new file mode 100644 index 0000000..c117f88 --- /dev/null +++ b/tests/txt-test.ts @@ -0,0 +1,20 @@ +import { assertStringIncludes } from "https://deno.land/std@0.92.0/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); +}) \ No newline at end of file