From ccea7c3df78e2cdd3697e2753c5340cd67d901a3 Mon Sep 17 00:00:00 2001 From: techgaun Date: Tue, 9 Apr 2019 23:13:48 -0500 Subject: [PATCH] prettify --- .prettierrc | 5 ++ js/main.js | 199 ++++++++++++++++++++++++++++------------------------ 2 files changed, 112 insertions(+), 92 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..8ff820d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "trailingComma": "es5", + "singleQuote": true, + "semi": true +} diff --git a/js/main.js b/js/main.js index d273ad9..e19aae8 100644 --- a/js/main.js +++ b/js/main.js @@ -1,122 +1,137 @@ window.addEventListener('load', () => { - initDT() // Initialize the DatatTable and window.columnNames variables + initDT(); // Initialize the DatatTable and window.columnNames variables - const repo = getRepoFromUrl(); + const repo = getRepoFromUrl(); - if (repo) { - document.getElementById('q').value = repo; - fetchData(); - } + if (repo) { + document.getElementById('q').value = repo; + fetchData(); + } }); -document.getElementById('form').addEventListener('submit', (e) => { - e.preventDefault(); - fetchData(); +document.getElementById('form').addEventListener('submit', e => { + e.preventDefault(); + fetchData(); }); function fetchData() { - const repo = document.getElementById('q').value; - const re = /[-_\w]+\/[-_.\w]+/; + const repo = document.getElementById('q').value; + const re = /[-_\w]+\/[-_.\w]+/; - const urlRepo = getRepoFromUrl(); + const urlRepo = getRepoFromUrl(); - if (!urlRepo || urlRepo !== repo) { - window.history.pushState('', '', `#${repo}`); - } + if (!urlRepo || urlRepo !== repo) { + window.history.pushState('', '', `#${repo}`); + } - if (re.test(repo)) { - fetchAndShow(repo); - } else { - showMsg('Invalid GitHub repository! Format is <username>/<repo>', 'danger'); - } + if (re.test(repo)) { + fetchAndShow(repo); + } else { + showMsg( + 'Invalid GitHub repository! Format is <username>/<repo>', + 'danger' + ); + } } -function updateDT( data ) { - // Remove any alerts, if any: - if ( $( '.alert' ) ) - $( '.alert' ).remove() +function updateDT(data) { + // Remove any alerts, if any: + if ($('.alert')) $('.alert').remove(); - // Format dataset and redraw DataTable. Use second index for key name - const forks = [] - for ( let fork of data ) { - fork.repoLink = `Link` - fork.ownerName = fork.owner.login - forks.push( fork ) - } - const dataSet = forks.map( fork => window.columnNamesMap.map( colNM => fork[colNM[1]] ) ) - window.forkTable.clear().rows.add( dataSet ).draw() + // Format dataset and redraw DataTable. Use second index for key name + const forks = []; + for (let fork of data) { + fork.repoLink = `Link`; + fork.ownerName = fork.owner.login; + forks.push(fork); + } + const dataSet = forks.map(fork => + window.columnNamesMap.map(colNM => fork[colNM[1]]) + ); + window.forkTable + .clear() + .rows.add(dataSet) + .draw(); } function initDT() { - // Create ordered Object with column name and mapped display name - window.columnNamesMap = [ - // [ 'Repository', 'full_name' ], - ['Link', 'repoLink'], // custom key - ['Owner', 'ownerName'], // custom key - ['Name', 'name'], - ['Branch', 'default_branch'], - ['Stars', 'stargazers_count'], - ['Forks', 'forks'], - ['Open Issues', 'open_issues_count'], - ['Size', 'size'], - ['Last Push', 'pushed_at'], - ] + // Create ordered Object with column name and mapped display name + window.columnNamesMap = [ + // [ 'Repository', 'full_name' ], + ['Link', 'repoLink'], // custom key + ['Owner', 'ownerName'], // custom key + ['Name', 'name'], + ['Branch', 'default_branch'], + ['Stars', 'stargazers_count'], + ['Forks', 'forks'], + ['Open Issues', 'open_issues_count'], + ['Size', 'size'], + ['Last Push', 'pushed_at'], + ]; - // Sort by stars: - const sortColName = 'Stars' - const sortColumnIdx = window.columnNamesMap.map( pair => pair[0] ).indexOf( sortColName ) + // Sort by stars: + const sortColName = 'Stars'; + const sortColumnIdx = window.columnNamesMap + .map(pair => pair[0]) + .indexOf(sortColName); - // Use first index for readable column name - // we use moment's fromNow() if we are rendering for `pushed_at`; better solution welcome - window.forkTable = $( '#forkTable' ).DataTable( { - columns: window.columnNamesMap.map( colNM => { - return { - title: colNM[0], - render: colNM[1] === 'pushed_at' ? (data, type, _row) => { - if (type === 'display') { - return moment(data).fromNow() + // Use first index for readable column name + // we use moment's fromNow() if we are rendering for `pushed_at`; better solution welcome + window.forkTable = $('#forkTable').DataTable({ + columns: window.columnNamesMap.map(colNM => { + return { + title: colNM[0], + render: + colNM[1] === 'pushed_at' + ? (data, type, _row) => { + if (type === 'display') { + return moment(data).fromNow(); + } + return data; } - return data - } : null - } - }), - 'order': [[sortColumnIdx, 'desc']], - } ) + : null, + }; + }), + order: [[sortColumnIdx, 'desc']], + }); } -function fetchAndShow( repo ) { - - repo = repo.replace('https://github.com/', ''); - repo = repo.replace('http://github.com/', ''); - repo = repo.replace('.git', ''); +function fetchAndShow(repo) { + repo = repo.replace('https://github.com/', ''); + repo = repo.replace('http://github.com/', ''); + repo = repo.replace('.git', ''); - fetch( `https://api.github.com/repos/${repo}/forks?sort=stargazers&per_page=100` ) - .then( ( response ) => { - if ( !response.ok ) - throw Error( response.statusText ) - return response.json() - } ) - .then( ( data ) => { - console.log( data ) - updateDT( data ) - } ) - .catch( ( error ) => { - const msg = error.toString().indexOf( 'Forbidden' ) >= 0 ? 'Error: API Rate Limit Exceeded' : error - showMsg( `${msg}. Additional info in console`, 'danger' ) - console.error( error ) - } ) + fetch( + `https://api.github.com/repos/${repo}/forks?sort=stargazers&per_page=100` + ) + .then(response => { + if (!response.ok) throw Error(response.statusText); + return response.json(); + }) + .then(data => { + console.log(data); + updateDT(data); + }) + .catch(error => { + const msg = + error.toString().indexOf('Forbidden') >= 0 + ? 'Error: API Rate Limit Exceeded' + : error; + showMsg(`${msg}. Additional info in console`, 'danger'); + console.error(error); + }); } function showMsg(msg, type) { - let alert_type = 'alert-info'; + let alert_type = 'alert-info'; - if (type === 'danger') { - alert_type = 'alert-danger'; - } + if (type === 'danger') { + alert_type = 'alert-danger'; + } - document.getElementById('footer').innerHTML = ''; + document.getElementById('footer').innerHTML = ''; - document.getElementById('data-body').innerHTML = ` + document.getElementById('data-body').innerHTML = `