utils.js 664 B

12345678910111213141516171819202122232425262728293031323334
  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. };