Skip to content

Support "replace" utility function #87

@rarkins

Description

@rarkins

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions