wishthis/node_modules/autoprefixer/lib/brackets.js

52 lines
849 B
JavaScript
Raw Permalink Normal View History

2022-01-21 08:28:41 +00:00
function last(array) {
2023-08-17 09:47:40 +00:00
return array[array.length - 1]
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let brackets = {
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Parse string to nodes tree
*/
parse(str) {
let current = ['']
let stack = [current]
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
for (let sym of str) {
2022-01-21 08:28:41 +00:00
if (sym === '(') {
2023-08-17 09:47:40 +00:00
current = ['']
last(stack).push(current)
stack.push(current)
continue
2022-01-21 08:28:41 +00:00
}
if (sym === ')') {
2023-08-17 09:47:40 +00:00
stack.pop()
current = last(stack)
current.push('')
continue
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
current[current.length - 1] += sym
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return stack[0]
2022-01-21 08:28:41 +00:00
},
/**
2023-08-17 09:47:40 +00:00
* Generate output string by nodes tree
*/
stringify(ast) {
let result = ''
for (let i of ast) {
2022-01-21 08:28:41 +00:00
if (typeof i === 'object') {
2023-08-17 09:47:40 +00:00
result += `(${brackets.stringify(i)})`
continue
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
result += i
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return result
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
}
module.exports = brackets