+17
| 'use strict' | ||
| var chalk = require('chalk') | ||
| var config = require('./config') | ||
| function Color() { | ||
| this.add = function(str, color) { | ||
| /* istanbul ignore next */ | ||
| if (!config.testing) str = chalk[color](str) | ||
| return str | ||
| } | ||
| } | ||
| module.exports = new Color() |
| var config = { | ||
| testing: process.env.NODE_ENV === 'test' | ||
| } | ||
| module.exports = config |
| 'use strict' | ||
| var imp = require('../_js/testImports') | ||
| describe('examples', function() { | ||
| var sandbox | ||
| beforeEach(function() { | ||
| sandbox = imp.sinon.sandbox.create() | ||
| sandbox.spy(imp.color, 'add') | ||
| }) | ||
| afterEach(function() { | ||
| sandbox.restore() | ||
| }) | ||
| it('color', function() { | ||
| var gitDiff = require('../../index') | ||
| var actual = gitDiff('a\nb\n', 'a\nc\n', true) | ||
| imp.expect(actual).to.equal('a\n-b\n+c\n') | ||
| 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') | ||
| }) | ||
| it('no color', function() { | ||
| var gitDiff = require('../../index') | ||
| var actual = gitDiff('a\nb\n', 'a\nc\n') | ||
| imp.expect(actual).to.equal('a\n-b\n+c\n') | ||
| imp.expect(imp.color.add).to.have.not.been.called | ||
| }) | ||
| }) |
| -This is a test for my diff tool | ||
| It is a big test | ||
| +This is a test for my difference tool | ||
| It is a small test | ||
| No diff here | ||
| -But there might be here | ||
| +But there might be here! | ||
| But not here | ||
| Or here |
| This is a test for my diff tool | ||
| It is a big test | ||
| No diff here | ||
| But there might be here | ||
| But not here | ||
| Or here |
| This is a test for my difference tool | ||
| It is a small test | ||
| No diff here | ||
| But there might be here! | ||
| But not here | ||
| Or here |
| process.env.NODE_ENV = 'test' |
+51
-3
| '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 |
+3
-1
| { | ||
| "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" |
+50
-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') | ||
| }) | ||
| }) |
+8
-0
| * Add more keywords | ||
| * Contrast output with git diff | ||
| * Update README | ||
| * Update examples spec file | ||
| * Tidy code |
+4
-0
@@ -1386,2 +1386,6 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
| readfilego@^1.0.3: | ||
| version "1.0.3" | ||
| resolved "https://registry.yarnpkg.com/readfilego/-/readfilego-1.0.3.tgz#7e64eae343c0611de92a3e65d039f5ce7681f9bf" | ||
| repeat-string@^1.5.2: | ||
@@ -1388,0 +1392,0 @@ version "1.6.1" |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
74761
9.02%20
53.85%203
123.08%61
454.55%2
100%10
11.11%2
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added