1function constrain(value, low, high) { 2 if (value < low) 3 return low; 4 if (value > high) 5 return high; 6 return value; 7} 8 9function mapRange(value, start1, stop1, start2, stop2) { 10 return (value - start1) / (stop1 - start1) * (stop2 - start2) + start2; 11} 12 13function percentile(values, percentile) { 14 var cutoff = values.length * percentile; 15 return values.slice(cutoff, cutoff + 1)[0]; 16} 17