simple-query-mutator
Install
$ npm install simple-query-mutator --save
formatQuery
formatQuery direct formats the query, can delete a property and change the value of another property at the same time or can be left null.
formatQuery(rawQuery,propToBeRemoved,propToBeChange,ChangeValue);
import { formatQuery } from 'simple-query-mutator';
const searchStr = '?language=eng&sort=price&type=product';
const result = formatQuery(searchStr, 'language');
const result = formatQuery(searchStr, null, 'sort', 'date');
const result = formatQuery(searchStr, 'type', 'language', 'alien');
checkAndFormatQuery
checkAndFormatQuery checks whether or not the prop exist then continue formatting.
checkAndFormatQuery(rawQuery,method,prop,ChangeValue);
import { checkAndFormatQuery } from 'simple-query-mutator';
const searchStr = '?language=eng&sort=price&type=product';
const result = checkAndFormatQuery(searchStr, 'get', 'type');
const result = checkAndFormatQuery(searchStr, 'has', 'type');
const result = checkAndFormatQuery(searchStr, 'change', 'language', 'alien');
const result = checkAndFormatQuery(searchStr, 'delete', 'language');
changeQueryProp
changeQueryProp change the query's property key to a new property key, can change value too.
changeQueryProp(searchQuery,oldProp,newProp,newValue); accept string or object. newValue can be null
import { changeQueryProp } from 'simple-query-mutator';
const searchObj = { language: 'eng', sort: 'date', type: 'product' };
const searchStr = '?language=eng&sort=date&type=product';
const resultObjWithNewKey = changeQueryProp(searchObj, 'type', 'category');
const resultObjWithNewKeyAndValue = changeQueryProp(
searchObj,
'type',
'category',
'notproduct'
);
const resultStrWithNewKey = changeQueryProp(searchStr, 'type', 'category');
const resultStrWithNewKeyAndValue = changeQueryProp(
searchStr,
'type',
'category',
'notproduct'
);
Can be combined with queryObjToString method.
queryObjToString
queryObjToString turns a search object into string;
queryObjToString(searchObj);
import { queryObjToString } from 'simple-query-mutator';
const searchObj = { language: 'eng', sort: 'date', type: 'product' };
const result = queryObjToString(searchObj);