The library provides extra-small and lightweight utils for working with URLs: convert query arguments to objects, convers objects to strings, merge queries to one, push query items into URL. TypeScript definitions included.
npm i --save mtarh-url-utils
Accepts plain object to be searilized into string query. The function has optional boolean argument for adding the '?' char at the string beginning.
import { queryObjectToString } from 'mtarh-url-utils';
const query = queryObjectToString({ name: 'Maksim', age: 30 });
// query = '?name=Maksim&age=30';
Works as queryObjectToString but in reverse way - it creates a plain object from query string.
import { queryStringToObject } from 'mtarh-url-utils';
const query = queryStringToObject('?name=Maksim&age=30');
// query = { name: 'Maksim', age: '30' };
Allows to combine queries into one object. Accepts a list of arguments where you can pass string variant of query or object.
import { mergeQueries } from 'mtarh-url-utils';
const query = mergeQueries('?name=maksim', { age: 30 });
// query = { name: 'maksim', age: 30 };
Injects queryies into url. Queries is a list and you can pass strings and objects as queries that should be pushed into url
import { pushQueryArguments } from 'mtarh-url-utils';
const url = pushQueryArguments('https://example.com/?q=search', '?name=maksim', { age: 30 });
// url = 'https://example.com/?q=search&name=maksim&age=30';