|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2022 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 | +# codePointAt |
| 22 | + |
| 23 | +> Return a Unicode [code point][code-point] from a string at a specified position. |
| 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 codePointAt = require( '@stdlib/string/base/code-point-at' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### codePointAt( string, position, backward ) |
| 44 | + |
| 45 | +Returns a Unicode [code point][code-point] from a string at a specified position. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var out = codePointAt( 'last man standing', 4, false ); |
| 49 | +// returns 32 |
| 50 | +``` |
| 51 | + |
| 52 | +The function supports a `backward` argument for performing backward iteration for low surrogates. |
| 53 | + |
| 54 | +```javascript |
| 55 | +var out = codePointAt( '🌷', 1, true ); |
| 56 | +// returns 127799 |
| 57 | +``` |
| 58 | + |
| 59 | +The function supports providing a negative `position`. |
| 60 | + |
| 61 | +```javascript |
| 62 | +var out = codePointAt( 'last man standing', -13, false ); |
| 63 | +// returns 32 |
| 64 | +``` |
| 65 | + |
| 66 | +</section> |
| 67 | + |
| 68 | +<!-- /.usage --> |
| 69 | + |
| 70 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 71 | + |
| 72 | +<section class="notes"> |
| 73 | + |
| 74 | +## Notes |
| 75 | + |
| 76 | +This function differs from [`String.prototype.codePointAt`][mdn-string-codepointat] in the following ways: |
| 77 | + |
| 78 | +- The function supports providing a negative `position`. If provided a negative `position`, the function determines the string position relative to the end of the string. |
| 79 | +- The function supports a `backward` argument for performing backward iteration for low surrogates. [`String.prototype.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. |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<!-- /.notes --> |
| 84 | + |
| 85 | +<!-- Package usage examples. --> |
| 86 | + |
| 87 | +<section class="examples"> |
| 88 | + |
| 89 | +## Examples |
| 90 | + |
| 91 | +<!-- eslint no-undef: "error" --> |
| 92 | + |
| 93 | +```javascript |
| 94 | +var codePointAt = require( '@stdlib/string/base/code-point-at' ); |
| 95 | + |
| 96 | +var v = codePointAt( 'last man standing', 4, false ); |
| 97 | +// returns 32 |
| 98 | + |
| 99 | +v = codePointAt( 'presidential election', 8, true ); |
| 100 | +// returns 116 |
| 101 | + |
| 102 | +v = codePointAt( 'अनुच्छेद', 2, false ); |
| 103 | +// returns 2369 |
| 104 | + |
| 105 | +v = codePointAt( '🌷', 1, true ); |
| 106 | +// returns 127799 |
| 107 | +``` |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.examples --> |
| 112 | + |
| 113 | +<!-- 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. --> |
| 114 | + |
| 115 | +<section class="references"> |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.references --> |
| 120 | + |
| 121 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 122 | + |
| 123 | +<section class="related"> |
| 124 | + |
| 125 | +</section> |
| 126 | + |
| 127 | +<!-- /.related --> |
| 128 | + |
| 129 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 130 | + |
| 131 | +<section class="links"> |
| 132 | + |
| 133 | +[code-point]: https://en.wikipedia.org/wiki/Code_point |
| 134 | + |
| 135 | +[mdn-string-codepointat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt |
| 136 | + |
| 137 | +[utf-16]: https://en.wikipedia.org/wiki/UTF-16 |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.links --> |
0 commit comments