time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2487B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MAIN //
     22 
     23 /**
     24 * Tests if a value is between two values.
     25 *
     26 * @param {*} value - value to test
     27 * @param {*} a - left comparison value
     28 * @param {*} b - right comparison value
     29 * @param {string} [left="closed"] - indicates whether the left comparison value is inclusive
     30 * @param {string} [right="closed"] - indicates whether the right comparison value is inclusive
     31 * @throws {TypeError} `left` must be a recognized string
     32 * @throws {TypeError} `right` must be a recognized string
     33 * @returns {boolean} boolean indicating whether a value is between two values
     34 *
     35 * @example
     36 * var bool = isBetween( 3.14, 3.0, 4.0 );
     37 * // returns true
     38 *
     39 * @example
     40 * var bool = isBetween( 4.5, 3.0, 4.0 );
     41 * // returns false
     42 *
     43 * @example
     44 * var bool = isBetween( 3.14, 3.14, 4.0 );
     45 * // returns true
     46 *
     47 * @example
     48 * var bool = isBetween( 3.14, 3.14, 4.0, 'open', 'closed' );
     49 * // returns false
     50 *
     51 * @example
     52 * var bool = isBetween( 3.14, 3.0, 3.14 );
     53 * // returns true
     54 *
     55 * @example
     56 * var bool = isBetween( 3.14, 3.0, 3.14, 'closed', 'open' );
     57 * // returns false
     58 */
     59 function isBetween( value, a, b, left, right ) {
     60 	if ( arguments.length > 3 ) {
     61 		if ( left !== 'closed' && left !== 'open' ) {
     62 			throw new TypeError( 'invalid argument. `left` must be one of the following strings: \'closed\' or \'open\'. Value: `'+left+'`.' );
     63 		}
     64 		if ( right !== 'closed' && right !== 'open' ) {
     65 			throw new TypeError( 'invalid argument. `right` must be one of the following strings: \'closed\' or \'open\'. Value: `'+right+'`.' );
     66 		}
     67 	}
     68 	if ( left === 'closed' || left === void 0 ) {
     69 		if ( right === 'closed' || right === void 0 ) {
     70 			return ( value >= a && value <= b );
     71 		}
     72 		return ( value >= a && value < b );
     73 	}
     74 	if ( right === 'closed' || right === void 0 ) {
     75 		return ( value > a && value <= b );
     76 	}
     77 	return ( value > a && value < b );
     78 }
     79 
     80 
     81 // EXPORTS //
     82 
     83 module.exports = isBetween;