edit-package-json0.2.3

Edit package.json without parsing, as string, to keep the formatting intact

§ Quick Take

import { strict as assert } from "assert";
import { set, del } from "dist/edit-package-json.esm";

// edit JSON as string
assert.equal(
  set(
    `{
  "name": "test",
  "dependencies": {
    "ranges-apply": "^2.0.0",
    "string-left-right": "^2.3.30"
  },
  "devDependencies": {}
}`,
    "dependencies.ranges-apply", // path to amend
    "^3.2.2" // new value
  ),
  `{
  "name": "test",
  "dependencies": {
    "ranges-apply": "^3.2.2",
    "string-left-right": "^2.3.30"
  },
  "devDependencies": {}
}`
);

// edit from JSON string
assert.equal(
  del(
    `{
  "name": "test",
  "dependencies": {
    "ranges-apply": "^2.0.0",
    "string-left-right": "^2.3.30"
  },
  "devDependencies": {}
}`,
    "devDependencies" // path to delete
  ),
  `{
  "name": "test",
  "dependencies": {
    "ranges-apply": "^2.0.0",
    "string-left-right": "^2.3.30"
  }
}`
);

§ Examples

§ Purpose

Edit JSON contents as strings to guarantee the formatting will be intact.

API uses object-path opens in a new tab notation to set values on any (for now, only already-existing) paths in JSON.

It's powering the update-versions CLI.

§ API

There are two methods: set() and del():

§ .set()

set(source, path, val)

Input argumentTypeObligatory?Description
strStringyesJSON file contents
pathStringyesDesired EXISTING path in the object, must follow object-path opens in a new tab notation
valToInsertWhateveryesWhat to insert at the given path

You can't create new paths, only amend existing-ones.

Output

An amended string is returned.

To repeat, set() can't create new paths yet, it's still in a baby state. set() can only edit existing paths in JSON.

§ .del()

del(source, path)

Input argumentTypeObligatory?Description
strStringyesJSON file contents
pathStringyesDesired path in the object to delete, must follow object-path opens in a new tab notation

Output

An amended string is returned.

§ Testing

Besides regular unit tests, we went great lengths to ensure this program works as intended. We took all 122 package.json's in this monorepo, then took every single leaf path in every package.json and amended and deleted it two ways:

  • editing it directly, using this program, string-in, string-out
  • editing using object-path opens in a new tab, plain-object-in, plain-object-out, then stringify

Every single path edit matched! By the way, we ended up with 12,850 asserts.

See the source of the test file opens in a new tab.

§ Changelog

See it in the monorepo opens in a new tab, on Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 object-path opens in a new tab
Access deep object properties using a path
📦 easy-replace 3.9.0
Replace strings with optional lookarounds, but without regexes
📦 str-indexes-of-plus 2.11.0
Like indexOf but returns array and counts per-grapheme
📦 email-all-chars-within-ascii 2.10.1
Scans all characters within a string and checks are they within ASCII range
📦 js-row-num 3.0.2
Update all row numbers in all console.logs in JS code