@etsoo/shared 1.2.47 → 1.2.49

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.
@@ -15,7 +15,7 @@ test('Tests for addBlankItem', () => {
15
15
 
16
16
  test('Tests for addUrlParam', () => {
17
17
  const url = 'https://www.etsoo.com';
18
- const result = Utils.addUrlParam(url, 'a', 'b');
18
+ const result = url.addUrlParam('a', 'b');
19
19
  expect(result).toBe('https://www.etsoo.com/?a=b');
20
20
  });
21
21
 
@@ -30,7 +30,7 @@ describe('Tests for addUrlParams', () => {
30
30
  f: ['a', 'b'],
31
31
  g: null
32
32
  };
33
- const result1 = Utils.addUrlParams(url, data);
33
+ const result1 = url.addUrlParams(data);
34
34
 
35
35
  test('addUrlParams', () => {
36
36
  expect(result1).toBe(
@@ -38,7 +38,7 @@ describe('Tests for addUrlParams', () => {
38
38
  );
39
39
  });
40
40
 
41
- const result2 = Utils.addUrlParams(url, data, true);
41
+ const result2 = url.addUrlParams(data, true);
42
42
 
43
43
  test('addUrlParams with array format', () => {
44
44
  expect(result2).toBe(
@@ -46,6 +46,13 @@ describe('Tests for addUrlParams', () => {
46
46
  );
47
47
  });
48
48
 
49
+ const result22 = '/'.addUrlParams(data);
50
+ test('addUrlParams with relative path', () => {
51
+ expect(result22).toBe(
52
+ '/?a=a&b=false&c=123&d=2022-01-28T10%3A00%3A00.000Z&e=1&e=2&f=a&f=b&g='
53
+ );
54
+ });
55
+
49
56
  global.URL = undefined as any;
50
57
 
51
58
  const result3 = url.addUrlParams(data);
@@ -19,6 +19,13 @@ declare global {
19
19
  * @returns New URL
20
20
  */
21
21
  addUrlParams(this: string, data: DataTypes.SimpleObject, arrayFormat?: boolean | string): string;
22
+ /**
23
+ * Add parameters to URL
24
+ * @param this URL to add parameters
25
+ * @param params Parameters string
26
+ * @returns New URL
27
+ */
28
+ addUrlParams(this: string, params: string): string;
22
29
  /**
23
30
  * Check the input string contains Chinese character or not
24
31
  * @param this Input
@@ -90,23 +97,6 @@ export declare namespace Utils {
90
97
  * @param blankLabel Blank label, default is ---
91
98
  */
92
99
  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;
110
100
  /**
111
101
  * Base64 chars to number
112
102
  * @param base64Chars Base64 chars
package/lib/cjs/Utils.js CHANGED
@@ -8,10 +8,69 @@ const DataTypes_1 = require("./DataTypes");
8
8
  const lodash_isequal_1 = __importDefault(require("lodash.isequal"));
9
9
  const DateUtils_1 = require("./DateUtils");
10
10
  String.prototype.addUrlParam = function (name, value, arrayFormat) {
11
- return Utils.addUrlParam(this, name, value, arrayFormat);
11
+ return this.addUrlParams({ [name]: value }, arrayFormat);
12
12
  };
13
13
  String.prototype.addUrlParams = function (data, arrayFormat) {
14
- return Utils.addUrlParams(this, data, arrayFormat);
14
+ if (typeof data === 'string') {
15
+ let url = this;
16
+ if (url.includes('?')) {
17
+ url += '&';
18
+ }
19
+ else {
20
+ if (!url.endsWith('/'))
21
+ url = url + '/';
22
+ url += '?';
23
+ }
24
+ return url + data;
25
+ }
26
+ // Simple check
27
+ if (typeof URL === 'undefined' || !this.includes('://')) {
28
+ const params = Object.entries(data)
29
+ .map(([key, value]) => {
30
+ let v;
31
+ if (Array.isArray(value)) {
32
+ if (arrayFormat == null || arrayFormat === false) {
33
+ return value
34
+ .map((item) => `${key}=${encodeURIComponent(`${item}`)}`)
35
+ .join('&');
36
+ }
37
+ else {
38
+ v = value.join(arrayFormat ? ',' : arrayFormat);
39
+ }
40
+ }
41
+ else if (value instanceof Date) {
42
+ v = value.toJSON();
43
+ }
44
+ else {
45
+ v = value == null ? '' : `${value}`;
46
+ }
47
+ return `${key}=${encodeURIComponent(v)}`;
48
+ })
49
+ .join('&');
50
+ return this.addUrlParams(params);
51
+ }
52
+ else {
53
+ const urlObj = new URL(this);
54
+ Object.entries(data).forEach(([key, value]) => {
55
+ if (Array.isArray(value)) {
56
+ if (arrayFormat == null || arrayFormat === false) {
57
+ value.forEach((item) => {
58
+ urlObj.searchParams.append(key, `${item}`);
59
+ });
60
+ }
61
+ else {
62
+ urlObj.searchParams.append(key, value.join(arrayFormat ? ',' : arrayFormat));
63
+ }
64
+ }
65
+ else if (value instanceof Date) {
66
+ urlObj.searchParams.append(key, value.toJSON());
67
+ }
68
+ else {
69
+ urlObj.searchParams.append(key, `${value == null ? '' : value}`);
70
+ }
71
+ });
72
+ return urlObj.toString();
73
+ }
15
74
  };
16
75
  String.prototype.containChinese = function () {
17
76
  const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
@@ -95,83 +154,6 @@ var Utils;
95
154
  return options;
96
155
  }
97
156
  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;
175
157
  /**
176
158
  * Base64 chars to number
177
159
  * @param base64Chars Base64 chars
@@ -19,6 +19,13 @@ declare global {
19
19
  * @returns New URL
20
20
  */
21
21
  addUrlParams(this: string, data: DataTypes.SimpleObject, arrayFormat?: boolean | string): string;
22
+ /**
23
+ * Add parameters to URL
24
+ * @param this URL to add parameters
25
+ * @param params Parameters string
26
+ * @returns New URL
27
+ */
28
+ addUrlParams(this: string, params: string): string;
22
29
  /**
23
30
  * Check the input string contains Chinese character or not
24
31
  * @param this Input
@@ -90,23 +97,6 @@ export declare namespace Utils {
90
97
  * @param blankLabel Blank label, default is ---
91
98
  */
92
99
  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;
110
100
  /**
111
101
  * Base64 chars to number
112
102
  * @param base64Chars Base64 chars
package/lib/mjs/Utils.js CHANGED
@@ -2,10 +2,69 @@ import { DataTypes } from './DataTypes';
2
2
  import isEqual from 'lodash.isequal';
3
3
  import { DateUtils } from './DateUtils';
4
4
  String.prototype.addUrlParam = function (name, value, arrayFormat) {
5
- return Utils.addUrlParam(this, name, value, arrayFormat);
5
+ return this.addUrlParams({ [name]: value }, arrayFormat);
6
6
  };
7
7
  String.prototype.addUrlParams = function (data, arrayFormat) {
8
- return Utils.addUrlParams(this, data, arrayFormat);
8
+ if (typeof data === 'string') {
9
+ let url = this;
10
+ if (url.includes('?')) {
11
+ url += '&';
12
+ }
13
+ else {
14
+ if (!url.endsWith('/'))
15
+ url = url + '/';
16
+ url += '?';
17
+ }
18
+ return url + data;
19
+ }
20
+ // Simple check
21
+ if (typeof URL === 'undefined' || !this.includes('://')) {
22
+ const params = Object.entries(data)
23
+ .map(([key, value]) => {
24
+ let v;
25
+ if (Array.isArray(value)) {
26
+ if (arrayFormat == null || arrayFormat === false) {
27
+ return value
28
+ .map((item) => `${key}=${encodeURIComponent(`${item}`)}`)
29
+ .join('&');
30
+ }
31
+ else {
32
+ v = value.join(arrayFormat ? ',' : arrayFormat);
33
+ }
34
+ }
35
+ else if (value instanceof Date) {
36
+ v = value.toJSON();
37
+ }
38
+ else {
39
+ v = value == null ? '' : `${value}`;
40
+ }
41
+ return `${key}=${encodeURIComponent(v)}`;
42
+ })
43
+ .join('&');
44
+ return this.addUrlParams(params);
45
+ }
46
+ else {
47
+ const urlObj = new URL(this);
48
+ Object.entries(data).forEach(([key, value]) => {
49
+ if (Array.isArray(value)) {
50
+ if (arrayFormat == null || arrayFormat === false) {
51
+ value.forEach((item) => {
52
+ urlObj.searchParams.append(key, `${item}`);
53
+ });
54
+ }
55
+ else {
56
+ urlObj.searchParams.append(key, value.join(arrayFormat ? ',' : arrayFormat));
57
+ }
58
+ }
59
+ else if (value instanceof Date) {
60
+ urlObj.searchParams.append(key, value.toJSON());
61
+ }
62
+ else {
63
+ urlObj.searchParams.append(key, `${value == null ? '' : value}`);
64
+ }
65
+ });
66
+ return urlObj.toString();
67
+ }
9
68
  };
10
69
  String.prototype.containChinese = function () {
11
70
  const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
@@ -89,83 +148,6 @@ export var Utils;
89
148
  return options;
90
149
  }
91
150
  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;
169
151
  /**
170
152
  * Base64 chars to number
171
153
  * @param base64Chars Base64 chars
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.47",
3
+ "version": "1.2.49",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/Utils.ts CHANGED
@@ -33,6 +33,14 @@ declare global {
33
33
  arrayFormat?: boolean | string
34
34
  ): string;
35
35
 
36
+ /**
37
+ * Add parameters to URL
38
+ * @param this URL to add parameters
39
+ * @param params Parameters string
40
+ * @returns New URL
41
+ */
42
+ addUrlParams(this: string, params: string): string;
43
+
36
44
  /**
37
45
  * Check the input string contains Chinese character or not
38
46
  * @param this Input
@@ -108,15 +116,77 @@ String.prototype.addUrlParam = function (
108
116
  value: DataTypes.Simple,
109
117
  arrayFormat?: boolean | string
110
118
  ) {
111
- return Utils.addUrlParam(this, name, value, arrayFormat);
119
+ return this.addUrlParams({ [name]: value }, arrayFormat);
112
120
  };
113
121
 
114
122
  String.prototype.addUrlParams = function (
115
123
  this: string,
116
- data: DataTypes.SimpleObject,
124
+ data: DataTypes.SimpleObject | string,
117
125
  arrayFormat?: boolean | string
118
126
  ) {
119
- return Utils.addUrlParams(this, data, arrayFormat);
127
+ if (typeof data === 'string') {
128
+ let url = this;
129
+ if (url.includes('?')) {
130
+ url += '&';
131
+ } else {
132
+ if (!url.endsWith('/')) url = url + '/';
133
+ url += '?';
134
+ }
135
+ return url + data;
136
+ }
137
+
138
+ // Simple check
139
+ if (typeof URL === 'undefined' || !this.includes('://')) {
140
+ const params = Object.entries(data)
141
+ .map(([key, value]) => {
142
+ let v: string;
143
+ if (Array.isArray(value)) {
144
+ if (arrayFormat == null || arrayFormat === false) {
145
+ return value
146
+ .map(
147
+ (item) =>
148
+ `${key}=${encodeURIComponent(`${item}`)}`
149
+ )
150
+ .join('&');
151
+ } else {
152
+ v = value.join(arrayFormat ? ',' : arrayFormat);
153
+ }
154
+ } else if (value instanceof Date) {
155
+ v = value.toJSON();
156
+ } else {
157
+ v = value == null ? '' : `${value}`;
158
+ }
159
+
160
+ return `${key}=${encodeURIComponent(v)}`;
161
+ })
162
+ .join('&');
163
+
164
+ return this.addUrlParams(params);
165
+ } else {
166
+ const urlObj = new URL(this);
167
+ Object.entries(data).forEach(([key, value]) => {
168
+ if (Array.isArray(value)) {
169
+ if (arrayFormat == null || arrayFormat === false) {
170
+ value.forEach((item) => {
171
+ urlObj.searchParams.append(key, `${item}`);
172
+ });
173
+ } else {
174
+ urlObj.searchParams.append(
175
+ key,
176
+ value.join(arrayFormat ? ',' : arrayFormat)
177
+ );
178
+ }
179
+ } else if (value instanceof Date) {
180
+ urlObj.searchParams.append(key, value.toJSON());
181
+ } else {
182
+ urlObj.searchParams.append(
183
+ key,
184
+ `${value == null ? '' : value}`
185
+ );
186
+ }
187
+ });
188
+ return urlObj.toString();
189
+ }
120
190
  };
121
191
 
122
192
  String.prototype.containChinese = function (this: string): boolean {
@@ -225,97 +295,6 @@ export namespace Utils {
225
295
  return options;
226
296
  }
227
297
 
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
-
319
298
  /**
320
299
  * Base64 chars to number
321
300
  * @param base64Chars Base64 chars