All files main.js

100% Statements 97/97
100% Branches 19/19
100% Functions 1/1
100% Lines 97/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 23x 24x 8x 8x 24x 5x 5x 24x 4x 4x 24x 4x 4x 19x 19x 19x 24x 57x 57x 10x 10x 57x 41x 41x 57x 57x 57x 19x 19x 57x 19x 24x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' );
var numGraphemeClusters = require( '@stdlib/string/num-grapheme-clusters' );
var max = require( '@stdlib/math/base/special/fast/max' );
 
 
// MAIN //
 
/**
* Slices a string based on grapheme cluster (i.e., user-perceived character) indices.
*
* @param {string} str - input string
* @param {integer} start - the `ith` grapheme cluster to start a slice (inclusive)
* @param {integer} end - the `jth` grapheme cluster to end a slice (exclusive)
* @returns {string} output string
*
* @example
* var out = sliceGraphemeClusters( 'Hello World', 0, 5 );
* // returns 'Hello'
*
* out = sliceGraphemeClusters( '👋👋👋', 0, 2 );
* // returns '👋👋'
*
* out = sliceGraphemeClusters( 'अनुच्छेद', 1, 3 );
* // returns 'नुच्'
*
* out = sliceGraphemeClusters( 'Hello World', -5, -1 );
* // returns 'Worl'
*/
function sliceGraphemeClusters( str, start, end ) {
	var numClusters;
	var result;
	var idx;
	var brk;
	var i;
 
	if ( str === '' ) {
		return '';
	}
	numClusters = numGraphemeClusters( str );
	if ( start < 0 ) {
		start = max( start + numClusters, 0 );
	}
	if ( end < 0 ) {
		end = max( end + numClusters, 0 );
	}
	if ( start >= numClusters || start >= end ) {
		return '';
	}
	if ( end > numClusters ) {
		end = numClusters;
	}
	result = '';
	idx = 0;
	i = 0;
	while ( idx < str.length ) {
		brk = nextGraphemeClusterBreak( str, idx );
		if ( brk === -1 ) {
			brk = str.length;
		}
		if ( i >= start && i < end ) {
			result += str.substring( idx, brk );
		}
		idx = brk;
		i += 1;
		if ( i >= end ) {
			break;
		}
	}
	return result;
}
 
 
// EXPORTS //
 
module.exports = sliceGraphemeClusters;