Skip to content

Commit 4604256

Browse files
committedJan 9, 2020
Refactoring of params
1 parent fdf8253 commit 4604256

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed
 

‎lib/params.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
module.exports = class Params {
2-
constructor(description = '') {
3-
// params [..<required>] [..[optional]]
4-
// <foo> - require
2+
constructor(params = '', context) {
3+
// params = ..<required> ..[optional]
4+
// <foo> - required
55
// [foo] - optional
6-
let tmp;
7-
let left = description.trim();
6+
let left = params.trim();
7+
let m;
88

9-
this.minCount = 0;
10-
this.maxCount = 0;
119
this.args = [];
1210

13-
do {
14-
tmp = left;
15-
left = left.replace(/^<([a-z][a-z0-9\-\_]*)>\s*/i, (_, name) => {
16-
this.args.push({ name, required: true });
17-
this.minCount++;
18-
this.maxCount++;
11+
while (m = left.match(/^<([^>]+)>\s*/)) {
12+
left = left.slice(m[0].length);
13+
this.args.push({ name: m[1], required: true });
14+
}
1915

20-
return '';
21-
});
22-
} while (tmp !== left);
16+
this.minCount = this.args.length;
2317

24-
do {
25-
tmp = left;
26-
left = left.replace(/^\[([a-z][a-z0-9\-\_]*)\]\s*/i, (_, name) => {
27-
this.args.push({ name, required: false });
28-
this.maxCount++;
18+
while (m = left.match(/^\[([^\]]+)\]\s*/)) {
19+
left = left.slice(m[0].length);
20+
this.args.push({ name: m[1], required: false });
21+
}
2922

30-
return '';
31-
});
32-
} while (tmp !== left);
23+
this.maxCount = this.args.length;
3324

3425
if (left) {
35-
throw new Error(`Bad parameter description "${description.trim()}" in ${context}`);
26+
throw new Error(`Bad parameters description "${params.trim()}" in ${context}`);
3627
}
3728
}
3829
};

0 commit comments

Comments
 (0)
Please sign in to comment.