@cabloy/utils 1.0.10 → 1.0.12

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
@@ -6,7 +6,11 @@ export declare function setProperty<T>(obj: object, name: string, value: T): voi
6
6
  export declare function getProperty<T>(obj: object, name: string, sep?: string): T | undefined;
7
7
  export declare function getPropertyObject<T>(obj: object, name: string, sep?: string): T | undefined;
8
8
  export declare function createFunction(expression: string, scopeKeys?: string[]): Function;
9
- export declare function evaluateSimple(expression: string): any;
9
+ export declare function evaluateSimple(expression: string, scope?: object): 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
@@ -83,13 +83,18 @@ export function createFunction(expression, scopeKeys) {
83
83
  }
84
84
  return fn;
85
85
  }
86
- export function evaluateSimple(expression) {
87
- const fn = createFunction(expression);
88
- return fn();
86
+ export function evaluateSimple(expression, scope) {
87
+ const scopeKeys = scope ? Object.keys(scope) : undefined;
88
+ const scopeValues = scope ? Object.values(scope) : undefined;
89
+ const fn = createFunction(expression, scopeKeys);
90
+ return scopeValues ? fn(...scopeValues) : fn();
89
91
  }
90
92
  export function getRandomInt(size, start = 0) {
91
93
  return Math.floor(Math.random() * size) + start;
92
94
  }
95
+ export function combineParamsAndQuery(path, options) {
96
+ return combineQueries(defaultPathSerializer(path, options?.params), options?.query);
97
+ }
93
98
  export function combineQueries(pagePath, queries) {
94
99
  pagePath = pagePath ?? '/';
95
100
  //
@@ -126,16 +131,20 @@ export function combineQueries(pagePath, queries) {
126
131
  return `${pagePath}&${str}`;
127
132
  }
128
133
  const PATH_PARAM_RE = /\{([^{}/]+)\}/g;
134
+ const PATH_PARAM_RE2 = /:([^/]+)/g;
129
135
  export function defaultPathSerializer(pathName, pathParams) {
130
136
  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
- });
137
+ for (const item of [PATH_PARAM_RE, PATH_PARAM_RE2]) {
138
+ pathName = pathName.replace(item, (_, _part) => {
139
+ if (_part.includes('?'))
140
+ _part = _part.substring(0, _part.length - 1);
141
+ const value = pathParams?.[_part];
142
+ if (value === undefined || value === null)
143
+ return '';
144
+ if (typeof value === 'object')
145
+ return encodeURIComponent(JSON.stringify(value));
146
+ return encodeURIComponent(value);
147
+ });
148
+ }
149
+ return pathName;
141
150
  }
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.12",
5
5
  "description": "cabloy utils",
6
6
  "publishConfig": {
7
7
  "access": "public"