From 108c7553132d045388dbe0218c44e1770f2b9675 Mon Sep 17 00:00:00 2001 From: techgaun Date: Thu, 30 Mar 2017 12:44:00 -0500 Subject: [PATCH] initial working stuff --- README.md | 5 ++++ index.html | 35 +++++++++++++++++++++++++ js/main.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 README.md create mode 100644 index.html create mode 100644 js/main.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..76cbcdd --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# active-forks + +> Find the active github forks of a project + +This project allows you to find the most active forks of a repository. diff --git a/index.html b/index.html new file mode 100644 index 0000000..932e039 --- /dev/null +++ b/index.html @@ -0,0 +1,35 @@ + + + + Find Active Github Forks + + + + +
+
+
+
+
+

Find Active Github Forks

+
+ +
+
+ + + + +
+ +
+
+
+
+
+
+
+
+
+ + diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..850e79a --- /dev/null +++ b/js/main.js @@ -0,0 +1,77 @@ +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"; +}