react-diff-view
Advanced tools
Comparing version 0.8.0 to 1.0.0
{ | ||
"name": "react-diff-view", | ||
"version": "0.8.0", | ||
"version": "1.0.0", | ||
"description": "A git diff component to consume the git unified diff output.", | ||
@@ -17,6 +17,6 @@ "main": "index.js", | ||
"diff": "^3.3.1", | ||
"gitdiff-parser": "0.0.3", | ||
"leven": "^2.1.0", | ||
"lodash.escape": "^4.0.1", | ||
"lodash.mapvalues": "^4.6.0", | ||
"parse-diff": "^0.4.0" | ||
"lodash.mapvalues": "^4.6.0" | ||
}, | ||
@@ -40,2 +40,3 @@ "devDependencies": { | ||
"eslint-plugin-react": "^7.3.0", | ||
"extract-text-webpack-plugin": "^3.0.0", | ||
"html-webpack-plugin": "^2.30.1", | ||
@@ -48,5 +49,5 @@ "lang-map": "^0.4.0", | ||
"raw-loader": "^0.5.1", | ||
"react": "^15.6.1", | ||
"react": "^16.0.0-rc.3", | ||
"react-addons-perf": "^15.4.2", | ||
"react-dom": "^15.6.1", | ||
"react-dom": "^16.0.0-rc.3", | ||
"react-infinite-scroller": "^1.0.15", | ||
@@ -66,4 +67,4 @@ "react-markdown": "^2.5.0", | ||
"peerDependencies": { | ||
"react": "^15.6.1", | ||
"prop-types": "^15.5.10" | ||
"react": "^15.6.2 || ^16.0.0", | ||
"prop-types": "^15.6.0" | ||
}, | ||
@@ -89,4 +90,8 @@ "repository": { | ||
"index.js.map", | ||
"parse.js", | ||
"parse.js.map", | ||
"index.css", | ||
"index.css.map", | ||
"src" | ||
] | ||
} |
@@ -9,6 +9,2 @@ # react-diff-view | ||
## API changes | ||
In version less than `1.0.0`, API is subjected to change, we may introduce breaking changes in minor versions. | ||
## Full features | ||
@@ -49,3 +45,3 @@ | ||
The `{File[] parseDiff({string} text, {Object} [options])` named export is a wrap of [parse-diff](https://www.npmjs.com/package/parse-diff) package with some extra options: | ||
The `{File[] parseDiff({string} text, {Object} [options])` named export is a wrap of [gitdiff-parser](https://www.npmjs.com/package/gitdiff-parser) package with some extra options: | ||
@@ -408,1 +404,10 @@ - `{boolean} stubHunk`: Whether to add a stub empty hunk at the tail of each hunk list, this can provide an extra hunk header when [customizing hunk header](#customize-hunk-header), for example, to expand code after the diff. | ||
I don't really know how to test such a complicated and UI centric component, any helps are welcome. | ||
## Change Log | ||
### 1.0.0 | ||
- Move to a self-implement diff parser which is faster and provides more information (binary, chmod, etc...). | ||
- Add a `react-diff-view/parse` module providing 2 named exports: `parseDiff` and `addStubHunk`, this can be used on web workers. | ||
- Styles are now in a standalone `index.css` file with source map support. | ||
- Upgrade `react` and `prop-types` dependency versions to MIT licensed ones, also `react ^16.0.0` is supported. |
export Diff from './Diff'; | ||
export Hunk from './Hunk'; | ||
export * from './parse'; | ||
export * from './utils'; |
@@ -1,3 +0,69 @@ | ||
import {parseDiff} from './utils'; | ||
import parser from 'gitdiff-parser'; | ||
export default parseDiff; | ||
const zipChanges = changes => { | ||
const [result] = changes.reduce( | ||
([result, last, lastDeletionIndex], current, i) => { | ||
if (!last) { | ||
result.push(current); | ||
return [result, current, current.isDelete ? i : -1]; | ||
} | ||
if (current.isInsert && lastDeletionIndex >= 0) { | ||
result.splice(lastDeletionIndex + 1, 0, current); | ||
// The new `lastDeletionIndex` may be out of range, but `splice` will fix it | ||
return [result, current, lastDeletionIndex + 2]; | ||
} | ||
result.push(current); | ||
// Keep the `lastDeletionIndex` if there are lines of deletions, | ||
// otherwise update it to the new deletion line | ||
const newLastDeletionIndex = current.isDelete | ||
? (last.isDelete ? lastDeletionIndex : i) | ||
: lastDeletionIndex; | ||
return [result, current, newLastDeletionIndex]; | ||
}, | ||
[[], null, -1] | ||
); | ||
return result; | ||
}; | ||
const mapHunk = (hunk, options) => { | ||
const changes = options.nearbySequences === 'zip' ? zipChanges(hunk.changes) : hunk.changes; | ||
return { | ||
...hunk, | ||
changes: changes | ||
}; | ||
}; | ||
const mapFile = (file, options) => { | ||
const hunks = file.hunks.map(hunk => mapHunk(hunk, options)); | ||
return { | ||
...file, | ||
hunks: options.stubHunk ? addStubHunk(hunks) : hunks | ||
}; | ||
}; | ||
export const parseDiff = (text, options = {}) => { | ||
const files = parser.parse(text); | ||
return files.map(file => mapFile(file, options)); | ||
}; | ||
export const addStubHunk = hunks => { | ||
if (!hunks || !hunks.length) { | ||
return hunks; | ||
} | ||
const {oldStart, oldLines, newStart, newLines} = hunks[hunks.length - 1]; | ||
const stubHunk = { | ||
oldStart: oldStart + oldLines, | ||
oldLines: 0, | ||
newStart: newStart + newLines, | ||
newLines: 0, | ||
content: 'STUB', | ||
changes: [] | ||
}; | ||
return [...hunks, stubHunk]; | ||
}; |
115
src/utils.js
@@ -1,119 +0,4 @@ | ||
import parse from 'parse-diff'; | ||
import leven from 'leven'; | ||
import {diffChars, diffWordsWithSpace} from 'diff'; | ||
const computeFileType = ({from, to}) => { | ||
if (from === '/dev/null') { | ||
return 'add'; | ||
} | ||
if (to === '/dev/null') { | ||
return 'delete'; | ||
} | ||
return 'modify'; | ||
}; | ||
const zipChanges = changes => { | ||
const [result] = changes.reduce( | ||
([result, last, lastDeletionIndex], current, i) => { | ||
if (!last) { | ||
result.push(current); | ||
return [result, current, current.del ? i : -1]; | ||
} | ||
if (current.add && lastDeletionIndex >= 0) { | ||
result.splice(lastDeletionIndex + 1, 0, current); | ||
// The new `lastDeletionIndex` may be out of range, but `splice` will fix it | ||
return [result, current, lastDeletionIndex + 2]; | ||
} | ||
result.push(current); | ||
// Keep the `lastDeletionIndex` if there are lines of deletions, | ||
// otherwise update it to the new deletion line | ||
const newLastDeletionIndex = current.del | ||
? (last.del ? lastDeletionIndex : i) | ||
: lastDeletionIndex; | ||
return [result, current, newLastDeletionIndex]; | ||
}, | ||
[[], null, -1] | ||
); | ||
return result; | ||
}; | ||
const mapChange = ({add, normal, ln1, ln2, ln, content}) => { | ||
if (normal) { | ||
return { | ||
type: 'normal', | ||
isNormal: true, | ||
oldLineNumber: ln1, | ||
newLineNumber: ln2, | ||
content: content.substring(1) | ||
}; | ||
} | ||
if (add) { | ||
return { | ||
type: 'insert', | ||
isInsert: true, | ||
lineNumber: ln, | ||
content: content.substring(1) | ||
}; | ||
} | ||
return { | ||
type: 'delete', | ||
isDelete: true, | ||
lineNumber: ln, | ||
content: content.substring(1) | ||
}; | ||
}; | ||
const mapHunk = (hunk, options) => { | ||
const changes = options.nearbySequences === 'zip' ? zipChanges(hunk.changes) : hunk.changes; | ||
return { | ||
...hunk, | ||
changes: changes.map(mapChange) | ||
}; | ||
}; | ||
const mapFile = (file, options) => { | ||
const hunks = file.chunks.map(hunk => mapHunk(hunk, options)); | ||
return { | ||
type: computeFileType(file), | ||
oldPath: file.from, | ||
newPath: file.to, | ||
additions: file.additions, | ||
deletions: file.deletions, | ||
hunks: options.stubHunk ? addStubHunk(hunks) : hunks | ||
}; | ||
}; | ||
// TODO: Implement a faster diff parser | ||
export const parseDiff = (text, options = {}) => { | ||
const files = parse(text); | ||
return files.map(file => mapFile(file, options)); | ||
}; | ||
export const addStubHunk = hunks => { | ||
if (!hunks || !hunks.length) { | ||
return hunks; | ||
} | ||
const {oldStart, oldLines, newStart, newLines} = hunks[hunks.length - 1]; | ||
const stubHunk = { | ||
oldStart: oldStart + oldLines, | ||
oldLines: 0, | ||
newStart: newStart + newLines, | ||
newLines: 0, | ||
content: 'STUB', | ||
changes: [] | ||
}; | ||
return [...hunks, stubHunk]; | ||
}; | ||
export const computeOldLineNumber = ({isNormal, lineNumber, oldLineNumber}) => (isNormal ? oldLineNumber : lineNumber); | ||
@@ -120,0 +5,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27
1
411
4
516965
39
1277
+ Addedgitdiff-parser@0.0.3
+ Addedgitdiff-parser@0.0.3(transitive)
+ Addedreact@16.14.0(transitive)
- Removedparse-diff@^0.4.0
- Removedasap@2.0.6(transitive)
- Removedcore-js@1.2.7(transitive)
- Removedcreate-react-class@15.7.0(transitive)
- Removedencoding@0.1.13(transitive)
- Removedfbjs@0.8.18(transitive)
- Removediconv-lite@0.6.3(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedisomorphic-fetch@2.2.1(transitive)
- Removednode-fetch@1.7.3(transitive)
- Removedparse-diff@0.4.2(transitive)
- Removedpromise@7.3.1(transitive)
- Removedreact@15.7.0(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsetimmediate@1.0.5(transitive)
- Removedua-parser-js@0.7.39(transitive)
- Removedwhatwg-fetch@3.6.20(transitive)