Skip to content

Commit 43b2582

Browse files
committed
chapter 01: creating simpleParser example
1 parent a03a1c4 commit 43b2582

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const InvalidOperationError = require('./InvalidOperationError');
2+
3+
function simpleParser() {
4+
/**
5+
* @param {string} numbers
6+
* @return {number}
7+
*/
8+
function parseAndSum(numbers) {
9+
if (numbers.length === 0) {
10+
return 0;
11+
}
12+
if (!numbers.includes(',')) {
13+
return Number(numbers);
14+
} else {
15+
throw new InvalidOperationError(
16+
'I can only handle 0 or 1 numbers for now!'
17+
);
18+
}
19+
}
20+
21+
return {
22+
parseAndSum,
23+
};
24+
}
25+
26+
module.exports = simpleParser;

0 commit comments

Comments
 (0)