time-to-botec

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

main.js (2610B)


      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( '@stdlib/assert/is-string' ).isPrimitive;
     24 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     25 var nextGraphemeClusterBreak = require( './../../next-grapheme-cluster-break' );
     26 var format = require( './../../format' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Removes the first character(s) of a string.
     33 *
     34 * @param {string} str - input string
     35 * @param {NonNegativeInteger} [n=1] - number of characters to remove
     36 * @throws {TypeError} must provide a string primitive
     37 * @throws {TypeError} second argument must be a nonnegative integer
     38 * @returns {string} updated string
     39 *
     40 * @example
     41 * var out = removeFirst( 'last man standing' );
     42 * // returns 'ast man standing'
     43 *
     44 * @example
     45 * var out = removeFirst( 'presidential election' );
     46 * // returns 'residential election'
     47 *
     48 * @example
     49 * var out = removeFirst( 'javaScript' );
     50 * // returns 'avaScript'
     51 *
     52 * @example
     53 * var out = removeFirst( 'Hidden Treasures' );
     54 * // returns 'idden Treasures'
     55 *
     56 * @example
     57 * var out = removeFirst( '🐶🐮🐷🐰🐸', 2 );
     58 * // returns '🐷🐰🐸'
     59 *
     60 * @example
     61 * var out = removeFirst( 'foo bar', 4 );
     62 * // returns 'bar'
     63 */
     64 function removeFirst( str, n ) {
     65 	var nextBreak;
     66 	if ( !isString( str ) ) {
     67 		throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
     68 	}
     69 	if ( arguments.length > 1 ) {
     70 		if ( !isNonNegativeInteger( n ) ) {
     71 			throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) );
     72 		}
     73 		nextBreak = 0;
     74 		while ( n > 0 ) {
     75 			nextBreak = nextGraphemeClusterBreak( str, nextBreak );
     76 			n -= 1;
     77 		}
     78 	}
     79 	else {
     80 		nextBreak = nextGraphemeClusterBreak( str );
     81 	}
     82 	// Value of `nextBreak` will be -1 if and only if `str` is an empty string or `str` has only 1 extended grapheme cluster...
     83 	if ( str === '' || nextBreak === -1 ) {
     84 		return '';
     85 	}
     86 	return str.substring( nextBreak );
     87 }
     88 
     89 
     90 // EXPORTS //
     91 
     92 module.exports = removeFirst;