Skip to content

Instantly share code, notes, and snippets.

@tokenvolt
Created April 9, 2020 19:43
Show Gist options
  • Save tokenvolt/b69dc6cb2d9ea2427d3e4b978401cc1a to your computer and use it in GitHub Desktop.
Save tokenvolt/b69dc6cb2d9ea2427d3e4b978401cc1a to your computer and use it in GitHub Desktop.
Create Hierarchy challange
const TOPIC = "topic"
const CATEGORY = "category"
const typeName = levels => (levels >= 3 ? TOPIC : CATEGORY)
const createNode = ({ perLevel, levels, remainingLevels }) => {
if (remainingLevels === 0) return { type: TOPIC, children: [] }
return {
type: typeName(levels - remainingLevels),
children: new Array(perLevel).fill(createNode({ perLevel, levels, remainingLevels: remainingLevels - 1 })),
}
}
const createHierarchy = ({ perLevel, levels }) => {
return new Array(perLevel).fill(createNode({ perLevel, levels, remainingLevels: levels - 1 }))
}
module.exports = { createHierarchy }
const test = require("tape")
const tapSpec = require("tap-spec")
const i = require("./createHierarchy")
test("timing test", function (t) {
const result = i.createHierarchy({ perLevel: 3, levels: 5 })
t.deepEqual(
[
{
type: "category",
children: [
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
{
type: "category",
children: [
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
{
type: "topic",
children: [
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
{
type: "topic",
children: [
{ type: "topic", children: [] },
{ type: "topic", children: [] },
{ type: "topic", children: [] },
],
},
],
},
],
},
],
},
],
result
)
t.end()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment