Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support regex flags #132

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __test__/find.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ describe('find comment tests', () => {
)
).toEqual(true)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '/^.*KaNsAs.*$/i',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'},
created_at: '2020-01-01T00:00:00Z'
}
)
).toEqual(true)

expect(
findCommentPredicate(
{
Expand Down
9 changes: 8 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.findComment = exports.findCommentPredicate = void 0;
const github = __importStar(__nccwpck_require__(5438));
function stringToRegex(s) {
const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/);
if (m)
return new RegExp(m[2], m[3]);
else
return new RegExp(s);
}
function findCommentPredicate(inputs, comment) {
return ((inputs.commentAuthor && comment.user
? comment.user.login === inputs.commentAuthor
Expand All @@ -56,7 +63,7 @@ function findCommentPredicate(inputs, comment) {
? comment.body.includes(inputs.bodyIncludes)
: true) &&
(inputs.bodyRegex && comment.body
? comment.body.match(inputs.bodyRegex) !== null
? comment.body.match(stringToRegex(inputs.bodyRegex)) !== null
: true));
}
exports.findCommentPredicate = findCommentPredicate;
Expand Down
8 changes: 7 additions & 1 deletion src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export interface Comment {
created_at: string
}

function stringToRegex(s: string): RegExp {
const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/)
if (m) return new RegExp(m[2], m[3])
else return new RegExp(s)
}

export function findCommentPredicate(
inputs: Inputs,
comment: Comment
Expand All @@ -31,7 +37,7 @@ export function findCommentPredicate(
? comment.body.includes(inputs.bodyIncludes)
: true) &&
(inputs.bodyRegex && comment.body
? comment.body.match(inputs.bodyRegex) !== null
? comment.body.match(stringToRegex(inputs.bodyRegex)) !== null
: true)
)
}
Expand Down