time-to-botec

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

win32.js (1842B)


      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 // MAIN //
     27 
     28 /**
     29 * Tests if a value is a Windows absolute path.
     30 *
     31 * @param {*} value - value to test
     32 * @returns {boolean} boolean indicating if a value is a Windows absolute path
     33 *
     34 * @example
     35 * var bool = isAbsolutePath( 'C:\\foo\\bar\\baz' );
     36 * // returns true
     37 *
     38 * @example
     39 * var bool = isAbsolutePath( 'foo\\bar\\baz' );
     40 * // returns false
     41 */
     42 function isAbsolutePath( value ) {
     43 	var code;
     44 	var len;
     45 	if ( !isString( value ) ) {
     46 		return false;
     47 	}
     48 	len = value.length;
     49 	if ( len === 0 ) {
     50 		return false;
     51 	}
     52 	code = value.charCodeAt( 0 );
     53 
     54 	// Check if the string begins with either a forward '/' or backward slash '\\':
     55 	if ( code === 47 || code === 92 ) {
     56 		return true;
     57 	}
     58 	// Check for a device root (e.g., C:\\)...
     59 	if (
     60 		(code >= 65 && code <= 90) || // A-Z
     61 		(code >= 97 && code <= 122)   // a-z
     62 	) {
     63 		// Check if the string has a colon ':' character:
     64 		if ( len > 2 && value.charCodeAt( 1 ) === 58 ) {
     65 			code = value.charCodeAt( 2 );
     66 
     67 			// Check for either a forward or backward slash:
     68 			if ( code === 47 || code === 92 ) {
     69 				return true;
     70 			}
     71 		}
     72 	}
     73 	return false;
     74 }
     75 
     76 
     77 // EXPORTS //
     78 
     79 module.exports = isAbsolutePath;