time-to-botec

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

main.js (3504B)


      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 isInteger = require( '@stdlib/assert/is-integer' );
     25 var codePointAt = require( './../../code-point-at' );
     26 var hasUTF16SurrogatePairAt = require( '@stdlib/assert/has-utf16-surrogate-pair-at' );
     27 var grapheme = require( './../../tools/grapheme-cluster-break' );
     28 var format = require( './../../format' );
     29 
     30 
     31 // VARIABLES //
     32 
     33 var breakType = grapheme.breakType;
     34 var breakProperty = grapheme.breakProperty;
     35 var emojiProperty = grapheme.emojiProperty;
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Returns the previous extended grapheme cluster break in a string before a specified position.
     42 *
     43 * @param {string} str - input string
     44 * @param {integer} [fromIndex=str.length-1] - position
     45 * @throws {TypeError} first argument must be a string
     46 * @throws {TypeError} second argument must be an integer
     47 * @returns {NonNegativeInteger} previous grapheme break position
     48 *
     49 * @example
     50 * var out = prevGraphemeClusterBreak( 'last man standing', 4 );
     51 * // returns 3
     52 *
     53 * @example
     54 * var out = prevGraphemeClusterBreak( 'presidential election', 8 );
     55 * // returns 7
     56 *
     57 * @example
     58 * var out = prevGraphemeClusterBreak( 'अनुच्छेद', 2 );
     59 * // returns 0
     60 *
     61 * @example
     62 * var out = prevGraphemeClusterBreak( '🌷', 1 );
     63 * // returns -1
     64 */
     65 function prevGraphemeClusterBreak( str, fromIndex ) {
     66 	var breaks;
     67 	var emoji;
     68 	var ans;
     69 	var len;
     70 	var idx;
     71 	var cp;
     72 	var i;
     73 
     74 	if ( !isString( str ) ) {
     75 		throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
     76 	}
     77 	len = str.length;
     78 	if ( arguments.length > 1 ) {
     79 		if ( !isInteger( fromIndex ) ) {
     80 			throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
     81 		}
     82 		idx = fromIndex;
     83 	} else {
     84 		idx = len - 1;
     85 	}
     86 	if ( len === 0 || idx <= 0 ) {
     87 		return -1;
     88 	}
     89 	if ( idx >= len ) {
     90 		idx = len - 1;
     91 	}
     92 
     93 	// Initialize caches for storing grapheme break and emoji properties:
     94 	breaks = [];
     95 	emoji = [];
     96 
     97 	// Get the code point for the starting index:
     98 	cp = codePointAt( str, 0 );
     99 
    100 	// Get the corresponding grapheme break and emoji properties:
    101 	breaks.push( breakProperty( cp ) );
    102 	emoji.push( emojiProperty( cp ) );
    103 
    104 	ans = -1;
    105 	for ( i = 1; i <= idx; i++ ) {
    106 		// If the current character is part of a surrogate pair, move along...
    107 		if ( hasUTF16SurrogatePairAt( str, i-1 ) ) {
    108 			ans = i-2;
    109 			breaks.length = 0;
    110 			emoji.length = 0;
    111 			continue;
    112 		}
    113 		cp = codePointAt( str, i );
    114 
    115 		// Get the corresponding grapheme break and emoji properties:
    116 		breaks.push( breakProperty( cp ) );
    117 		emoji.push( emojiProperty( cp ) );
    118 
    119 		// Determine if we've encountered a grapheme cluster break...
    120 		if ( breakType( breaks, emoji ) > 0 ) {
    121 			ans = i-1;
    122 			breaks.length = 0;
    123 			emoji.length = 0;
    124 			continue;
    125 		}
    126 	}
    127 	return ans;
    128 }
    129 
    130 
    131 // EXPORTS //
    132 
    133 module.exports = prevGraphemeClusterBreak;