What is urlencode?
The `urlencode` npm package is used for encoding and decoding URL strings. It provides simple methods to encode a string into a URL-safe format and decode a URL-encoded string back to its original format.
What are urlencode's main functionalities?
URL Encoding
This feature allows you to encode a string into a URL-safe format. The `urlencode` function takes a string as input and returns the encoded version of that string.
const urlencode = require('urlencode');
const encoded = urlencode('Hello World!');
console.log(encoded); // Outputs: Hello%20World%21
URL Decoding
This feature allows you to decode a URL-encoded string back to its original format. The `decode` function takes an encoded string as input and returns the decoded version of that string.
const urlencode = require('urlencode');
const decoded = urlencode.decode('Hello%20World%21');
console.log(decoded); // Outputs: Hello World!
Encoding with Character Set
This feature allows you to encode a string using a specific character set. The `urlencode` function can take an additional parameter specifying the character set to use for encoding.
const urlencode = require('urlencode');
const encoded = urlencode('你好', 'gbk');
console.log(encoded); // Outputs: %C4%E3%BA%C3
Decoding with Character Set
This feature allows you to decode a URL-encoded string using a specific character set. The `decode` function can take an additional parameter specifying the character set to use for decoding.
const urlencode = require('urlencode');
const decoded = urlencode.decode('%C4%E3%BA%C3', 'gbk');
console.log(decoded); // Outputs: 你好
Other packages similar to urlencode
querystring
The `querystring` module is part of Node.js core and provides utilities for parsing and formatting URL query strings. It offers similar functionality for encoding and decoding URL components but is more focused on query strings rather than general URL encoding.
qs
The `qs` package is a query string parser with support for nested objects. It provides more advanced parsing and stringifying capabilities compared to `urlencode`, making it suitable for complex query string manipulations.
url
The `url` module is part of Node.js core and provides utilities for URL resolution and parsing. While it includes methods for URL encoding and decoding, it is more comprehensive and includes additional functionalities for working with URLs.
urlencode
encodeURIComponent with charset.
jscoverage: 100%
Install
$ npm install urlencode
Usage
var urlencode = require('urlencode');
console.log(urlencode('苏千'));
console.log(urlencode('苏千', 'gbk'));
urlencode.decode('%CB%D5%C7%A7', 'gbk');
urlencode.parse('nick=%CB%D5%C7%A7', 'gbk');
Benchmark
$ make benchmark
urlencode(str) x 11,931 ops/sec ±0.85% (89 runs sampled)
urlencode(str, "gbk") x 7,516 ops/sec ±2.67% (90 runs sampled)
encodeURIComponent(str) x 11,723 ops/sec ±3.93% (91 runs sampled)
Fastest is urlencode(str),encodeURIComponent(str)
License
(The MIT License)
Copyright (c) 2012 - 2014 fengmk2 <fengmk2@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.