Skip to content

Commit

Permalink
feat(utils): add uilities for obtaining patterns relative to the curr…
Browse files Browse the repository at this point in the history
…ent directory
  • Loading branch information
mrmlnc committed Jun 26, 2021
1 parent e4a3e7f commit 295db2a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/utils/pattern.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
26 changes: 26 additions & 0 deletions src/utils/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down

0 comments on commit 295db2a

Please sign in to comment.