-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlazy.js
More file actions
39 lines (33 loc) · 838 Bytes
/
lazy.js
File metadata and controls
39 lines (33 loc) · 838 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
35
36
37
38
39
'use strict';
const createPoint = (x, y) => {
const errors = [];
if (!Number.isFinite(x)) errors.push(new TypeError(`Invalid x: ${x}`));
if (!Number.isFinite(y)) errors.push(new TypeError(`Invalid y: ${y}`));
if (errors.length > 0) {
const cause = new AggregateError(errors, 'Validation');
throw new RangeError('Bad coordinates', { cause });
}
return {
x: () => x,
y: () => y,
};
};
const move = (point, dx, dy) => ({
x: () => point.x() + dx,
y: () => point.y() + dy,
});
const clone = (point) => ({
x: () => point.x(),
y: () => point.y(),
});
const toString = (point) => {
const x = point.x();
const y = point.y();
return `(${x}, ${y})`;
};
// Usage
const p1 = createPoint(10, 20);
console.log(toString(p1));
const c1 = clone(p1);
const c2 = move(c1, -5, 10);
console.log(toString(c2));