@cabloy/utils 1.0.10 → 1.0.11

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 CHANGED
@@ -8,5 +8,9 @@ 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 combineParamsAndQuery(path: string, options?: {
12
+ params?: object;
13
+ query?: object;
14
+ }): string;
11
15
  export declare function combineQueries(pagePath?: string, queries?: Record<string, any>): string;
12
16
  export declare function defaultPathSerializer(pathName: string, pathParams?: Record<string, any>): string;
package/dist/utils.js CHANGED
@@ -90,6 +90,9 @@ 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 combineParamsAndQuery(path, options) {
94
+ return combineQueries(defaultPathSerializer(path, options?.params), options?.query);
95
+ }
93
96
  export function combineQueries(pagePath, queries) {
94
97
  pagePath = pagePath ?? '/';
95
98
  //
@@ -126,16 +129,20 @@ export function combineQueries(pagePath, queries) {
126
129
  return `${pagePath}&${str}`;
127
130
  }
128
131
  const PATH_PARAM_RE = /\{([^{}/]+)\}/g;
132
+ const PATH_PARAM_RE2 = /:([^/]+)/g;
129
133
  export function defaultPathSerializer(pathName, pathParams) {
130
134
  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
- });
135
+ for (const item of [PATH_PARAM_RE, PATH_PARAM_RE2]) {
136
+ pathName = pathName.replace(item, (_, _part) => {
137
+ if (_part.includes('?'))
138
+ _part = _part.substring(0, _part.length - 1);
139
+ const value = pathParams?.[_part];
140
+ if (value === undefined || value === null)
141
+ return '';
142
+ if (typeof value === 'object')
143
+ return encodeURIComponent(JSON.stringify(value));
144
+ return encodeURIComponent(value);
145
+ });
146
+ }
147
+ return pathName;
141
148
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cabloy/utils",
3
3
  "type": "module",
4
- "version": "1.0.10",
4
+ "version": "1.0.11",
5
5
  "description": "cabloy utils",
6
6
  "publishConfig": {
7
7
  "access": "public"