-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (55 loc) · 1.8 KB
/
index.js
File metadata and controls
59 lines (55 loc) · 1.8 KB
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
import Foldmaker, { tokenize } from './foldmaker'
export default string => {
// Tokenize the input
let tokens = tokenize(
string.replace(/\r\n/, '\n'),
[
['i', /(\/\/).*?(?=\n|$)/], // Comment
['i', /\/\*[\s\S]*?\*\//], // Multiline comment
['i', /\s+/], // spaces or newlines
['{', /{/],
['}', /}/],
['s', /^[^{}\n]+?,/], // Line ending in comma (most probably selector)
['p', /^[\s\S]+?;?(?= ?[{}\n])/] // Selector or Property
],
({ type, value }) => {
// This function ignores comments, multiline comments and spaces, right from the start
if (type === 'i') return null
return { type, value }
}
)
let outputString = ''
Foldmaker(tokens)
.parse(/(s*p){[1ps]*}/, ({raw, map}) => {
let selectorsLength = map[1].length
let selectors = raw.slice(0, selectorsLength).join('').split(',')
let body = raw.slice(selectorsLength)
return { selectors, body }
}, e => console.warn(e))
.traverse((parent, also) => {
if (typeof parent === 'object') {
parent.body
.filter(child => typeof child === 'object')
.forEach(child => {
let newSelectors = []
parent.selectors.forEach(sel => {
child.selectors.forEach(ch => {
newSelectors.push(/&/.exec(ch) ? ch.replace(/&/g, sel.trim()) : sel + ' ' + ch)
})
})
child.selectors = newSelectors
})
also(parent.body)
}
return parent
})
.traverse((obj, also) => {
if (typeof obj === 'object') {
let rule = obj.selectors.join(',\n') + ' ' + obj.body.filter(el => typeof el === 'string').join('\n') + '\n'
outputString += rule
also(obj.body)
}
return obj
})
return outputString
}