simple-query-mutator
Advanced tools
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; |
{ | ||
"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'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9559
190
112