{"id":17278124,"url":"https://github.com/mingun/structured-diff","last_synced_at":"2026-05-20T14:13:09.292Z","repository":{"id":141717772,"uuid":"125104300","full_name":"Mingun/structured-diff","owner":"Mingun","description":"Library for generating diffs for objects for use in assertion libraries","archived":false,"fork":false,"pushed_at":"2018-04-22T19:28:05.000Z","size":52,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-31T15:40:07.170Z","etag":null,"topics":["diff","json"],"latest_commit_sha":null,"homepage":null,"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/Mingun.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-13T19:33:51.000Z","updated_at":"2018-04-22T19:24:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"1befc5f0-d3d1-4938-8e0a-d52c9db99393","html_url":"https://github.com/Mingun/structured-diff","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/Mingun%2Fstructured-diff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mingun%2Fstructured-diff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mingun%2Fstructured-diff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mingun%2Fstructured-diff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mingun","download_url":"https://codeload.github.com/Mingun/structured-diff/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245668650,"owners_count":20653011,"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":["diff","json"],"created_at":"2024-10-15T09:10:52.269Z","updated_at":"2026-05-20T14:13:04.247Z","avatar_url":"https://github.com/Mingun.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://img.shields.io/travis/Mingun/structured-diff.svg?label=travis)](https://travis-ci.org/Mingun/structured-diff)\n[![Coverage Status](https://coveralls.io/repos/github/Mingun/structured-diff/badge.svg?branch=master)](https://coveralls.io/github/Mingun/structured-diff?branch=master)\n[![npm](https://img.shields.io/npm/v/structured-diff.svg)](https://www.npmjs.com/package/structured-diff)\n[![License](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT)\n\n# structured-diff\nGenerates structurual diffs for two JS objects. Objects can have circular references.\n\nThe first of all purpose of library -- to create nice diffs for assertion libraries.\n\n## Installation\n```\nnpm install structured-diff\n```\n\n## Usage\n```js\nlet api = require('structured-diff');\n\nlet objectDiff = api.generateDiff(\n  { some: 'foo', object: true },\n  { object: 'is', cool: ['yes', true] }\n);\n// or\nlet stringDiff = new api.Diff('some string', 'another string');\n\n// Get unified diff with inline diffs in each line with 4 lines of context\n// before and after changed lines\nlet unified = objectDiff.unified(4);\n\n// Get inline diff with 4 lines of context before and after changed lines\nlet inline  = stringDiff.inline(4);\n```\n`diff` has format describing by [JSON Schemas](https://github.com/Mingun/structured-diff/tree/master/schemas).\nAlso see [Diff format](#diff-format) section.\n\nAlso see [examples](https://github.com/Mingun/structured-diff/tree/master/examples).\n\n## API\nModule exports object with one function and one class for generating diffs:\n\n### Class `Diff`\n\n#### `constructor(expected: String, actual: String[, options: Object])`\n\nCreates diffs by comparing `expected` and `actual`. Both parameters must be strings.\n\n**Parameters**\n\n- `expected`: Base string which will be show as removed part of diff\n- `actual`: New string which will be showed as added part of diff\n- `options`: Options for diff algorithm. Optional parameter, defaults to `{}`. Supported options:\n  - `ignoreWhitespace` (type: `boolean`, default: `false`): ignores whitespace changes in lines;\n  - `newlineIsToken` (type: `boolean`, default: `false`): if `true`, each symbol `\\n` or sequence\n    `\\r\\n` considered as separate token, otherwise it includes as part in preceding token;\n  - `ignoreCase` (type: `boolean`, default: `false`): if `true`, tokens are compared case-insensitive;\n  - `comparator` (type: `function(String, String) -\u003e boolean`, default: `undefined`): function for\n    comparing tokens, must return `-1`, `0`, or `1` if first token less then, equals or\n    greater then second accordingly;\n  - `hint` (type: `Object|string`, default: `undefined`): the hint to diff algorithm with what\n    type of objects it works. Can be or string with type name or object `{ type: string, ... }`\n    with type and additional options dependent on type. For now only one type is known with\n    one additional option:\n    - `{ type: 'json', indent: Number }`. Parameter `indent` specify indentation size, used\n      for JSON stringification. This hint will be used by diff algorithm for produce nice\n      output in differences in indents\n\n#### `unified([context: Number]) -\u003e Object[]`\u003cbr/\u003e`inline ([context: Number]) -\u003e Object[]`\n\nConverts `Diff` object to array with hunks or lines, depending on presence of the `context`\nparameter. If the `context` is `undefined` then method returns an array, each element of which\nrepresents one line of the text. The text is divided into lines by `\\n` symbol or `\\r\\n`\nsequence. Otherwise array contains hunks with only changed lines and specified number of context\nlines before and after changed lines. In particular, if `context===0`, each hunk contains only\nthe changed lines.\n\nIf `context===null` then method returns one hunk with all lines if there exist some changes and\nempty array otherwise. It differs from behavior if `context` is `undefined` -- in this case\nthe array of all lines always returns even if there are no changes.\n\nEach line for `unified` method represented by the following object:\n```js\n{ kind: ' /+/-', value: 'line value' }\n// or\n{ kind: '+/-', changes: [{ kind: ' /+/-', value: 'part of string' }] }\n```\nwhere\n\n- `kind`: one of `' '`, `'+'` or `'-'`. Represents kind of change -- line not changed (context\n  line), line added in `actual` or line is removed in `actual`;\n- `value`: contains line text; for the changed lines existence of this property means that in them\n  there are no inline differences, otherwise object contains property `changes`. Context lines\n  always contains only that property;\n- `changes`: array of objects represents inline changes in the line. This property is used instead\n  of `value` property for lines with inline differences. Each array element is object with following\n  structure:\n  - `kind`: one of `' '`, `'+'` or `'-'`. Represents kind of change -- piece not changed, piece\n    added or removed;\n  - `value`: string contains part of text that the same in `expected` and `actual`, added or removed\n    within line.\n\nEach line for `inline` method represented by the following object:\n```js\n{ kind: ' /+/-/?', value: 'line value' }\n// or\n{ kind: '+/-', changes: [ { kind: ' /+/-', value: 'part of string' }] }\n```\nwhere\n\n- `kind`: one of `' '`, `'+'`, `'-'` or `'?'`. Represents kind of change -- line not changed (context\n  line), line added in `actual`, line is removed in `actual`, line contains inline differences;\n- `value`: for all kinds except `'?'`, contains line text; for the `kind = '?'` object contains\n  property `changes` instead of this property;\n- `changes`: array of objects represents inline changes in the line. This property is used only for\n  `kind = '?'` instead of `value` property. Each array element is object with following structure:\n  - `kind`: one of `' '`, `'+'` or `'-'`. Represents kind of change -- piece not changed, piece\n    added or removed;\n  - `value`: string contains part of text that the same in `expected` and `actual`, added or removed\n    within line.\n\nEach hunk in both method results represented by the following object:\n```js\n{\n  oldStart: \u003cNumber\u003e, oldLines: \u003cNumber\u003e,\n  newStart: \u003cNumber\u003e, newLines: \u003cNumber\u003e,\n  lines: \u003carray of Line objects described above\u003e\n}\n```\n\n`oldStart` represents line number (starts with 1) when the hunk is begin in `expected` string and\n`oldLines` represents all lines in the hunk that come from `expected` (context lines and removed lines).\n\n`newStart` and `newLines` represents the same things in the `actual` string, but `newLines` count\ncontext lines and added lines.\n\n**Parameters**\n\n- `context`: Number of unchanged lines to include in result around changed lines (specified count\n  applied to lines below and above changed lines, so 2 means up to 4 context lines)\n\n**Returns**: See description\n\n### `generateDiff(expected: Object, actual: Object[, options: Object]) -\u003e Diff`\n\nCreates diffs by comparing `expected` and `actual` converted to strings.\n\nIf both `expected` and `actual` are already strings, then the behavior is the same as\n`new Diff(expected, actual, options)`. Otherwise both objects will be converted to strings\nwith use of the stable algorithm working with circular references. At the moment stringification\nperformed with [safe-stable-stringify](https://github.com/BridgeAR/safe-stable-stringify). This\nis optional dependency so you can use this method only if it is installed, otherwise method\nthrow `Error`.\n\n**Parameters**\n\n- `expected`: `Object`, Base object which will be show as removed part of diff\n- `actual`: `Object`, New object which will be showed as added part of diff\n\n**Returns**: `Object[]`, Array with one `change` element for each line of text diff between\nstringified representation of compared objects. Each `change` object can also have information\nabout inline differences.\n\n## Diff format\nIn both diff formats deletions appears before insertions in changed parts.\n### Unified\nFunction `Diff.unified` returns array when each element describes one line in unified diff output.\nDescription of format also available as [JSON Schema v7](https://github.com/Mingun/structured-diff/blob/master/schemas/unified.schema.json).\n\nEach array element has `kind` property:\n- `' '` -- line not changed (the same in both sides). Line content in the `value` property\n- `'-'` -- line is removed (line is exist in `expected` and missing in `actual`).\n  Line content or in `value` or in `changes` property\n- `'+'` -- line is inserted (line is missing in `expected` and exist in `actual`).\n  Line content or in `value` or in `changes` property\n\nWhen line kind is `+` or `-`, line content can have inline changes.\nInline changes stored in the property `changes` as array of `inlineChange` object with properties `kind` and `value`.\nEach `value` always string with part of line content and `kind` property has values:\n- `' '` -- this part of line not changed (the same in both sides)\n- `'-'` -- this part of line is removed (is exist in `expected` and missing in `actual`)\n- `'+'` -- this part of line is inserted (is missing in `expected` and exist in `actual`)\n\nWhen line do not have inline changes it can contains property `value` instead of `changes` with string with all line content.\n\nFor example from [Usage](#usage) section the following diff will be generated (variable `unified`):\n```js\n[\n  { kind: ' ', value: '{' },\n  {\n    kind: '-',\n    changes: [\n      { kind: ' ', value: '  \"' },\n      { kind: '-', value: 'some' },\n      { kind: ' ', value: '\": \"' },\n      { kind: '-', value: 'foo' },\n      { kind: ' ', value: '\"' }\n    ]\n  },\n  {\n    kind: '-',\n    changes: [\n      { kind: ' ', value: '  \"' },\n      { kind: '-', value: 'object' },\n      { kind: ' ', value: '\": ' },\n      { kind: ' ', value: 'true' }\n    ]\n  },\n  {\n    kind: '+',\n    changes: [\n      { kind: ' ', value: '  \"' },\n      { kind: '+', value: 'object' },\n      { kind: ' ', value: '\": \"' },\n      { kind: '+', value: 'is' },\n      { kind: ' ', value: '\"' }\n    ]\n  },\n  {\n    kind: '+',\n    changes: [\n      { kind: ' ', value: '  \"' },\n      { kind: '+', value: 'cool' },\n      { kind: ' ', value: '\": ' },\n      { kind: '+', value: '[' }\n    ]\n  },\n  { kind: '+', value: '    \"yes\"' },\n  {\n    kind: '+',\n    changes: [\n      { kind: '+', value: '    ' },\n      { kind: ' ', value: 'true' }\n    ]\n  },\n  { kind: '+', value: '  ]' },\n  { kind: ' ', value: '}' }\n]\n```\n\n### Inline\nFunction `Diff.inline` returns array when each element describes one line in inline diff output.\nDescription of format also available as [JSON Schema v7](https://github.com/Mingun/structured-diff/blob/master/schemas/inline.schema.json).\n\nEach array element has `kind` property:\n- `' '` -- line not changed (the same in both sides). Line content in the `value` property\n- `'-'` -- line is removed (line is exist in `expected` and missing in `actual`).\n  Line content in the `value` property\n- `'+'` -- line is inserted (line is missing in `expected` and exist in `actual`).\n  Line content in the `value` property\n- `'?'` -- line exists in both sides but not identical. Line content in the `changes` property\n\nWhen line kind is `?`, line content has inline changes.\nInline changes stored in the property `changes` as array of `inlineChange` object with properties `kind` and `value`.\nEach `value` always string with part of line content and `kind` property has values:\n- `' '` -- this part of line not changed (the same in both sides)\n- `'-'` -- this part of line is removed (is exist in `expected` and missing in `actual`)\n- `'+'` -- this part of line is inserted (is missing in `expected` and exist in `actual`)\n\nFor example from [Usage](#usage) section the following diff will be generated (variable `inline`):\n```js\n[\n  {\n    kind: '?',\n    changes: [\n      { kind: '-', value: 'some' },\n      { kind: '+', value: 'another' },\n      { kind: ' ', value: ' string' }\n    ]\n  }\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmingun%2Fstructured-diff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmingun%2Fstructured-diff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmingun%2Fstructured-diff/lists"}