-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathfp-namespace.js
More file actions
31 lines (27 loc) · 865 Bytes
/
fp-namespace.js
File metadata and controls
31 lines (27 loc) · 865 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 validatePoint = (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}`));
return errors;
};
const point = {
create: (x, y) => {
const errors = validatePoint(x, y);
if (errors.length > 0) {
const cause = new AggregateError(errors, 'Validation');
throw new RangeError('Bad coordinates', { cause });
}
return Object.freeze({ x, y });
},
move: ({ x, y }, dx, dy) => ({ x: x + dx, y: y + dy }),
clone: ({ x, y }) => point.create(x, y),
toString: ({ x, y }) => `(${x}, ${y})`,
};
// Usage
const p1 = point.create(10, 20);
console.log(point.toString(p1));
const c0 = point.clone(p1);
console.log(point.toString(c0));
const c1 = point.move(p1, -5, 10);
console.log(point.toString(c1));