Skip to content

Commit 953910a

Browse files
committed
inital commit
0 parents  commit 953910a

File tree

6 files changed

+3231
-0
lines changed

6 files changed

+3231
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
*.iml
3+
node_modules/
4+
.DS_Store

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
**/.*
2+
example
3+
script
4+
specs
5+
bower.json
6+
karma.conf.js
7+
webpack.config.js
8+
.DS_Store

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# react-diff
2+
3+
NOTE: I stole this lib from [cezary/react-diff](https://github.com/cezary/react-diff)
4+
5+
Highlights differences between two strings, uses the [diff](https://www.npmjs.com/package/diff) module
6+
7+
## Installation
8+
9+
```
10+
npm install react-diff
11+
```
12+
13+
## Demo
14+
15+
http://cezary.github.io/react-diff/
16+
17+
## Example
18+
19+
```javascript
20+
import React from "react";
21+
import Diff from "react-diff";
22+
23+
const MyComponent = () => {
24+
return <Diff inputA="gogol" inputB="google" type="chars" />;
25+
};
26+
```
27+
28+
## License
29+
30+
MIT

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import PropTypes from "prop-types";
2+
import React from "react";
3+
import jsdiff from "diff";
4+
5+
const fnMap = {
6+
chars: jsdiff.diffChars,
7+
words: jsdiff.diffWords,
8+
sentences: jsdiff.diffSentences,
9+
json: jsdiff.diffJson
10+
};
11+
12+
export default function Diff({ inputA = "", inputB = "", type = "chars" }) {
13+
var diff = fnMap[type](inputA, inputB);
14+
var result = diff.map(function(part, index) {
15+
var spanStyle = {
16+
backgroundColor: part.added
17+
? "lightgreen"
18+
: part.removed
19+
? "salmon"
20+
: "lightgrey"
21+
};
22+
return (
23+
<span key={index} style={spanStyle}>
24+
{part.value}
25+
</span>
26+
);
27+
});
28+
return <pre className="diff-result">{result}</pre>;
29+
}
30+
31+
Diff.propTypes = {
32+
inputA: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
33+
inputB: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
34+
type: PropTypes.oneOf(["chars", "words", "sentences", "json"])
35+
};

0 commit comments

Comments
 (0)