Created
April 9, 2020 19:43
-
-
Save tokenvolt/b69dc6cb2d9ea2427d3e4b978401cc1a to your computer and use it in GitHub Desktop.
Create Hierarchy challange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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