Skip to content

Commit 034abe9

Browse files
authored
feat: support regex flags (#132)
1 parent a420f9b commit 034abe9

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

__test__/find.unit.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ describe('find comment tests', () => {
210210
)
211211
).toEqual(true)
212212

213+
expect(
214+
findCommentPredicate(
215+
{
216+
token: 'token',
217+
repository: 'repository',
218+
issueNumber: 1,
219+
commentAuthor: 'dorothy',
220+
bodyIncludes: '',
221+
bodyRegex: '/^.*KaNsAs.*$/i',
222+
direction: 'direction'
223+
},
224+
{
225+
id: 1,
226+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
227+
user: {login: 'dorothy'},
228+
created_at: '2020-01-01T00:00:00Z'
229+
}
230+
)
231+
).toEqual(true)
232+
213233
expect(
214234
findCommentPredicate(
215235
{

dist/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
4848
Object.defineProperty(exports, "__esModule", ({ value: true }));
4949
exports.findComment = exports.findCommentPredicate = void 0;
5050
const github = __importStar(__nccwpck_require__(5438));
51+
function stringToRegex(s) {
52+
const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/);
53+
if (m)
54+
return new RegExp(m[2], m[3]);
55+
else
56+
return new RegExp(s);
57+
}
5158
function findCommentPredicate(inputs, comment) {
5259
return ((inputs.commentAuthor && comment.user
5360
? comment.user.login === inputs.commentAuthor
@@ -56,7 +63,7 @@ function findCommentPredicate(inputs, comment) {
5663
? comment.body.includes(inputs.bodyIncludes)
5764
: true) &&
5865
(inputs.bodyRegex && comment.body
59-
? comment.body.match(inputs.bodyRegex) !== null
66+
? comment.body.match(stringToRegex(inputs.bodyRegex)) !== null
6067
: true));
6168
}
6269
exports.findCommentPredicate = findCommentPredicate;

src/find.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export interface Comment {
1919
created_at: string
2020
}
2121

22+
function stringToRegex(s: string): RegExp {
23+
const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/)
24+
if (m) return new RegExp(m[2], m[3])
25+
else return new RegExp(s)
26+
}
27+
2228
export function findCommentPredicate(
2329
inputs: Inputs,
2430
comment: Comment
@@ -31,7 +37,7 @@ export function findCommentPredicate(
3137
? comment.body.includes(inputs.bodyIncludes)
3238
: true) &&
3339
(inputs.bodyRegex && comment.body
34-
? comment.body.match(inputs.bodyRegex) !== null
40+
? comment.body.match(stringToRegex(inputs.bodyRegex)) !== null
3541
: true)
3642
)
3743
}

0 commit comments

Comments
 (0)