function fetchData() { const repo = document.getElementById('repo').value const re = /[-_\w]+\/[-_.\w]+/ if (re.test(repo)) { fetchAndShow(repo) } else { showError('Invalid github repo given. Format is /') } } function fetchAndShow(repo) { fetch(`https://api.github.com/repos/${repo}/forks?sort=stargazers`) .then(function(response) { response.json() .then(function(data) { showData(data) }) }) } function showError(msg) { document.getElementById('data-body').innerHTML = `
${msg}
` } function showData(data) { if (!Array.isArray(data)) { showError('Github Repo does not exist') return } if (data.length === 0) { document.getElementById('data-body').innerHTML = `
No forks exist
` return } const thead = 'RepositoryStargazersForksLast Update' const html = [] for (const fork of data) { const item = ` ${fork.full_name} ${fork.stargazers_count} ${fork.forks_count} ${timeSince(fork.updated_at)} ago ` html.push(item) } document.getElementById('data-body').innerHTML = `${thead}${html.join()}
` } function timeSince(date_str) { const date = new Date(date_str) const seconds = Math.floor((new Date() - date) / 1000); let interval = Math.floor(seconds / 31536000); if (interval > 1) { return interval + " years"; } interval = Math.floor(seconds / 2592000); if (interval > 1) { return interval + " months"; } interval = Math.floor(seconds / 86400); if (interval > 1) { return interval + " days"; } interval = Math.floor(seconds / 3600); if (interval > 1) { return interval + " hours"; } interval = Math.floor(seconds / 60); if (interval > 1) { return interval + " minutes"; } return Math.floor(seconds) + " seconds"; }