Skip to content

Instantly share code, notes, and snippets.

@jsdario
Created September 11, 2018 11:05
Show Gist options
  • Save jsdario/86f0dfee31947ec11d636720b0b00a1c to your computer and use it in GitHub Desktop.
Save jsdario/86f0dfee31947ec11d636720b0b00a1c to your computer and use it in GitHub Desktop.
IndianredVibrantChapters created by jsdario - https://repl.it/@jsdario/IndianredVibrantChapters
const fetch = require('node-fetch')
fetch('https://api.github.com/repos/netbeast/dashboard/git/trees/master?recursive=1')
.then(r => r.json())
.then(data => {
const h = turnGitHubTreeIntoD3Hierarchy(data)
console.log(h)
})
.catch(console.error)
const emptyHierarchy = {
name: 'root',
children: []
}
function turnGitHubTreeIntoD3Hierarchy (githubData) {
return githubData.tree.reduce(reducer, [])
}
function reducer (acc, curr, currIdx, arr) {
const children = arr.filter(node =>
isChildOf(node.path, curr.path)
)
const [name, ...rest] = curr.path.split('/').reverse()
const depth = curr.path.split('/').length - 1
// Buenísima jugada esta
if (depth > 0 && children.length < 1) {
return acc
}
return [...acc, {...curr, name, depth, children}]
}
const isChildOf = (child, parent) => {
if (child === parent) return false
const parentTokens = parent.split('/').filter(i => i.length)
return parentTokens.every((t, i) => child.split('/')[i] === t)
}
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"node-fetch": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.2.0.tgz",
"integrity": "sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA=="
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment