time-to-botec

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

main.js (3373B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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 splitGraphemeClusters = require( './../../split-grapheme-clusters' );
     25 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     26 var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
     27 var replace = require( './../../replace' );
     28 var rescape = require( '@stdlib/utils/escape-regexp-string' );
     29 var format = require( './../../format' );
     30 
     31 
     32 // VARIABLES //
     33 
     34 var WHITESPACE_CHARS = '\u0020\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Trims `n` characters from the end of a string.
     41 *
     42 * @param {string} str - input string
     43 * @param {NonNegativeInteger} n - number of characters to trim
     44 * @param {(string|StringArray)} [chars] - characters to trim (defaults to whitespace characters)
     45 * @throws {TypeError} first argument must be a string
     46 * @throws {TypeError} second argument must be a nonnegative integer
     47 * @throws {TypeError} third argument must be a string or an array of strings
     48 * @returns {string} trimmed string
     49 *
     50 * @example
     51 * var str = '   abc   ';
     52 * var out = rtrimN( str, 2 );
     53 * // returns '   abc '
     54 *
     55 * @example
     56 * var str = '   abc   ';
     57 * var out = rtrimN( str, str.length );
     58 * // returns '   abc'
     59 *
     60 * @example
     61 * var str = '~~abc!~~';
     62 * var out = rtrimN( str, str.length, [ '~', '!' ] );
     63 * // returns '~~abc'
     64 *
     65 * @example
     66 * var str = '🤖👨🏼‍🎨🤖👨🏼‍🎨🤖👨🏼‍🎨';
     67 * var out = rtrimN( str, str.length, '👨🏼‍🎨🤖' );
     68 * // returns ''
     69 */
     70 function rtrimN( str, n, chars ) {
     71 	var nElems;
     72 	var reStr;
     73 	var isStr;
     74 	var RE;
     75 	var i;
     76 	if ( !isString( str ) ) {
     77 		throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', str ) );
     78 	}
     79 	if ( !isNonNegativeInteger( n ) ) {
     80 		throw new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', n ) );
     81 	}
     82 	if ( arguments.length > 2 ) {
     83 		isStr = isString( chars );
     84 		if ( !isStr && !isStringArray( chars ) ) {
     85 			throw new TypeError( format( 'invalid argument. Must provide a string or an array of strings. Value: `%s`.', chars ) );
     86 		}
     87 		if ( isStr ) {
     88 			chars = splitGraphemeClusters( chars );
     89 		}
     90 		nElems = chars.length - 1;
     91 		reStr = '';
     92 		for ( i = 0; i < nElems; i++ ) {
     93 			reStr += rescape( chars[ i ] );
     94 			reStr += '|';
     95 		}
     96 		reStr += rescape( chars[ nElems ] );
     97 
     98 		// Case: Trim a specific set of characters from the end of a string..
     99 		RE = new RegExp( '(?:' + reStr + '){0,'+n+'}$' );
    100 	} else {
    101 		// Case: Trim `n` whitespace characters from the end of a string...
    102 		RE = new RegExp( '[' + WHITESPACE_CHARS + ']{0,'+n+'}$' );
    103 	}
    104 	return replace( str, RE, '' );
    105 }
    106 
    107 
    108 // EXPORTS //
    109 
    110 module.exports = rtrimN;