An open API service indexing awesome lists of open source software.

https://github.com/zhanba/codiff

diff algorithms in vscode
https://github.com/zhanba/codiff

Last synced: 4 months ago
JSON representation

diff algorithms in vscode

Awesome Lists containing this project

README

          

# Codiff

Codiff is a library for computing line by line diff using the same diff algorithms in VSCode.

## Install

```sh
npm install codiff
```

# Usage

```typescript
import { linesDiffComputers } from "codiff";

const doc = `one
two
three
four
five`;

const docModified = doc.replace(/t/g, "T") + "\nSix";

const codiff = new Codiff();
const result = codiff.computeDiff(doc, docmodified);
```

diff result:

```json
{
"changes": [
{
"original": {
"startLineNumber": 2,
"endLineNumberExclusive": 4
},
"modified": {
"startLineNumber": 2,
"endLineNumberExclusive": 4
},
"innerChanges": [
{
"originalRange": {
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 2
},
"modifiedRange": {
"startLineNumber": 2,
"startColumn": 1,
"endLineNumber": 2,
"endColumn": 2
}
},
{
"originalRange": {
"startLineNumber": 3,
"startColumn": 1,
"endLineNumber": 3,
"endColumn": 2
},
"modifiedRange": {
"startLineNumber": 3,
"startColumn": 1,
"endLineNumber": 3,
"endColumn": 2
}
}
]
},
{
"original": {
"startLineNumber": 6,
"endLineNumberExclusive": 6
},
"modified": {
"startLineNumber": 6,
"endLineNumberExclusive": 7
},
"innerChanges": [
{
"originalRange": {
"startLineNumber": 5,
"startColumn": 5,
"endLineNumber": 5,
"endColumn": 5
},
"modifiedRange": {
"startLineNumber": 5,
"startColumn": 5,
"endLineNumber": 6,
"endColumn": 4
}
}
]
}
],
"quitEarly": false,
"identical": false,
"moves": []
}
```

## Playground

Codiff provide a [playground](https://zhanba.github.io/codiff/) to test the diff algorithm and preview the diff result in code diff editor and JSON editor.

## Performance

- Diff performance is related to the number of lines of code and the number of changes.
- Suggestion:
- Cache the diff result for same content.
- Use wroker to do the diff in the background.