Clamp an untrusted coordinate into the normalized 0..1 range, or reject it.
(value: unknown)
| 9 | |
| 10 | /** Clamp an untrusted coordinate into the normalized 0..1 range, or reject it. */ |
| 11 | function toUnit(value: unknown): number | null { |
| 12 | return typeof value === 'number' && Number.isFinite(value) |
| 13 | ? Math.min(Math.max(value, 0), 1) |
| 14 | : null |
| 15 | } |
| 16 | |
| 17 | function send(peer: { send: (data: string) => void }, msg: ServerMessage) { |
| 18 | peer.send(JSON.stringify(msg)) |