diff --git a/src/graph.js b/src/graph.js index a921380..3452816 100644 --- a/src/graph.js +++ b/src/graph.js @@ -24,7 +24,7 @@ const fs = require('fs'); const graph = (path) => { - return fs.readFileSync(path, "utf-8"); + return fs.readFileSync(path, 'utf-8'); }; module.exports = graph; diff --git a/src/index.js b/src/index.js index 5052188..973d4e0 100644 --- a/src/index.js +++ b/src/index.js @@ -31,8 +31,8 @@ const path = require('path'); const filed = require('./tokens.js'); const query = require('./graph.js'); const pkg = require('../package.json'); -let fs = require('fs'); -const nestedProp = require("./nested-prop.js"); +const fs = require('fs'); +const nestedProp = require('./nested-prop.js'); console.log(`Running ghminer@${pkg.version}`); const argv = minimist(process.argv.slice(2)); @@ -43,8 +43,8 @@ const startDate = argv.start || '2008-01-01'; const endDate = argv.end || now; const dateType = argv.date || 'created'; const print = argv.json || false; -const gpath = argv.graphql || "ghminer.graphql"; -const schema = argv.schema || "ghminer.json"; +const gpath = argv.graphql || 'ghminer.graphql'; +const schema = argv.schema || 'ghminer.json'; let tokens; if (argv.tokens) { @@ -231,7 +231,7 @@ async function fetchAllResults() { * @param {Object} json Json objects */ function writeFiles(json) { - let format = JSON.parse(fs.readFileSync(schema, "utf-8")); + const format = JSON.parse(fs.readFileSync(schema, 'utf-8')); const formattedResults = json.map((result) => { const data = {}; for (const [key, path] of Object.entries(format)) { diff --git a/src/nested-prop.js b/src/nested-prop.js index 8df5b5d..34963a8 100644 --- a/src/nested-prop.js +++ b/src/nested-prop.js @@ -35,9 +35,13 @@ const nestedProp = (obj, path) => { return acc[arrayKey] && Array.isArray(acc[arrayKey]) ? acc[arrayKey] : []; } } - return Array.isArray(acc) - ? acc.map(item => item[key] !== undefined ? item[key] : null) - : acc[key] !== undefined ? acc[key] : null; + if (Array.isArray(acc)) { + return acc.map((item) => item[key] !== undefined ? item[key] : null); + } else if (acc[key] !== undefined) { + return acc[key]; + } else { + return null; + } }, obj); }; diff --git a/test/nested-prop.test.js b/test/nested-prop.test.js index 4f6a232..b261055 100644 --- a/test/nested-prop.test.js +++ b/test/nested-prop.test.js @@ -24,12 +24,12 @@ const assert = require('assert'); const nested = require('../src/nested-prop'); -describe('Test case for nested-prop.js', function () { - it('returns prop', function () { +describe('Test case for nested-prop.js', function() { + it('returns prop', function() { const obj = { - test: "aaa" + test: 'aaa' }; - const prop = nested(obj, "test") + const prop = nested(obj, 'test') const expected = 'aaa'; assert.equal( prop, @@ -37,13 +37,13 @@ describe('Test case for nested-prop.js', function () { `parsed ${prop} does not match with expected ${expected}` ); }); - it('returns nested prop', function () { + it('returns nested prop', function() { const obj = { test: { - aaa: "bbb" + aaa: 'bbb' } }; - const prop = nested(obj, "test.aaa") + const prop = nested(obj, 'test.aaa') const expected = 'bbb'; assert.equal( prop, @@ -51,7 +51,7 @@ describe('Test case for nested-prop.js', function () { `parsed ${prop} does not match with expected ${expected}` ); }); - it('returns first nested prop from array', function () { + it('returns first nested prop from array', function() { const obj = { defaultBranchRef: { target: { @@ -59,7 +59,7 @@ describe('Test case for nested-prop.js', function () { edges: [ { node: { - committedDate: "2024-10-01" + committedDate: '2024-10-01' } } ] @@ -69,7 +69,7 @@ describe('Test case for nested-prop.js', function () { }; const prop = nested( obj, - "defaultBranchRef.target.history.edges[0].node.committedDate" + 'defaultBranchRef.target.history.edges[0].node.committedDate' ); const expected = '2024-10-01'; assert.equal( @@ -78,28 +78,28 @@ describe('Test case for nested-prop.js', function () { `parsed ${prop} does not match with expected ${expected}` ); }); - it('returns array of nested props', function () { + it('returns array of nested props', function() { const obj = { repositoryTopics: { edges: [ { node: { topic: { - name: "java" + name: 'java' } } }, { node: { topic: { - name: "jvm" + name: 'jvm' } } }, { node: { topic: { - name: "compilers" + name: 'compilers' } } } @@ -108,7 +108,7 @@ describe('Test case for nested-prop.js', function () { }; const prop = nested( obj, - "repositoryTopics.edges[].node.topic.name" + 'repositoryTopics.edges[].node.topic.name' ); const expected = 'java,jvm,compilers'; assert.equal(