@terzogenito/json-utils 1.0.13 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +37 -1
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -766,6 +766,41 @@ function filterByKeyPattern(jsonObject, pattern) {
|
|
|
766
766
|
return filterProperties(jsonObject, (key) => regex.test(key));
|
|
767
767
|
}
|
|
768
768
|
|
|
769
|
+
function toQueryString(jsonObject, prefix = '') {
|
|
770
|
+
try {
|
|
771
|
+
if (typeof jsonObject === 'string') {
|
|
772
|
+
jsonObject = JSON.parse(jsonObject);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
776
|
+
return '';
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
const params = [];
|
|
780
|
+
|
|
781
|
+
for (const [key, value] of Object.entries(jsonObject)) {
|
|
782
|
+
const newKey = prefix ? `${prefix}[${key}]` : key;
|
|
783
|
+
|
|
784
|
+
if (value === null || value === undefined) {
|
|
785
|
+
continue;
|
|
786
|
+
} else if (typeof value === 'object' && !Array.isArray(value)) {
|
|
787
|
+
params.push(toQueryString(value, newKey));
|
|
788
|
+
} else if (Array.isArray(value)) {
|
|
789
|
+
value.forEach((item, index) => {
|
|
790
|
+
params.push(`${newKey}[${index}]=${encodeURIComponent(item)}`);
|
|
791
|
+
});
|
|
792
|
+
} else {
|
|
793
|
+
params.push(`${newKey}=${encodeURIComponent(value)}`);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
return params.join('&');
|
|
798
|
+
} catch (error) {
|
|
799
|
+
console.error('Error in toQueryString:', error);
|
|
800
|
+
return '';
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
769
804
|
module.exports = {
|
|
770
805
|
getString,
|
|
771
806
|
getFile,
|
|
@@ -799,5 +834,6 @@ module.exports = {
|
|
|
799
834
|
filterProperties,
|
|
800
835
|
filterByType,
|
|
801
836
|
removeEmptyProperties,
|
|
802
|
-
filterByKeyPattern
|
|
837
|
+
filterByKeyPattern,
|
|
838
|
+
toQueryString
|
|
803
839
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terzogenito/json-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "JSON data utilities",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
|
-
"node": ">=
|
|
17
|
+
"node": ">=8.3.0"
|
|
18
18
|
},
|
|
19
19
|
"keywords": [
|
|
20
20
|
"nodejs",
|