[6a3a178] | 1 | 'use strict';
|
---|
| 2 | module.exports = {
|
---|
| 3 | wrap: wrapRange,
|
---|
| 4 | limit: limitRange,
|
---|
| 5 | validate: validateRange,
|
---|
| 6 | test: testRange,
|
---|
| 7 | curry: curry,
|
---|
| 8 | name: name
|
---|
| 9 | };
|
---|
| 10 |
|
---|
| 11 | function wrapRange(min, max, value) {
|
---|
| 12 | var maxLessMin = max - min;
|
---|
| 13 | return ((value - min) % maxLessMin + maxLessMin) % maxLessMin + min;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | function limitRange(min, max, value) {
|
---|
| 17 | return Math.max(min, Math.min(max, value));
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | function validateRange(min, max, value, minExclusive, maxExclusive) {
|
---|
| 21 | if (!testRange(min, max, value, minExclusive, maxExclusive)) {
|
---|
| 22 | throw new Error(value + ' is outside of range [' + min + ',' + max + ')');
|
---|
| 23 | }
|
---|
| 24 | return value;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | function testRange(min, max, value, minExclusive, maxExclusive) {
|
---|
| 28 | return !(
|
---|
| 29 | value < min ||
|
---|
| 30 | value > max ||
|
---|
| 31 | (maxExclusive && (value === max)) ||
|
---|
| 32 | (minExclusive && (value === min))
|
---|
| 33 | );
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | function name(min, max, minExcl, maxExcl) {
|
---|
| 37 | return (minExcl ? '(' : '[') + min + ',' + max + (maxExcl ? ')' : ']');
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | function curry(min, max, minExclusive, maxExclusive) {
|
---|
| 41 | var boundNameFn = name.bind(null, min, max, minExclusive, maxExclusive);
|
---|
| 42 | return {
|
---|
| 43 | wrap: wrapRange.bind(null, min, max),
|
---|
| 44 | limit: limitRange.bind(null, min, max),
|
---|
| 45 | validate: function(value) {
|
---|
| 46 | return validateRange(min, max, value, minExclusive, maxExclusive);
|
---|
| 47 | },
|
---|
| 48 | test: function(value) {
|
---|
| 49 | return testRange(min, max, value, minExclusive, maxExclusive);
|
---|
| 50 | },
|
---|
| 51 | toString: boundNameFn,
|
---|
| 52 | name: boundNameFn
|
---|
| 53 | };
|
---|
| 54 | }
|
---|