This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiffer.js
164 lines (146 loc) · 4.65 KB
/
differ.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// For simplicity in integrating this into other packages, let's make a goal of this
// to be a function that takes the two JS files and returns a string formatted for
// GitHub's comments
// Alright, let's get started
// this file is really cursed, though it's wayyy safer than regex and eval
// or parsing from a regex
import getLangStrings from "./getLangStrings.js";
import getEndpoints from "./getEndpoints.js";
// For example we grab these two files which have had lang changes between the two
// const fs = require('fs');
// const file1 = fs.readFileSync(__dirname + '/old.js', 'utf-8')
// const file2 = fs.readFileSync(__dirname + '/main.js', 'utf-8')
// console.log(doWork(file1, file2));
const formats = {
codeblock: {
startString: {
strings: "## Strings\n```diff",
endpoints: "## Endpoints\n```diff",
},
endString: "```",
addedHeader: "\n# Added\n",
updatedHeader: "\n# Updated\n",
removedHeader: "\n# Removed\n",
added: (diffedString) => `+ ${diffedString[0]}: ${diffedString[1]}\n`,
updated: (diffedString) =>
`- ${diffedString[0]}: ${diffedString[1]}\n+ ${diffedString[0]}: ${diffedString[2]}\n`,
removed: (diffedString) => `- ${diffedString[0]}: ${diffedString[1]}\n`,
},
inline: {
startString: {
strings: "## Strings\n```diff",
endpoints: "## Endpoints\n```diff",
},
endString: "",
addedHeader: "### Added\n",
updatedHeader: "### Updated\n",
removedHeader: "### Removed\n",
added: (diffedString) =>
` - \`${diffedString[0]}\`: \`${diffedString[1]}\`\n`,
updated: (diffedString) =>
` - \`${diffedString[0]}\`: \`${diffedString[1]}\` -> \`${diffedString[2]}\`\n`,
removed: (diffedString) =>
` - \`${diffedString[0]}\`: \`${diffedString[1]}\`\n`,
},
};
let FORMAT;
/**
* The main work function
* @param {string} file1 - Contents of first file
* @param {string} file2 - Contents of second file
* @param {('codeblock'|'inline')} format - Which format to use when building the strings
*/
function doWork(file1, file2, format) {
const filesLangStrings = [getLangStrings(file1), getLangStrings(file2)];
const filesEndpoints = [getEndpoints(file1), getEndpoints(file2)];
const { addedStrings, updatedStrings, removedStrings } =
diff(filesLangStrings);
const {
addedStrings: addedEndpoints,
updatedStrings: updatedEndpoints,
removedStrings: removedEndpoints,
} = diff(filesEndpoints);
FORMAT = format;
const builtString = buildString(
"strings",
addedStrings,
updatedStrings,
removedStrings
);
const builtEndpoint = buildString(
"endpoints",
addedEndpoints,
updatedEndpoints,
removedEndpoints
);
let result = "";
if (builtString) {
result += builtString;
}
if (builtEndpoint) {
result += builtString ? "\n" + builtEndpoint : builtEndpoint;
}
return result;
}
function diff(strings) {
const removedStrings = [];
const updatedStrings = [];
const addedStrings = [];
for (const i of Object.keys(strings[0])) {
if (strings[1][i] != undefined) {
if (strings[0][i] !== strings[1][i]) {
updatedStrings.push([i, strings[0][i], strings[1][i]]);
}
} else {
removedStrings.push([i, strings[0][i]]);
}
}
for (const i of Object.keys(strings[1])) {
if (strings[0][i] == undefined) {
addedStrings.push([i, strings[1][i]]);
}
}
return {
addedStrings,
updatedStrings,
removedStrings,
};
}
function buildString(
type,
addedStrings,
updatedStrings,
removedStrings
) {
let builtString = "";
// if any of the following have data, we build a string
if (addedStrings[0] || updatedStrings[0] || removedStrings[0]) {
builtString += formats[FORMAT].startString[type];
// check if there's an entry in the strings array
if (addedStrings[0]) {
builtString += formats[FORMAT].addedHeader;
// done this cause it's repeated code and people would get mad at me
builtString += buildDiffString(addedStrings, "added");
}
// the following two are basically the same as above
if (updatedStrings[0]) {
builtString += formats[FORMAT].updatedHeader;
builtString += buildDiffString(updatedStrings, "updated");
}
if (removedStrings[0]) {
builtString += formats[FORMAT].removedHeader;
builtString += buildDiffString(removedStrings, "removed");
}
builtString += formats[FORMAT].endString;
}
return builtString;
}
function buildDiffString(diffedStrings, type) {
// i've sort of gotten lost with variable names
let builtDiff = "";
diffedStrings.forEach((diffedString) => {
builtDiff += formats[FORMAT][type](diffedString);
});
return builtDiff;
}
export default doWork;