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);
;// ...const searchStr = '?language=eng&sort=price&type=product';const result = ;// "?sort=price&type=product" const result = ;// "?language=eng&sort=date&type=product" const result = ;// "?language=alien&sort=date"
checkAndFormatQuery
checkAndFormatQuery checks whether or not the prop exist then continue formatting.
checkAndFormatQuery(rawQuery,method,prop,ChangeValue);
;// ...const searchStr = '?language=eng&sort=price&type=product';const result = ;// "product" const result = ;// true const result = ;// "?language=alien&sort=date" const result = ;// "?sort=date"
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
;// ...const searchObj = language: 'eng' sort: 'date' type: 'product' ;const searchStr = '?language=eng&sort=date&type=product'; const resultObjWithNewKey = ;// { language: 'eng', sort: 'date', category: 'product' }const resultObjWithNewKeyAndValue = ;// { language: 'eng', sort: 'date', category: 'notproduct' } const resultStrWithNewKey = ;// { language: 'eng', sort: 'date', category: 'product' }const resultStrWithNewKeyAndValue = ;// { language: 'eng', sort: 'date', category: 'notproduct' }
Can be combined with queryObjToString method.
queryObjToString
queryObjToString turns a search object into string;
queryObjToString(searchObj);
;// ...const searchObj = language: 'eng' sort: 'date' type: 'product' ;const result = ;// '?language=eng&sort=date&type=product';