{"id":20668649,"url":"https://github.com/lustan3216/json-history","last_synced_at":"2025-04-19T18:02:00.397Z","repository":{"id":42811767,"uuid":"269316119","full_name":"lustan3216/json-history","owner":"lustan3216","description":"A plugin can redo, undo deep nested JSON. Vue and React friendly.","archived":false,"fork":false,"pushed_at":"2023-01-05T20:58:10.000Z","size":961,"stargazers_count":4,"open_issues_count":9,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T00:12:13.815Z","etag":null,"topics":["json","json-","state-management"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lustan3216.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-04T09:28:19.000Z","updated_at":"2024-12-19T15:56:14.000Z","dependencies_parsed_at":"2023-02-04T16:15:23.147Z","dependency_job_id":null,"html_url":"https://github.com/lustan3216/json-history","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lustan3216%2Fjson-history","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lustan3216%2Fjson-history/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lustan3216%2Fjson-history/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lustan3216%2Fjson-history/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lustan3216","download_url":"https://codeload.github.com/lustan3216/json-history/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249755880,"owners_count":21320974,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","json-","state-management"],"created_at":"2024-11-16T20:10:35.407Z","updated_at":"2025-04-19T18:02:00.325Z","avatar_url":"https://github.com/lustan3216.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-history\n* A plugin can `redo`, `undo` deep nested JSON.\n* **Vue** or **React** friendly.\n* Support `Date` as value but `regex` and `function`\n* min+gzipped 12.2kB\n* uses [google-diff-match-patch](https://github.com/google/diff-match-patch) for long text diffs (diff at character level)\n* Import [jsondiffpatch](https://github.com/benjamine/jsondiffpatch) but without the formatter.\n\nThe source code still messy but works fine. I will refactor once have time.\n\n## Installing\n```javascript\n  yarn add json-history\n```\n```javascript\nconst history = new JsonHistory({\n  tree: object | array\n})\n```\n\n## Options\n```javascript\nnew JsonHistory({\n  tree = {}, \n  steps = 50, // how many steps can redo \n  backUpDeltas = [], // can backup the previous deltas\n  callback = {\n    onRecorded() {},\n    onEachPatch() {},\n    onUndo() {},\n    onUndid() {},\n    onRedo() {},\n    onRedid() {},\n  },  \n  setter(tree, key, value) {\n    Vue.set(tree, key, value)\n  },\n  deleter(tree, key) {\n    Vue.delete(tree, key)\n  }\n})\n```\n## Record / Redo / Undo\nUpdate any value of object or array by multi records. Can `redo` or `undo` after `record`.\n\nAccept array or an object.\n```javascript\nconst history = new JsonHistory({\n  tree: {\n    1: [1, 3],\n    a: [{}, { 1: 3 }],\n    b: { c: 5, d: { e: 6 }}\n  }\n})\n\nhistory.record({ \n  path: '1[4]',\n  value: {},\n  insertArray: true\n})\n\nexpect(history.tree).toEqual({\n  1: [1, 3, null, null, {}],\n  a: [{}, {1: 3}],\n  b: {c: 5, d: {e: 6}}\n})\n\nhistory.record([\n  {\n    path: '1[4]',\n    value: [],\n    insertArray: true\n  },\n  {\n    path: '1[5]',\n    value: [1, 2, 3]\n  }\n])\n\nexpect(history.tree).toEqual({\n  1: [1, 3, null, null, [], [1, 2, 3]],\n  a: [{}, {1: 3}],\n  b: {c: 5, d: {e: 6}}\n})\n\nhistory.record([{\n  path: 'a.b',\n  value: 123\n}])\n\nexpect(history.tree).toEqual({\n  1: [1, 3, null, null, [], [1, 2, 3]],\n  a: { b: 123 },\n  b: {c: 5, d: {e: 6}}\n})\n\nexpect(history.deltas.length).toEqual(3)\n\nhistory.undo()\nhistory.redo()\nhistory.undo()\nhistory.undo()\nhistory.undo()\nexpect(history.tree).toEqual(tree)\n```\n\n## Delete\nDelete object key.\n\nSame as `record` with `undefined` value.\n```javascript\nconst history = new JsonHistory({\n  tree: {\n    1: [1, 3],\n    a: [{}, { 1: 3 }],\n    b: { c: 5, d: { e: 6 }}\n  }\n})\n\nhistory.delete([{\n  path: '1'\n}])\n\nexpect(history.tree).toEqual({\n  a: [{}, {1: 3}],\n  b: {c: 5, d: {e: 6}}\n})\n```\n\n## Snapshot\nReplace the whole tree and record a step.\n\n```javascript\nconst history = new JsonHistory({\n  tree: [1, 2, 3]\n})\n\nhistory.snapShot([1, { a: 4 }])\n\nexpect(history.tree).toEqual([1, { a: 4 }])\n\nhistory.undo()\n\nexpect(history.tree).toEqual([1, 2, 3])\n```\n\n## Merge records\nCan merge multi actions, such as `delete` or `record`.\n\n```javascript\n const history = new JsonHistory({\n  tree: {\n    1: [1, 3],\n    a: [{}, { 1: 3 }],\n    b: { c: 5, d: { e: 6 }}\n  }\n})\n\nhistory.recordsMerge(() =\u003e {\n  history.record([\n    {\n      path: 'b',\n      value: undefined\n    }\n  ])\n\n  history.record([\n    {\n      path: 1,\n      value: []\n    }\n  ])\n\n  history.record([\n    {\n      path: 'a.length.length',\n      value: {}\n    }\n  ])\n})\n\nexpect(history.deltas.length).toEqual(1)\n\nexpect(history.tree).toEqual({\n  1: [],\n  a: {\n    length: {\n      length: {}\n    }\n  }\n})\n```\n\n## Clean records\nCan pass a function to check the delta should delete or not.\n\n```javascript\nconst shouldDeleteFunction = function (defalt) {\n  const key = Object.keys(delta)[0]\n  return key === 'this one should delete'\n}\ncleanDeltas(shouldDeleteFunction)\n```\n\n```javascript\nconst history = new JsonHistory({\n  tree: {\n    1: [1, 3],\n    a: [{}, { 1: 3 }],\n    b: { c: 5, d: { e: 6 }}\n  }\n})\n\nhistory.record([\n  {\n    path: 'b',\n    value: undefined\n  }\n])\n\nhistory.record([\n  {\npath: 1,\nvalue: []\n  }\n])\n\nhistory.record([\n  {\npath: 'a.length.length',\nvalue: {}\n  }\n])\n\nhistory.undo()\n\nhistory.cleanDeltas(delta =\u003e {\n  const key = Object.keys(delta)[0]\n  return key === '1'\n})\n\nhistory.undo()\nhistory.undo()\n\nexpect(history.tree).toEqual({\n  1: [],\n  a: [{}, {1: 3}],\n  b: {c: 5, d: {e: 6}}\n})\n```\n\n## More cases\nhttps://github.com/lustan3216/json-history/tree/master/tests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flustan3216%2Fjson-history","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flustan3216%2Fjson-history","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flustan3216%2Fjson-history/lists"}