This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgetPol2.js
128 lines (74 loc) · 2.42 KB
/
getPol2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const unPopulated = require("./findEmptySub")
/* unPopulated test */
var globalExit
var recursiondepth = 1
var globalList = []
async function getPermutations (g) {
// Sort is placed to ensure certain termination order
g.sort((a,b) => {
if (a.match('users:') ) {
return -1
}
/* if (a.toLowerCase()< b.toLowerCase()) {
return -1
} */
/* if (a.match('clientAppTypes:') && !b.match('users:') && !b.match('locations:') ) {
return -1
} */
})
rcPerm(g)
return globalList
}
function rcPerm(list, subItem) {
if (globalExit) {
return globalList
}
// new set here is redundant, (for uniqueness, but keeping this for documentation)
for (let item of new Set(list.map(s => s.split(':')[0]))) {
if (globalExit) {
return
}
if (subItem) {
recursiondepth++
console.log(recursiondepth)
let nested = list.filter(subItem => subItem.match(item)).map(rootItem => {
return {
rootItem,
subItems: []
}
})
let outerList = list.filter(subItem => !subItem.match(item))
if (nested.length > 0 ) {
if (recursiondepth == 4) {
console.log()
}
let altSub = unPopulated(subItem,nested)
/* subItem.map(ins => ins.subItems = nested) */
if (outerList.length == 0) {
globalExit=true
globalList=altSub
return
}
rcPerm(outerList, altSub)
}
} else {
if (globalExit) {
return
}
// console.log(globalExit)
let innerList = list.filter(subItem => subItem.match(item))
let outerList = list.filter(subItem => !subItem.match(item))
if (innerList.length > 0 && !globalExit) {
let goForward = innerList.map(rootItem => {
return {
rootItem,
subItems: []
}
})
rcPerm(outerList, goForward)
}
}
}
return ;
}
module.exports={getPermutations}