提交 edf88741 编写于 作者: J Jason Park 提交者: Jason

Fix some of eslint errors

上级 2c0d5897
...@@ -3,5 +3,8 @@ module.exports = { ...@@ -3,5 +3,8 @@ module.exports = {
'rules': { 'rules': {
'no-plusplus': 'off', 'no-plusplus': 'off',
'no-mixed-operators': 'off', 'no-mixed-operators': 'off',
'no-bitwise': 'off',
'import/no-unresolved': 'off',
'import/extensions': 'off',
} }
}; };
\ No newline at end of file
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const lebab = require('lebab');
const categories = fs.readdirSync(path.resolve(__dirname, '..', 'algorithm')); const categories = fs.readdirSync(path.resolve(__dirname, '..', 'algorithm'));
for (const category of categories) { for (const category of categories) {
...@@ -8,19 +9,13 @@ for (const category of categories) { ...@@ -8,19 +9,13 @@ for (const category of categories) {
for (const algorithm of algorithms) { for (const algorithm of algorithms) {
if (algorithm.startsWith('.')) continue; if (algorithm.startsWith('.')) continue;
const filepath = path.resolve(__dirname, '..', 'algorithm', category, algorithm, 'code.js'); const filepath = path.resolve(__dirname, '..', 'algorithm', category, algorithm, 'code.js');
const code = fs.readFileSync(filepath, 'utf-8'); const oldCode = fs.readFileSync(filepath, 'utf-8');
const tracers = [ try {
'Array1DTracer', const { code: newCode, warnings } = lebab.transform(oldCode, ['let', 'arrow', 'multi-var', 'template', 'default-param', 'includes']);
'Array2DTracer', // fs.writeFileSync(filepath, newCode, 'utf-8');
'ChartTracer', } catch (e) {
'GraphTracer', console.log(filepath);
'LogTracer', console.error(e);
'Randomize', }
'Tracer',
];
const used = tracers.filter(tracer => code.includes(tracer));
const importLine = `import { ${used.join(', ')} } from 'algorithm-visualizer';\n\n`;
const newCode = importLine + code;
fs.writeFileSync(filepath, newCode, 'utf-8');
} }
} }
\ No newline at end of file
...@@ -2,6 +2,15 @@ const integer = (min = 1, max = 9) => { ...@@ -2,6 +2,15 @@ const integer = (min = 1, max = 9) => {
return (Math.random() * (max - min + 1) | 0) + min; return (Math.random() * (max - min + 1) | 0) + min;
}; };
const string = length => {
const choices = 'abcdefghijklmnopqrstuvwxyz';
let text = '';
for (let i = 0; i < length; i++) {
text += choices[integer(0, choices.length - 1)];
}
return text;
};
const array1D = (N, options) => { const array1D = (N, options) => {
return array2D(1, N, options)[0]; return array2D(1, N, options)[0];
}; };
...@@ -41,6 +50,7 @@ const graph = (N = 5, options = {}) => { ...@@ -41,6 +50,7 @@ const graph = (N = 5, options = {}) => {
export default { export default {
integer, integer,
string,
array1D, array1D,
array2D, array2D,
graph, graph,
......
...@@ -33,16 +33,16 @@ class GraphData extends Data { ...@@ -33,16 +33,16 @@ class GraphData extends Data {
for (let i = 0; i < array2d.length; i++) { for (let i = 0; i < array2d.length; i++) {
const id = i; const id = i;
const weight = null; const weight = null;
const visited = false; const visitedCount = 0;
this.graph.addNode(id, weight, visited); this.graph.addNode(id, weight, visitedCount);
for (let j = 0; j < array2d.length; j++) { for (let j = 0; j < array2d.length; j++) {
const value = array2d[i][j]; const value = array2d[i][j];
if (value) { if (value) {
const source = i; const source = i;
const target = j; const target = j;
const weight = weighted ? value : null; const weight = weighted ? value : null;
const visited = false; const visitedCount = 0;
this.graph.addEdge(source, target, weight, visited); this.graph.addEdge(source, target, weight, visitedCount);
} }
} }
} }
...@@ -80,10 +80,10 @@ class GraphData extends Data { ...@@ -80,10 +80,10 @@ class GraphData extends Data {
visitOrLeave(target, source, weight, visit) { visitOrLeave(target, source, weight, visit) {
const edge = this.graph.findEdge(source, target); const edge = this.graph.findEdge(source, target);
if (edge) edge.visited = visit; if (edge) edge.visitedCount += visit ? 1 : -1;
const node = this.graph.findNode(target); const node = this.graph.findNode(target);
node.weight = weight; node.weight = weight;
node.visited = visit; node.visitedCount += visit ? 1 : -1;
this.render(); this.render();
if (this.logData) { if (this.logData) {
this.logData.print(visit ? (source || '') + ' -> ' + target : (source || '') + ' <- ' + target); this.logData.print(visit ? (source || '') + ' -> ' + target : (source || '') + ' <- ' + target);
...@@ -108,14 +108,14 @@ class Graph { ...@@ -108,14 +108,14 @@ class Graph {
this.directed = directed; this.directed = directed;
} }
addNode(id, weight, visited, x = 0, y = 0) { addNode(id, weight, visitedCount, x = 0, y = 0) {
if (this.findNode(id)) return; if (this.findNode(id)) return;
this.nodes.push({ id, weight, visited, x, y }); this.nodes.push({ id, weight, visitedCount, x, y });
} }
addEdge(source, target, weight, visited) { addEdge(source, target, weight, visitedCount) {
if (this.findEdge(source, target)) return; if (this.findEdge(source, target)) return;
this.edges.push({ source, target, weight, visited }); this.edges.push({ source, target, weight, visitedCount });
} }
findNode(id) { findNode(id) {
......
...@@ -8,23 +8,25 @@ class Array2DRenderer extends Renderer { ...@@ -8,23 +8,25 @@ class Array2DRenderer extends Renderer {
const { data } = this.props.data; const { data } = this.props.data;
return ( return (
<div className={styles.array_2d} <table className={styles.array_2d}
style={{ marginLeft: -this.centerX * 2, marginTop: -this.centerY * 2, fontSize: this.zoom }}> style={{ marginLeft: -this.centerX * 2, marginTop: -this.centerY * 2, fontSize: this.zoom }}>
<tbody>
{ {
data.map((row, i) => ( data.map((row, i) => (
<div className={styles.row} key={i}> <tr className={styles.row} key={i}>
{ {
row.map((col, j) => ( row.map((col, j) => (
<div className={classes(styles.col, col.selected && styles.selected, col.notified && styles.notified)} <td className={classes(styles.col, col.selected && styles.selected, col.notified && styles.notified)}
key={j}> key={j}>
<span className={styles.value}>{col.value}</span> <span className={styles.value}>{this.toString(col.value)}</span>
</div> </td>
)) ))
} }
</div> </tr>
)) ))
} }
</div> </tbody>
</table>
); );
} }
} }
......
...@@ -12,12 +12,13 @@ ...@@ -12,12 +12,13 @@
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
width: 28em; min-width: 28em;
height: 28em; height: 28em;
background-color: $theme-normal; background-color: $theme-normal;
border: 1em solid $theme-light; border: 1em solid $theme-light;
margin-right: -1em; margin-right: -1em;
margin-bottom: -1em; margin-bottom: -1em;
padding: 0 4em;
.value { .value {
font-size: 12em; font-size: 12em;
......
...@@ -58,8 +58,8 @@ class GraphRenderer extends Renderer { ...@@ -58,8 +58,8 @@ class GraphRenderer extends Renderer {
</marker> </marker>
</defs> </defs>
{ {
graph.edges.sort((a, b) => a.visited - b.visited).map(edge => { graph.edges.sort((a, b) => a.visitedCount - b.visitedCount).map(edge => {
const { source, target, weight, visited } = edge; const { source, target, weight, visitedCount } = edge;
const { x: sx, y: sy } = graph.findNode(source); const { x: sx, y: sy } = graph.findNode(source);
let { x: ex, y: ey } = graph.findNode(target); let { x: ex, y: ey } = graph.findNode(target);
const mx = (sx + ex) / 2; const mx = (sx + ex) / 2;
...@@ -76,12 +76,13 @@ class GraphRenderer extends Renderer { ...@@ -76,12 +76,13 @@ class GraphRenderer extends Renderer {
} }
return ( return (
<g className={classes(styles.edge, visited && styles.visited)} key={`${source}-${target}`}> <g className={classes(styles.edge, visitedCount && styles.visited)} key={`${source}-${target}`}>
<path d={`M${sx},${sy} L${ex},${ey}`} className={classes(styles.line, directed && styles.directed)} /> <path d={`M${sx},${sy} L${ex},${ey}`} className={classes(styles.line, directed && styles.directed)} />
{ {
weighted && weighted &&
<g transform={`translate(${mx},${my})`}> <g transform={`translate(${mx},${my})`}>
<text className={styles.weight} transform={`rotate(${degree})`} y={-edgeWeightGap}>{weight}</text> <text className={styles.weight} transform={`rotate(${degree})`}
y={-edgeWeightGap}>{this.toString(weight)}</text>
</g> </g>
} }
</g> </g>
...@@ -90,15 +91,15 @@ class GraphRenderer extends Renderer { ...@@ -90,15 +91,15 @@ class GraphRenderer extends Renderer {
} }
{ {
graph.nodes.map(node => { graph.nodes.map(node => {
const { id, x, y, weight, visited } = node; const { id, x, y, weight, visitedCount } = node;
return ( return (
<g className={classes(styles.node, visited && styles.visited)} key={id} <g className={classes(styles.node, visitedCount && styles.visited)} key={id}
transform={`translate(${x},${y})`}> transform={`translate(${x},${y})`}>
<circle className={styles.circle} r={nodeRadius} /> <circle className={styles.circle} r={nodeRadius} />
<text className={styles.id}>{id}</text> <text className={styles.id}>{id}</text>
{ {
weighted && weighted &&
<text className={styles.weight} x={nodeRadius + nodeWeightGap}>{weight}</text> <text className={styles.weight} x={nodeRadius + nodeWeightGap}>{this.toString(weight)}</text>
} }
</g> </g>
); );
......
...@@ -89,6 +89,17 @@ class Renderer extends React.Component { ...@@ -89,6 +89,17 @@ class Renderer extends React.Component {
this.refresh(); this.refresh();
} }
toString(value) {
switch (typeof(value)) {
case 'number':
return value === Infinity ? '' : Number.isInteger(value) ? value.toString() : value.toFixed(3);
case 'boolean':
return value ? 'T' : 'F';
default:
return value;
}
}
refresh() { refresh() {
this.forceUpdate(); this.forceUpdate();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册