From 295db2a41b1c1dade4cfee3482d9c25de78fd33e Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Sat, 26 Jun 2021 17:38:29 +0300 Subject: [PATCH] feat(utils): add uilities for obtaining patterns relative to the current directory --- src/utils/pattern.spec.ts | 46 +++++++++++++++++++++++++++++++++++++++ src/utils/pattern.ts | 26 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/utils/pattern.spec.ts b/src/utils/pattern.spec.ts index 8b56187b..59983e14 100644 --- a/src/utils/pattern.spec.ts +++ b/src/utils/pattern.spec.ts @@ -257,6 +257,52 @@ describe('Utils → Pattern', () => { }); }); + describe('.getPatternsInsideCurrentDirectory', () => { + it('should return patterns', () => { + const expected: Pattern[] = ['.', './*', '*', 'a/*']; + + const actual = util.getPatternsInsideCurrentDirectory(['.', './*', '*', 'a/*', '..', '../*', './..', './../*']); + + assert.deepStrictEqual(actual, expected); + }); + }); + + describe('.getPatternsOutsideCurrentDirectory', () => { + it('should return patterns', () => { + const expected: Pattern[] = ['..', '../*', './..', './../*']; + + const actual = util.getPatternsOutsideCurrentDirectory(['.', './*', '*', 'a/*', '..', '../*', './..', './../*']); + + assert.deepStrictEqual(actual, expected); + }); + }); + + describe('.isPatternRelatedToParentDirectory', () => { + it('should be `false` when the pattern refers to the current directory', () => { + const actual = util.isPatternRelatedToParentDirectory('.'); + + assert.ok(!actual); + }); + + it('should be `true` when the pattern equals to `..`', () => { + const actual = util.isPatternRelatedToParentDirectory('..'); + + assert.ok(actual); + }); + + it('should be `true` when the pattern starts with `..` segment', () => { + const actual = util.isPatternRelatedToParentDirectory('../*'); + + assert.ok(actual); + }); + + it('should be `true` when the pattern starts with `./..` segment', () => { + const actual = util.isPatternRelatedToParentDirectory('./../*'); + + assert.ok(actual); + }); + }); + describe('.getBaseDirectory', () => { it('should returns base directory', () => { const expected = 'root'; diff --git a/src/utils/pattern.ts b/src/utils/pattern.ts index 9801e138..6190b66f 100644 --- a/src/utils/pattern.ts +++ b/src/utils/pattern.ts @@ -81,6 +81,32 @@ export function getPositivePatterns(patterns: Pattern[]): Pattern[] { return patterns.filter(isPositivePattern); } +/** + * Returns patterns that can be applied inside the current directory. + * + * @example + * // ['./*', '*', 'a/*'] + * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*']) + */ +export function getPatternsInsideCurrentDirectory(patterns: Pattern[]): Pattern[] { + return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern)); +} + +/** + * Returns patterns to be expanded relative to (outside) the current directory. + * + * @example + * // ['../*', './../*'] + * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*']) + */ +export function getPatternsOutsideCurrentDirectory(patterns: Pattern[]): Pattern[] { + return patterns.filter(isPatternRelatedToParentDirectory); +} + +export function isPatternRelatedToParentDirectory(pattern: Pattern): boolean { + return pattern.startsWith('..') || pattern.startsWith('./..'); +} + export function getBaseDirectory(pattern: Pattern): string { return globParent(pattern, { flipBackslashes: false }); }