-
Notifications
You must be signed in to change notification settings - Fork 1
/
candidates.js
127 lines (94 loc) · 2.69 KB
/
candidates.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const _ = require('lodash')
let { parse, stringify } = require('scss-parser')
let createQueryWrapper = require('./query-ast')
let $
module.exports = class Candidates {
constructor(ast) {
this.ast = ast
$ = createQueryWrapper(this.ast)
}
maps() {
// Eff up some comments
$('comment_singleline').remove()
let firstarray = $()
.find('value')
.hasParents( 'parentheses' )
.hasParents( 'declaration' )
.map((n) => {
if($(n).has('parentheses').length() === 0) return n
})
let parentMap = (arr) => {
return arr.map((n) => {
return $(n)
.closest('declaration')
.has('property')
.find('property')
.first()
.value()
})
}
let mapValues = [firstarray.map((n) => {
return _.isUndefined(n) ? '' : n.node.type === 'stylesheet' ? '' : stringify($(n).get(0))
})]
let current = []
let obj = {}
while (!_.every(firstarray, _.isEmpty)) {
current = parentMap(firstarray)
if ((!_.isEqual(current, _.last(mapValues)))
&& (!_.every(current, _.isEmpty))) mapValues.push(current)
firstarray = firstarray.map((n) => {
return _.isUndefined(n) ? undefined : n.parent
})
}
_.reverse(mapValues)
let count = 0
for (let value of mapValues[0]) {
current = mapValues.reduce((acc, curr) => {
if(!_.isUndefined(curr[count])) acc.push(curr[count].trim())
return acc
}, [])
count++
current = _.compact(current)
current.unshift(current.splice(0, current.length - 1).join('-stylejam-'))
if (current[0] != '') obj['#' + current[0]] = current[1]
}
return obj
}
borders() {
let borderProps = ['dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset']
let borderQuery = (prop) => {
return $((n) => n.node.value === prop).closest('declaration')
}
let obj = _.stubObject()
for (let prop of borderProps) {
let variables = borderQuery(prop).closest('declaration').find('variable').hasParent('property').map((n) => {
return $(n).value()
})
let values = borderQuery(prop).closest('declaration').has('variable').find('value').map((n) => {
return stringify($(n).get(0))
})
if(variables.length > 0) {
variables.forEach((val, idx) => {
obj['#' + val] = values[idx]
})
}
variables = []
values = []
}
return obj
}
colors() {
let obj = {}
let colors = $('stylesheet')
.children('declaration')
.children('property')
.has('variable').filter((n) => {
return $(n).closest('declaration').has('parentheses').length() === 0
}).closest('declaration').each((n) => {
if(!_.isUndefined(n)) {
obj['#' + $(n).find('variable').eq(0).value()] = stringify($(n).children('value').get(0)).trim()
}
})
return obj
}
}