Skip to content

Commit 92b789a

Browse files
author
Adrien Blanc
committed
add all JSON.stringify args to writeJSON function
1 parent 97acd26 commit 92b789a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed
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)