Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "replace" utility function #87

Open
rarkins opened this issue Mar 17, 2024 · 0 comments
Open

Support "replace" utility function #87

rarkins opened this issue Mar 17, 2024 · 0 comments

Comments

@rarkins
Copy link

rarkins commented Mar 17, 2024

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant