benjamine/jsondiffpatch được TopGit xếp vào nhóm dự án mã nguồn mở, với 5.4k sao trên GitHub, viết chủ yếu bằng TypeScript. Diff & patch JavaScript objects
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
smart array diffing using LCS, IMPORTANT NOTE: to match objects inside an array you must provide an objectHash function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check Array diff documentation
(optionally) text diffing of long strings powered by google-diff-match-patch (diff at character level)
reverse a delta, unpatch (eg. revert object to its original state using a delta)
multiple output formats:
pure JSON, low footprint delta format
visualdiff (html), see demo
annotated JSON (html), to help explain the delta format with annotations
JSON Patch (RFC 6902), can generate patches, and also apply them
// sample data
const country = {
name: 'Argentina',
capital: 'Buenos Aires',
independence: new Date(1816, 6, 9),
};
// clone country, using dateReviver for Date objects
const country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver);
// make some changes
country2.name = 'Republica Argentina';
country2.population = 41324992;
delete country2.capital;
const delta = jsondiffpatch.diff(country, country2);
assertSame(delta, {
name: ['Argentina', 'Republica Argentina'], // old value, new value
population: ['41324992'], // new value
capital: ['Buenos Aires', 0, 0], // deleted
});
// patch original
jsondiffpatch.patch(country, delta);
// reverse diff
const reverseDelta = jsondiffpatch.reverse(delta);
// also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta);
const delta2 = jsondiffpatch.diff(country, country2);
assert(delta2 === undefined);
// undefined => no difference
Array diffing:
// sample data
const country = {
name: 'Argentina',
cities: [
{
name: 'Buenos Aires',
population: 13028000,
},
{
name: 'Cordoba',
population: 1430023,
},
{
name: 'Rosario',
population: 1136286,
},
{
name: 'Mendoza',
population: 901126,
},
{
name: 'San Miguel de Tucuman',
population: 800000,
},
],
};
// clone country
const country2 = JSON.parse(JSON.stringify(country));
// delete Cordoba
country.cities.splice(1, 1);
// add La Plata
country.cities.splice(4, 0, {
name: 'La Plata',
});
// modify Rosario, and move it
const rosario = country.cities.splice(1, 1)[0];
rosario.population += 1234;
country.cities.push(rosario);
// create a configured instance, match objects by name
const diffpatcher = jsondiffpatch.create({
objectHash: function (obj) {
return obj.name;
},
});
const delta = diffpatcher.diff(country, country2);
assertSame(delta, {
cities: {
_t: 'a', // indicates this node is an array (not an object)
1: [
// inserted at index 1
{
name: 'Cordoba',
population: 1430023,
},
],
2: {
// population modified at index 2 (Rosario)
population: [1137520, 1136286],
},
_3: [
// removed from index 3
{
name: 'La Plata',
},
0,
0,
],
_4: [
// move from index 4 to index 2
'',
2,
3,
],
},
});
For more example cases (nested objects or arrays, long text diffs) check packages/jsondiffpatch/test/examples/
If you want to understand deltas, see delta format documentation
Installing
NPM
This works for node, or in browsers if you already do bundling on your app
npm install jsondiffpatch
import {* as jsondiffpatch} from 'jsondiffpatch';
const jsondiffpatchInstance = jsondiffpatch.create(options);
browser
In a browser, you can load a bundle using a tool like esm.sh or Skypack.
Options
import * as jsondiffpatch from 'jsondiffpatch';
// Only import if you want text diffs using diff-match-patch
import { diff_match_patch } from '@dmsnell/diff-match-patch';
const jsondiffpatchInstance = jsondiffpatch.create({
// used to match objects when diffing arrays, by default only === operator is used
objectHash: function (obj) {
// this function is used only to when objects are not equal by ref
return obj._id || obj.id;
},
arrays: {
// default true, detect items moved inside the array (otherwise they will be registered as remove+add)
detectMove: true,
// default false, the value of items moved is not included in deltas
includeValueOnMove: false,
},
textDiff: {
// If using text diffs, it's required to pass in the diff-match-patch library in through this proprty.
// Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options.
diffMatchPatch: diff_match_patch,
// default 60, minimum string length (left and right sides) to use text diff algorithm: google-diff-match-patch
minLength: 60,
},
propertyFilter: function (name, context) {
/*
this optional function can be specified to ignore object properties (eg. volatile data)
name: property name, present in either context.left or context.right objects
context: the diff context (has context.left and context.right objects)
*/
return name.slice(0, 1) !== '$';
},
cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned
(using jsondiffpatch.clone by default), to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching the same objects multiple times without serializing deltas.
instead of true, a function can be specified here to provide a custom clone(value).
*/
omitRemovedValues: false /* if you don't need to unpatch (reverse deltas),
"old"/"left" values (removed or replaced) are not included in the delta.
you can set this to true to get more compact deltas.
*/,
});
diff(), patch() and reverse() functions are implemented using Pipes & Filters pattern, making it extremely customizable by adding or replacing filters on a pipe.
Check Plugins documentation for details.
Related Projects
jsondiffpatch.net (C#)
SystemTextJson.JsonDiffPatch
(C#)
Go JSON Diff (and Patch)
json-diff-patch (python)
jsondiffpatch-react, also check docs for usage in react
benjamine/jsondiffpatch có 5.4k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/benjamine/jsondiffpatch. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
benjamine/jsondiffpatch có những chủ đề gì?
GitHub topics của benjamine/jsondiffpatch: "delta", "diff", "diffing", "json", "jsondiffpatch", "patch", "text-diff". TopGit xếp repo vào nhóm mã nguồn mở.
benjamine/jsondiffpatch có phải mã nguồn mở không?
Có — benjamine/jsondiffpatch phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/benjamine/jsondiffpatch.
benjamine/jsondiffpatch có trang demo không?
Dự án có trang chủ ở https://jsondiffpatch.com. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
benjamine/jsondiffpatch còn đang phát triển không?
Commit gần nhất trên benjamine/jsondiffpatch là 13 ngày trước (theo timestamp GitHub). Repo có 497 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc thêm về benjamine/jsondiffpatch ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/benjamine/jsondiffpatch là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về jsondiffpatch?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về jsondiffpatch.