@tim-code/my-util 0.0.20 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/fs.js +13 -1
- package/src/fs.test.js +28 -1
package/package.json
CHANGED
package/src/fs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFile, stat } from "node:fs/promises"
|
|
1
|
+
import { readFile, stat, writeFile } from "node:fs/promises"
|
|
2
2
|
import { tmpdir } from "node:os"
|
|
3
3
|
import { promisify } from "node:util"
|
|
4
4
|
import { gunzip as _gunzip } from "node:zlib"
|
|
@@ -18,6 +18,18 @@ export async function getJSON(path) {
|
|
|
18
18
|
return JSON.parse(buffer.toString())
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Write JSON to a path.
|
|
23
|
+
* @param {string} path
|
|
24
|
+
* @param {Object|Array} object
|
|
25
|
+
* @param {Object} $1
|
|
26
|
+
* @param {number=} $1.indent Indent used to format JSON object. Default 2. If 0, does not indent object.
|
|
27
|
+
*/
|
|
28
|
+
export async function writeJSON(path, object, { indent = 2 } = {}) {
|
|
29
|
+
const string = JSON.stringify(object, undefined, indent)
|
|
30
|
+
await writeFile(path, string)
|
|
31
|
+
}
|
|
32
|
+
|
|
21
33
|
/**
|
|
22
34
|
* Get gzipped JSON from a path.
|
|
23
35
|
* @param {string} path
|
package/src/fs.test.js
CHANGED
|
@@ -4,10 +4,14 @@ const readFileMock = jest.fn()
|
|
|
4
4
|
const statMock = jest.fn()
|
|
5
5
|
const tmpdirMock = jest.fn()
|
|
6
6
|
const gunzipMock = jest.fn()
|
|
7
|
+
const writeFileMock = jest.fn()
|
|
7
8
|
jest.unstable_mockModule("node:fs/promises", () => ({
|
|
8
9
|
readFile: readFileMock,
|
|
9
10
|
stat: statMock,
|
|
10
11
|
}))
|
|
12
|
+
jest.unstable_mockModule("node:fs", () => ({
|
|
13
|
+
writeFile: writeFileMock,
|
|
14
|
+
}))
|
|
11
15
|
jest.unstable_mockModule("node:os", () => ({
|
|
12
16
|
tmpdir: tmpdirMock,
|
|
13
17
|
}))
|
|
@@ -20,7 +24,7 @@ jest.unstable_mockModule("node:util", () => ({
|
|
|
20
24
|
|
|
21
25
|
// Now import the module under test
|
|
22
26
|
const mod = await import("./fs.js")
|
|
23
|
-
const { getJSON, getCompressedJSON, pathExists, makeTempDirectory } = mod
|
|
27
|
+
const { getJSON, writeJSON, getCompressedJSON, pathExists, makeTempDirectory } = mod
|
|
24
28
|
|
|
25
29
|
describe("getJSON", () => {
|
|
26
30
|
beforeEach(() => jest.clearAllMocks())
|
|
@@ -32,6 +36,29 @@ describe("getJSON", () => {
|
|
|
32
36
|
})
|
|
33
37
|
})
|
|
34
38
|
|
|
39
|
+
describe("writeJSON", () => {
|
|
40
|
+
beforeEach(() => jest.clearAllMocks())
|
|
41
|
+
it("writes object as JSON with default indent", async () => {
|
|
42
|
+
await writeJSON("foo.json", { x: 2 })
|
|
43
|
+
const formatted = ["{", ' "x": 2', "}"].join("\n")
|
|
44
|
+
expect(writeFileMock).toHaveBeenCalledWith("foo.json", formatted)
|
|
45
|
+
})
|
|
46
|
+
it("writes array as JSON with default indent", async () => {
|
|
47
|
+
await writeJSON("foo.json", [1, 2])
|
|
48
|
+
const formatted = ["[", " 1,", " 2", "]"].join("\n")
|
|
49
|
+
expect(writeFileMock).toHaveBeenCalledWith("foo.json", formatted)
|
|
50
|
+
})
|
|
51
|
+
it("writes object as JSON with custom indent", async () => {
|
|
52
|
+
await writeJSON("foo.json", { x: 2 }, { indent: 4 })
|
|
53
|
+
const formatted = ["{", ' "x": 2', "}"].join("\n")
|
|
54
|
+
expect(writeFileMock).toHaveBeenCalledWith("foo.json", formatted)
|
|
55
|
+
})
|
|
56
|
+
it("writes object as JSON with no indent", async () => {
|
|
57
|
+
await writeJSON("foo.json", { x: 2 }, { indent: 0 })
|
|
58
|
+
expect(writeFileMock).toHaveBeenCalledWith("foo.json", '{"x":2}')
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
35
62
|
describe("getCompressedJSON", () => {
|
|
36
63
|
beforeEach(() => jest.clearAllMocks())
|
|
37
64
|
it("reads, decompresses, and parses JSON", async () => {
|