query-converter
v1.1.0
Published
Convert object to query string and query string to object. Can work with multilevel objects.
Downloads
2
Readme
Query Converter
Very small lib to work with url query.
Supports multilevel with indexes in brackets.
Features:
- convert
object
to querystring
- convert query
string
to object
Dependencies: none
Install
npm install query-converter
Usage
tl;dr
import QueryConverter from 'query-converter'
const queryObject = QueryConverter.stringToObject(location.search)
const queryString = QueryConverter.objectToString({
string: "string",
array: ["one", "two"],
object: {
number: 123,
array: [
true,
null
]
}
})
Import QueryConverter class. Both methods are static.
import QueryConverter from 'query-converter'
Method objectToString(object: object)
const object = {
string: "string",
array: ["one", "two"],
object: {
number: 123,
array: [
true,
null
]
}
}
const queryString = QueryConverter.objectToString(object)
// queryString => string=string&array[0]=one&array[1]=two&object[number]=123&object[array][0]=1&object[array][1]=
Method stringToObject(string: string)
// front "?" is optional - will be cut if needed
const string = '?string=string&array[0]=one&array[1]=two&object[number]=123&object[array][0]=1&object[array][1]='
const queryObject = QueryConverter.stringToObject(string)
// queryObject => {
// string: "string",
// array: [
// "one",
// "two"
// ],
// object: {
// number: "123",
// array: [
// "1",
// ""
// ]
// }
// }