microsoft / microsoft/node-jsonc-parser

Support "replace" utility function

Open
#87 0 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
759
Forks
66
Avg merge
5d 10h
Merged PRs (30d)
7

Description

Similar to the "modify" function, but allowing the key to be changed too.

The two use cases I have in mind for this is:

  • Renaming a key in-place while retaining the existing value
  • Replacing an existing key/value with new key/value in the same place

Of course, the existing modify function could be overloaded to support this, but I suggest a new function because:

  • It's not valid to supply the root for a replace
  • The meaning of undefined value param changes

Example of how this would look if "replace" is implemented:

/**
 * Computes the edit operations needed to replace a key/value in the JSON document.
 * 
 * @param documentText The input text 
 * @param path The path of the value to change. The path represents either to a property or an array item.
 * If the path points to an non-existing property or item, an error will be thrown. 
 * @param key The new key name for the specified property or item.
 * @param value The new value for the specified property or item. If the value is undefined,
 * the existing value will be retained.
 * @param options Options
 * @returns The edit operations describing the changes to the original document, following the format described in {@linkcode EditResult}.
 * To apply the edit operations to the input, use {@linkcode applyEdits}.
 */
export function replace(text: string, path: JSONPath, key: string, value: any, options: ModificationOptions): EditResult;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing "modify" function and its handling of paths, keys, and undefined values. Define the replace behavior for renaming a key, replacing a key/value, rejecting the root path, and retaining the value when it is undefined; done means the exported function produces the documented edit operations for those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.