1.1.1 • Published 3 years ago
locustjs-querystring v1.1.1
locustjs-querystring
This library provides querystring utilities.
functions
parseQuery: Parses strings in querystring format and returns a json object.
Example 1: basic example
import { parseQuery } from 'locustjs-querystring';
var result = parseQuery('name=John%20Doe&age=23&city=UK&agree=true');
console.log(result);
/*
{
age: "23",
agree: "true",
city: "UK",
name: "John Doe",
}
*/Example 2: enabling automatic-conversion
import { parseQuery } from 'locustjs-querystring';
var result = parseQuery('name=John%20Doe&age=23&city=UK&agree=true', true);
console.log(result);
/*
{
age: 23,
agree: true,
city: "UK",
name: "John Doe",
}
*/createQuery: converts a javascript object into querystring.
Example 1: basic example
import { createQuery } from 'locustjs-querystring';
var result = createQuery({ name: 'John Doe', age: 23, city: 'UK', agree: true });
console.log(result);
/*
name=John%20Doe&age=23&city=UK&agree=true
*/Example 2: using ignore props
import { createQuery } from 'locustjs-querystring';
var result = createQuery({ name: 'John Doe', age: 23, city: 'UK', agree: true }, 'age,city');
console.log(result);
/*
name=John%20Doe&agree=true
*/Helpers
QueryHelper: A helper object with two methods, parse() and stringify() that are maped to parseQuery and createQuery methods. This helper is exported as default object from library.
Example:
import QueryHelper from 'locustjs-querystring'
console.log(QueryHelper.parse('name=John%20Doe&agree=true')); // { name: 'John Doe', agree: 'true' }
console.log(QueryHelper.stringify({ name: 'John Doe', age: 23 })); // name=John%20Doe&age=23