utils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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) {
  54. var escape = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  55. if (escape) {
  56. word = this.escapeRegexp(word);
  57. }
  58. return new RegExp('(^|[\\s,(])(' + word + '($|[\\s(,]))', 'gi');
  59. },
  60. /**
  61. * Change comma list
  62. */
  63. editList: function editList(value, callback) {
  64. var origin = list.comma(value);
  65. var changed = callback(origin, []);
  66. if (origin === changed) {
  67. return value;
  68. }
  69. var join = value.match(/,\s*/);
  70. join = join ? join[0] : ', ';
  71. return changed.join(join);
  72. }
  73. };