README.md (4345B)
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 # Percent-encoding 22 23 > [Percent-encode][percent-encoding] a [UTF-16][utf-16] encoded string according to [RFC 3986][rfc-3986-percent-encoding]. 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 percentEncode = require( '@stdlib/string/percent-encode' ); 41 ``` 42 43 #### percentEncode( str ) 44 45 [Percent-encodes][percent-encoding] a [UTF-16][utf-16] encoded string according to [RFC 3986][rfc-3986-percent-encoding]. 46 47 ```javascript 48 var out = percentEncode( '☃' ); 49 // returns '%E2%98%83' 50 ``` 51 52 </section> 53 54 <!-- /.usage --> 55 56 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 57 58 <section class="notes"> 59 60 ## Notes 61 62 - The function [percent-encodes][percent-encoding] an **entire** `string`. Hence, if provided a URI, the function [percent-encodes][percent-encoding] the entire URI. 63 64 ```javascript 65 var out = percentEncode( 'https://en.wikipedia.org/wiki/Mode_(statistics)' ); 66 // returns 'https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMode_%28statistics%29' 67 ``` 68 69 To [percent-encode][percent-encoding] a URI, split the URI into separate components, [percent-encode][percent-encoding] relevant components, and then reassemble. 70 71 </section> 72 73 <!-- /.notes --> 74 75 <!-- Package usage examples. --> 76 77 <section class="examples"> 78 79 ## Examples 80 81 <!-- eslint no-undef: "error" --> 82 83 ```javascript 84 var percentEncode = require( '@stdlib/string/percent-encode' ); 85 86 var values; 87 var out; 88 var i; 89 90 values = [ 91 'Ladies + Gentlemen', 92 'An encoded string!', 93 'Dogs, Cats & Mice', 94 '☃', 95 'æ', 96 '𐐷' 97 ]; 98 for ( i = 0; i < values.length; i++ ) { 99 out = percentEncode( values[ i ] ); 100 console.log( '%s: %s', values[ i ], out ); 101 } 102 ``` 103 104 </section> 105 106 <!-- /.examples --> 107 108 <!-- Section for describing a command-line interface. --> 109 110 * * * 111 112 <section class="cli"> 113 114 ## CLI 115 116 <!-- CLI usage documentation. --> 117 118 <section class="usage"> 119 120 ### Usage 121 122 ```text 123 Usage: percent-encode [options] [<string>] 124 125 Options: 126 127 -h, --help Print this message. 128 -V, --version Print the package version. 129 ``` 130 131 </section> 132 133 <!-- /.usage --> 134 135 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 136 137 <section class="notes"> 138 139 </section> 140 141 <!-- /.notes --> 142 143 <!-- CLI usage examples. --> 144 145 <section class="examples"> 146 147 ### Examples 148 149 ```bash 150 $ percent-encode ☃ 151 %E2%98%83 152 ``` 153 154 To use as a [standard stream][standard-streams], 155 156 ```bash 157 $ echo -n '☃' | percent-encode 158 %E2%98%83 159 ``` 160 161 </section> 162 163 <!-- /.examples --> 164 165 </section> 166 167 <!-- /.cli --> 168 169 <!-- 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. --> 170 171 <section class="references"> 172 173 </section> 174 175 <!-- /.references --> 176 177 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 178 179 <section class="related"> 180 181 </section> 182 183 <!-- /.related --> 184 185 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 186 187 <section class="links"> 188 189 [percent-encoding]: https://en.wikipedia.org/wiki/Percent-encoding 190 191 [rfc-3986-percent-encoding]: https://tools.ietf.org/html/rfc3986#section-2.1 192 193 [utf-16]: https://en.wikipedia.org/wiki/UTF-16 194 195 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 196 197 </section> 198 199 <!-- /.links -->