-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseFlags.js
More file actions
28 lines (25 loc) · 744 Bytes
/
parseFlags.js
File metadata and controls
28 lines (25 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module.exports = (string = '') => {
const [message, ...parts] = string.split(/[ \n]+\-\-/g);
const flags = {};
parts.forEach((text) => {
const [name, ...rest] = text.split(' ');
if (!name) return; // -- broken flag
const value = rest.join(' ').trim();
const prev = flags[name];
if (prev && value) {
if (Array.isArray(prev) && !prev.includes(value)) {
prev.push(value);
} else if (prev === true) { // Currently "true"? Set as value
flags[name] = value;
} else if (prev !== value) { // Create a string array
flags[name] = [prev, value]
}
} else if (!prev) {
flags[name] = value || true;
}
});
return {
message: message.trim(),
flags,
};
};