source: trip-planner-front/node_modules/normalize-range/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use strict';
2module.exports = {
3 wrap: wrapRange,
4 limit: limitRange,
5 validate: validateRange,
6 test: testRange,
7 curry: curry,
8 name: name
9};
10
11function wrapRange(min, max, value) {
12 var maxLessMin = max - min;
13 return ((value - min) % maxLessMin + maxLessMin) % maxLessMin + min;
14}
15
16function limitRange(min, max, value) {
17 return Math.max(min, Math.min(max, value));
18}
19
20function 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
27function 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
36function name(min, max, minExcl, maxExcl) {
37 return (minExcl ? '(' : '[') + min + ',' + max + (maxExcl ? ')' : ']');
38}
39
40function 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}
Note: See TracBrowser for help on using the repository browser.