README.md (6683B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # fromCodePoint 22 23 > Create a string from a sequence of Unicode [code points][code-point]. 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 fromCodePoint = require( '@stdlib/string/from-code-point' ); 41 ``` 42 43 #### fromCodePoint( pt1\[, pt2\[, pt3\[, ...]]] ) 44 45 Creates a `string` from a sequence of Unicode [code points][code-point]. 46 47 ```javascript 48 var out = fromCodePoint( 9731 ); 49 // returns '☃' 50 ``` 51 52 In addition to providing [code points][code-point] as separate arguments, the function supports providing an array-like `object` as a single argument containing a sequence of [code points][code-point]. 53 54 ```javascript 55 var Uint16Array = require( '@stdlib/array/uint16' ); 56 57 var out = fromCodePoint( 97, 98, 99 ); 58 // returns 'abc' 59 60 out = fromCodePoint( new Uint16Array( [ 97, 98, 99 ] ) ); 61 // returns 'abc' 62 ``` 63 64 </section> 65 66 <!-- /.usage --> 67 68 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 69 70 <section class="notes"> 71 72 ## Notes 73 74 - This function differs from [`String.fromCharCode`][mdn-string-fromcharcode] in the following ways: 75 76 - The function provides support for all valid Unicode values (up to `21` bits). While most common Unicode values can be represented using one 16-bit unit, higher code point characters require two 16-bit units (a surrogate pair consisting of a high and a low surrogate) to form a single character. [`String.fromCharCode`][mdn-string-fromcharcode] does **not** support surrogate pairs, supporting only UCS-2, a subset of [UTF-16][utf-16]. 77 78 - This function differs from [`String.fromCodePoint`][mdn-string-fromcodepoint] in the following ways: 79 80 - The function supports explicitly providing an array-like `object` containing a sequence of [code points][code-point]. 81 - The function requires **at least** one [code point][code-point]. 82 - The function requires that all [code points][code-point] be nonnegative integers. The function does **not** support values, such as `null`, `undefined`, `true`, `false`, `'0'`, `'1'`, etc., which can be cast to integer values. 83 84 </section> 85 86 <!-- /.notes --> 87 88 <!-- Package usage examples. --> 89 90 <section class="examples"> 91 92 ## Examples 93 94 <!-- eslint no-undef: "error" --> 95 96 ```javascript 97 var randu = require( '@stdlib/random/base/randu' ); 98 var floor = require( '@stdlib/math/base/special/floor' ); 99 var UNICODE_MAX_BMP = require( '@stdlib/constants/unicode/max-bmp' ); 100 var fromCodePoint = require( '@stdlib/string/from-code-point' ); 101 102 var x; 103 var i; 104 105 for ( i = 0; i < 100; i++ ) { 106 x = floor( randu()*UNICODE_MAX_BMP ); 107 console.log( '%d => %s', x, fromCodePoint( x ) ); 108 } 109 ``` 110 111 </section> 112 113 <!-- /.examples --> 114 115 <!-- Section for describing a command-line interface. --> 116 117 * * * 118 119 <section class="cli"> 120 121 ## CLI 122 123 <!-- CLI usage documentation. --> 124 125 <section class="usage"> 126 127 ### Usage 128 129 ```text 130 Usage: from-code-point [options] [<code_point> <code_point> ...] 131 132 Options: 133 134 -h, --help Print this message. 135 -V, --version Print the package version. 136 --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. 137 ``` 138 139 </section> 140 141 <!-- /.usage --> 142 143 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 144 145 <section class="notes"> 146 147 ### Notes 148 149 - If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. 150 151 ```bash 152 # Not escaped... 153 $ echo -n $'97\n98\n99' | from-code-point --split /\r?\n/ 154 155 # Escaped... 156 $ echo -n $'97\n98\n99' | from-code-point --split /\\r?\\n/ 157 ``` 158 159 - The implementation ignores trailing delimiters. 160 161 </section> 162 163 <!-- /.notes --> 164 165 <!-- CLI usage examples. --> 166 167 <section class="examples"> 168 169 ### Examples 170 171 ```bash 172 $ from-code-point 9731 173 ☃ 174 ``` 175 176 To use as a [standard stream][standard-streams], 177 178 ```bash 179 $ echo -n '9731' | from-code-point 180 ☃ 181 ``` 182 183 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. 184 185 ```bash 186 $ echo -n '97\t98\t99\t' | from-code-point --split '\t' 187 abc 188 ``` 189 190 </section> 191 192 <!-- /.examples --> 193 194 </section> 195 196 <!-- /.cli --> 197 198 <!-- 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. --> 199 200 <section class="references"> 201 202 </section> 203 204 <!-- /.references --> 205 206 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 207 208 <section class="related"> 209 210 * * * 211 212 ## See Also 213 214 - <span class="package-name">[`@stdlib/string/code-point-at`][@stdlib/string/code-point-at]</span><span class="delimiter">: </span><span class="description">return a Unicode code point from a string at a specified position.</span> 215 216 </section> 217 218 <!-- /.related --> 219 220 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 221 222 <section class="links"> 223 224 [code-point]: https://en.wikipedia.org/wiki/Code_point 225 226 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 227 228 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 229 230 [mdn-string-fromcharcode]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode 231 232 [mdn-string-fromcodepoint]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint 233 234 [utf-16]: https://en.wikipedia.org/wiki/UTF-16 235 236 <!-- <related-links> --> 237 238 [@stdlib/string/code-point-at]: https://github.com/stdlib-js/string/tree/main/code-point-at 239 240 <!-- </related-links> --> 241 242 </section> 243 244 <!-- /.links -->