UNPKG

3.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Glob = void 0;
4/**
5 * Matches state names using glob-like pattern strings.
6 *
7 * Globs can be used in specific APIs including:
8 *
9 * - [[StateService.is]]
10 * - [[StateService.includes]]
11 * - The first argument to Hook Registration functions like [[TransitionService.onStart]]
12 * - [[HookMatchCriteria]] and [[HookMatchCriterion]]
13 *
14 * A `Glob` string is a pattern which matches state names.
15 * Nested state names are split into segments (separated by a dot) when processing.
16 * The state named `foo.bar.baz` is split into three segments ['foo', 'bar', 'baz']
17 *
18 * Globs work according to the following rules:
19 *
20 * ### Exact match:
21 *
22 * The glob `'A.B'` matches the state named exactly `'A.B'`.
23 *
24 * | Glob |Matches states named|Does not match state named|
25 * |:------------|:--------------------|:---------------------|
26 * | `'A'` | `'A'` | `'B'` , `'A.C'` |
27 * | `'A.B'` | `'A.B'` | `'A'` , `'A.B.C'` |
28 * | `'foo'` | `'foo'` | `'FOO'` , `'foo.bar'`|
29 *
30 * ### Single star (`*`)
31 *
32 * A single star (`*`) is a wildcard that matches exactly one segment.
33 *
34 * | Glob |Matches states named |Does not match state named |
35 * |:------------|:---------------------|:--------------------------|
36 * | `'*'` | `'A'` , `'Z'` | `'A.B'` , `'Z.Y.X'` |
37 * | `'A.*'` | `'A.B'` , `'A.C'` | `'A'` , `'A.B.C'` |
38 * | `'A.*.*'` | `'A.B.C'` , `'A.X.Y'`| `'A'`, `'A.B'` , `'Z.Y.X'`|
39 *
40 * ### Double star (`**`)
41 *
42 * A double star (`'**'`) is a wildcard that matches *zero or more segments*
43 *
44 * | Glob |Matches states named |Does not match state named |
45 * |:------------|:----------------------------------------------|:----------------------------------|
46 * | `'**'` | `'A'` , `'A.B'`, `'Z.Y.X'` | (matches all states) |
47 * | `'A.**'` | `'A'` , `'A.B'` , `'A.C.X'` | `'Z.Y.X'` |
48 * | `'**.X'` | `'X'` , `'A.X'` , `'Z.Y.X'` | `'A'` , `'A.login.Z'` |
49 * | `'A.**.X'` | `'A.X'` , `'A.B.X'` , `'A.B.C.X'` | `'A'` , `'A.B.C'` |
50 *
51 * @packageDocumentation
52 */
53var Glob = /** @class */ (function () {
54 function Glob(text) {
55 this.text = text;
56 this.glob = text.split('.');
57 var regexpString = this.text
58 .split('.')
59 .map(function (seg) {
60 if (seg === '**')
61 return '(?:|(?:\\.[^.]*)*)';
62 if (seg === '*')
63 return '\\.[^.]*';
64 return '\\.' + seg;
65 })
66 .join('');
67 this.regexp = new RegExp('^' + regexpString + '$');
68 }
69 /** Returns true if the string has glob-like characters in it */
70 Glob.is = function (text) {
71 return !!/[!,*]+/.exec(text);
72 };
73 /** Returns a glob from the string, or null if the string isn't Glob-like */
74 Glob.fromString = function (text) {
75 return Glob.is(text) ? new Glob(text) : null;
76 };
77 Glob.prototype.matches = function (name) {
78 return this.regexp.test('.' + name);
79 };
80 return Glob;
81}());
82exports.Glob = Glob;
83//# sourceMappingURL=glob.js.map
\No newline at end of file