@cabloy/utils 1.0.8 → 1.0.10
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/utils.d.ts +2 -1
- package/dist/utils.js +38 -13
- package/package.json +1 -1
package/dist/utils.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export declare function getPropertyObject<T>(obj: object, name: string, sep?: st
|
|
|
8
8
|
export declare function createFunction(expression: string, scopeKeys?: string[]): Function;
|
|
9
9
|
export declare function evaluateSimple(expression: string): any;
|
|
10
10
|
export declare function getRandomInt(size: number, start?: number): number;
|
|
11
|
-
export declare function combineQueries(
|
|
11
|
+
export declare function combineQueries(pagePath?: string, queries?: Record<string, any>): string;
|
|
12
|
+
export declare function defaultPathSerializer(pathName: string, pathParams?: Record<string, any>): string;
|
package/dist/utils.js
CHANGED
|
@@ -90,27 +90,52 @@ export function evaluateSimple(expression) {
|
|
|
90
90
|
export function getRandomInt(size, start = 0) {
|
|
91
91
|
return Math.floor(Math.random() * size) + start;
|
|
92
92
|
}
|
|
93
|
-
export function combineQueries(
|
|
93
|
+
export function combineQueries(pagePath, queries) {
|
|
94
|
+
pagePath = pagePath ?? '/';
|
|
94
95
|
//
|
|
95
96
|
if (!queries)
|
|
96
|
-
return
|
|
97
|
+
return pagePath;
|
|
97
98
|
//
|
|
99
|
+
const query2 = [];
|
|
98
100
|
const parts = [];
|
|
99
|
-
|
|
100
|
-
|
|
101
|
+
if (queries) {
|
|
102
|
+
for (const key in queries) {
|
|
103
|
+
const value = queries[key];
|
|
104
|
+
if (value && typeof value === 'object') {
|
|
105
|
+
query2.push([key, value]);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// query2
|
|
113
|
+
for (const [key, value] of query2) {
|
|
114
|
+
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`);
|
|
101
115
|
}
|
|
102
116
|
if (parts.length === 0)
|
|
103
|
-
return
|
|
117
|
+
return pagePath;
|
|
104
118
|
//
|
|
105
119
|
const str = parts.join('&');
|
|
106
120
|
//
|
|
107
|
-
|
|
108
|
-
return `?${str}`;
|
|
109
|
-
//
|
|
110
|
-
const pos = url.indexOf('?');
|
|
121
|
+
const pos = pagePath.indexOf('?');
|
|
111
122
|
if (pos === -1)
|
|
112
|
-
return `${
|
|
113
|
-
if (pos ===
|
|
114
|
-
return `${
|
|
115
|
-
return `${
|
|
123
|
+
return `${pagePath}?${str}`;
|
|
124
|
+
if (pos === pagePath.length - 1)
|
|
125
|
+
return `${pagePath}${str}`;
|
|
126
|
+
return `${pagePath}&${str}`;
|
|
127
|
+
}
|
|
128
|
+
const PATH_PARAM_RE = /\{([^{}/]+)\}/g;
|
|
129
|
+
export function defaultPathSerializer(pathName, pathParams) {
|
|
130
|
+
pathParams = pathParams ?? {};
|
|
131
|
+
return pathName.replace(PATH_PARAM_RE, (_, _part) => {
|
|
132
|
+
if (_part.includes('?'))
|
|
133
|
+
_part = _part.substring(0, _part.length - 1);
|
|
134
|
+
const value = pathParams?.[_part];
|
|
135
|
+
if (value === undefined || value === null)
|
|
136
|
+
return '';
|
|
137
|
+
if (typeof value === 'object')
|
|
138
|
+
return encodeURIComponent(JSON.stringify(value));
|
|
139
|
+
return encodeURIComponent(value);
|
|
140
|
+
});
|
|
116
141
|
}
|