@etsoo/shared 1.2.46 → 1.2.47
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/README.md +2 -0
- package/__tests__/Utils.ts +48 -0
- package/lib/cjs/Utils.d.ts +34 -0
- package/lib/cjs/Utils.js +83 -0
- package/lib/mjs/Utils.d.ts +34 -0
- package/lib/mjs/Utils.js +83 -0
- package/package.json +2 -2
- package/src/Utils.ts +136 -0
package/README.md
CHANGED
|
@@ -120,6 +120,8 @@ Data type definitions and type safe functions. ListItemType, ListItemType1 and L
|
|
|
120
120
|
|DataType|Data type enum|
|
|
121
121
|
|AddAndEditType|Add and edit data type|
|
|
122
122
|
|AddOrEditType|Add or edit conditional type|
|
|
123
|
+
|addUrlParam|Add parameter to URL|
|
|
124
|
+
|addUrlParams|Add parameters to URL|
|
|
123
125
|
|Basic|Basic types, includes number, bigint, Date, boolean, string|
|
|
124
126
|
|BasicArray|Basic type name array|
|
|
125
127
|
|BasicConditional|Conditional type based on BasicNames|
|
package/__tests__/Utils.ts
CHANGED
|
@@ -13,6 +13,54 @@ test('Tests for addBlankItem', () => {
|
|
|
13
13
|
expect(options.length).toBe(3);
|
|
14
14
|
});
|
|
15
15
|
|
|
16
|
+
test('Tests for addUrlParam', () => {
|
|
17
|
+
const url = 'https://www.etsoo.com';
|
|
18
|
+
const result = Utils.addUrlParam(url, 'a', 'b');
|
|
19
|
+
expect(result).toBe('https://www.etsoo.com/?a=b');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('Tests for addUrlParams', () => {
|
|
23
|
+
const url = 'https://www.etsoo.com';
|
|
24
|
+
const data = {
|
|
25
|
+
a: 'a',
|
|
26
|
+
b: false,
|
|
27
|
+
c: 123,
|
|
28
|
+
d: new Date(Date.UTC(2022, 0, 28, 10)),
|
|
29
|
+
e: [1, 2],
|
|
30
|
+
f: ['a', 'b'],
|
|
31
|
+
g: null
|
|
32
|
+
};
|
|
33
|
+
const result1 = Utils.addUrlParams(url, data);
|
|
34
|
+
|
|
35
|
+
test('addUrlParams', () => {
|
|
36
|
+
expect(result1).toBe(
|
|
37
|
+
'https://www.etsoo.com/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1&e=2&f=a&f=b&g='
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const result2 = Utils.addUrlParams(url, data, true);
|
|
42
|
+
|
|
43
|
+
test('addUrlParams with array format', () => {
|
|
44
|
+
expect(result2).toBe(
|
|
45
|
+
'https://www.etsoo.com/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1%2C2&f=a%2Cb&g='
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
global.URL = undefined as any;
|
|
50
|
+
|
|
51
|
+
const result3 = url.addUrlParams(data);
|
|
52
|
+
|
|
53
|
+
test('addUrlParams with traditional way', () => {
|
|
54
|
+
expect(result3).toBe(result1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const result4 = url.addUrlParams(data, true);
|
|
58
|
+
|
|
59
|
+
test('addUrlParams with traditional way and array format', () => {
|
|
60
|
+
expect(result4).toBe(result2);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
16
64
|
test('Tests for containChinese', () => {
|
|
17
65
|
expect('123 abC'.containChinese()).toBeFalsy();
|
|
18
66
|
expect('亿速思维'.containChinese()).toBeTruthy();
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -2,6 +2,23 @@ import { DataTypes, IdType } from './DataTypes';
|
|
|
2
2
|
import { ParsedPath } from './types/ParsedPath';
|
|
3
3
|
declare global {
|
|
4
4
|
interface String {
|
|
5
|
+
/**
|
|
6
|
+
* Add parameter to URL
|
|
7
|
+
* @param this URL to add parameter
|
|
8
|
+
* @param name Parameter name
|
|
9
|
+
* @param value Parameter value
|
|
10
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
11
|
+
* @returns New URL
|
|
12
|
+
*/
|
|
13
|
+
addUrlParam(this: string, name: string, value: DataTypes.Simple, arrayFormat?: boolean | string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Add parameters to URL
|
|
16
|
+
* @param this URL to add parameters
|
|
17
|
+
* @param data Parameters
|
|
18
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
19
|
+
* @returns New URL
|
|
20
|
+
*/
|
|
21
|
+
addUrlParams(this: string, data: DataTypes.SimpleObject, arrayFormat?: boolean | string): string;
|
|
5
22
|
/**
|
|
6
23
|
* Check the input string contains Chinese character or not
|
|
7
24
|
* @param this Input
|
|
@@ -73,6 +90,23 @@ export declare namespace Utils {
|
|
|
73
90
|
* @param blankLabel Blank label, default is ---
|
|
74
91
|
*/
|
|
75
92
|
function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
|
|
93
|
+
/**
|
|
94
|
+
* Add parameter to URL
|
|
95
|
+
* @param url URL to add parameter
|
|
96
|
+
* @param name Parameter name
|
|
97
|
+
* @param value Parameter value
|
|
98
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
99
|
+
* @returns New URL
|
|
100
|
+
*/
|
|
101
|
+
function addUrlParam(url: string, name: string, value: DataTypes.Simple, arrayFormat?: boolean | string): string;
|
|
102
|
+
/**
|
|
103
|
+
* Add parameters to URL
|
|
104
|
+
* @param url URL to add parameters
|
|
105
|
+
* @param data Parameters
|
|
106
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
107
|
+
* @returns New URL
|
|
108
|
+
*/
|
|
109
|
+
function addUrlParams(url: string, data: DataTypes.SimpleObject, arrayFormat?: boolean | string): string;
|
|
76
110
|
/**
|
|
77
111
|
* Base64 chars to number
|
|
78
112
|
* @param base64Chars Base64 chars
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -7,6 +7,12 @@ exports.Utils = void 0;
|
|
|
7
7
|
const DataTypes_1 = require("./DataTypes");
|
|
8
8
|
const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
|
|
9
9
|
const DateUtils_1 = require("./DateUtils");
|
|
10
|
+
String.prototype.addUrlParam = function (name, value, arrayFormat) {
|
|
11
|
+
return Utils.addUrlParam(this, name, value, arrayFormat);
|
|
12
|
+
};
|
|
13
|
+
String.prototype.addUrlParams = function (data, arrayFormat) {
|
|
14
|
+
return Utils.addUrlParams(this, data, arrayFormat);
|
|
15
|
+
};
|
|
10
16
|
String.prototype.containChinese = function () {
|
|
11
17
|
const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
|
|
12
18
|
return regExp.test(this);
|
|
@@ -89,6 +95,83 @@ var Utils;
|
|
|
89
95
|
return options;
|
|
90
96
|
}
|
|
91
97
|
Utils.addBlankItem = addBlankItem;
|
|
98
|
+
/**
|
|
99
|
+
* Add parameter to URL
|
|
100
|
+
* @param url URL to add parameter
|
|
101
|
+
* @param name Parameter name
|
|
102
|
+
* @param value Parameter value
|
|
103
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
104
|
+
* @returns New URL
|
|
105
|
+
*/
|
|
106
|
+
function addUrlParam(url, name, value, arrayFormat) {
|
|
107
|
+
return addUrlParams(url, { [name]: value }, arrayFormat);
|
|
108
|
+
}
|
|
109
|
+
Utils.addUrlParam = addUrlParam;
|
|
110
|
+
/**
|
|
111
|
+
* Add parameters to URL
|
|
112
|
+
* @param url URL to add parameters
|
|
113
|
+
* @param data Parameters
|
|
114
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
115
|
+
* @returns New URL
|
|
116
|
+
*/
|
|
117
|
+
function addUrlParams(url, data, arrayFormat) {
|
|
118
|
+
if (typeof URL === 'undefined') {
|
|
119
|
+
if (url.includes('?')) {
|
|
120
|
+
url += '&';
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
if (!url.endsWith('/'))
|
|
124
|
+
url = url + '/';
|
|
125
|
+
url += '?';
|
|
126
|
+
}
|
|
127
|
+
url += Object.entries(data)
|
|
128
|
+
.map(([key, value]) => {
|
|
129
|
+
let v;
|
|
130
|
+
if (Array.isArray(value)) {
|
|
131
|
+
if (arrayFormat == null || arrayFormat === false) {
|
|
132
|
+
return value
|
|
133
|
+
.map((item) => `${key}=${encodeURIComponent(`${item}`)}`)
|
|
134
|
+
.join('&');
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
v = value.join(arrayFormat ? ',' : arrayFormat);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if (value instanceof Date) {
|
|
141
|
+
v = value.toJSON();
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
v = value == null ? '' : `${value}`;
|
|
145
|
+
}
|
|
146
|
+
return `${key}=${encodeURIComponent(v)}`;
|
|
147
|
+
})
|
|
148
|
+
.join('&');
|
|
149
|
+
return url;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const urlObj = new URL(url);
|
|
153
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
154
|
+
if (Array.isArray(value)) {
|
|
155
|
+
if (arrayFormat == null || arrayFormat === false) {
|
|
156
|
+
value.forEach((item) => {
|
|
157
|
+
urlObj.searchParams.append(key, `${item}`);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
urlObj.searchParams.append(key, value.join(arrayFormat ? ',' : arrayFormat));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else if (value instanceof Date) {
|
|
165
|
+
urlObj.searchParams.append(key, value.toJSON());
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
urlObj.searchParams.append(key, `${value == null ? '' : value}`);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return urlObj.toString();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
Utils.addUrlParams = addUrlParams;
|
|
92
175
|
/**
|
|
93
176
|
* Base64 chars to number
|
|
94
177
|
* @param base64Chars Base64 chars
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -2,6 +2,23 @@ import { DataTypes, IdType } from './DataTypes';
|
|
|
2
2
|
import { ParsedPath } from './types/ParsedPath';
|
|
3
3
|
declare global {
|
|
4
4
|
interface String {
|
|
5
|
+
/**
|
|
6
|
+
* Add parameter to URL
|
|
7
|
+
* @param this URL to add parameter
|
|
8
|
+
* @param name Parameter name
|
|
9
|
+
* @param value Parameter value
|
|
10
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
11
|
+
* @returns New URL
|
|
12
|
+
*/
|
|
13
|
+
addUrlParam(this: string, name: string, value: DataTypes.Simple, arrayFormat?: boolean | string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Add parameters to URL
|
|
16
|
+
* @param this URL to add parameters
|
|
17
|
+
* @param data Parameters
|
|
18
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
19
|
+
* @returns New URL
|
|
20
|
+
*/
|
|
21
|
+
addUrlParams(this: string, data: DataTypes.SimpleObject, arrayFormat?: boolean | string): string;
|
|
5
22
|
/**
|
|
6
23
|
* Check the input string contains Chinese character or not
|
|
7
24
|
* @param this Input
|
|
@@ -73,6 +90,23 @@ export declare namespace Utils {
|
|
|
73
90
|
* @param blankLabel Blank label, default is ---
|
|
74
91
|
*/
|
|
75
92
|
function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
|
|
93
|
+
/**
|
|
94
|
+
* Add parameter to URL
|
|
95
|
+
* @param url URL to add parameter
|
|
96
|
+
* @param name Parameter name
|
|
97
|
+
* @param value Parameter value
|
|
98
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
99
|
+
* @returns New URL
|
|
100
|
+
*/
|
|
101
|
+
function addUrlParam(url: string, name: string, value: DataTypes.Simple, arrayFormat?: boolean | string): string;
|
|
102
|
+
/**
|
|
103
|
+
* Add parameters to URL
|
|
104
|
+
* @param url URL to add parameters
|
|
105
|
+
* @param data Parameters
|
|
106
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
107
|
+
* @returns New URL
|
|
108
|
+
*/
|
|
109
|
+
function addUrlParams(url: string, data: DataTypes.SimpleObject, arrayFormat?: boolean | string): string;
|
|
76
110
|
/**
|
|
77
111
|
* Base64 chars to number
|
|
78
112
|
* @param base64Chars Base64 chars
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
2
|
import isEqual from 'lodash.isequal';
|
|
3
3
|
import { DateUtils } from './DateUtils';
|
|
4
|
+
String.prototype.addUrlParam = function (name, value, arrayFormat) {
|
|
5
|
+
return Utils.addUrlParam(this, name, value, arrayFormat);
|
|
6
|
+
};
|
|
7
|
+
String.prototype.addUrlParams = function (data, arrayFormat) {
|
|
8
|
+
return Utils.addUrlParams(this, data, arrayFormat);
|
|
9
|
+
};
|
|
4
10
|
String.prototype.containChinese = function () {
|
|
5
11
|
const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
|
|
6
12
|
return regExp.test(this);
|
|
@@ -83,6 +89,83 @@ export var Utils;
|
|
|
83
89
|
return options;
|
|
84
90
|
}
|
|
85
91
|
Utils.addBlankItem = addBlankItem;
|
|
92
|
+
/**
|
|
93
|
+
* Add parameter to URL
|
|
94
|
+
* @param url URL to add parameter
|
|
95
|
+
* @param name Parameter name
|
|
96
|
+
* @param value Parameter value
|
|
97
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
98
|
+
* @returns New URL
|
|
99
|
+
*/
|
|
100
|
+
function addUrlParam(url, name, value, arrayFormat) {
|
|
101
|
+
return addUrlParams(url, { [name]: value }, arrayFormat);
|
|
102
|
+
}
|
|
103
|
+
Utils.addUrlParam = addUrlParam;
|
|
104
|
+
/**
|
|
105
|
+
* Add parameters to URL
|
|
106
|
+
* @param url URL to add parameters
|
|
107
|
+
* @param data Parameters
|
|
108
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
109
|
+
* @returns New URL
|
|
110
|
+
*/
|
|
111
|
+
function addUrlParams(url, data, arrayFormat) {
|
|
112
|
+
if (typeof URL === 'undefined') {
|
|
113
|
+
if (url.includes('?')) {
|
|
114
|
+
url += '&';
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
if (!url.endsWith('/'))
|
|
118
|
+
url = url + '/';
|
|
119
|
+
url += '?';
|
|
120
|
+
}
|
|
121
|
+
url += Object.entries(data)
|
|
122
|
+
.map(([key, value]) => {
|
|
123
|
+
let v;
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
if (arrayFormat == null || arrayFormat === false) {
|
|
126
|
+
return value
|
|
127
|
+
.map((item) => `${key}=${encodeURIComponent(`${item}`)}`)
|
|
128
|
+
.join('&');
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
v = value.join(arrayFormat ? ',' : arrayFormat);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (value instanceof Date) {
|
|
135
|
+
v = value.toJSON();
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
v = value == null ? '' : `${value}`;
|
|
139
|
+
}
|
|
140
|
+
return `${key}=${encodeURIComponent(v)}`;
|
|
141
|
+
})
|
|
142
|
+
.join('&');
|
|
143
|
+
return url;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const urlObj = new URL(url);
|
|
147
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
148
|
+
if (Array.isArray(value)) {
|
|
149
|
+
if (arrayFormat == null || arrayFormat === false) {
|
|
150
|
+
value.forEach((item) => {
|
|
151
|
+
urlObj.searchParams.append(key, `${item}`);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
urlObj.searchParams.append(key, value.join(arrayFormat ? ',' : arrayFormat));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (value instanceof Date) {
|
|
159
|
+
urlObj.searchParams.append(key, value.toJSON());
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
urlObj.searchParams.append(key, `${value == null ? '' : value}`);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return urlObj.toString();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
Utils.addUrlParams = addUrlParams;
|
|
86
169
|
/**
|
|
87
170
|
* Base64 chars to number
|
|
88
171
|
* @param base64Chars Base64 chars
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.47",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"jest": "^29.7.0",
|
|
60
60
|
"jest-environment-jsdom": "^29.7.0",
|
|
61
61
|
"ts-jest": "^29.2.5",
|
|
62
|
-
"typescript": "^5.6.
|
|
62
|
+
"typescript": "^5.6.3"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
package/src/Utils.ts
CHANGED
|
@@ -5,6 +5,34 @@ import { ParsedPath } from './types/ParsedPath';
|
|
|
5
5
|
|
|
6
6
|
declare global {
|
|
7
7
|
interface String {
|
|
8
|
+
/**
|
|
9
|
+
* Add parameter to URL
|
|
10
|
+
* @param this URL to add parameter
|
|
11
|
+
* @param name Parameter name
|
|
12
|
+
* @param value Parameter value
|
|
13
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
14
|
+
* @returns New URL
|
|
15
|
+
*/
|
|
16
|
+
addUrlParam(
|
|
17
|
+
this: string,
|
|
18
|
+
name: string,
|
|
19
|
+
value: DataTypes.Simple,
|
|
20
|
+
arrayFormat?: boolean | string
|
|
21
|
+
): string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Add parameters to URL
|
|
25
|
+
* @param this URL to add parameters
|
|
26
|
+
* @param data Parameters
|
|
27
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
28
|
+
* @returns New URL
|
|
29
|
+
*/
|
|
30
|
+
addUrlParams(
|
|
31
|
+
this: string,
|
|
32
|
+
data: DataTypes.SimpleObject,
|
|
33
|
+
arrayFormat?: boolean | string
|
|
34
|
+
): string;
|
|
35
|
+
|
|
8
36
|
/**
|
|
9
37
|
* Check the input string contains Chinese character or not
|
|
10
38
|
* @param this Input
|
|
@@ -74,6 +102,23 @@ declare global {
|
|
|
74
102
|
}
|
|
75
103
|
}
|
|
76
104
|
|
|
105
|
+
String.prototype.addUrlParam = function (
|
|
106
|
+
this: string,
|
|
107
|
+
name: string,
|
|
108
|
+
value: DataTypes.Simple,
|
|
109
|
+
arrayFormat?: boolean | string
|
|
110
|
+
) {
|
|
111
|
+
return Utils.addUrlParam(this, name, value, arrayFormat);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
String.prototype.addUrlParams = function (
|
|
115
|
+
this: string,
|
|
116
|
+
data: DataTypes.SimpleObject,
|
|
117
|
+
arrayFormat?: boolean | string
|
|
118
|
+
) {
|
|
119
|
+
return Utils.addUrlParams(this, data, arrayFormat);
|
|
120
|
+
};
|
|
121
|
+
|
|
77
122
|
String.prototype.containChinese = function (this: string): boolean {
|
|
78
123
|
const regExp =
|
|
79
124
|
/[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
|
|
@@ -180,6 +225,97 @@ export namespace Utils {
|
|
|
180
225
|
return options;
|
|
181
226
|
}
|
|
182
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Add parameter to URL
|
|
230
|
+
* @param url URL to add parameter
|
|
231
|
+
* @param name Parameter name
|
|
232
|
+
* @param value Parameter value
|
|
233
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
234
|
+
* @returns New URL
|
|
235
|
+
*/
|
|
236
|
+
export function addUrlParam(
|
|
237
|
+
url: string,
|
|
238
|
+
name: string,
|
|
239
|
+
value: DataTypes.Simple,
|
|
240
|
+
arrayFormat?: boolean | string
|
|
241
|
+
) {
|
|
242
|
+
return addUrlParams(url, { [name]: value }, arrayFormat);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Add parameters to URL
|
|
247
|
+
* @param url URL to add parameters
|
|
248
|
+
* @param data Parameters
|
|
249
|
+
* @param arrayFormat Array format to array style or not to multiple fields
|
|
250
|
+
* @returns New URL
|
|
251
|
+
*/
|
|
252
|
+
export function addUrlParams(
|
|
253
|
+
url: string,
|
|
254
|
+
data: DataTypes.SimpleObject,
|
|
255
|
+
arrayFormat?: boolean | string
|
|
256
|
+
) {
|
|
257
|
+
if (typeof URL === 'undefined') {
|
|
258
|
+
if (url.includes('?')) {
|
|
259
|
+
url += '&';
|
|
260
|
+
} else {
|
|
261
|
+
if (!url.endsWith('/')) url = url + '/';
|
|
262
|
+
url += '?';
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
url += Object.entries(data)
|
|
266
|
+
.map(([key, value]) => {
|
|
267
|
+
let v: string;
|
|
268
|
+
if (Array.isArray(value)) {
|
|
269
|
+
if (arrayFormat == null || arrayFormat === false) {
|
|
270
|
+
return value
|
|
271
|
+
.map(
|
|
272
|
+
(item) =>
|
|
273
|
+
`${key}=${encodeURIComponent(
|
|
274
|
+
`${item}`
|
|
275
|
+
)}`
|
|
276
|
+
)
|
|
277
|
+
.join('&');
|
|
278
|
+
} else {
|
|
279
|
+
v = value.join(arrayFormat ? ',' : arrayFormat);
|
|
280
|
+
}
|
|
281
|
+
} else if (value instanceof Date) {
|
|
282
|
+
v = value.toJSON();
|
|
283
|
+
} else {
|
|
284
|
+
v = value == null ? '' : `${value}`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return `${key}=${encodeURIComponent(v)}`;
|
|
288
|
+
})
|
|
289
|
+
.join('&');
|
|
290
|
+
|
|
291
|
+
return url;
|
|
292
|
+
} else {
|
|
293
|
+
const urlObj = new URL(url);
|
|
294
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
295
|
+
if (Array.isArray(value)) {
|
|
296
|
+
if (arrayFormat == null || arrayFormat === false) {
|
|
297
|
+
value.forEach((item) => {
|
|
298
|
+
urlObj.searchParams.append(key, `${item}`);
|
|
299
|
+
});
|
|
300
|
+
} else {
|
|
301
|
+
urlObj.searchParams.append(
|
|
302
|
+
key,
|
|
303
|
+
value.join(arrayFormat ? ',' : arrayFormat)
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
} else if (value instanceof Date) {
|
|
307
|
+
urlObj.searchParams.append(key, value.toJSON());
|
|
308
|
+
} else {
|
|
309
|
+
urlObj.searchParams.append(
|
|
310
|
+
key,
|
|
311
|
+
`${value == null ? '' : value}`
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
return urlObj.toString();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
183
319
|
/**
|
|
184
320
|
* Base64 chars to number
|
|
185
321
|
* @param base64Chars Base64 chars
|