-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathPlugin.js
More file actions
34 lines (26 loc) · 978 Bytes
/
MathPlugin.js
File metadata and controls
34 lines (26 loc) · 978 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
29
30
31
32
33
34
const lodash = require('lodash');
module.exports = function MathPlugin(opts) {
var seneca = this;
seneca.add('role:math,cmd:sum,ver:1', sum_v1);
seneca.add('role:math,cmd:product,ver:1', product_v1);
return { name: 'MathPlugin' };
// ------------------------------------------------------------------------
function sum_v1(args, done) {
validate(args);
done({ sum: args.a + args.b });
}
function product_v1(args, done) {
validate(args);
done({ product: args.a * args.b });
}
function validate(args) {
if(!lodash.has(args, 'a'))
throw new Error("Missing argument 'a'");
else if(typeof args.a !== 'number')
throw new Error("Wrong type for argument 'a'");
else if(!lodash.has(args, 'b'))
throw new Error("Missing argument 'b'");
else if(typeof args.b !== 'number')
throw new Error("Wrong type for argument 'b'");
}
}