json-sculpt
Version:
JSON Sculpt is a utility library designed to transform or "sculpt" JSON objects into your desired shape.
35 lines (34 loc) • 1.07 kB
TypeScript
import { JsonObject, JsonValue } from "../types";
/**
* Replaces matched objects within a JSON structure with a specified replacement object
*
* @template Result - Type of the resulting object
* @param json - JSON object within which to perform the replacement
* @param replacement - Object that will replace matched objects
* @param matcher - JSON object used to match objects within the provided JSON structure
* @returns JSON object with all matched objects replaced with the replacement object
*
* @example
* const data = {
* name: "Brennan",
* age: 30,
* child: {
* name: "Daisy",
* age: 5,
* },
* };
*
* const replacement = {
* name: "Daisy Jr.",
* age: 1,
* };
*
* const matcher = {
* name: "Daisy",
* };
*
* const updatedData = replaceObjectInJson(data, replacement, matcher);
*
* console.log(updatedData); // { name: "Brennan", age: 30, child: { name: "Daisy Jr.", age: 1 } }
*/
export declare const replaceObjectInJson: <T extends JsonValue>(json: T, replacement: JsonObject, matcher: JsonObject) => T;