-
Notifications
You must be signed in to change notification settings - Fork 79
Consider supporting JSONPatch #16
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
Comments
That could be done, but it would require a breaking change and might come at a bit of a performance cost because of the value handling (the value can be an array or object instead of just going a level deeper). |
I am returning to this, as this is more possible than I initially thought. |
I am not an existing user but my frontend basically necessitate a diff library that output JSON-Patch given that libraries on the backend commonly expect (or generate) this format. |
I am looking at doing this again. However, a feature some people rely on in Microdiff is the |
I would personally vote for the config but it's your codebase, do as you please :) to be clear what you mean is: for the given input {
"baz": "qux",
"foo": "bar"
} and following patch [
{ "op": "replace", "path": "/baz", "value": "boo" },
{ "op": "add", "path": "/hello", "value": ["world"] },
{ "op": "remove", "path": "/foo" }
] microdiff would output: {
"oldValueBaz": "qux",
"oldValueFoo": "bar",
"oldValueBaz": "???"
"baz": "boo",
"hello": ["world"]
} instead of {
"baz": "boo",
"hello": ["world"]
} ? What's the exact format |
I am little confused about what you mean. I am not talking about adding patching functionality for Microdiff. What I am saying is that Microdiff would output JSON Patch statements. For example, if oldValue was included, the output could be:
As for use cases, it is anything that needs the past value, as getting it from the path and the old object can be slow and annoying. For example, the old value would be helpful if you built a diff viewer. |
Hello, I think keeping "oldValue" is reasonable if any other key is compatible with json-patch. Maybe making json-patch available through a new file to import might help to avoid config / breaking changes.
|
I'm going to play with microdiff and json-patch soon by transforming the result to a patch. Do you have a branch with json-patch? I might play with microdiff locally and try to implement something |
@Julienng There is an implementation of patching functionality, although it is currently only on GitHub, not NPM (https://github.com/AsyncBanana/micropatch). |
+1 for this! Love your micro library :) |
👍 Bump. @AsyncBanana any chance you might be able to do a quick prototype of the JSONPatch result? Maybe then I or someone else could finish it up. Thank you! |
Here is a quick function I wrote for converting Microdiff's patching to JSONPatch @jgornick import microdiff, { type Difference } from "microdiff";
interface BasePatch {
op: string;
path: string;
}
interface AddPatch extends BasePatch {
op: "add";
value: any;
}
interface ReplacePatch extends BasePatch {
op: "replace";
value: any;
}
interface RemovePatch extends BasePatch {
op: "remove";
}
type JSONPatch = AddPatch | ReplacePatch | RemovePatch;
function convertPath(path: Difference["path"]): string {
return (
"/" +
path
.map((pathEl) =>
typeof pathEl === "string"
? pathEl.replace(/[~/]/g, (c) => (c === "~" ? "~0" : "~1"))
: pathEl,
)
.join("/")
);
}
function convertToJSONPatch(input: Difference[]): JSONPatch[] {
return input.map((diff) =>
diff.type === "CREATE" || diff.type === "CHANGE"
? {
op: diff.type === "CREATE" ? "add" : "replace",
path: convertPath(diff.path),
value: diff.value,
}
: { op: "remove", path: convertPath(diff.path) },
);
} I haven't optimized or tested it much, but because most of the differences between JSONPatch and Microdiff's diff format are relatively superficial, there shouldn't be any major issues. Moving forward, I still see three options: integrating a conversion function into Microdiff under |
http://jsonpatch.com/
as the format is "standard" and is widely supported on backend stacks
The text was updated successfully, but these errors were encountered: