-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (69 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Scans `dnaBranch` and returns Array of
*
* CellInfo {
* name: String,
* dna: DNA,
* groups: Array[String],
* dnaBranchPath: String
* }
*
* where properties are computed as follows:
*
* * `name` reflects to dna branch having both `cellKind` and `cellInfo` properties
* * `dna` reflects to the dna branch itself
* * `groups` reflects to the branch.groups concatinated with the branch's
* path split as single names
* * `dnaBranchPath` contains dot notated dna branch path
*/
module.exports.getAllCells = function (dnaBranch, cellIdentifierFn) {
const r = walk(dnaBranch, [], '', cellIdentifierFn || defaultCellIdentifierFn)
return r
}
/**
* Returns the first found cell using `getAllCells` matched by name
*/
module.exports.getCell = function (dnaBranch, cellName, cellIdentifierFn) {
const cells = module.exports.getAllCells(dnaBranch, cellIdentifierFn)
for (let i = 0; i < cells.length; i++) {
if (cells[i].name === cellName) {
return cells[i]
}
}
}
const defaultCellIdentifierFn = function (branch) {
return typeof branch.cellKind === 'string' &&
branch.cellInfo === 'v1'
}
const walk = function (branch, branchRoots, branchName, cellIdentifierFn) {
if (typeof branch !== 'object') throw new Error('can not walk ' + typeof branch + ' at ' + branchRoots.join('.') + '#' + branchName)
if (branch === null) throw new Error('can not access null branch')
let results = []
const isCell = cellIdentifierFn(branch)
if (isCell) {
const cellInfo = {
name: branch.name || branchName,
dna: branch,
groups: consolidateGroups(branchRoots, branch),
dnaBranchPath: branchRoots.concat([branchName]).filter(v => v).join('.')
}
results.push(cellInfo)
}
if (branchName) {
branchRoots = branchRoots.concat([branchName])
}
for (const key in branch) {
if (!branch[key] || typeof branch[key] !== 'object') continue
results = results.concat(walk(branch[key], branchRoots, key, cellIdentifierFn))
}
return results
}
const consolidateGroups = function (branchRoots, branch) {
if (branch.group) {
return branchRoots.concat([branch.group])
}
if (branch.groups) {
return branchRoots.concat(branch.groups)
}
return branchRoots
}