Skip to content

Commit 3724d29

Browse files
committed
feat(utils/pattern): add method to split pattern into segments
1 parent 87b23e7 commit 3724d29

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

src/tests/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as entry from './utils/entry';
22
import * as errno from './utils/errno';
3+
import * as pattern from './utils/pattern';
34
import * as task from './utils/task';
45

56
export {
67
entry,
78
errno,
9+
pattern,
810
task
911
};

src/tests/utils/pattern.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { PatternSegment, Pattern, MicromatchOptions } from '../../types';
2+
import * as utils from '../../utils';
3+
4+
class PatternSegmentBuilder {
5+
private readonly _segment: PatternSegment = {
6+
dynamic: false,
7+
pattern: ''
8+
};
9+
10+
public dynamic(): this {
11+
this._segment.dynamic = true;
12+
13+
return this;
14+
}
15+
16+
public pattern(pattern: Pattern): this {
17+
this._segment.pattern = pattern;
18+
19+
return this;
20+
}
21+
22+
public build(options: MicromatchOptions = {}): PatternSegment {
23+
if (!this._segment.dynamic) {
24+
return this._segment;
25+
}
26+
27+
return {
28+
...this._segment,
29+
patternRe: utils.pattern.makeRe(this._segment.pattern, options)
30+
};
31+
}
32+
}
33+
34+
export function segment(): PatternSegmentBuilder {
35+
return new PatternSegmentBuilder();
36+
}

src/types/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ export type Pattern = string;
99
export type PatternRe = RegExp;
1010
export type PatternsGroup = Record<string, Pattern[]>;
1111

12+
export type PatternSegment = StaticPatternSegment | DynamicPatternSegment;
13+
14+
export type StaticPatternSegment = {
15+
dynamic: false;
16+
pattern: Pattern;
17+
};
18+
19+
export type DynamicPatternSegment = {
20+
dynamic: true;
21+
pattern: Pattern;
22+
patternRe: PatternRe;
23+
};
24+
1225
export type ReaderOptions = fsWalk.Options & {
1326
transform(entry: Entry): EntryItem;
1427
deepFilter: DeepFilterFunction;

src/utils/pattern.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as assert from 'assert';
22

3-
import { Pattern } from '../types';
3+
import { Pattern, PatternSegment } from '../types';
4+
import * as tests from '../tests';
45
import * as util from './pattern';
56

67
describe('Utils → Pattern', () => {
@@ -385,6 +386,22 @@ describe('Utils → Pattern', () => {
385386
});
386387
});
387388

389+
describe('.getPatternSegments', () => {
390+
it('should return an array of pattern segments', () => {
391+
const expected: PatternSegment[] = [
392+
tests.pattern.segment().pattern('a').build(),
393+
tests.pattern.segment().dynamic().pattern('*').build(),
394+
tests.pattern.segment().pattern('b').build(),
395+
tests.pattern.segment().dynamic().pattern('**').build(),
396+
tests.pattern.segment().pattern('c').build()
397+
];
398+
399+
const actual = util.getPatternSegments('a/*/b/**/c', {});
400+
401+
assert.deepStrictEqual(actual, expected);
402+
});
403+
});
404+
388405
describe('.makeRe', () => {
389406
it('should return regexp for provided pattern', () => {
390407
const actual = util.makeRe('*.js', {});

src/utils/pattern.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from 'path';
33
import * as globParent from 'glob-parent';
44
import * as micromatch from 'micromatch';
55

6-
import { MicromatchOptions, Pattern, PatternRe } from '../types';
6+
import { MicromatchOptions, Pattern, PatternRe, PatternSegment } from '../types';
77

88
const GLOBSTAR = '**';
99
const ESCAPE_SYMBOL = '\\';
@@ -143,6 +143,27 @@ export function getPatternParts(pattern: Pattern, options: MicromatchOptions): P
143143
return info.parts;
144144
}
145145

146+
export function getPatternSegments(pattern: Pattern, options: MicromatchOptions): PatternSegment[] {
147+
const parts = getPatternParts(pattern, options);
148+
149+
return parts.map((part) => {
150+
const dynamic = isDynamicPattern(part);
151+
152+
if (!dynamic) {
153+
return {
154+
dynamic: false,
155+
pattern: part
156+
};
157+
}
158+
159+
return {
160+
dynamic: true,
161+
pattern: part,
162+
patternRe: makeRe(part, options)
163+
};
164+
});
165+
}
166+
146167
export function makeRe(pattern: Pattern, options: MicromatchOptions): PatternRe {
147168
return micromatch.makeRe(pattern, options);
148169
}

0 commit comments

Comments
 (0)