Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-query-mutator

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-query-mutator - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

24

index.js

@@ -65,4 +65,28 @@ function turnQueryIntoObj(searchStr, method, prop, changeValue) {

}
function changeQueryProp(searchQuery, oldProp, newProp, newValue) {
// newValue can be null;
const changeProp = (oldProp, newProp, { [oldProp]: oldValue, ...rest }) => {
return {
[newProp]: newValue || oldValue,
...rest
};
};
if (typeof searchQuery === 'string') {
const newSearchObj = turnQueryIntoObj(searchQuery);
if (!newSearchObj[oldProp]) {
return false;
}
return changeProp(oldProp, newProp, newSearchObj);
}
if (typeof searchQuery === 'object') {
if (!searchQuery[oldProp]) {
return false;
}
return changeProp(oldProp, newProp, searchQuery);
}
}
exports.formatQuery = formatQuery;
exports.checkAndFormatQuery = checkAndFormatQuery;
exports.queryObjToString = queryObjToString;
exports.changeQueryProp = changeQueryProp;

2

package.json
{
"name": "simple-query-mutator",
"version": "2.1.0",
"version": "2.2.0",
"description": "for mutating a search query,delete,change prop.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -13,3 +13,3 @@ # simple-query-mutator

formatQuery direct formats the query.
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.

@@ -59,2 +59,41 @@ ```

## 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
```
```js
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');
// { language: 'eng', sort: 'date', category: 'product' }
const resultObjWithNewKeyAndValue = changeQueryProp(
searchObj,
'type',
'category',
'notproduct'
);
// { language: 'eng', sort: 'date', category: 'notproduct' }
const resultStrWithNewKey = changeQueryProp(searchStr, 'type', 'category');
// { language: 'eng', sort: 'date', category: 'product' }
const resultStrWithNewKeyAndValue = changeQueryProp(
searchStr,
'type',
'category',
'notproduct'
);
// { language: 'eng', sort: 'date', category: 'notproduct' }
```
```
Can be combined with queryObjToString method.
```
## queryObjToString

@@ -61,0 +100,0 @@

const {
formatQuery,
checkAndFormatQuery,
queryObjToString
queryObjToString,
changeQueryProp
} = require('../index');

@@ -93,1 +94,16 @@

});
test('should change the query old prop to new prop', () => {
expect(changeQueryProp(testCase2, 'type', 'category')['category']).toBe(
'product'
);
expect(
changeQueryProp(testCase2, 'type', 'category', 'notproduct')['category']
).toBe('notproduct');
expect(changeQueryProp(testCase3, 'type', 'category').category).toBe(
'product'
);
expect(
changeQueryProp(testCase3, 'type', 'category', 'notproduct').category
).toBe('notproduct');
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc