utils.js 2.4 KB

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