-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
27 lines (27 loc) · 901 Bytes
/
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
module.exports = (selector, attribute, test, stylesheet) => {
const attr = (selector + attribute + test).replace(/\W/g, '')
const result = Array.from(document.querySelectorAll(selector))
.reduce((output, tag, count) => {
if (
test(
attribute === 'value'
? tag.value
: tag.getAttribute(attribute)
)
) {
output.add.push({tag: tag, count: count})
output.styles.push(
stylesheet.replace(
/:self|\$this|\[--self\]/g,
`[data-compare-${attr}="${count}"]`
)
)
} else {
output.remove.push(tag)
}
return output
}, {add: [], remove: [], styles: []})
result.add.forEach(tag => tag.tag.setAttribute(`data-compare-${attr}`, tag.count))
result.remove.forEach(tag => tag.setAttribute(`data-compare-${attr}`, ''))
return result.styles.join('\n')
}