Skip to content

Commit 59569f9

Browse files
authored
Merge pull request #7 from adblanc/feat/writeJSON-arguments
add all JSON.stringify args to writeJSON function
2 parents 97acd26 + f42f02e commit 59569f9

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,26 @@ const json = await readJSON('www.url.com/file.json')
177177
#### writeJSON
178178

179179
```ts
180-
writeJSON(path: string, data: any): void
180+
writeJSON(path: string, data: any, replacer?: any, space?: string | number): void
181+
182+
181183
```
182184

183185
Args:
184186

185187
* **path**: path to a local JSON file
186188
* **data**: data to store as JSON
189+
* **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
190+
* **space**: adds indentation, white space, and line break characters to the to the JSON text to make it easier to read.
191+
187192

188193
Usage:
189194

190195
```ts
191196
const data = { age: 40 }
192197
await writeJSON('./path/to/file.json', data)
198+
199+
await writeJSON('./path/to/file-with-indentation', data, null, 2)
193200
```
194201

195202
### XLSX
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"prices": "$20",
3+
"name": "table"
4+
}

src/json.ts

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

6-
export async function writeJSON(path: string, data: any) {
7-
await Deno.writeTextFile(path, JSON.stringify(data))
6+
export async function writeJSON(path: string, ...args: Parameters<typeof JSON.stringify>) {
7+
await Deno.writeTextFile(path, JSON.stringify(...args))
88
}
99

1010
export async function readJSONFromURL(url: string) {

tests/json-test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { readJSON, writeJSON } from '../src/json.ts'
33

44
const jsonReadPath = './examples/json/data.json'
55
const jsonWritePath = './examples/json/json-test.json'
6+
const jsonIndentWritePath = './examples/json/json-indent-test.json'
67

78
Deno.test("reads a json file", async () => {
89
const json = await readJSON(jsonReadPath)
@@ -18,5 +19,16 @@ Deno.test("writes a json file", async () => {
1819
await writeJSON(jsonWritePath, data)
1920
const json = await readJSON(jsonWritePath)
2021

22+
assertObjectMatch(json, { prices: "$20", name: "table" });
23+
})
24+
25+
Deno.test("writes a json file with space indent", async () => {
26+
const data = {
27+
prices: '$20',
28+
name: 'table'
29+
}
30+
await writeJSON(jsonIndentWritePath, data, null, 2)
31+
const json = await readJSON(jsonIndentWritePath)
32+
2133
assertObjectMatch(json, { prices: "$20", name: "table" });
2234
})

0 commit comments

Comments
 (0)