Skip to content

Commit 650075f

Browse files
committed
docs: update guide
1 parent ed62e17 commit 650075f

File tree

1 file changed

+79
-13
lines changed

1 file changed

+79
-13
lines changed

readme.md

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ Simplest way to modify JSON files
66

77
## Why?
88

9-
Becaues I got tired of writing read/write function for JSON files, especially when I need to change dozens of files.
9+
Becaues I got tired of writing `read`/`write` functions for JSON files, especially when I need to change dozens of files.
1010

1111
## Usage
1212

13+
### Basics
14+
1315
- Only async use
1416

1517
Let's say we *package.json* file:
@@ -29,18 +31,22 @@ Let's say we *package.json* file:
2931
And this code:
3032

3133
```ts
32-
import { modifyJsonFile } from "@zardoy/modify-json";
33-
34-
// of course, you should use path module here
35-
await modifyJsonFile("package.json", {
36-
name: s => `super ${s}`,
37-
main: "build/electron.js",
38-
dependencies: {
39-
"type-fest": "^1.0.0"
34+
import { modifyJsonFile } from "modify-json-file";
35+
36+
// modify package.json in the same dir
37+
await modifyJsonFile(
38+
path.join(__dirname, "package.json"),
39+
{
40+
name: s => `super ${s}`,
41+
main: "build/electron.js",
42+
dependencies: {
43+
"type-fest": "^1.0.0"
44+
}
4045
}
41-
})
46+
)
4247
```
43-
After running this code, we'll get this package.json:
48+
49+
After running this code, *package.json* will be:
4450

4551
```json
4652
{
@@ -53,14 +59,74 @@ After running this code, we'll get this package.json:
5359
}
5460
```
5561

56-
As you can see above, `modifyJsonFile` only merges fields on 1 level depth. Currently, we don't support merging in nested fields.
62+
As you can see above, `modifyJsonFile` only merges fields **only on top level**. Currently, we don't support merging in nested fields.
63+
64+
Note that to simplify docs I won't use `path` module anymore. It implies that you always use `path` module to specify path.
65+
66+
Also, I've decided to not to add
67+
68+
### Non-object root value
69+
70+
> Remember, that at root level value can be any valid JSON value: `string`, `number`, `boolean`, `null`, `object` or `array`.
71+
72+
Be aware of modifying non object JSON files (where root type is not an object). For example:
73+
74+
Our code:
75+
76+
```ts
77+
import { modifyJsonFile } from "modify-json-file";
78+
79+
// telling that root type is number (in this case it's obligatory)
80+
await modifyJsonFile<number>("package.json", n => n + 1);
81+
```
82+
83+
Expected JSON:
84+
85+
```json
86+
// 📁someNumber.json
87+
5
88+
```
89+
90+
Actual JSON:
91+
92+
```json
93+
// 📁someNumber.json
94+
{
95+
"retries": 5
96+
}
97+
```
98+
99+
After running the code above, without any warnings you will get this:
100+
101+
```json
102+
// 📁someNumber.json
103+
"[object Object]1"
104+
```
105+
106+
That's because callback `n => n + 1` has transformed `n` (object) into string.
107+
108+
Here, despite of the TS type (number), `n` is object in runtime, so `n + 1` just stringified `n` and returned `[object Object]1`.
109+
Then this module just stringified the string to store output as valid JSON string in file.
57110

58-
We're using [detect-indent](https://www.npmjs.com/package/detect-indent) to preserve the tab size in `.json` files.
111+
Remember, **this module doesn't do any type checking in runtime**, you need to use `typeof` in callback for checking root types or schema validators (like [ajv](http://npmjs.com/ajv)) for objects.
59112

113+
### Formatting
114+
115+
By default, it will preserve tab size (thanks to [detect-indent](https://www.npmjs.com/package/detect-indent)), but you can control this by overriding `tabSize` option. For example, we can use it to just format the file:
116+
117+
```ts
118+
import { modifyJsonFile } from "modify-json-file";
119+
120+
// this will format file to use \t (hard tabs)
121+
await modifyJsonFile("someFile.json", {}, { tabSize: "hard" });
122+
```
60123

61124
## TODO
62125

63126
Docs:
127+
128+
- [ ] Examples with immer
129+
- [ ] Make usage more clear
64130
- [ ] Fix auto generated docs
65131
- [ ] Describe all possible usage cases
66132
- [ ] Give a hint, that it doesn't perform schema checking again actual file contents when type is passed into generic function `modifyJsonFile`

0 commit comments

Comments
 (0)