README.md (2890B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # isBetween 22 23 > Test if a value is between two values. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var isBetween = require( '@stdlib/assert/is-between' ); 31 ``` 32 33 #### isBetween( value, a, b\[, left, right] ) 34 35 Tests if a `value` is between two values `a` (left comparison value) and `b` (right comparison value). 36 37 ```javascript 38 var bool = isBetween( 4, 3, 5 ); 39 // returns true 40 41 bool = isBetween( 2, 3, 5 ); 42 // returns false 43 44 bool = isBetween( 6, 3, 5 ); 45 // returns false 46 ``` 47 48 By default, the function assumes that `a` and `b` are inclusive. 49 50 ```javascript 51 var bool = isBetween( 3, 3, 5 ); 52 // returns true 53 54 bool = isBetween( 3, 3, 5, 'closed', 'closed' ); 55 // returns true 56 57 bool = isBetween( 5, 3, 5 ); 58 // returns true 59 60 bool = isBetween( 5, 3, 5, 'closed', 'closed' ); 61 // returns true 62 ``` 63 64 To make `a` and/or `b` exclusive, set the respective arguments to `'open'`. 65 66 ```javascript 67 var bool = isBetween( 3, 3, 5, 'open', 'closed' ); 68 // returns false 69 70 bool = isBetween( 5, 3, 5, 'closed', 'open' ); 71 // returns false 72 ``` 73 74 </section> 75 76 <!-- /.usage --> 77 78 <section class="notes"> 79 80 ## Notes 81 82 - If `a` and `b` are inclusive, the comparison is equivalent to 83 84 ```text 85 a <= v <= b 86 ``` 87 88 - If `a` is exclusive and `b` is inclusive, the comparison is equivalent to 89 90 ```text 91 a < v <= b 92 ``` 93 94 - If `a` is inclusive and `b` is exclusive, the comparison is equivalent to 95 96 ```text 97 a <= v < b 98 ``` 99 100 - If `a` and `b` are exclusive, the comparison is equivalent to 101 102 ```text 103 a < v < b 104 ``` 105 106 - If provided non-numeric values, comparisons are performed according to lexicographic order. 107 108 </section> 109 110 <!-- /.notes --> 111 112 <section class="examples"> 113 114 ## Examples 115 116 <!-- eslint no-undef: "error" --> 117 118 ```javascript 119 var randu = require( '@stdlib/random/base/randu' ); 120 var round = require( '@stdlib/math/base/special/round' ); 121 var isBetween = require( '@stdlib/assert/is-between' ); 122 123 var bool; 124 var a; 125 var b; 126 var v; 127 var i; 128 129 for ( i = 0; i < 100; i++ ) { 130 a = round( (randu()*10.0) + 5.0 ); 131 b = round( (randu()*10.0) + 15.0 ); 132 v = round( randu()*25.0 ); 133 bool = isBetween( v, a, b, 'open', 'closed' ); 134 console.log( '%d < %d <= %d: %s', a, v, b, bool.toString() ); 135 } 136 ``` 137 138 </section> 139 140 <!-- /.examples --> 141 142 <section class="links"> 143 144 </section> 145 146 <!-- /.links -->