123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- import { GLOBSTAR } from 'minimatch';
- const isPatternList = (pl) => pl.length >= 1;
- const isGlobList = (gl) => gl.length >= 1;
- export class Pattern {
- #patternList;
- #globList;
- #index;
- length;
- #platform;
- #rest;
- #globString;
- #isDrive;
- #isUNC;
- #isAbsolute;
- #followGlobstar = true;
- constructor(patternList, globList, index, platform) {
- if (!isPatternList(patternList)) {
- throw new TypeError('empty pattern list');
- }
- if (!isGlobList(globList)) {
- throw new TypeError('empty glob list');
- }
- if (globList.length !== patternList.length) {
- throw new TypeError('mismatched pattern list and glob list lengths');
- }
- this.length = patternList.length;
- if (index < 0 || index >= this.length) {
- throw new TypeError('index out of range');
- }
- this.#patternList = patternList;
- this.#globList = globList;
- this.#index = index;
- this.#platform = platform;
-
- if (this.#index === 0) {
-
-
-
-
-
-
-
-
- if (this.isUNC()) {
-
- const [p0, p1, p2, p3, ...prest] = this.#patternList;
- const [g0, g1, g2, g3, ...grest] = this.#globList;
- if (prest[0] === '') {
-
- prest.shift();
- grest.shift();
- }
- const p = [p0, p1, p2, p3, ''].join('/');
- const g = [g0, g1, g2, g3, ''].join('/');
- this.#patternList = [p, ...prest];
- this.#globList = [g, ...grest];
- this.length = this.#patternList.length;
- }
- else if (this.isDrive() || this.isAbsolute()) {
- const [p1, ...prest] = this.#patternList;
- const [g1, ...grest] = this.#globList;
- if (prest[0] === '') {
-
- prest.shift();
- grest.shift();
- }
- const p = p1 + '/';
- const g = g1 + '/';
- this.#patternList = [p, ...prest];
- this.#globList = [g, ...grest];
- this.length = this.#patternList.length;
- }
- }
- }
-
- pattern() {
- return this.#patternList[this.#index];
- }
-
- isString() {
- return typeof this.#patternList[this.#index] === 'string';
- }
-
- isGlobstar() {
- return this.#patternList[this.#index] === GLOBSTAR;
- }
-
- isRegExp() {
- return this.#patternList[this.#index] instanceof RegExp;
- }
-
- globString() {
- return (this.#globString =
- this.#globString ||
- (this.#index === 0 ?
- this.isAbsolute() ?
- this.#globList[0] + this.#globList.slice(1).join('/')
- : this.#globList.join('/')
- : this.#globList.slice(this.#index).join('/')));
- }
-
- hasMore() {
- return this.length > this.#index + 1;
- }
-
- rest() {
- if (this.#rest !== undefined)
- return this.#rest;
- if (!this.hasMore())
- return (this.#rest = null);
- this.#rest = new Pattern(this.#patternList, this.#globList, this.#index + 1, this.#platform);
- this.#rest.#isAbsolute = this.#isAbsolute;
- this.#rest.#isUNC = this.#isUNC;
- this.#rest.#isDrive = this.#isDrive;
- return this.#rest;
- }
-
- isUNC() {
- const pl = this.#patternList;
- return this.#isUNC !== undefined ?
- this.#isUNC
- : (this.#isUNC =
- this.#platform === 'win32' &&
- this.#index === 0 &&
- pl[0] === '' &&
- pl[1] === '' &&
- typeof pl[2] === 'string' &&
- !!pl[2] &&
- typeof pl[3] === 'string' &&
- !!pl[3]);
- }
-
-
-
-
-
-
- isDrive() {
- const pl = this.#patternList;
- return this.#isDrive !== undefined ?
- this.#isDrive
- : (this.#isDrive =
- this.#platform === 'win32' &&
- this.#index === 0 &&
- this.length > 1 &&
- typeof pl[0] === 'string' &&
- /^[a-z]:$/i.test(pl[0]));
- }
-
-
-
-
- isAbsolute() {
- const pl = this.#patternList;
- return this.#isAbsolute !== undefined ?
- this.#isAbsolute
- : (this.#isAbsolute =
- (pl[0] === '' && pl.length > 1) ||
- this.isDrive() ||
- this.isUNC());
- }
-
- root() {
- const p = this.#patternList[0];
- return (typeof p === 'string' && this.isAbsolute() && this.#index === 0) ?
- p
- : '';
- }
-
- checkFollowGlobstar() {
- return !(this.#index === 0 ||
- !this.isGlobstar() ||
- !this.#followGlobstar);
- }
-
- markFollowGlobstar() {
- if (this.#index === 0 || !this.isGlobstar() || !this.#followGlobstar)
- return false;
- this.#followGlobstar = false;
- return true;
- }
- }
|