• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var validate = require('aproba')
3var moduleName = require('../utils/module-name.js')
4
5module.exports = flattenTree
6module.exports.flatName = flatName
7module.exports.flatNameFromTree = flatNameFromTree
8
9function flattenTree (tree) {
10  validate('O', arguments)
11  var seen = new Set()
12  var flat = {}
13  var todo = [[tree, '/']]
14  while (todo.length) {
15    var next = todo.shift()
16    var pkg = next[0]
17    seen.add(pkg)
18    var path = next[1]
19    flat[path] = pkg
20    if (path !== '/') path += '/'
21    for (var ii = 0; ii < pkg.children.length; ++ii) {
22      var child = pkg.children[ii]
23      if (!seen.has(child)) {
24        todo.push([child, flatName(path, child)])
25      }
26    }
27  }
28  return flat
29}
30
31function flatName (path, child) {
32  validate('SO', arguments)
33  return path + (moduleName(child) || 'TOP')
34}
35
36function flatNameFromTree (tree) {
37  validate('O', arguments)
38  if (tree.isTop) return '/'
39  var path = flatNameFromTree(tree.parent)
40  if (path !== '/') path += '/'
41  return flatName(path, tree)
42}
43