-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathb-adder-on.js
More file actions
31 lines (27 loc) · 540 Bytes
/
Copy pathb-adder-on.js
File metadata and controls
31 lines (27 loc) · 540 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
'use strict';
const adder = (a) => {
let onZerro = null;
const obj = {};
const value = () => a;
const add = (b) => {
let x = a + b;
if (x < 0) {
x = 0;
if (onZerro) onZerro();
}
return adder(x);
};
const on = (name, callback) => {
if (name === 'zero') onZerro = callback;
return obj;
};
return Object.assign(obj, { add, value, on });
};
// Usage
const res = adder(3)
.on('zero', () => console.log('Less than zero'))
.add(-9)
.add(12)
.add(5)
.value();
console.log({ res });