Skip to content

Commit 295db2a

Browse files
committed
feat(utils): add uilities for obtaining patterns relative to the current directory
1 parent e4a3e7f commit 295db2a

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/utils/pattern.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,52 @@ describe('Utils → Pattern', () => {
257257
});
258258
});
259259

260+
describe('.getPatternsInsideCurrentDirectory', () => {
261+
it('should return patterns', () => {
262+
const expected: Pattern[] = ['.', './*', '*', 'a/*'];
263+
264+
const actual = util.getPatternsInsideCurrentDirectory(['.', './*', '*', 'a/*', '..', '../*', './..', './../*']);
265+
266+
assert.deepStrictEqual(actual, expected);
267+
});
268+
});
269+
270+
describe('.getPatternsOutsideCurrentDirectory', () => {
271+
it('should return patterns', () => {
272+
const expected: Pattern[] = ['..', '../*', './..', './../*'];
273+
274+
const actual = util.getPatternsOutsideCurrentDirectory(['.', './*', '*', 'a/*', '..', '../*', './..', './../*']);
275+
276+
assert.deepStrictEqual(actual, expected);
277+
});
278+
});
279+
280+
describe('.isPatternRelatedToParentDirectory', () => {
281+
it('should be `false` when the pattern refers to the current directory', () => {
282+
const actual = util.isPatternRelatedToParentDirectory('.');
283+
284+
assert.ok(!actual);
285+
});
286+
287+
it('should be `true` when the pattern equals to `..`', () => {
288+
const actual = util.isPatternRelatedToParentDirectory('..');
289+
290+
assert.ok(actual);
291+
});
292+
293+
it('should be `true` when the pattern starts with `..` segment', () => {
294+
const actual = util.isPatternRelatedToParentDirectory('../*');
295+
296+
assert.ok(actual);
297+
});
298+
299+
it('should be `true` when the pattern starts with `./..` segment', () => {
300+
const actual = util.isPatternRelatedToParentDirectory('./../*');
301+
302+
assert.ok(actual);
303+
});
304+
});
305+
260306
describe('.getBaseDirectory', () => {
261307
it('should returns base directory', () => {
262308
const expected = 'root';

src/utils/pattern.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ export function getPositivePatterns(patterns: Pattern[]): Pattern[] {
8181
return patterns.filter(isPositivePattern);
8282
}
8383

84+
/**
85+
* Returns patterns that can be applied inside the current directory.
86+
*
87+
* @example
88+
* // ['./*', '*', 'a/*']
89+
* getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
90+
*/
91+
export function getPatternsInsideCurrentDirectory(patterns: Pattern[]): Pattern[] {
92+
return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
93+
}
94+
95+
/**
96+
* Returns patterns to be expanded relative to (outside) the current directory.
97+
*
98+
* @example
99+
* // ['../*', './../*']
100+
* getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
101+
*/
102+
export function getPatternsOutsideCurrentDirectory(patterns: Pattern[]): Pattern[] {
103+
return patterns.filter(isPatternRelatedToParentDirectory);
104+
}
105+
106+
export function isPatternRelatedToParentDirectory(pattern: Pattern): boolean {
107+
return pattern.startsWith('..') || pattern.startsWith('./..');
108+
}
109+
84110
export function getBaseDirectory(pattern: Pattern): string {
85111
return globParent(pattern, { flipBackslashes: false });
86112
}

0 commit comments

Comments
 (0)