Skip to content

Commit 27d62fb

Browse files
committed
initial commit
0 parents  commit 27d62fb

File tree

9 files changed

+481
-0
lines changed

9 files changed

+481
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

diff.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
var path = require("path");
2+
var fs = require("fs");
3+
var diffChecker = require("./src/diffchecker.js");
4+
5+
var typeConverters = {
6+
stache: 'html',
7+
mustache: 'html',
8+
component: 'html'
9+
};
10+
var convertType = function(type) {
11+
return typeConverters[type] || type;
12+
};
13+
14+
var createGroupedLines = function(sequentialLines) {
15+
var groupedLines = sequentialLines[0] + '-' + sequentialLines[sequentialLines.length-1]
16+
return groupedLines
17+
}
18+
19+
var checkSequence = function(diffLines) {
20+
//check to chunk lines together
21+
var singleLines = [];
22+
//temp array to hold sequences
23+
var sequentialLines = [];
24+
var prevLine = null;
25+
for (var index = 0; index < diffLines.length; index++) {
26+
var line = diffLines[index];
27+
28+
if (prevLine ) {
29+
if (prevLine + 1 === line) {
30+
sequentialLines.push(line);
31+
}
32+
else {
33+
//break sequencing
34+
if (sequentialLines.length > 1) {
35+
//updating singles array
36+
var startingSeqIndex = singleLines.indexOf(sequentialLines[0]);
37+
singleLines.splice(startingSeqIndex, sequentialLines.length, createGroupedLines(sequentialLines) )
38+
39+
}
40+
sequentialLines = [];
41+
sequentialLines.push(line);
42+
}
43+
// last line is part of sequence
44+
if(index + 1 === diffLines.length) {
45+
if (prevLine + 1 === line) {
46+
sequentialLines.push(line);
47+
if (sequentialLines.length > 1) {
48+
var startingSeqIndex = singleLines.indexOf(sequentialLines[0]);
49+
singleLines.splice(startingSeqIndex, sequentialLines.length, createGroupedLines(sequentialLines) )
50+
sequentialLines = [];
51+
}
52+
}
53+
else {
54+
singleLines.push(line);
55+
}
56+
}
57+
else {
58+
singleLines.push(line);
59+
}
60+
prevLine = line;
61+
}
62+
else {
63+
singleLines.push(line);
64+
sequentialLines.push(line);
65+
prevLine = line;
66+
}
67+
}
68+
return singleLines;
69+
}
70+
71+
/**
72+
* @parent bit-docs-tag-sourceref/tags
73+
* @module {bit-docs-process-tags/types/tag} bit-docs-tag-sourceref/tags/sourceref @sourceref
74+
*
75+
* @description Creates a diff from two files to get the changed lines and wraps them with markdown for code block.
76+
*
77+
* @signature `@diff PATH1 PATH2 only`
78+
*
79+
* Wraps file contents with triple \` so it shows up as a code block.
80+
*
81+
* @param {String} PATH1 The path to original file to be checked against.
82+
* @param {String} PATH2 The path to new version of original file.
83+
* @param {String} only Option flag to only show changed lines
84+
*
85+
*/
86+
module.exports = {
87+
add: function(line, curData) {
88+
var onlyFlag = line.indexOf('only') > -1 ? 'only': '';
89+
var files = line.replace(/@diff|only/g, "").trim().split(" ");
90+
//break if more then two throw new error
91+
if (files.length > 2) {
92+
return false;
93+
}
94+
if (this.src.path) {
95+
var pathedFiles = []
96+
for (var fileI = 0; fileI < files.length; fileI++) {
97+
let fileName = files[fileI];
98+
pathedFiles.push(path.join(path.dirname(this.src.path), fileName));
99+
}
100+
files = pathedFiles;
101+
}
102+
var file1 = fs.readFileSync(files[0]).toString();
103+
var file2 = fs.readFileSync(files[1]).toString();
104+
var validCurData = (curData && curData.length !== 2);
105+
var fileType = convertType(path.extname(files[1]).substring(1));
106+
var useCurData = useCurData ? validCurData && (typeof curData.description === "string") && !curData.body : false;;
107+
var markdown = '\n```' + fileType + '\n' + file2 + '\n```';
108+
var diffLines = diffChecker.diff(file1, file2);
109+
if(diffLines.length) {
110+
var lines = checkSequence(diffLines);
111+
markdown += '\n <div line-highlight="' + lines.toString() + ',' + onlyFlag + '"></div>';
112+
};
113+
if (useCurData) {
114+
curData.description += markdown;
115+
} else {
116+
this.body += markdown;
117+
}
118+
}
119+
};

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "bit-docs-tag-diff",
3+
"version": "1.0.0",
4+
"description": "Create a diff from two filepaths",
5+
"main": "diff.js",
6+
"scripts": {
7+
"test": "mocha test/test.js --reporter spec",
8+
"browser": "mocha test/test.js --inspect-brk"
9+
},
10+
"keywords": [
11+
"bit-docs"
12+
],
13+
"author": "Bitovi",
14+
"license": "ISC",
15+
"devDependencies": {
16+
"bit-docs-process-tags": "0.0.6",
17+
"mocha": "^6.1.4"
18+
},
19+
"dependencies": {
20+
"diff": "^4.0.1",
21+
"prettydiff": "^101.0.3"
22+
}
23+
}

src/diffchecker.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
diff: function(ogString, newString) {
3+
var diffLines = [];
4+
var ogLines = ogString.split('\n');
5+
var ogLinesStripped = []
6+
for (var index = 0; index < ogLines.length; index++) {
7+
ogLinesStripped.push(ogLines[index].replace(/;/g, ""));
8+
}
9+
var newLines = newString.split('\n');
10+
11+
for (var index = 0; index < newLines.length; index++) {
12+
var lineNumber = index + 1;
13+
var newLine = newLines[index].replace(/;/g, "");
14+
var newLineIndx = ogLinesStripped.indexOf(newLine);
15+
if (newLine != '') {
16+
if (newLineIndx === -1) {
17+
diffLines.push(lineNumber);
18+
}
19+
else {
20+
ogLinesStripped.splice(newLineIndx, 1);
21+
}
22+
}
23+
}
24+
return diffLines
25+
}
26+
}

test/dir1/hello-world1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("hello world");

test/dir1/ngfile.service.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { RestaurantService } from './restaurant.service';
4+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
5+
6+
describe('RestaurantService', () => {
7+
let httpMock : HttpTestingController;
8+
let restaurantService: RestaurantService;
9+
10+
beforeEach(() => TestBed.configureTestingModule({
11+
imports: [HttpClientTestingModule],
12+
providers: [
13+
RestaurantService
14+
]
15+
}));
16+
17+
beforeEach(() => {
18+
httpMock = TestBed.get(HttpTestingController);
19+
restaurantService = TestBed.get(RestaurantService);
20+
})
21+
22+
it('should be created', () => {
23+
expect(restaurantService).toBeTruthy();
24+
});
25+
26+
it('should make a get request to restaurants', () => {
27+
const mockRestaurants = {
28+
data: [{
29+
"name":"Brunch Place",
30+
"slug":"brunch-place",
31+
"images":{
32+
"thumbnail":"node_modules/place-my-order-assets/images/4-thumbnail.jpg",
33+
"owner":"node_modules/place-my-order-assets/images/2-owner.jpg",
34+
"banner":"node_modules/place-my-order-assets/images/2-banner.jpg"},
35+
"menu":{
36+
"lunch":[
37+
{"name":"Ricotta Gnocchi","price":15.99},
38+
{"name":"Garlic Fries","price":15.99},
39+
{"name":"Charred Octopus","price":25.99}
40+
],
41+
"dinner":[
42+
{"name":"Steamed Mussels","price":21.99},
43+
{"name":"Roasted Salmon","price":23.99},
44+
{"name":"Crab Pancakes with Sorrel Syrup","price":35.99}
45+
]
46+
},
47+
"address":{
48+
"street":"2451 W Washburne Ave",
49+
"city":"Ann Arbor","state":"MI","zip":"53295"},
50+
"_id":"xugqxQIX5rPJTLBv"
51+
},
52+
{
53+
"name":"Taco Joint",
54+
"slug":"taco-joint",
55+
"images":{
56+
"thumbnail":"node_modules/place-my-order-assets/images/4-thumbnail.jpg",
57+
"owner":"node_modules/place-my-order-assets/images/2-owner.jpg",
58+
"banner":"node_modules/place-my-order-assets/images/2-banner.jpg"},
59+
"menu":{
60+
"lunch":[
61+
{"name":"Beef Tacos","price":15.99},
62+
{"name":"Chicken Tacos","price":15.99},
63+
{"name":"Guacamole","price":25.99}
64+
],
65+
"dinner":[
66+
{"name":"Shrimp Tacos","price":21.99},
67+
{"name":"Chicken Enchilada","price":23.99},
68+
{"name":"Elotes","price":35.99}
69+
]
70+
},
71+
"address":{
72+
"street":"13 N 21st St",
73+
"city":"Chicago","state":"IL","zip":"53295"},
74+
"_id":"xugqxQIX5dfgdgTLBv"
75+
}]
76+
};
77+
78+
restaurantService.getRestaurants().subscribe((restaurants:any) => {
79+
expect(restaurants).toEqual(mockRestaurants);
80+
});
81+
82+
let url = '/api/restaurants';
83+
const req = httpMock.expectOne(url);
84+
85+
86+
expect(req.request.method).toEqual('GET');
87+
req.flush(mockRestaurants);
88+
89+
httpMock.verify();
90+
});
91+
92+
});

test/dir2/hello-world2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log("hello world");
2+
console.log("hello universe");

0 commit comments

Comments
 (0)