You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
664 B

4 years ago
  1. 'use strict';
  2. var toRegex = require('to-regex');
  3. var regexNot = require('regex-not');
  4. var cached;
  5. /**
  6. * Get the last element from `array`
  7. * @param {Array} `array`
  8. * @return {*}
  9. */
  10. exports.last = function(arr) {
  11. return arr[arr.length - 1];
  12. };
  13. /**
  14. * Create and cache regex to use for text nodes
  15. */
  16. exports.createRegex = function(pattern, include) {
  17. if (cached) return cached;
  18. var opts = {contains: true, strictClose: false};
  19. var not = regexNot.create(pattern, opts);
  20. var re;
  21. if (typeof include === 'string') {
  22. re = toRegex('^(?:' + include + '|' + not + ')', opts);
  23. } else {
  24. re = toRegex(not, opts);
  25. }
  26. return (cached = re);
  27. };