Comparing version 1.0.3 to 1.0.4
54
index.js
'use strict' | ||
var jsDiff = require('diff') | ||
var color = require('./color') | ||
var pkg = require('./package.json') | ||
function toBoolean(bool) { | ||
@@ -8,7 +12,51 @@ if (bool === 'false') bool = false | ||
var gitDiff = function(str1, str2, color) { | ||
color = toBoolean(color) | ||
return '' + str1 + str2 + color | ||
function replaceAllButLast(str, pOld, pNew) { | ||
str = str.replace(new RegExp(pOld, 'g'), pNew) | ||
str = str.replace(new RegExp(pNew + '$'), pOld) | ||
return str | ||
} | ||
var gitDiff = function(str1, str2, pColor) { | ||
if (typeof str1 !== 'string' || typeof str2 !== 'string') { | ||
throw TypeError('Both inputs to ' + pkg.name + ' must be strings') | ||
} | ||
pColor = toBoolean(pColor) | ||
var diff = jsDiff.diffLines(str1, str2) | ||
var isDiff = diff.some(function(item) { | ||
return item.added || item.removed | ||
}) | ||
var accumulatedDiff = '' | ||
if (isDiff) { | ||
diff.forEach(function(part) { | ||
var culla, prefix | ||
if (part.added) { | ||
culla = 'green' | ||
prefix = '+' | ||
part.value = replaceAllButLast(part.value, '\n', '\n ') | ||
} else if (part.removed) { | ||
culla = 'red' | ||
prefix = '-' | ||
part.value = replaceAllButLast(part.value, '\n', '\n ') | ||
} else { | ||
culla = 'grey' | ||
prefix = '' | ||
} | ||
part.diff = prefix + part.value | ||
if (pColor) part.diff = color.add(part.diff, culla) | ||
accumulatedDiff += part.diff | ||
}) | ||
return (accumulatedDiff) | ||
} else { | ||
var noDiff = 'no difference' | ||
if (pColor) noDiff = color.add(noDiff, 'grey') | ||
return (noDiff) | ||
} | ||
} | ||
module.exports = gitDiff |
{ | ||
"name": "git-diff", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Returns the git diff of two strings", | ||
@@ -28,2 +28,3 @@ "author": "Daniel Lewis BSc (Hons)", | ||
"dependencies": { | ||
"chalk": "^2.1.0", | ||
"diff": "^3.3.0" | ||
@@ -39,2 +40,3 @@ }, | ||
"npm-run-all": "^4.0.2", | ||
"readfilego": "^1.0.3", | ||
"sinon": "^3.2.1", | ||
@@ -41,0 +43,0 @@ "sinon-chai": "^2.13.0" |
@@ -11,1 +11,51 @@ # git-diff | ||
**Returns the git diff of two strings** | ||
<br> | ||
## Usage | ||
`npm install --save git-diff` | ||
git-diff takes 3 arguments, the **first string to diff**, the **second string to diff** and optionally a **color flag (boolean)** | ||
git-diff returns a string containing the difference | ||
The color flag (defaults to false) indicates whether you want the return value to be colorized with [chalk](https://www.npmjs.com/package/chalk) | ||
Examples to demonstrate usage follow. | ||
With color: | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff') | ||
var actual = gitDiff('a\nb\n', 'a\nc\n', true) | ||
expect(actual).to.equal('a\n-b\n+c\n') | ||
``` | ||
Without color: | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff') | ||
var actual = gitDiff('a\nb\n', 'a\nc\n') | ||
expect(actual).to.equal('a\n-b\n+c\n') | ||
``` | ||
<br> | ||
## Author says | ||
What's the **difference** between how God treats the righteous and the wicked? | ||
> And God saw that the light was good. And God separated the light from the darkness. [Genesis 1:4 ESV](https://www.biblegateway.com/passage/?search=Genesis+1%3A4&version=ESV) | ||
And he will do it again: | ||
> Let both grow together until the harvest, and at harvest time I will tell the reapers, “Gather the weeds first and bind them in bundles to be burned, but gather the wheat into my barn.” [Matthew 13:30 ESV](https://www.biblegateway.com/passage/?search=matthew+13%3A30&version=ESV) | ||
Much love :D | ||
<br><br><br><br><br> |
@@ -12,10 +12,14 @@ 'use strict' | ||
// others | ||
var readfilego = require('readfilego') | ||
// custom | ||
var color = require('../../color') | ||
var testImports = { | ||
expect: expect, | ||
sinon: sinon | ||
sinon: sinon, | ||
readfilego: readfilego, | ||
color: color | ||
} | ||
module.exports = testImports |
@@ -5,32 +5,66 @@ 'use strict' | ||
var gitDiff = require('../../index') | ||
var pkg = require('../../package.json') | ||
describe('gitDiff', function() { | ||
// var DEFAULTS, sandbox | ||
// | ||
// before(function() { | ||
// imp.logger.setLevel('silent') | ||
// }) | ||
// | ||
// beforeEach(function() { | ||
// delete require.cache[require.resolve('../../js/normaliseOptions/defaultOptions')] | ||
// DEFAULTS = require('../../js/normaliseOptions/defaultOptions') | ||
// sandbox = imp.sinon.sandbox.create() | ||
// sandbox.spy(imp.logger, 'info') | ||
// sandbox.stub(DEFAULTS, 'logFunc') | ||
// }) | ||
// | ||
// afterEach(function() { | ||
// sandbox.restore() | ||
// }) | ||
var sandbox | ||
it('gitDiff', function() { | ||
var actual = gitDiff('', '', 'false') | ||
imp.expect(actual).to.equal('false') | ||
beforeEach(function() { | ||
sandbox = imp.sinon.sandbox.create() | ||
sandbox.spy(imp.color, 'add') | ||
}) | ||
it('gitDiff', function() { | ||
var actual = gitDiff('', '', true) | ||
imp.expect(actual).to.equal('true') | ||
afterEach(function() { | ||
sandbox.restore() | ||
}) | ||
describe('color', function() { | ||
it('difference', function() { | ||
var str1 = imp.readfilego(__dirname + '/str1.txt', {throw: true, save: true}) | ||
var str2 = imp.readfilego(__dirname + '/str2.txt') | ||
var expected = imp.readfilego(__dirname + '/gitDiff.txt') | ||
var actual = gitDiff(str1, str2, true) | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'green') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'red') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'grey') | ||
imp.expect(actual).to.equal(expected) | ||
}) | ||
it('no difference', function() { | ||
var actual = gitDiff('', '', true) | ||
imp.expect(imp.color.add).to.have.not.been.calledWith(imp.sinon.match.any, 'green') | ||
imp.expect(imp.color.add).to.have.not.been.calledWith(imp.sinon.match.any, 'red') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'grey') | ||
imp.expect(actual).to.equal('no difference') | ||
}) | ||
}) | ||
describe('no color', function() { | ||
it('difference', function() { | ||
var str1 = imp.readfilego(__dirname + '/str1.txt', {throw: true, save: true}) | ||
var str2 = imp.readfilego(__dirname + '/str2.txt') | ||
var expected = imp.readfilego(__dirname + '/gitDiff.txt') | ||
var actual = gitDiff(str1, str2) | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
imp.expect(actual).to.equal(expected) | ||
}) | ||
it('no difference', function() { | ||
var actual = gitDiff('', '', 'false') | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
imp.expect(actual).to.equal('no difference') | ||
}) | ||
}) | ||
it('type error', function() { | ||
imp.expect(function() { | ||
gitDiff(undefined, '') | ||
}).to.throw('Both inputs to ' + pkg.name + ' must be strings') | ||
imp.expect(function() { | ||
gitDiff('', undefined) | ||
}).to.throw('Both inputs to ' + pkg.name + ' must be strings') | ||
}) | ||
}) |
* Add more keywords | ||
* Contrast output with git diff | ||
* Update README | ||
* Update examples spec file | ||
* Tidy code |
Sorry, the diff of this file is not supported yet
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
74761
20
203
61
2
10
+ Addedchalk@^2.1.0
+ Addedansi-styles@3.2.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)