UNPKG

expand-brackets

Version:

Expand POSIX bracket expressions (character classes) in glob patterns.

35 lines (27 loc) 672 B
'use strict'; const toRegex = require('to-regex'); const regexNot = require('regex-not'); let cached; /** * Get the last element from `array` * @param {Array} `array` * @return {*} */ exports.last = function(arr) { return arr[arr.length - 1]; }; /** * Create and cache regex to use for text nodes */ exports.createRegex = function(pattern, include) { if (cached) return cached; const opts = {contains: true, strictClose: false}; const not = regexNot.create(pattern, opts); let re; if (typeof include === 'string') { re = toRegex('^(?:' + include + '|' + not + ')', opts); } else { re = toRegex(not, opts); } return (cached = re); };