Skip to content

Commit ef93782

Browse files
Fishrock123phillipj
authored andcommitted
Refactor labels to use Regex-keyed Maps (#41)
* Refactor labels to use Regex-keyed Maps Allows for more extensibility where labels may be the same. * Requires Node.js v6+ (Due to destructuring)
1 parent 9c72402 commit ef93782

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- 4
5-
- 5
64
- 6

lib/node-labels.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
'use strict'
22

33
// order of entries in this map *does* matter for the resolved labels
4-
const subSystemLabelsMap = {
4+
// earlier entries override later entries
5+
const subSystemLabelsMap = new Map([
56
// don't want to label it a c++ update when we're "only" bumping the Node.js version
6-
'c++': /^src\/(?!node_version\.h)/,
7+
[/^src\/(?!node_version\.h)/, 'c++'],
78
// meta is a very specific label for things that are policy and or meta-info related
8-
'meta': /^([A-Z]+$|CODE_OF_CONDUCT|ROADMAP|WORKING_GROUPS|GOVERNANCE|CHANGELOG|\.mail|\.git.+)/,
9+
[/^([A-Z]+$|CODE_OF_CONDUCT|ROADMAP|WORKING_GROUPS|GOVERNANCE|CHANGELOG|\.mail|\.git.+)/, 'meta'],
910
// things that edit top-level .md files are always a doc change
10-
'doc': /^\w+\.md$/,
11+
[/^\w+\.md$/, 'doc'],
1112
// libuv needs an explicit mapping, as the ordinary /deps/ mapping below would
1213
// end up as libuv changes labeled with "uv" (which is a non-existing label)
13-
'libuv': /^deps\/uv\//,
14-
'$1': /^deps\/([^/]+)/
15-
}
14+
[/^deps\/uv\//, 'libuv'],
15+
[/^deps\/([^/]+)/, '$1']
16+
])
1617

17-
const exclusiveLabelsMap = {
18-
test: /^test\//,
19-
doc: /^doc\//,
20-
benchmark: /^benchmark\//
21-
}
18+
const exclusiveLabelsMap = new Map([
19+
[/^test\//, 'test'],
20+
[/^doc\//, 'doc'],
21+
[/^benchmark\//, 'benchmark']
22+
])
2223

2324
function resolveLabels (filepathsChanged) {
2425
const exclusiveLabels = matchExclusiveSubSystem(filepathsChanged)
@@ -54,30 +55,23 @@ function matchSubSystemsByRegex (rxLabelsMap, filepathsChanged) {
5455
}
5556

5657
function mappedSubSystemForFile (labelsMap, filepath) {
57-
return Object.keys(labelsMap).map((labelName) => {
58-
const rxForLabel = labelsMap[labelName]
59-
const matches = rxForLabel.exec(filepath)
58+
for (const [regex, label] of labelsMap) {
59+
const matches = regex.exec(filepath)
6060

61-
// return undefined when subsystem regex didn't match,
62-
// we'll filter out these values with the .filter() below
6361
if (matches === null) {
64-
return undefined
62+
continue
6563
}
6664

6765
// label names starting with $ means we want to extract a matching
6866
// group from the regex we've just matched against
69-
if (labelName.startsWith('$')) {
70-
const wantedMatchGroup = labelName.substr(1)
67+
if (label.startsWith('$')) {
68+
const wantedMatchGroup = label.substr(1)
7169
return matches[wantedMatchGroup]
7270
}
7371

7472
// use label name as is when label doesn't look like a regex matching group
75-
return labelName
76-
}).filter(withoutUndefinedValues)[0]
77-
}
78-
79-
function withoutUndefinedValues (label) {
80-
return label !== undefined
73+
return label
74+
}
8175
}
8276

8377
function matchesAnExclusiveLabel (filepath) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test:watch": "nodemon -q -x 'npm test'"
99
},
1010
"engines": {
11-
"node": ">= 4.2.0"
11+
"node": ">= 6.0.0"
1212
},
1313
"private": true,
1414
"license": "MIT",

0 commit comments

Comments
 (0)