Installation
Quick Take
Examples
- Delete an array item by its dot-path index
- Delete a nested object key
- Minimal example,
del() - Minimal example,
set() - Replace an existing array item by its dot-path index
- Replace an existing nested value with null
- Set a numeric value without changing surrounding formatting
- Replace an object value while preserving outer formatting
Purpose
Edit JSON contents as strings to guarantee the formatting will be intact.
API uses object-path notation to set values on any (for now, only already-existing) paths in JSON.
It’s powering the update-versions CLI.
API — set()
The function set() is imported like this:
It’s a function which takes three input arguments:
| Input argument | Type | Obligatory | Description |
|---|---|---|---|
strType: String Obligatory: yes | |||
str | String | yes | JSON file contents |
pathType: String Obligatory: yes | |||
path | String | yes | Desired EXISTING path in the object, must follow object-path notation |
valToInsertType: Whatever Obligatory: yes | |||
valToInsert | Whatever | yes | What to insert at the given path |
You can’t create new paths, only amend existing-ones.
Function returns an amended string.
API — del()
The function del() is imported like this:
It’s a function which takes two input arguments:
| Input argument | Type | Obligatory | Description |
|---|---|---|---|
strType: String Obligatory: yes | |||
str | String | yes | JSON file contents |
pathType: String Obligatory: yes | |||
path | String | yes | Desired path in the object to delete, must follow object-path notation |
Function returns an amended string.
API — version
You can import version:
API — types
This package is written in TypeScript and exports the type Inputs — the internal request shape both set() and del() build — the source str, the path, the mode and, for set(), the valToInsert.
import type { Inputs } from "edit-package-json";