[javascript] What's the most elegant way to cap a number to a segment?