UNPKG

1.72 kBJavaScriptView Raw
1let { list } = require('postcss')
2
3/**
4 * Throw special error, to tell beniary,
5 * that this error is from Autoprefixer.
6 */
7module.exports.error = function (text) {
8 let err = new Error(text)
9 err.autoprefixer = true
10 throw err
11}
12
13/**
14 * Return array, that doesn’t contain duplicates.
15 */
16module.exports.uniq = function (array) {
17 return [...new Set(array)]
18}
19
20/**
21 * Return "-webkit-" on "-webkit- old"
22 */
23module.exports.removeNote = function (string) {
24 if (!string.includes(' ')) {
25 return string
26 }
27
28 return string.split(' ')[0]
29}
30
31/**
32 * Escape RegExp symbols
33 */
34module.exports.escapeRegexp = function (string) {
35 return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&')
36}
37
38/**
39 * Return regexp to check, that CSS string contain word
40 */
41module.exports.regexp = function (word, escape = true) {
42 if (escape) {
43 word = this.escapeRegexp(word)
44 }
45 return new RegExp(`(^|[\\s,(])(${word}($|[\\s(,]))`, 'gi')
46}
47
48/**
49 * Change comma list
50 */
51module.exports.editList = function (value, callback) {
52 let origin = list.comma(value)
53 let changed = callback(origin, [])
54
55 if (origin === changed) {
56 return value
57 }
58
59 let join = value.match(/,\s*/)
60 join = join ? join[0] : ', '
61 return changed.join(join)
62}
63
64/**
65 * Split the selector into parts.
66 * It returns 3 level deep array because selectors can be comma
67 * separated (1), space separated (2), and combined (3)
68 * @param {String} selector selector string
69 * @return {Array<Array<Array>>} 3 level deep array of split selector
70 * @see utils.test.js for examples
71 */
72module.exports.splitSelector = function (selector) {
73 return list.comma(selector).map(i => {
74 return list.space(i).map(k => {
75 return k.split(/(?=\.|#)/g)
76 })
77 })
78}