-
-
Notifications
You must be signed in to change notification settings - Fork 805
feat: add string/base/for-each-right
#1369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Planeshifter
merged 7 commits into
stdlib-js:develop
from
AhmedKhaled590:add-string-base-for-each-right
Feb 25, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
86ed0b5
feat: add @stdlib/string/for-each-right
AhmedKhaled590 ee08dff
fix: change license year from 2023 to 2024
AhmedKhaled590 63ce45c
Update lib/node_modules/@stdlib/string/base/for-each-right/docs/types…
Planeshifter ab8b3a2
Update lib/node_modules/@stdlib/string/base/for-each-right/docs/repl.txt
Planeshifter fed2deb
fix: update function description and docs
AhmedKhaled590 c700a25
style: add trailing newline
AhmedKhaled590 7b5bd1e
Update lib/node_modules/@stdlib/string/base/for-each-right/docs/repl.txt
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
lib/node_modules/@stdlib/string/base/for-each-right/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# forEachRight | ||
|
||
> Invokes a function for each UTF-16 code unit 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 forEachRight = require( '@stdlib/string/base/for-each-right' ); | ||
``` | ||
|
||
#### forEachRight( str, clbk\[, thisArg ] ) | ||
|
||
Invokes a function for each UTF-16 code unit in a string iterating from right to left. | ||
|
||
```javascript | ||
function log( value, index ) { | ||
console.log( '%d: %s', index, value ); | ||
} | ||
|
||
forEachRight( 'Beep!', log ); | ||
/* => | ||
4: ! | ||
3: p | ||
2: e | ||
1: e | ||
0: B | ||
*/ | ||
``` | ||
|
||
The invoked function is provided three arguments: | ||
|
||
- **value**: character. | ||
- **index**: character 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 | ||
}; | ||
|
||
forEachRight( str, clbk, ctx ); | ||
|
||
var cnt = ctx.count; | ||
// returns 4 | ||
``` | ||
|
||
</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 forEachRight = require( '@stdlib/string/base/for-each-right' ); | ||
|
||
function log( value, index ) { | ||
console.log( '%d: %s', index, value ); | ||
} | ||
|
||
forEachRight( 'presidential election', log ); | ||
forEachRight( 'Iñtërnâtiônàlizætiøn', log ); | ||
forEachRight( '🌷🍕', log ); | ||
forEachRight( '\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"> | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
61 changes: 61 additions & 0 deletions
61
lib/node_modules/@stdlib/string/base/for-each-right/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
lib/node_modules/@stdlib/string/base/for-each-right/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
{{alias}}( str, clbk[, thisArg] ) | ||
Invokes a function for each UTF-16 code unit in a string, iterating from | ||
right to left. | ||
|
||
When invoked, the provided function is provided three arguments: | ||
|
||
- value: character | ||
- index: character index | ||
- str: input string | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string over which to iterate. | ||
|
||
clbk: Function | ||
Function to invoke for each UTF-16 code unit 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
93
lib/node_modules/@stdlib/string/base/for-each-right/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 UTF-16 code unit in a string. | ||
* | ||
* @returns result | ||
*/ | ||
type Nullary = () => any; | ||
|
||
/** | ||
* Callback invoked for each UTF-16 code unit in a string. | ||
* | ||
* @param value - character | ||
* @returns result | ||
*/ | ||
type Unary = ( value: string ) => any; | ||
|
||
/** | ||
* Callback invoked for each UTF-16 code unit in a string. | ||
* | ||
* @param value - character | ||
* @param index - character index | ||
* @returns result | ||
*/ | ||
type Binary = ( value: string, index: number ) => any; | ||
|
||
/** | ||
* Callback invoked for each UTF-16 code unit in a string. | ||
* | ||
* @param value - character | ||
* @param index - character index | ||
* @param str - input string | ||
* @returns result | ||
*/ | ||
type Ternary = ( value: string, index: number, str: string ) => any; | ||
|
||
/** | ||
* Callback invoked for each UTF-16 code unit in a string. | ||
* | ||
* @param value - character | ||
* @param index - character index | ||
* @param str - input string | ||
* @returns result | ||
*/ | ||
type Callback = Nullary | Unary | Binary | Ternary; | ||
|
||
/** | ||
* Invokes a function for each UTF-16 code unit in a string iterating from right to left. | ||
* | ||
* ## Notes | ||
* | ||
* - When invoked, the provided function is provided three arguments: | ||
* | ||
* - **value**: character. | ||
* - **index**: character 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 ); | ||
* } | ||
* | ||
* forEach( 'Hello, World!', log ); | ||
*/ | ||
declare function forEachRight( str: string, clbk: Callback, thisArg?: any ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = forEachRight; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Planeshifter We can improve the type for
thisArg
, correct? That will impact the callbacks above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kgryte Yes, specifically making use of
ThisParameterType
to extract the type of the 'this' parameter of the callback function type, and then using generics for theforEachRight
function that will be passed to thethis
parameter of the callbacks. But that is currently not done in any of thestring/base
packages and maybe we should address it in a follow-up PR / issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is fine. Would you mind creating an issue for this with a link to some sample code where we do use
ThisParameterType
? This could be a good first issue for contributors to work on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kgryte Will do!