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, e.g.: gbk
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', {charset: 'gbk'});
var str = 'x[y][0][v][w]=' + urlencode('雾空', 'gbk');
var obj = {'x' : {'y' : [{'v' : {'w' : '雾空'}}]}};
urlencode.stringify(obj, {charset: 'gbk'}).should.equal(str);
Benchmark
urlencode(str, encoding)
$ node benchmark/urlencode.js
node version: v0.10.26
urlencode(str) x 11,980 ops/sec ±1.13% (100 runs sampled)
urlencode(str, "gbk") x 8,575 ops/sec ±1.58% (94 runs sampled)
encodeURIComponent(str) x 11,677 ops/sec ±2.32% (93 runs sampled)
Fastest is urlencode(str)
urlencode.decode(str, encoding)
$ node benchmark/urlencode.decode.js
node version: v0.10.26
urlencode.decode(str) x 26,027 ops/sec ±7.51% (73 runs sampled)
urlencode.decode(str, "gbk") x 14,409 ops/sec ±1.72% (98 runs sampled)
decodeURIComponent(str) x 36,052 ops/sec ±0.90% (96 runs sampled)
urlencode.parse(qs, {charset: "gbk"}) x 16,401 ops/sec ±1.09% (98 runs sampled)
urlencode.parse(qs, {charset: "utf8"}) x 23,381 ops/sec ±2.22% (93 runs sampled)
Fastest is decodeURIComponent(str)
TODO
License
MIT