Skip to content

Commit

Permalink
Merge pull request #6 from bigman73/bugfix/improve-code-quality
Browse files Browse the repository at this point in the history
Bugfix/improve code quality
  • Loading branch information
bigman73 authored Nov 4, 2019
2 parents afdb286 + 464ffca commit 7200024
Show file tree
Hide file tree
Showing 16 changed files with 1,125 additions and 100 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@
"padded-blocks": "off",
"padding-line-between-statements": "error",
"prefer-arrow-callback": "off",
"prefer-const": "off",
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}],
"prefer-destructuring": "off",
"prefer-numeric-literals": "error",
"prefer-promise-reject-errors": "error",
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"workbench.colorCustomizations": {
"titleBar.activeBackground": "#5994f3",
"titleBar.activeForeground": "#ffffff"
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.

## [Released]

## [1.2.2] - TBD

### Changed
- ESLint Code quality improved


## [1.2.1] - 2019-10-27

### Added
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const path = require('path');

const program = async () => {
console.log('Building the NBA JGF Graph...');
let container = new JGFContainer(singleGraph = true);
let graph = container.graph;
const container = new JGFContainer(singleGraph = true);
const graph = container.graph;
graph.type = 'sports';
graph.label = 'NBA Demo Graph';

Expand Down Expand Up @@ -62,16 +62,16 @@ const program = async () => {
await container.saveToFile(filename, prettyPrint = true);

console.log('Load the saved JGF file');
let container2 = new JGFContainer();
const container2 = new JGFContainer();
await container2.loadFromFile(filename);

console.log('Graph nodes:');
for (let node of container2.graph.nodes) {
for (const node of container2.graph.nodes) {
console.log(`\t${node.label} {${node.metadata.type}}`);
}

console.log('Graph edges:');
for (let edge of container2.graph.edges) {
for (const edge of container2.graph.edges) {
console.log(`\t${edge.source} (->${edge.relation}->) ${edge.target}`);
}

Expand Down
10 changes: 5 additions & 5 deletions demo/nba-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const path = require('path');
*/
const program = async () => {
console.log('Building the NBA JGF Graph...');
let container = new JGFContainer(singleGraph = true);
let graph = container.graph;
const container = new JGFContainer(singleGraph = true);
const graph = container.graph;
graph.type = 'sports';
graph.label = 'NBA Demo Graph';

Expand Down Expand Up @@ -37,16 +37,16 @@ const program = async () => {
await container.saveToFile(filename, prettyPrint = true);

console.log('Load the saved JGF file');
let container2 = new JGFContainer();
const container2 = new JGFContainer();
await container2.loadFromFile(filename);

console.log('Graph nodes:');
for (let node of container2.graph.nodes) {
for (const node of container2.graph.nodes) {
console.log(`\t${node.label} {${node.metadata.type}}`);
}

console.log('Graph edges:');
for (let edge of container2.graph.edges) {
for (const edge of container2.graph.edges) {
console.log(`\t${edge.source} (->${edge.label}->) ${edge.target}`);
}

Expand Down
20 changes: 10 additions & 10 deletions jgfContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class JGFContainer {
* Adds an empty graph
*/
addEmptyGraph() {
let graph = new JGFGraph();
const graph = new JGFGraph();
this._graphs.push(graph);

return graph;
Expand All @@ -84,7 +84,7 @@ class JGFContainer {
}

this.json = await fsExtra.readJson(filename);
let valid = this.JGFSchemaValidator.validate(this.json, jgfSchema);
const valid = this.JGFSchemaValidator.validate(this.json, jgfSchema);

if (!valid.valid) {
throw new Error(`Invalid JGF format. Validation Errors: ${JSON.stringify(valid.errors)}`)
Expand Down Expand Up @@ -123,22 +123,22 @@ class JGFContainer {

this._graphs = [];
this.isSingleGraph = true;
let mainGraph = this.addEmptyGraph();
const mainGraph = this.addEmptyGraph();
mainGraph.type = type;
mainGraph.label = label;

let files = await misc.getMatchingfiles(filenameWildcard);
const files = await misc.getMatchingfiles(filenameWildcard);
let firstTime = true;

let allEdgesGroups = [];
const allEdgesGroups = [];

// 1st pass - Read all partial graphs and only add the nodes, accumulate the edges for the 2nd pass
for (let filename of files) {
for (const filename of files) {
console.debug(`Loading partial graph file: ${filename}`);

// Load partial JGF graph file
let partialJson = await fsExtra.readJson(filename); // eslint-disable-line no-await-in-loop
let valid = this.JGFSchemaValidator.validate(this.json, jgfSchema);
const partialJson = await fsExtra.readJson(filename); // eslint-disable-line no-await-in-loop
const valid = this.JGFSchemaValidator.validate(this.json, jgfSchema);

if (!valid) {
throw new Error(`Invalid graph, filename = ${filename}`);
Expand Down Expand Up @@ -170,7 +170,7 @@ class JGFContainer {
}

// Second pass - now that all nodes are added to the graph, add the edges
for (let edgesGroup of allEdgesGroups) {
for (const edgesGroup of allEdgesGroups) {
mainGraph.addEdges(edgesGroup);
}
} catch (error) {
Expand All @@ -185,7 +185,7 @@ class JGFContainer {
*/
async saveToFile(filename, prettyPrint = false) {
try {
let containerJson = {};
const containerJson = {};
if (this.isSingleGraph) {
containerJson.graph = this._graphs[0].json;
} else {
Expand Down
17 changes: 9 additions & 8 deletions jgfGraph.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
const Validator = require('jsonschema').Validator;
const check = require('check-types');
const _ = require('lodash');
Expand Down Expand Up @@ -119,7 +120,7 @@ class JGFGraph {
* Returns the graph as JGF Json
*/
get json() {
let json = {
const json = {
type: this._type,
label: this._label,
directed: this._directed,
Expand Down Expand Up @@ -162,7 +163,7 @@ class JGFGraph {
throw new Error(`A node already exists with id = ${id}`);
}

let newNode = {
const newNode = {
id,
label
};
Expand All @@ -180,7 +181,7 @@ class JGFGraph {
* @param {*} nodes A collection of JGF node objects
*/
addNodes(nodes) {
for (let node of nodes) {
for (const node of nodes) {
if (node.id in this._nodes) {
throw new Error(`A node already exists with id = ${node.id}`);
}
Expand All @@ -201,7 +202,7 @@ class JGFGraph {
throw new Error(`Can't update node. A node doesn't exist with id = ${nodeId}`);
}

let node = this._nodes[nodeId];
const node = this._nodes[nodeId];

if (check.assigned(label)) {
node.label = label;
Expand Down Expand Up @@ -266,7 +267,7 @@ class JGFGraph {
}
}

let edge = {
const edge = {
source,
target
};
Expand All @@ -293,7 +294,7 @@ class JGFGraph {
*/
addEdges(edges) {
if (edges) {
for (let edge of edges) {
for (const edge of edges) {
this.addEdge(edge.source, edge.target, edge.relation, edge.label, edge.metadata, edge.directed);
}
}
Expand Down Expand Up @@ -330,7 +331,7 @@ class JGFGraph {
}
}

let edges = _.filter(this._edges, (edge) => {
const edges = _.filter(this._edges, (edge) => {
return edge.source === source &&
edge.target === target &&
(relation === '' || edge.relation === relation);
Expand All @@ -343,7 +344,7 @@ class JGFGraph {
* Returns the graph dimensions - Number of nodes and edges
*/
get graphDimensions() {
let dimensions = {
const dimensions = {
nodes: 0,
edges: 0
};
Expand Down
6 changes: 3 additions & 3 deletions misc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const glob = require('glob');

/**
* Returns all files matching the wild card
* @param {*} filenameWildcard filename wildcard
* Returns all files matching the wild card pattern
* @param {*} filenameWildcard filename wildcard pattern
*/
const getMatchingfiles = (filenameWildcard) => {
return new Promise((resolve, reject) => {
glob(filenameWildcard, (err, files) => {
if (err) {
console.log(err);
console.log(`Failed getting filenames, error = ${err}`);

reject(err);
} else {
Expand Down
Loading

0 comments on commit 7200024

Please sign in to comment.