@fellow/prosemirror-recreate-transform
Version:
Create a set of steps transforming one prosemirror json document to another
22 lines (20 loc) • 681 B
text/typescript
import { JSONValue } from "./types";
/**
* get target value from json-pointer (e.g. /content/0/content)
* @param {AnyObject} obj object to resolve path into
* @param {string} path json-pointer
* @return {any} target value
*/
export function getFromPath(obj: JSONValue, path: string): JSONValue {
const pathParts = path.split("/");
pathParts.shift(); // remove root-entry
while (pathParts.length) {
if (typeof obj != "object") {
throw new Error();
}
const property = pathParts.shift() as string;
// @ts-expect-error property is a string but obj may be an array
obj = obj[property];
}
return obj;
}