time-to-botec

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

starts_with.js (3014B)


      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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
     24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     25 var format = require( './../../format' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Tests if a string starts with the characters of another string.
     32 *
     33 * @param {string} str - input string
     34 * @param {string} search - search string
     35 * @param {integer} [position=0] - position at which to start searching
     36 * @throws {TypeError} first argument must be a string
     37 * @throws {TypeError} second argument must be a string
     38 * @throws {TypeError} third argument must be an integer
     39 * @returns {boolean} boolean indicating if the input string starts with the search string
     40 *
     41 * @example
     42 * var bool = startsWith( 'Remember the story I used to tell you when you were a boy?', 'Remember' );
     43 * // returns true
     44 *
     45 * @example
     46 * var bool = startsWith( 'Remember the story I used to tell you when you were a boy?', 'Remember, remember' );
     47 * // returns false
     48 *
     49 * @example
     50 * var bool = startsWith( 'To be, or not to be, that is the question.', 'To be' );
     51 * // returns true
     52 *
     53 * @example
     54 * var bool = startsWith( 'To be, or not to be, that is the question.', 'to be' );
     55 * // returns false
     56 *
     57 * @example
     58 * var bool = startsWith( 'To be, or not to be, that is the question.', 'to be', 14 );
     59 * // returns true
     60 *
     61 * @example
     62 * var bool = startsWith( 'To be, or not to be, that is the question.', 'quest', -9 );
     63 * // returns true
     64 */
     65 function startsWith( str, search, position ) {
     66 	var pos;
     67 	var i;
     68 	if ( !isString( str ) ) {
     69 		throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
     70 	}
     71 	if ( !isString( search ) ) {
     72 		throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', search ) );
     73 	}
     74 	if ( arguments.length > 2 ) {
     75 		if ( !isInteger( position ) ) {
     76 			throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', position ) );
     77 		}
     78 		if ( position < 0 ) {
     79 			pos = str.length + position;
     80 		} else {
     81 			pos = position;
     82 		}
     83 	} else {
     84 		pos = 0;
     85 	}
     86 	if ( search.length === 0 ) {
     87 		return true;
     88 	}
     89 	if (
     90 		pos < 0 ||
     91 		pos + search.length > str.length
     92 	) {
     93 		return false;
     94 	}
     95 	for ( i = 0; i < search.length; i++ ) {
     96 		if ( str.charCodeAt( pos + i ) !== search.charCodeAt( i ) ) {
     97 			return false;
     98 		}
     99 	}
    100 	return true;
    101 }
    102 
    103 
    104 // EXPORTS //
    105 
    106 module.exports = startsWith;