main.js (3547B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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' ).isPrimitive; 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 next extended grapheme cluster break in a string after a specified position. 42 * 43 * @param {string} str - input string 44 * @param {integer} [fromIndex=0] - position 45 * @throws {TypeError} first argument must be a string 46 * @throws {TypeError} second argument must be an integer 47 * @returns {NonNegativeInteger} next grapheme break position 48 * 49 * @example 50 * var out = nextGraphemeClusterBreak( 'last man standing', 4 ); 51 * // returns 5 52 * 53 * @example 54 * var out = nextGraphemeClusterBreak( 'presidential election', 8 ); 55 * // returns 9 56 * 57 * @example 58 * var out = nextGraphemeClusterBreak( 'अनुच्छेद', 1 ); 59 * // returns 3 60 * 61 * @example 62 * var out = nextGraphemeClusterBreak( '🌷' ); 63 * // returns -1 64 */ 65 function nextGraphemeClusterBreak( str, fromIndex ) { 66 var breaks; 67 var emoji; 68 var len; 69 var idx; 70 var cp; 71 var i; 72 73 if ( !isString( str ) ) { 74 throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); 75 } 76 if ( arguments.length > 1 ) { 77 if ( !isInteger( fromIndex ) ) { 78 throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); 79 } 80 idx = fromIndex; 81 } else { 82 idx = 0; 83 } 84 len = str.length; 85 if ( idx < 0 ) { 86 idx += len; 87 if ( idx < 0 ) { 88 idx = 0; 89 } 90 } 91 if ( len === 0 || idx >= len ) { 92 return -1; 93 } 94 // Initialize caches for storing grapheme break and emoji properties: 95 breaks = []; 96 emoji = []; 97 98 // Get the code point for the starting index: 99 cp = codePointAt( str, idx ); 100 101 // Get the corresponding grapheme break and emoji properties: 102 breaks.push( breakProperty( cp ) ); 103 emoji.push( emojiProperty( cp ) ); 104 105 // Begin searching for the next grapheme cluster break... 106 for ( i = idx+1; i < len; i++ ) { 107 // If the current character is part of a surrogate pair, move along... 108 if ( hasUTF16SurrogatePairAt( str, i-1 ) ) { 109 continue; 110 } 111 // Get the next code point: 112 cp = codePointAt( str, i ); 113 114 // Get the corresponding grapheme break and emoji properties: 115 breaks.push( breakProperty( cp ) ); 116 emoji.push( emojiProperty( cp ) ); 117 118 // Determine if we've encountered a grapheme cluster break... 119 if ( breakType( breaks, emoji ) > 0 ) { 120 // We've found a break! 121 return i; 122 } 123 } 124 // Unable to find a grapheme cluster break: 125 return -1; 126 } 127 128 129 // EXPORTS // 130 131 module.exports = nextGraphemeClusterBreak;