{"id":13447865,"url":"https://github.com/newxps/var-diff","last_synced_at":"2025-03-22T01:31:29.318Z","repository":{"id":34878211,"uuid":"186401254","full_name":"newxps/var-diff","owner":"newxps","description":"javascript variable diff algorithm","archived":false,"fork":false,"pushed_at":"2022-12-30T17:43:14.000Z","size":149,"stargazers_count":6,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T10:16:56.055Z","etag":null,"topics":[],"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/newxps.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":"2019-05-13T10:51:21.000Z","updated_at":"2022-02-04T16:47:44.000Z","dependencies_parsed_at":"2023-01-15T09:50:16.492Z","dependency_job_id":null,"html_url":"https://github.com/newxps/var-diff","commit_stats":null,"previous_names":["flfwzgl/var-diff"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newxps%2Fvar-diff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newxps%2Fvar-diff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newxps%2Fvar-diff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newxps%2Fvar-diff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/newxps","download_url":"https://codeload.github.com/newxps/var-diff/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244893458,"owners_count":20527597,"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":[],"created_at":"2024-07-31T05:01:28.924Z","updated_at":"2025-03-22T01:31:29.065Z","avatar_url":"https://github.com/newxps.png","language":"JavaScript","readme":"\n\n# var-diff\n\nget the difference between two variables\n\n\u003cp\u003e\n  \u003ca href=\"https://codecov.io/gh/flfwzgl/var-diff\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/flfwzgl/var-diff/branch/master/graph/badge.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/var-diff\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/var-diff.svg\" alt=\"Version\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n### Compatibility\n|   IE     | Chrome | Firefox  | Opera |  Safari     |\n|   :-:    |  :-:   |   :-:    | :-:   |   :--:      |\n|    ✔️    |   ✔️   |   ✔️     |  ✔️  |    ✔️       |\n### Installation\n\n```bash\nnpm i var-diff\n```\nor\n```html\n\u003cscript src=\"var-diff.js\"\u003e\u003c/script\u003e\n```\n\n### Usage\n\nuse `var patches = diff.get(origin, target, [key])` to get difference `patches` between origin and target variables.\n\n`key` is an optional parameter used to improve sorting performance.\n`patches` is an array containing all of the differences betwen two variables\n\n```\n// patches\nt: type   1-create 2-delete 3-edit 4-replace\np: path {Array}\nv: value\ni: new index(when type is replace)\n```\n\nuse `diff.apply(origin, patches)` to make the origin variable apply the `patches` then become the target varible. but all references of the origin variable are reserved.\n\n```javascript\nconst diff = require('var-diff');\n\nlet a = [1, 2, 3, {id: 1, name: 'tom'}, {id: 2, name: 'jack'}]\nlet b = [2, {id: 2, name: 'will'}, 3, {id: 1, name: 'lucy'}]\n\nlet patches = diff.get(origin, target, 'id');\nconsole.log(patches);\n\n/**\n * patches\n[ \n  { t: 2, p: [ 0 ] },                       // remove the 0th element \n  { t: 3, p: [ 2, 'name' ], v: 'lucy' },    // update the name of the 2th element to 'lucy'\n  { t: 3, p: [ 3, 'name' ], v: 'will' },    // update the name of the 3th element to 'will'\n  { t: 4, p: [ 3 ], i: 1 },                 // change the 3th element to the 1th position\n  { t: 4, p: [ 3 ], i: 2 }                  // change the 3th element to the 2th position\n]\n */\n\n\na = diff.apply(a, patches);\n\nconsole.log(JSON.stringify(a) === JSON.stringify(b));\n\n// true\n```\n\n\n\n### Examples\n\n```javascript\nvar a = {\n  name: 'tom',\n  gender: 'male',\n  list: [{\n    id: 1,\n    label: 'teacher'\n  }]\n}\n\nvar b = {\n  name: 'jack',\n  list: [{\n    id: 2,\n    label: 'worker'\n  }, {\n    id: 3,\n    label: 'student'\n  }, {\n    id: 1,\n    label: ''\n  }]\n}\n\np = diff.get(a, b, 'id');\n\nconsole.log(p);\n/**\n  t: type   1-create 2-delete 3-edit 4-replace\n  p: path {Array}\n  v: value\n  i: new index(when type is replace)\n\n [{ t: 3, p: [ 'name' ], v: 'jack' },\n  { t: 2, p: [ 'gender' ] },\n  { t: 3, p: [ 'list', 0, 'label' ], v: '' },\n  { t: 1, p: [ 'list', 1 ], v: { id: 2, label: 'worker' } },\n  { t: 1, p: [ 'list', 2 ], v: { id: 3, label: 'student' } },\n  { t: 4, p: [ 'list', 0 ], i: 2 },\n  { t: 4, p: [ 'list', 1 ], i: 0 },\n  { t: 1, p: [ 'age' ], v: 25 }]\n */\n\nvar aa = diff.apply(a, p, 'id'); // key\nconsole.log(JSON.stringify(aa) === JSON.stringify(b));  // true\n```\n\n\n```javascript\nvar a = [NaN, NaN];\nvar b = [NaN, NaN];\n\nvar p = diff.get(a, b, 'id');\n\nconsole.log(p); // []\n```\n\n\n### LICENSE\nMIT","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnewxps%2Fvar-diff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnewxps%2Fvar-diff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnewxps%2Fvar-diff/lists"}