time-to-botec

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

main.js (1695B)


      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 // MODULES //
     22 
     23 var isString = require( './../../is-string' ).isPrimitive;
     24 
     25 
     26 // VARIABLES //
     27 
     28 // Character codes:
     29 var ZERO = 48;
     30 var NINE = 57;
     31 var A = 65;
     32 var F = 70;
     33 var a = 97;
     34 var f = 102;
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Tests whether a string contains only hexadecimal digits.
     41 *
     42 * @param {*} x - value to test
     43 * @returns {boolean} boolean indicating if a string contains only hexadecimal digits
     44 *
     45 * @example
     46 * var out = isHexString( '0123456789abcdefABCDEF' );
     47 * // returns true
     48 *
     49 * @example
     50 * var out = isHexString( '0xffffff' );
     51 * // returns false
     52 *
     53 * @example
     54 * var out = isHexString( '' );
     55 * // returns false
     56 *
     57 * @example
     58 * var out = isHexString( 123 );
     59 * // returns false
     60 */
     61 function isHexString( x ) {
     62 	var len;
     63 	var ch;
     64 	var i;
     65 	if ( !isString( x ) ) {
     66 		return false;
     67 	}
     68 	len = x.length;
     69 	if ( !len ) {
     70 		return false;
     71 	}
     72 	for ( i = 0; i < len; i++ ) {
     73 		ch = x.charCodeAt( i );
     74 		if (
     75 			ch < ZERO ||
     76 			( ch > NINE && ch < A ) ||
     77 			( ch > F && ch < a ) ||
     78 			ch > f
     79 		) {
     80 			return false;
     81 		}
     82 	}
     83 	return true;
     84 }
     85 
     86 
     87 // EXPORTS //
     88 
     89 module.exports = isHexString;