{"id":24922862,"url":"https://github.com/liddiard/text-diff","last_synced_at":"2025-04-04T14:05:33.445Z","repository":{"id":35128765,"uuid":"39331815","full_name":"liddiard/text-diff","owner":"liddiard","description":"Visual JavaScript text diff library. CommonJS module created from https://code.google.com/p/google-diff-match-patch/.","archived":false,"fork":false,"pushed_at":"2015-07-19T13:27:17.000Z","size":136,"stargazers_count":150,"open_issues_count":1,"forks_count":22,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T13:06:29.092Z","etag":null,"topics":["characters","equality","javascript-diff-library","text-diff"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liddiard.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-19T11:40:54.000Z","updated_at":"2025-02-24T17:27:14.000Z","dependencies_parsed_at":"2022-08-17T21:30:52.501Z","dependency_job_id":null,"html_url":"https://github.com/liddiard/text-diff","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liddiard%2Ftext-diff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liddiard%2Ftext-diff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liddiard%2Ftext-diff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liddiard%2Ftext-diff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liddiard","download_url":"https://codeload.github.com/liddiard/text-diff/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247190166,"owners_count":20898697,"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":["characters","equality","javascript-diff-library","text-diff"],"created_at":"2025-02-02T11:33:57.107Z","updated_at":"2025-04-04T14:05:33.416Z","avatar_url":"https://github.com/liddiard.png","language":"JavaScript","readme":"# TextDiff\n## JavaScript diff library with support for visual, HTML-formatted output\n\nThis repository contains the diff functionality of the [google-diff-match-patch library](http://code.google.com/p/google-diff-match-patch/) by Neil Fraser, turned into a node module which is suitable for `require`ing into projects.\n\n## [Demo](http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html)\n\n## Example usage\n\n```javascript\nvar Diff = require('text-diff');\n\nvar diff = new Diff(); // options may be passed to constructor; see below\nvar textDiff = diff.main('text1', 'text2'); // produces diff array\ndiff.prettyHtml(textDiff); // produces a formatted HTML string\n\n```\n\n### Initialization options\n\nArguments may be passed into the `Diff` constructor in the form of an object:\n\n- `timeout`: Number of seconds to map a diff before giving up (0 for infinity).\n- `editCost`: Cost of an empty edit operation in terms of edit characters.\n\nExample initialization with arguments: `var diff = new Diff({ timeout: 2, editCost: 6 });`\n\n## Documentation\n\nThe API documentation below has been modified from the [original API documentation](https://code.google.com/p/google-diff-match-patch/wiki/API).\n\n### Initialization\n\nThe first step is to create a new diff object (see example above). This object contains various properties which set the behaviour of the algorithms, as well as the following methods/functions:\n\n### main(text1, text2) =\u003e diffs\n\nAn array of differences is computed which describe the transformation of text1 into text2. Each difference is an array.  The first element specifies if it is an insertion (1), a deletion (-1) or an equality (0). The second element specifies the affected text.\n\n```javascript\nmain(\"Good dog\", \"Bad dog\") =\u003e [(-1, \"Goo\"), (1, \"Ba\"), (0, \"d dog\")]\n```\n\nDespite the large number of optimisations used in this function, diff can take a while to compute. The `timeout` setting is available to set how many seconds any diff's exploration phase may take (see \"Initialization options\" section above). The default value is 1.0. A value of 0 disables the timeout and lets diff run until completion. Should diff time out, the return value will still be a valid difference, though probably non-optimal.\n\n### cleanupSemantic(diffs) =\u003e null\n\nA diff of two unrelated texts can be filled with coincidental matches. For example, the diff of \"mouse\" and \"sofas\" is `[(-1, \"m\"), (1, \"s\"), (0, \"o\"), (-1, \"u\"), (1, \"fa\"), (0, \"s\"), (-1, \"e\")]`. While this is the optimum diff, it is difficult for humans to understand. Semantic cleanup rewrites the diff, expanding it into a more intelligible format. The above example would become: `[(-1, \"mouse\"), (1, \"sofas\")]`. If a diff is to be human-readable, it should be passed to cleanupSemantic.\n\n### cleanupEfficiency(diffs) =\u003e null\n\nThis function is similar to `cleanupSemantic`, except that instead of optimising a diff to be human-readable, it optimises the diff to be efficient for machine processing. The results of both cleanup types are often the same.\n\nThe efficiency cleanup is based on the observation that a diff made up of large numbers of small diffs edits may take longer to process (in downstream applications) or take more capacity to store or transmit than a smaller number of larger diffs. The `diff.EditCost` property sets what the cost of handling a new edit is in terms of handling extra characters in an existing edit. The default value is 4, which means if expanding the length of a diff by three characters can eliminate one edit, then that optimisation will reduce the total costs.\n\n### levenshtein(diffs) =\u003e int\n\nGiven a diff, measure its Levenshtein distance in terms of the number of inserted, deleted or substituted characters. The minimum distance is 0 which means equality, the maximum distance is the length of the longer string.\n\n### prettyHtml(diffs) =\u003e html\n\nTakes a diff array and returns a string of pretty HTML. Deletions are wrapped in `\u003cdel\u003e\u003c/del\u003e` tags, and insertions are wrapped in `\u003cins\u003e\u003c/ins\u003e` tags. Use CSS to apply styling to these tags.\n\n## Tests\n\nTests have not been ported to this library fork, however tests are [available in the original library](https://github.com/liddiard/google-diff-match-patch/tree/master/javascript). If you would like to port tests over, you will need to do some function call renaming (viz. the `diff_` prefix has been removed from functions in the fork) and remove tests specific to the \"patch\" and \"match\" functionalities of the original library.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliddiard%2Ftext-diff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliddiard%2Ftext-diff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliddiard%2Ftext-diff/lists"}