Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions lib/node_modules/@stdlib/string/code-point-at/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<!--

@license Apache-2.0

Copyright (c) 2020 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.

-->

# codePointAt

> Return a Unicode [code point][code-point] from a string at a specified position.

<!-- 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 codePointAt = require( '@stdlib/string/code-point-at' );
```

#### codePointAt( string, position\[, backward] )

Returns a Unicode [code point][code-point] from a string at a specified position.

```javascript
var out = codePointAt( 'last man standing', 4 );
// returns 32
```

The function supports providing a `boolean` argument for performing backward iteration for low surrogates.

```javascript
var out = codePointAt( '🌷', 1, true );
// returns 127799
```

</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">

## Notes

This function differs from [`String.codePointAt`][mdn-string-codepointat] in the following ways:

- The function supports providing a `boolean` argument for performing backward iteration for low surrogates. [`String.codePointAt`][mdn-string-codepointat] simply returns the low surrogate value if no [UTF-16][utf-16] surrogate pair begins at the specified position. If invoked with `backward` set to `true`, this function will return the code point after aggregating with the preceding high surrogate, if the specified position does not mark the start of a surrogate pair.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var codePointAt = require( '@stdlib/string/code-point-at' );

console.log( codePointAt( 'last man standing', 4 ) );
// => 32

console.log( codePointAt( 'presidential election', 8, true ) );
// => 116

console.log( codePointAt( 'अनुच्छेद', 2 ) );
// => 2369

console.log( codePointAt( '🌷', 1, true ) );
// => 127799
```

</section>

<!-- /.examples -->

<!-- Section for describing a command-line interface. -->

* * *

<section class="cli">

## CLI

<!-- CLI usage documentation. -->

<section class="usage">

### Usage

```text
Usage: code-point-at [options] [<string>] --pos=<index>

Options:

-h, --help Print this message.
-V, --version Print the package version.
-b, --backward Backward iteration for low surrogates.
--pos index Position in string.
```

</section>

<!-- /.usage -->

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

<section class="notes">

</section>

<!-- /.notes -->

<!-- CLI usage examples. -->

<section class="examples">

### Examples

```bash
$ code-point-at --pos=2 अनुच्छेद
2369
```

To use as a [standard stream][standard-streams],

```bash
$ echo -n 'अनुच्छेद' | code-point-at --pos=2
2369
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- 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 all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[code-point]: https://en.wikipedia.org/wiki/Code_point

[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams

[mdn-string-codepointat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt

[utf-16]: https://en.wikipedia.org/wiki/UTF-16

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
var floor = require( '@stdlib/math/base/special/floor' );
var fromCodePoint = require( '@stdlib/string/from-code-point' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var UNICODE_MAX = require( '@stdlib/constants/string/unicode-max' );
var pkg = require( './../package.json' ).name;
var codePointAt = require( './../lib' );


// VARIABLES //

var opts = {
'skip': ( typeof String.prototype.codePointAt !== 'function' )
};


// MAIN //

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu() * UNICODE_MAX );
out = codePointAt( fromCodePoint( x ), 0 );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( !isNonNegativeInteger( out ) ) {
b.fail( 'should return a nonnegative integer' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::built-in', opts, function benchmark( b ) {
var out;
var x;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu() * UNICODE_MAX );
out = String.prototype.codePointAt.call( fromCodePoint( x ), 0 );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( !isNonNegativeInteger( out ) ) {
b.fail( 'should return a nonnegative integer' );
}
b.pass( 'benchmark finished' );
b.end();
});
87 changes: 87 additions & 0 deletions lib/node_modules/@stdlib/string/code-point-at/bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env node

/**
* @license Apache-2.0
*
* Copyright (c) 2020 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 resolve = require( 'path' ).resolve;
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var CLI = require( '@stdlib/tools/cli' );
var stdin = require( '@stdlib/process/read-stdin' );
var codePointAt = require( './../lib' );


// MAIN //

/**
* Main execution sequence.
*
* @private
* @returns {void}
*/
function main() {
var flags;
var args;
var cli;
var pos;

// Create a command-line interface:
cli = new CLI({
'pkg': require( './../package.json' ),
'options': require( './../etc/cli_opts.json' ),
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
'encoding': 'utf8'
})
});

// Get any provided command-line options:
flags = cli.flags();
if ( flags.help || flags.version ) {
return;
}
pos = parseInt( flags.pos, 10 );

// Get any provided command-line arguments:
args = cli.args();

// Check if we are receiving data from `stdin`...
if ( process.stdin.isTTY ) {
return console.log( codePointAt( args[ 0 ], pos, flags.backward ) ); // eslint-disable-line no-console
}
return stdin( onRead );

/**
* Callback invoked upon reading from `stdin`.
*
* @private
* @param {(Error|null)} error - error object
* @param {Buffer} data - data
* @returns {void}
*/
function onRead( error, data ) {
if ( error ) {
return cli.error( error );
}
console.log( codePointAt( data.toString(), pos, flags.backward ) ); // eslint-disable-line no-console
}
}

main();
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/string/code-point-at/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

{{alias}}( str, idx[, backward] )
Returns a Unicode code point from a string at a specified position.

Parameters
----------
str: string
Input string.

idx: integer
Position.

backward: boolean (optional)
Backward iteration for low surrogates.

Returns
-------
out: integer
Unicode code point.

Examples
--------
> var out = {{alias}}( 'last man standing', 4 )
32
> out = {{alias}}( 'presidential election', 8, true )
116
> out = {{alias}}( 'अनुच्छेद', 2 )
2369
> out = {{alias}}( '🌷', 1, true )
127799

See Also
--------
Loading