Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Apr 20, 2024
1 parent 78c687b commit e75c13a
Show file tree
Hide file tree
Showing 11 changed files with 816 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Adarsh Palaskar <[email protected]>
Aditya Sapra <[email protected]>
AgPriyanshu18 <[email protected]>
Aleksandr <[email protected]>
Ali Salesi <[email protected]>
Aman Bhansali <[email protected]>
Amit Jimiwal <[email protected]>
Expand Down Expand Up @@ -69,8 +70,10 @@ Seyyed Parsa Neshaei <[email protected]>
Shashank Shekhar Singh <[email protected]>
Shivam <[email protected]>
Shraddheya Shendre <[email protected]>
Shubh Mehta <[email protected]>
Shubham Mishra <[email protected]>
Snehil Shah <[email protected]>
Soumajit Chatterjee <[email protected]>
Spandan Barve <[email protected]>
Stephannie Jiménez Gacha <[email protected]>
Suraj kumar <[email protected]>
Expand All @@ -83,4 +86,5 @@ Yernar Yergaziyev <[email protected]>
naveen <[email protected]>
nishant-s7 <[email protected]>
orimiles5 <[email protected]>
rainn <[email protected]>
rei2hu <[email protected]>
146 changes: 146 additions & 0 deletions base/for-each-code-point-right/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!--
@license Apache-2.0
Copyright (c) 2024 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.
-->

# forEachCodePointRight

> Invokes a function for each Unicode code point in a string, iterating from right to left.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var forEachCodePointRight = require( '@stdlib/string/base/for-each-code-point-right' );
```

#### forEachCodePointRight( str, clbk\[, thisArg ] )

Invokes a function for each Unicode code point in a string, iterating from right to left.

```javascript
function log( value, index ) {
console.log( '%d: %s', index, value );
}

forEachCodePointRight( 'Beep!', log );
/* =>
4: !
3: p
2: e
1: e
0: B
*/
```

The invoked function is provided three arguments:

- **value**: Unicode code point.
- **index**: starting Unicode code point index.
- **str**: input string.

To set the function execution context, provide a `thisArg`.

```javascript
function clbk() {
this.count += 1;
}

var str = '👉🏿';

var ctx = {
'count': 0
};

forEachCodePointRight( str, clbk, ctx );

var cnt = ctx.count;
// returns 2
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var forEachCodePointRight = require( '@stdlib/string/base/for-each-code-point-right' );

function log( value, index ) {
console.log( '%d: %s', index, value );
}

forEachCodePointRight( 'presidential election', log );
forEachCodePointRight( 'Iñtërnâtiônàlizætiøn', log );
forEachCodePointRight( '🌷🍕', log );
forEachCodePointRight( '\uD834\uDD1E', log );
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
61 changes: 61 additions & 0 deletions base/for-each-code-point-right/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var forEachRight = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var out;
var i;

values = [
'Iñtërnâtiônàlizætiøn',
'presidential election',
'🐶🐮🐷🐰🐸'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = forEachRight( values[ i%values.length ], clbk );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( out ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();

function clbk( v ) {
if ( typeof v !== 'string' ) {
b.fail( 'unexpected value' );
}
}
});
38 changes: 38 additions & 0 deletions base/for-each-code-point-right/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

{{alias}}( str, clbk[, thisArg] )
Invokes a function for each Unicode code point in a string, iterating from
right to left.

When invoked, the provided function is provided three arguments:

- value: code point
- index: starting code point index
- str: input string

Parameters
----------
str: string
Input string over which to iterate.

clbk: Function
The function to invoke for each Unicode code point in the input string.

thisArg: any (optional)
Execution context.

Returns
-------
out: string
Input string.

Examples
--------
> var n = 0;
> function fcn() { n += 1; };
> {{alias}}( 'hello world!', fcn );
> n
12

See Also
--------

93 changes: 93 additions & 0 deletions base/for-each-code-point-right/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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.
*/

// TypeScript Version: 4.1

/**
* Callback invoked for each Unicode code point in a string.
*
* @returns result
*/
type Nullary<T> = ( this: T ) => any;

/**
* Callback invoked for each Unicode code point in a string.
*
* @param value - code point
* @returns result
*/
type Unary<T> = ( this: T, value: string ) => any;

/**
* Callback invoked for each Unicode code point in a string.
*
* @param value - code point
* @param index - starting code point index
* @returns result
*/
type Binary<T> = ( this: T, value: string, index: number ) => any;

/**
* Callback invoked for each Unicode code point in a string.
*
* @param value - code point
* @param index - starting code point index
* @param str - input string
* @returns result
*/
type Ternary<T> = ( this: T, value: string, index: number, str: string ) => any;

/**
* Callback invoked for each Unicode code point in a string.
*
* @param value - code point
* @param index - starting code point index
* @param str - input string
* @returns result
*/
type Callback<T> = Nullary<T> | Unary<T> | Binary<T> | Ternary<T>;

/**
* Invokes a function for each Unicode code point in a string, iterating from right to left.
*
* ## Notes
*
* - When invoked, the provided function is provided three arguments:
*
* - **value**: code point.
* - **index**: starting code point index.
* - **str**: input string.
*
* @param str - input string
* @param clbk - function to invoke
* @param thisArg - execution context
* @returns input string
*
* @example
* function log( value, index ) {
* console.log( '%d: %s', index, value );
* }
*
* forEachRight( 'Hello, World!', log );
*/
declare function forEachRight<T = unknown>( str: string, clbk: Callback<T>, thisArg?: ThisParameterType<Callback<T>> ): string;


// EXPORTS //

export = forEachRight;
Loading

0 comments on commit e75c13a

Please sign in to comment.