Skip to content

Commit 91f84da

Browse files
feat: add string/base/for-each-code-point-right
PR-URL: #2163 Ref: #856 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 753fbf2 commit 91f84da

File tree

10 files changed

+812
-0
lines changed

10 files changed

+812
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# forEachCodePointRight
22+
23+
> Invokes a function for each Unicode code point in a string, iterating from right to left.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var forEachCodePointRight = require( '@stdlib/string/base/for-each-code-point-right' );
41+
```
42+
43+
#### forEachCodePointRight( str, clbk\[, thisArg ] )
44+
45+
Invokes a function for each Unicode code point in a string, iterating from right to left.
46+
47+
```javascript
48+
function log( value, index ) {
49+
console.log( '%d: %s', index, value );
50+
}
51+
52+
forEachCodePointRight( 'Beep!', log );
53+
/* =>
54+
4: !
55+
3: p
56+
2: e
57+
1: e
58+
0: B
59+
*/
60+
```
61+
62+
The invoked function is provided three arguments:
63+
64+
- **value**: Unicode code point.
65+
- **index**: starting Unicode code point index.
66+
- **str**: input string.
67+
68+
To set the function execution context, provide a `thisArg`.
69+
70+
```javascript
71+
function clbk() {
72+
this.count += 1;
73+
}
74+
75+
var str = '👉🏿';
76+
77+
var ctx = {
78+
'count': 0
79+
};
80+
81+
forEachCodePointRight( str, clbk, ctx );
82+
83+
var cnt = ctx.count;
84+
// returns 2
85+
```
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
92+
93+
<section class="notes">
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<!-- Package usage examples. -->
100+
101+
<section class="examples">
102+
103+
## Examples
104+
105+
<!-- eslint no-undef: "error" -->
106+
107+
```javascript
108+
var forEachCodePointRight = require( '@stdlib/string/base/for-each-code-point-right' );
109+
110+
function log( value, index ) {
111+
console.log( '%d: %s', index, value );
112+
}
113+
114+
forEachCodePointRight( 'presidential election', log );
115+
forEachCodePointRight( 'Iñtërnâtiônàlizætiøn', log );
116+
forEachCodePointRight( '🌷🍕', log );
117+
forEachCodePointRight( '\uD834\uDD1E', log );
118+
```
119+
120+
</section>
121+
122+
<!-- /.examples -->
123+
124+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="references">
127+
128+
</section>
129+
130+
<!-- /.references -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
</section>
145+
146+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var forEachRight = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'Iñtërnâtiônàlizætiøn',
38+
'presidential election',
39+
'🐶🐮🐷🐰🐸'
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = forEachRight( values[ i%values.length ], clbk );
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( out ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
56+
function clbk( v ) {
57+
if ( typeof v !== 'string' ) {
58+
b.fail( 'unexpected value' );
59+
}
60+
}
61+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( str, clbk[, thisArg] )
3+
Invokes a function for each Unicode code point in a string, iterating from
4+
right to left.
5+
6+
When invoked, the provided function is provided three arguments:
7+
8+
- value: code point
9+
- index: starting code point index
10+
- str: input string
11+
12+
Parameters
13+
----------
14+
str: string
15+
Input string over which to iterate.
16+
17+
clbk: Function
18+
The function to invoke for each Unicode code point in the input string.
19+
20+
thisArg: any (optional)
21+
Execution context.
22+
23+
Returns
24+
-------
25+
out: string
26+
Input string.
27+
28+
Examples
29+
--------
30+
> var n = 0;
31+
> function fcn() { n += 1; };
32+
> {{alias}}( 'hello world!', fcn );
33+
> n
34+
12
35+
36+
See Also
37+
--------
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Callback invoked for each Unicode code point in a string.
23+
*
24+
* @returns result
25+
*/
26+
type Nullary<T> = ( this: T ) => any;
27+
28+
/**
29+
* Callback invoked for each Unicode code point in a string.
30+
*
31+
* @param value - code point
32+
* @returns result
33+
*/
34+
type Unary<T> = ( this: T, value: string ) => any;
35+
36+
/**
37+
* Callback invoked for each Unicode code point in a string.
38+
*
39+
* @param value - code point
40+
* @param index - starting code point index
41+
* @returns result
42+
*/
43+
type Binary<T> = ( this: T, value: string, index: number ) => any;
44+
45+
/**
46+
* Callback invoked for each Unicode code point in a string.
47+
*
48+
* @param value - code point
49+
* @param index - starting code point index
50+
* @param str - input string
51+
* @returns result
52+
*/
53+
type Ternary<T> = ( this: T, value: string, index: number, str: string ) => any;
54+
55+
/**
56+
* Callback invoked for each Unicode code point in a string.
57+
*
58+
* @param value - code point
59+
* @param index - starting code point index
60+
* @param str - input string
61+
* @returns result
62+
*/
63+
type Callback<T> = Nullary<T> | Unary<T> | Binary<T> | Ternary<T>;
64+
65+
/**
66+
* Invokes a function for each Unicode code point in a string, iterating from right to left.
67+
*
68+
* ## Notes
69+
*
70+
* - When invoked, the provided function is provided three arguments:
71+
*
72+
* - **value**: code point.
73+
* - **index**: starting code point index.
74+
* - **str**: input string.
75+
*
76+
* @param str - input string
77+
* @param clbk - function to invoke
78+
* @param thisArg - execution context
79+
* @returns input string
80+
*
81+
* @example
82+
* function log( value, index ) {
83+
* console.log( '%d: %s', index, value );
84+
* }
85+
*
86+
* forEachRight( 'Hello, World!', log );
87+
*/
88+
declare function forEachRight<T = unknown>( str: string, clbk: Callback<T>, thisArg?: ThisParameterType<Callback<T>> ): string;
89+
90+
91+
// EXPORTS //
92+
93+
export = forEachRight;

0 commit comments

Comments
 (0)