@cripty2001/utils 0.0.12 → 0.0.13
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/dist/index.d.ts +4 -0
- package/dist/index.js +31 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -16,3 +16,7 @@ export declare function isEqual(a: any, b: any): boolean;
|
|
|
16
16
|
export declare function arrayStep(from: number, to: number, step: number): number[];
|
|
17
17
|
export declare function copyToClipboard(text: string): void;
|
|
18
18
|
export declare function stableLog(obj: any, message?: string): void;
|
|
19
|
+
export declare function parseQuery(query: string | Record<string, any> | URLSearchParams, fields: string[]): {
|
|
20
|
+
extracted: URLSearchParams;
|
|
21
|
+
left: URLSearchParams;
|
|
22
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ exports.isEqual = isEqual;
|
|
|
10
10
|
exports.arrayStep = arrayStep;
|
|
11
11
|
exports.copyToClipboard = copyToClipboard;
|
|
12
12
|
exports.stableLog = stableLog;
|
|
13
|
+
exports.parseQuery = parseQuery;
|
|
13
14
|
const lodash_1 = require("lodash");
|
|
14
15
|
function getRandom(_alphabeth, length) {
|
|
15
16
|
const alphabeth = _alphabeth.split("");
|
|
@@ -87,3 +88,33 @@ function copyToClipboard(text) {
|
|
|
87
88
|
function stableLog(obj, message = '') {
|
|
88
89
|
console.log(message, JSON.parse(JSON.stringify(obj)));
|
|
89
90
|
}
|
|
91
|
+
function parseQuery(query, fields) {
|
|
92
|
+
const data = (() => {
|
|
93
|
+
if (typeof query === 'string') {
|
|
94
|
+
return new URLSearchParams(query);
|
|
95
|
+
}
|
|
96
|
+
else if (query instanceof URLSearchParams) {
|
|
97
|
+
return query;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const toReturn = new URLSearchParams();
|
|
101
|
+
for (const k in query) {
|
|
102
|
+
toReturn.set(k, query[k]);
|
|
103
|
+
}
|
|
104
|
+
return toReturn;
|
|
105
|
+
}
|
|
106
|
+
})();
|
|
107
|
+
// Extracting fields
|
|
108
|
+
const toReturn = new URLSearchParams();
|
|
109
|
+
for (const field of fields) {
|
|
110
|
+
if (!data.has(field))
|
|
111
|
+
throw new Error(`Missing field ${field}`);
|
|
112
|
+
toReturn.set(field, data.get(field));
|
|
113
|
+
data.delete(field);
|
|
114
|
+
}
|
|
115
|
+
// Returning extracted fields
|
|
116
|
+
return {
|
|
117
|
+
extracted: toReturn,
|
|
118
|
+
left: data
|
|
119
|
+
};
|
|
120
|
+
}
|
package/package.json
CHANGED