@billjs/query-string
v1.0.2
Published
A simple and lightweight QueryString by TypeScript for Node.js or Browsers.
Downloads
591
Maintainers
Readme
query-string
A simple and lightweight QueryString by TypeScript for Node.js or Browsers.
Installation
Installation is done using the npm install
command:
npm install -S @billjs/query-string
Overview
API
parse
Parse the string to an object. Support the parsing of multiple keys with the same name, and parse them into arrays.
The parsed keys and values are decoded by decodeURIComponent
function.
If you specified the fn
argument, that means you can to use it for customize value.
- str (
string
) source string - [sep] (
string
|null
optional
) group separator, default&
- [eq] (
string
|null
optional
) key-value separator, default=
- [fn] (ParseFunction) a function, it can be used to customize return values.
- return (
object
)
Parse a normal query-string.
const data = parse('a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }
Parse the search from URL.
const data = parse('http://foo.com?a=1&b=s');
console.log(data); // ==> { a: '1', b: 's' }
Parse it when if key or value is encoded.
const data = parse('test%3D=test%20%26*%20test');
console.log(data); // ==> { 'test=': 'test &* test' }
Parse it when if sep and eq are specified.
const data = parse('a#1|b#s|b#s2', '|', '#');
console.log(data); // ==> { a: '1', b: ['s', 's2'] }
Parse the same name key as an array.
const data = parse('a=1&b=s1&b=s2');
console.log(data); // ==> { a: '1', b: ['s1', 's2'] }
Parse it for customize value.
const fn = (key: string, value: string) => {
if (key === 'b') return +value;
if (key === 'c') return { on: true, off: false }[value];
return value;
};
const data = parse('a=test&b=1&c=on', null, null, fn);
console.log(data); // ==> { a: 'test', b: 1, c: true }
stringify
Stringify the object to a string. Support the stringifying of arrays into keys of the same name.
The stringified keys and values are encoded by encodeURIComponent
function.
If you specified the fn
argument, that means you can to use it for customize value.
- obj (
object
) source object - [sep] (
string
|null
optional
) group separator, default&
- [eq] (
string
|null
optional
) key-value separator, default=
- [fn] (StringifyFunction) a function, it can be used to customize return values.
- return (
string
)
Stringify an object.
const query = stringify({ a: '1', b: 's' });
console.log(query); // ==> 'a=1&b=s'
Stringify the array to same name key.
const query = stringify({ a: '1', b: ['s1', 's2'] });
console.log(query); // ==> 'a=1&b=s1&b=s2'
Stringify the boolean value.
const query = stringify({ a: '1', b: 's', c: false });
console.log(query); // ==> 'a=1&b=s&c=false'
Stringify it when if the key or value need to encoded.
const query = stringify({ 'test=': 'test &* test' });
console.log(query); // ==> 'test%3D=test%20%26*%20test'
Stringify it when if sep and eq are specified.
const query = stringify({ a: '1', b: ['s', 's2'] }, '|', '#');
console.log(query); // ==> 'a#1|b#s|b#s2'
Stringify it for customize value.
const fn = (key, value) => {
if (key === 'c') return value ? 'on' : 'off';
return value;
};
const obj = { a: 'test', b: 1, c: true };
const query = stringify(obj, null, null, fn);
console.log(query); // ==> 'a=test&b=1&c=on'
ParseFunction
A function for customize parse.
The declaration is like this:
type ParseFunction = (key: string, value: string) => any;
StringifyFunction
A function for customize stringify.
The declaration is like this:
type StringifyFunction = (key: string, value: any) => any;