Skip to content

Commit f810453

Browse files
committed
refactor: apply suggestions from code review
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent fa73af6 commit f810453

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/docs/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
* // returns '👋👋'
3535
*
3636
* out = sliceGraphemeClusters( 'अनुच्छेद', 1, 3 );
37-
* // returns 'नु'
37+
* // returns 'नुच्'
3838
*
3939
* out = sliceGraphemeClusters( 'Hello World', -5, -1 );
40-
* // returns 'World'
40+
* // returns 'Worl'
4141
*/
4242
declare function sliceGraphemeClusters( str: string, start: number, end: number ): string;
4343

lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/lib/main.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var max = require( '@stdlib/math/base/special/fast/max' );
2828
// MAIN //
2929

3030
/**
31-
* Slice a string based on grapheme cluster (i.e., user-perceived character) indices.
31+
* Slices a string based on grapheme cluster (i.e., user-perceived character) indices.
3232
*
3333
* @param {string} str - input string
3434
* @param {integer} start - the `ith` grapheme cluster to start a slice (inclusive)
@@ -58,47 +58,36 @@ function sliceGraphemeClusters( str, start, end ) {
5858
if ( str === '' ) {
5959
return '';
6060
}
61-
6261
numClusters = numGraphemeClusters( str );
63-
6462
if ( start < 0 ) {
6563
start = max( start + numClusters, 0 );
6664
}
67-
6865
if ( end < 0 ) {
6966
end = max( end + numClusters, 0 );
7067
}
71-
7268
if ( start >= numClusters || start >= end ) {
7369
return '';
7470
}
75-
7671
if ( end > numClusters ) {
7772
end = numClusters;
7873
}
79-
8074
result = '';
8175
idx = 0;
8276
i = 0;
83-
8477
while ( idx < str.length ) {
8578
brk = nextGraphemeClusterBreak( str, idx );
8679
if ( brk === -1 ) {
8780
brk = str.length;
8881
}
89-
9082
if ( i >= start && i < end ) {
9183
result += str.substring( idx, brk );
9284
}
93-
9485
idx = brk;
9586
i += 1;
96-
9787
if ( i >= end ) {
9888
break;
9989
}
10090
}
101-
10291
return result;
10392
}
10493

lib/node_modules/@stdlib/string/base/slice-grapheme-clusters/test/test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ tape( 'the function returns an empty string if provided an empty input string',
4545
t.end();
4646
});
4747

48-
tape( 'the function returns an empty string if starting index is greater than or equal to ending index', function test( t ) {
48+
tape( 'the function returns an empty string if the starting index is greater than or equal to the ending index', function test( t ) {
4949
var out;
5050

5151
out = sliceGraphemeClusters( 'hello', 2, 2 );
@@ -57,7 +57,7 @@ tape( 'the function returns an empty string if starting index is greater than or
5757
t.end();
5858
});
5959

60-
tape( 'the function returns an empty string if starting index is greater than or equal to string length', function test( t ) {
60+
tape( 'the function returns an empty string if the starting index is greater than or equal to the string length', function test( t ) {
6161
var out;
6262

6363
out = sliceGraphemeClusters( 'hello', 5, 6 );
@@ -90,6 +90,21 @@ tape( 'the function slices an input string based on grapheme cluster indices', f
9090
t.end();
9191
});
9292

93+
tape('the function slices an input string based on grapheme cluster indices (skin-tone emojis)', function test(t) {
94+
var out;
95+
96+
out = sliceGraphemeClusters('🌷👨‍👩‍👧‍👦👉🏿', 1, 2);
97+
t.strictEqual(out, '👨‍👩‍👧‍👦', 'returns expected value');
98+
99+
out = sliceGraphemeClusters('🏝️👨‍👩‍👧‍👦', 1, 2);
100+
t.strictEqual(out, '👨‍👩‍👧‍👦', 'returns expected value');
101+
102+
out = sliceGraphemeClusters('👋🏾🤦🏽‍♀️🧑🏿', 1, 2);
103+
t.strictEqual(out, '🤦🏽‍♀️', 'returns expected value');
104+
105+
t.end();
106+
});
107+
93108
tape( 'the function supports providing negative indices', function test( t ) {
94109
var out;
95110

@@ -111,6 +126,21 @@ tape( 'the function supports providing negative indices', function test( t ) {
111126
t.end();
112127
});
113128

129+
tape('the function supports providing negative indices (skin-tone emojis)', function test(t) {
130+
var out;
131+
132+
out = sliceGraphemeClusters( '🌷👨‍👩‍👧‍👦👉🏿', -2, -1 );
133+
t.strictEqual( out, '👨‍👩‍👧‍👦', 'returns expected value' );
134+
135+
out = sliceGraphemeClusters( '🏝️👨‍👩‍👧‍👦', -1, 4 );
136+
t.strictEqual( out, '👨‍👩‍👧‍👦', 'returns expected value' );
137+
138+
out = sliceGraphemeClusters( '👋🏾🤦🏽‍♀️🧑🏿‍🦱', -2, -1 );
139+
t.strictEqual( out, '🤦🏽‍♀️', 'returns expected value' );
140+
141+
t.end();
142+
});
143+
114144
tape( 'the function truncates the end index to the string length', function test( t ) {
115145
var out;
116146

0 commit comments

Comments
 (0)