From 92b789a009f291554d8151aeaaeaec4e72ec0409 Mon Sep 17 00:00:00 2001 From: Adrien Blanc Date: Wed, 21 Jul 2021 20:54:28 +0200 Subject: [PATCH 1/2] add all JSON.stringify args to writeJSON function --- examples/json/json-indent-test.json | 4 ++++ src/json.ts | 4 ++-- tests/json-test.ts | 12 ++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 examples/json/json-indent-test.json diff --git a/examples/json/json-indent-test.json b/examples/json/json-indent-test.json new file mode 100644 index 0000000..78da38f --- /dev/null +++ b/examples/json/json-indent-test.json @@ -0,0 +1,4 @@ +{ + "prices": "$20", + "name": "table" +} \ No newline at end of file diff --git a/src/json.ts b/src/json.ts index bd60be2..12fd6a4 100644 --- a/src/json.ts +++ b/src/json.ts @@ -3,8 +3,8 @@ export async function readJSON(path: string) { return JSON.parse(text) } -export async function writeJSON(path: string, data: any) { - await Deno.writeTextFile(path, JSON.stringify(data)) +export async function writeJSON(path: string, ...args: Parameters) { + await Deno.writeTextFile(path, JSON.stringify(...args)) } export async function readJSONFromURL(url: string) { diff --git a/tests/json-test.ts b/tests/json-test.ts index 73d3bf8..349004d 100644 --- a/tests/json-test.ts +++ b/tests/json-test.ts @@ -3,6 +3,7 @@ import { readJSON, writeJSON } from '../src/json.ts' const jsonReadPath = './examples/json/data.json' const jsonWritePath = './examples/json/json-test.json' +const jsonIndentWritePath = './examples/json/json-indent-test.json' Deno.test("reads a json file", async () => { const json = await readJSON(jsonReadPath) @@ -18,5 +19,16 @@ Deno.test("writes a json file", async () => { await writeJSON(jsonWritePath, data) const json = await readJSON(jsonWritePath) + assertObjectMatch(json, { prices: "$20", name: "table" }); +}) + +Deno.test("writes a json file with space indent", async () => { + const data = { + prices: '$20', + name: 'table' + } + await writeJSON(jsonIndentWritePath, data, null, 2) + const json = await readJSON(jsonIndentWritePath) + assertObjectMatch(json, { prices: "$20", name: "table" }); }) \ No newline at end of file From f42f02e059ab021db605e391f5f284505b1f1abb Mon Sep 17 00:00:00 2001 From: Adrien Blanc Date: Fri, 23 Jul 2021 15:33:24 +0200 Subject: [PATCH 2/2] updated writeJSON README --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b24290f..6f9c907 100644 --- a/README.md +++ b/README.md @@ -177,19 +177,26 @@ const json = await readJSON('www.url.com/file.json') #### writeJSON ```ts -writeJSON(path: string, data: any): void +writeJSON(path: string, data: any, replacer?: any, space?: string | number): void + + ``` Args: * **path**: path to a local JSON file * **data**: data to store as JSON +* **replacer**: [replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#parameters) function that transforms the results or an array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified +* **space**: adds indentation, white space, and line break characters to the to the JSON text to make it easier to read. + Usage: ```ts const data = { age: 40 } await writeJSON('./path/to/file.json', data) + +await writeJSON('./path/to/file-with-indentation', data, null, 2) ``` ### XLSX