UNPKG

2.45 kBJavaScriptView Raw
1"use strict";
2
3var list = require('postcss').list;
4
5module.exports = {
6 /**
7 * Throw special error, to tell beniary,
8 * that this error is from Autoprefixer.
9 */
10 error: function error(text) {
11 var err = new Error(text);
12 err.autoprefixer = true;
13 throw err;
14 },
15
16 /**
17 * Return array, that doesn’t contain duplicates.
18 */
19 uniq: function uniq(array) {
20 var filtered = [];
21
22 for (var _iterator = array, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
23 var _ref;
24
25 if (_isArray) {
26 if (_i >= _iterator.length) break;
27 _ref = _iterator[_i++];
28 } else {
29 _i = _iterator.next();
30 if (_i.done) break;
31 _ref = _i.value;
32 }
33
34 var i = _ref;
35
36 if (!filtered.includes(i)) {
37 filtered.push(i);
38 }
39 }
40
41 return filtered;
42 },
43
44 /**
45 * Return "-webkit-" on "-webkit- old"
46 */
47 removeNote: function removeNote(string) {
48 if (!string.includes(' ')) {
49 return string;
50 }
51
52 return string.split(' ')[0];
53 },
54
55 /**
56 * Escape RegExp symbols
57 */
58 escapeRegexp: function escapeRegexp(string) {
59 return string.replace(/[.?*+^$[\]\\(){}|-]/g, '\\$&');
60 },
61
62 /**
63 * Return regexp to check, that CSS string contain word
64 */
65 regexp: function regexp(word, escape) {
66 if (escape === void 0) {
67 escape = true;
68 }
69
70 if (escape) {
71 word = this.escapeRegexp(word);
72 }
73
74 return new RegExp("(^|[\\s,(])(" + word + "($|[\\s(,]))", 'gi');
75 },
76
77 /**
78 * Change comma list
79 */
80 editList: function editList(value, callback) {
81 var origin = list.comma(value);
82 var changed = callback(origin, []);
83
84 if (origin === changed) {
85 return value;
86 }
87
88 var join = value.match(/,\s*/);
89 join = join ? join[0] : ', ';
90 return changed.join(join);
91 },
92
93 /**
94 * Split the selector into parts.
95 * It returns 3 level deep array because selectors can be comma
96 * separated (1), space separated (2), and combined (3)
97 * @param {String} selector selector string
98 * @return {Array<Array<Array>>} 3 level deep array of split selector
99 * @see utils.test.js for examples
100 */
101 splitSelector: function splitSelector(selector) {
102 return list.comma(selector).map(function (i) {
103 return list.space(i).map(function (k) {
104 return k.split(/(?=\.|#)/g);
105 });
106 });
107 }
108};
\No newline at end of file