|
| 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 | +# last |
| 22 | + |
| 23 | +> Return the last character(s) of a string. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var last = require( '@stdlib/string/last' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### last( str\[, n]\[, options] ) |
| 34 | + |
| 35 | +Returns the last character(s) of a string. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var out = last( 'last man standing' ); |
| 39 | +// returns 'g' |
| 40 | + |
| 41 | +out = last( 'Hidden Treasures' ); |
| 42 | +// returns 's' |
| 43 | +``` |
| 44 | + |
| 45 | +The function supports the following options: |
| 46 | + |
| 47 | +- **mode**: type of characters to return. Must be one of the following: |
| 48 | + |
| 49 | + - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). |
| 50 | + - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). |
| 51 | + - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). |
| 52 | + |
| 53 | + Default: `'grapheme'`. |
| 54 | + |
| 55 | +By default, the function returns the last character. To return the last `n` characters, provide a second argument specifying the number of characters to return. |
| 56 | + |
| 57 | +```javascript |
| 58 | +var out = last( 'foo bar', 3 ); |
| 59 | +// returns 'bar' |
| 60 | + |
| 61 | +out = last( 'foo bar', 10 ); |
| 62 | +// returns 'foo bar' |
| 63 | +``` |
| 64 | + |
| 65 | +</section> |
| 66 | + |
| 67 | +<!-- /.usage --> |
| 68 | + |
| 69 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 70 | + |
| 71 | +<section class="notes"> |
| 72 | + |
| 73 | +## Notes |
| 74 | + |
| 75 | +- By default, the function assumes the general case in which an input string may contain an arbitrary number of grapheme clusters. This assumption comes with a performance cost. Accordingly, if an input string is known to only contain visual characters of a particular type (e.g., only alphanumeric), one can achieve better performance by specifying the appropriate `mode` option. |
| 76 | + |
| 77 | +</section> |
| 78 | + |
| 79 | +<!-- /.notes --> |
| 80 | + |
| 81 | +<section class="examples"> |
| 82 | + |
| 83 | +## Examples |
| 84 | + |
| 85 | +<!-- eslint no-undef: "error" --> |
| 86 | + |
| 87 | +```javascript |
| 88 | +var last = require( '@stdlib/string/last' ); |
| 89 | + |
| 90 | +var str = last( 'last man standing' ); |
| 91 | +// returns 'g' |
| 92 | + |
| 93 | +str = last( 'presidential election' ); |
| 94 | +// returns 'n' |
| 95 | + |
| 96 | +str = last( 'javaScript' ); |
| 97 | +// returns 't' |
| 98 | + |
| 99 | +str = last( 'Hidden Treasures' ); |
| 100 | +// returns 's' |
| 101 | + |
| 102 | +str = last( 'The Last of the Mohicans', 8 ); |
| 103 | +// returns 'Mohicans' |
| 104 | + |
| 105 | +str = last( '🐶🐮🐷🐰🐸', 2 ); |
| 106 | +// returns '🐰🐸' |
| 107 | + |
| 108 | +str = last( '🐶🐮🐷🐰🐸', 10 ); |
| 109 | +// returns '🐶🐮🐷🐰🐸' |
| 110 | +``` |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.examples --> |
| 115 | + |
| 116 | +* * * |
| 117 | + |
| 118 | +<section class="cli"> |
| 119 | + |
| 120 | +## CLI |
| 121 | + |
| 122 | +<section class="usage"> |
| 123 | + |
| 124 | +### Usage |
| 125 | + |
| 126 | +```text |
| 127 | +Usage: last [options] [<string>] |
| 128 | +
|
| 129 | +Options: |
| 130 | +
|
| 131 | + -h, --help Print this message. |
| 132 | + -V, --version Print the package version. |
| 133 | + --n num Number of characters to return. Default: 1. |
| 134 | + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. |
| 135 | + --mode mode Type of character to return. Default: 'grapheme'. |
| 136 | +``` |
| 137 | + |
| 138 | +</section> |
| 139 | + |
| 140 | +<!-- /.usage --> |
| 141 | + |
| 142 | +<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 143 | + |
| 144 | +<section class="notes"> |
| 145 | + |
| 146 | +### Notes |
| 147 | + |
| 148 | +- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. |
| 149 | + |
| 150 | + ```bash |
| 151 | + # Not escaped... |
| 152 | + $ echo -n $'beep\nboop' | last --split /\r?\n/ |
| 153 | + |
| 154 | + # Escaped... |
| 155 | + $ echo -n $'beep\nboop' | last --split /\\r?\\n/ |
| 156 | + ``` |
| 157 | + |
| 158 | +- The implementation ignores trailing delimiters. |
| 159 | + |
| 160 | +</section> |
| 161 | + |
| 162 | +<!-- /.notes --> |
| 163 | + |
| 164 | +<section class="examples"> |
| 165 | + |
| 166 | +### Examples |
| 167 | + |
| 168 | +```bash |
| 169 | +$ last beep |
| 170 | +p |
| 171 | +``` |
| 172 | + |
| 173 | +To use as a [standard stream][standard-streams], |
| 174 | + |
| 175 | +```bash |
| 176 | +$ echo -n 'beep\nboop' | last --n=2 |
| 177 | +ep |
| 178 | +op |
| 179 | +``` |
| 180 | + |
| 181 | +By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. |
| 182 | + |
| 183 | +```bash |
| 184 | +$ echo -n 'beep\tboop' | last --split '\t' |
| 185 | +p |
| 186 | +p |
| 187 | +``` |
| 188 | + |
| 189 | +</section> |
| 190 | + |
| 191 | +<!-- /.examples --> |
| 192 | + |
| 193 | +</section> |
| 194 | + |
| 195 | +<!-- /.cli --> |
| 196 | + |
| 197 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 198 | + |
| 199 | +<section class="related"> |
| 200 | + |
| 201 | +* * * |
| 202 | + |
| 203 | + |
| 204 | +</section> |
| 205 | + |
| 206 | +<!-- /.related --> |
| 207 | + |
| 208 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 209 | + |
| 210 | +<section class="links"> |
| 211 | + |
| 212 | +[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams |
| 213 | + |
| 214 | +[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions |
| 215 | + |
| 216 | +<!-- <related-links> --> |
| 217 | + |
| 218 | +<!-- </related-links> --> |
| 219 | + |
| 220 | +</section> |
| 221 | + |
| 222 | +<!-- /.links --> |
0 commit comments