Skip to content

Commit 40ab7f8

Browse files
authored
Merge pull request mergeability#162 from shine2lay/feat_match_option
fix: match subOption to accept and process array inputs
2 parents e5482fc + 9edccd0 commit 40ab7f8

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

docs/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ Supported events:
164164
regex: 'DO NOT MERGE'
165165
message: 'Custom message...' # optional
166166
begins_with:
167-
match: '### Goals' # or comma delimited list of strings
167+
match: '### Goals' # or array of strings
168168
message: 'Some message...' #optional
169169
ends_with:
170-
match: 'Any last sentence' # or comma delimited list of strings
170+
match: 'Any last sentence' # array of strings
171171
message: 'Come message...' # optional
172172
```
173173
Supported events:
@@ -190,10 +190,10 @@ Supported events:
190190
regex: 'DO NOT MERGE'
191191
message: 'Custom message...'
192192
begins_with:
193-
match: 'A String' # or comma delimited list of strings
193+
match: 'A String' # or array of strings
194194
message: 'Some message...'
195195
ends_with:
196-
match: 'A String' # or comma delimited list of strings
196+
match: 'A String' # or array of strings
197197
message: 'Come message...'
198198
# all of the message sub-option is optional
199199
```
@@ -217,10 +217,10 @@ Supported events:
217217
regex: 'DO NOT MERGE'
218218
message: 'Custom message...'
219219
begins_with:
220-
match: 'A String' # or comma delimited list of strings
220+
match: 'A String' # array of strings
221221
message: 'Some message...'
222222
ends_with:
223-
match: 'A String' # or comma delimited list of strings
223+
match: 'A String' # array list of strings
224224
message: 'Come message...'
225225
# all of the message sub-option is optional
226226
```
@@ -246,10 +246,10 @@ Supported events:
246246
regex: 'DO NOT MERGE'
247247
message: 'Custom message...'
248248
begins_with:
249-
match: 'A String' # or comma delimited list of strings
249+
match: 'A String' # array of strings
250250
message: 'Some message...'
251251
ends_with:
252-
match: 'A String' # or comma delimited list of strings
252+
match: 'A String' # array of strings
253253
message: 'Come message...'
254254
# all of the message sub-option is optional
255255
```
@@ -290,10 +290,10 @@ Supported events:
290290
regex: 'DO NOT MERGE|WIP'
291291
message: 'Custom message...'
292292
begins_with:
293-
match: 'doc','feat','fix','chore'
293+
match: ['doc','feat','fix','chore']
294294
message: 'Some message...'
295295
ends_with:
296-
match: 'A String' # or comma delimited list of strings
296+
match: 'A String' # or array of strings
297297
message: 'Come message...'
298298
# all of the message sub-option is optional
299299
```
@@ -476,7 +476,7 @@ Automatically create a comment when a new issue is `openened` to remind the auth
476476
validate:
477477
- do: title
478478
begins_with:
479-
match: AUTH|SOCIAL|CORE
479+
match: ['AUTH', 'SOCIAL', 'CORE']
480480
- do: label
481481
must_include:
482482
regex: bug|enhancement

lib/validators/options_processor/options/begins_with.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const MATCH_NOT_FOUND_ERROR = `Failed to run the test because 'match' is not provided for 'begins_with' option. Please check README for more information about configuration`
2+
const UNKNOWN_MATCH_TYPE_ERROR = `'match' type invalid, expected string or Array type`
23
const UNKNOWN_INPUT_TYPE_ERROR = `Input type invalid, expected string or Array as input`
34

45
class BeginsWith {
@@ -11,16 +12,14 @@ class BeginsWith {
1112
throw new Error(MATCH_NOT_FOUND_ERROR)
1213
}
1314

14-
let isMergeable
15-
1615
const DEFAULT_SUCCESS_MESSAGE = `${validatorContext.name} does begins with '${match}'`
1716
if (!description) description = `${validatorContext.name} must begins with "${match}"`
1817

19-
if (typeof input === 'string') {
20-
isMergeable = input.indexOf(match) === 0
21-
} else if (Array.isArray(input)) {
22-
isMergeable = input.some(item => item.indexOf(match) === 0)
23-
} else {
18+
let isMergeable
19+
20+
try {
21+
isMergeable = checkIfMergeable(input, match)
22+
} catch (err) {
2423
throw new Error(UNKNOWN_INPUT_TYPE_ERROR)
2524
}
2625

@@ -31,4 +30,30 @@ class BeginsWith {
3130
}
3231
}
3332

33+
function checkIfMergeable (input, match) {
34+
if (typeof input !== 'string' && !Array.isArray(input)) {
35+
throw new Error(UNKNOWN_INPUT_TYPE_ERROR)
36+
}
37+
38+
if (typeof match !== 'string' && !Array.isArray(match)) {
39+
throw new Error(UNKNOWN_MATCH_TYPE_ERROR)
40+
}
41+
42+
if (typeof input === 'string') {
43+
return checkIfInputMatches(match, (item) => input.indexOf(item) === 0)
44+
} else {
45+
return input.some(inputItem =>
46+
checkIfInputMatches(match, (matchItem) => inputItem.indexOf(matchItem) === 0)
47+
)
48+
}
49+
}
50+
51+
function checkIfInputMatches (match, func) {
52+
if (typeof match === 'string') {
53+
return func(match)
54+
} else {
55+
return match.some(item => func(item))
56+
}
57+
}
58+
3459
module.exports = BeginsWith
Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const MATCH_NOT_FOUND_ERROR = `Failed to run the test because 'match' is not provided for 'ends_with' option. Please check README for more information about configuration`
2+
const UNKNOWN_MATCH_TYPE_ERROR = `'match' type invalid, expected string or Array type`
23
const UNKNOWN_INPUT_TYPE_ERROR = `Input type invalid, expected string or Array as input`
34

45
class EndsWith {
@@ -11,23 +12,48 @@ class EndsWith {
1112
throw new Error(MATCH_NOT_FOUND_ERROR)
1213
}
1314

14-
let isMergeable = input.indexOf(match) === (input.length - match.length)
15-
1615
const DEFAULT_SUCCESS_MESSAGE = `${validatorContext.name} does end with '${match}'`
1716
if (!description) description = `${validatorContext.name} must end with "${match}"`
1817

19-
if (typeof input === 'string') {
20-
isMergeable = input.indexOf(match) === (input.length - match.length)
21-
} else if (Array.isArray(input)) {
22-
isMergeable = input.some(item => item.indexOf(match) === (item.length - match.length))
23-
} else {
18+
let isMergeable
19+
20+
try {
21+
isMergeable = checkIfMergeable(input, match)
22+
} catch (err) {
2423
throw new Error(UNKNOWN_INPUT_TYPE_ERROR)
2524
}
25+
2626
return {
2727
status: isMergeable ? 'pass' : 'fail',
2828
description: isMergeable ? DEFAULT_SUCCESS_MESSAGE : description
2929
}
3030
}
3131
}
3232

33+
function checkIfMergeable (input, match) {
34+
if (typeof input !== 'string' && !Array.isArray(input)) {
35+
throw new Error(UNKNOWN_INPUT_TYPE_ERROR)
36+
}
37+
38+
if (typeof match !== 'string' && !Array.isArray(match)) {
39+
throw new Error(UNKNOWN_MATCH_TYPE_ERROR)
40+
}
41+
42+
if (typeof input === 'string') {
43+
return checkIfInputMatches(match, (item) => input.indexOf(item) === (input.length - item.length))
44+
} else {
45+
return input.some(inputItem =>
46+
checkIfInputMatches(match, (matchItem) => inputItem.indexOf(matchItem) === (inputItem.length - matchItem.length))
47+
)
48+
}
49+
}
50+
51+
function checkIfInputMatches (match, func) {
52+
if (typeof match === 'string') {
53+
return func(match)
54+
} else {
55+
return match.some(item => func(item))
56+
}
57+
}
58+
3359
module.exports = EndsWith

0 commit comments

Comments
 (0)