Skip to content

Commit

Permalink
feat(bin): add support for regex syntax in bind
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Now regex behaves different in cli

Closes #24
  • Loading branch information
euberdeveloper committed Feb 19, 2023
1 parent 6717f43 commit 5976548
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions source/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import { writeFileSync } from 'fs';
import * as dree from '../lib/index';
import { ParseOptions, ScanOptions, SortDiscriminator } from '../lib/index';

function parseRegExp(patterns: string[]): RegExp[] {
let result: RegExp[] = [];
if (patterns && patterns.length) {
for (const pattern of patterns) {
result.push(new RegExp(pattern.replace(/\\/g, '\\\\')));
}
}
return result;
function escapeStringRegexp(string) {
return string
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
}

function parseRegExp(patterns: string[]): (RegExp | string)[] {
const isRegexRegex = /^\/(?<pattern>.*)\/(?<flags>[igm]*)?$/;
return patterns ? patterns.map(pattern => {
const match = pattern.match(isRegexRegex);
return match ? new RegExp(escapeStringRegexp(match.groups!.pattern), match.groups!.flags) : pattern;
}) : [];
}

function parseSorted(sorted?: 'ascending' | 'descending'): SortDiscriminator | boolean | undefined {
Expand All @@ -36,6 +40,7 @@ yargs
},
argv => {
const args: any = argv;
console.log(parseRegExp(args.exclude));
const options: ParseOptions = {
symbolicLinks: args.symbolicLinks,
followLinks: args.followLinks,
Expand Down Expand Up @@ -200,12 +205,12 @@ yargs
hidden: true
},
'exclude': {
describe: 'An array of regex whose all matched path will not be considered during the elaboration',
describe: 'An array of strings (glob patterns) or regex (just write them as you would with js, e.g. /^[a-b]*$/g) whose all matched path will not be considered during the elaboration',
type: 'array',
hidden: true
},
'matches': {
describe: 'It is a regex or array of regex and all the non-matching paths will not be considered by the algorithm. Note: All the ancestors of a matching node will be added',
describe: 'An array of strings (glob patterns) or regex (just write them as you would with js, e.g. /^[a-b]*$/g) and all the non-matching paths will not be considered by the algorithm. Note: All the ancestors of a matching node will be added',
type: 'array',
hidden: true
},
Expand Down

2 comments on commit 5976548

@vercel
Copy link

@vercel vercel bot commented on 5976548 Feb 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 5976548 Feb 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.