selector-unique-matches.js 773 B

1234567891011121314151617181920212223242526272829
  1. var processSelectors = require('./process-selectors');
  2. /**
  3. * Returns the matches of the first capture group in the given regular
  4. * expression in the specified rules (AST), without repetition
  5. *
  6. * @example
  7. * var rules = getRulesFromCode('[href] { background: red }');
  8. * var regexp = /\[(\w+)\]/g; // Notice the parenthesis!
  9. * selectorUniqueMatches(rules, regexp);
  10. * //> ['href']
  11. *
  12. * @param {Object[]} rules
  13. * @param {RegExp} regexp
  14. * @return {string[]}
  15. */
  16. function selectorUniqueMatches(rules, regexp) {
  17. var resultSet = {};
  18. processSelectors(rules, function(selector) {
  19. var match;
  20. while (!!(match = regexp.exec(selector))) {
  21. resultSet[match[1]] = true;
  22. }
  23. });
  24. return Object.keys(resultSet);
  25. }
  26. module.exports = selectorUniqueMatches;