@etsoo/shared 1.2.46 → 1.2.48

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 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|
@@ -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 = url.addUrlParam('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 = url.addUrlParams(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 = url.addUrlParams(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();
@@ -2,6 +2,30 @@ 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;
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;
5
29
  /**
6
30
  * Check the input string contains Chinese character or not
7
31
  * @param this Input
package/lib/cjs/Utils.js CHANGED
@@ -7,6 +7,70 @@ 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 this.addUrlParams({ [name]: value }, arrayFormat);
12
+ };
13
+ String.prototype.addUrlParams = function (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
+ if (typeof URL === 'undefined') {
27
+ const params = Object.entries(data)
28
+ .map(([key, value]) => {
29
+ let v;
30
+ if (Array.isArray(value)) {
31
+ if (arrayFormat == null || arrayFormat === false) {
32
+ return value
33
+ .map((item) => `${key}=${encodeURIComponent(`${item}`)}`)
34
+ .join('&');
35
+ }
36
+ else {
37
+ v = value.join(arrayFormat ? ',' : arrayFormat);
38
+ }
39
+ }
40
+ else if (value instanceof Date) {
41
+ v = value.toJSON();
42
+ }
43
+ else {
44
+ v = value == null ? '' : `${value}`;
45
+ }
46
+ return `${key}=${encodeURIComponent(v)}`;
47
+ })
48
+ .join('&');
49
+ return this.addUrlParams(params);
50
+ }
51
+ else {
52
+ const urlObj = new URL(this);
53
+ Object.entries(data).forEach(([key, value]) => {
54
+ if (Array.isArray(value)) {
55
+ if (arrayFormat == null || arrayFormat === false) {
56
+ value.forEach((item) => {
57
+ urlObj.searchParams.append(key, `${item}`);
58
+ });
59
+ }
60
+ else {
61
+ urlObj.searchParams.append(key, value.join(arrayFormat ? ',' : arrayFormat));
62
+ }
63
+ }
64
+ else if (value instanceof Date) {
65
+ urlObj.searchParams.append(key, value.toJSON());
66
+ }
67
+ else {
68
+ urlObj.searchParams.append(key, `${value == null ? '' : value}`);
69
+ }
70
+ });
71
+ return urlObj.toString();
72
+ }
73
+ };
10
74
  String.prototype.containChinese = function () {
11
75
  const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
12
76
  return regExp.test(this);
@@ -2,6 +2,30 @@ 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;
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;
5
29
  /**
6
30
  * Check the input string contains Chinese character or not
7
31
  * @param this Input
package/lib/mjs/Utils.js CHANGED
@@ -1,6 +1,70 @@
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 this.addUrlParams({ [name]: value }, arrayFormat);
6
+ };
7
+ String.prototype.addUrlParams = function (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
+ if (typeof URL === 'undefined') {
21
+ const params = Object.entries(data)
22
+ .map(([key, value]) => {
23
+ let v;
24
+ if (Array.isArray(value)) {
25
+ if (arrayFormat == null || arrayFormat === false) {
26
+ return value
27
+ .map((item) => `${key}=${encodeURIComponent(`${item}`)}`)
28
+ .join('&');
29
+ }
30
+ else {
31
+ v = value.join(arrayFormat ? ',' : arrayFormat);
32
+ }
33
+ }
34
+ else if (value instanceof Date) {
35
+ v = value.toJSON();
36
+ }
37
+ else {
38
+ v = value == null ? '' : `${value}`;
39
+ }
40
+ return `${key}=${encodeURIComponent(v)}`;
41
+ })
42
+ .join('&');
43
+ return this.addUrlParams(params);
44
+ }
45
+ else {
46
+ const urlObj = new URL(this);
47
+ Object.entries(data).forEach(([key, value]) => {
48
+ if (Array.isArray(value)) {
49
+ if (arrayFormat == null || arrayFormat === false) {
50
+ value.forEach((item) => {
51
+ urlObj.searchParams.append(key, `${item}`);
52
+ });
53
+ }
54
+ else {
55
+ urlObj.searchParams.append(key, value.join(arrayFormat ? ',' : arrayFormat));
56
+ }
57
+ }
58
+ else if (value instanceof Date) {
59
+ urlObj.searchParams.append(key, value.toJSON());
60
+ }
61
+ else {
62
+ urlObj.searchParams.append(key, `${value == null ? '' : value}`);
63
+ }
64
+ });
65
+ return urlObj.toString();
66
+ }
67
+ };
4
68
  String.prototype.containChinese = function () {
5
69
  const regExp = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;
6
70
  return regExp.test(this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.2.46",
3
+ "version": "1.2.48",
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.2"
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,42 @@ 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
+
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
+
8
44
  /**
9
45
  * Check the input string contains Chinese character or not
10
46
  * @param this Input
@@ -74,6 +110,84 @@ declare global {
74
110
  }
75
111
  }
76
112
 
113
+ String.prototype.addUrlParam = function (
114
+ this: string,
115
+ name: string,
116
+ value: DataTypes.Simple,
117
+ arrayFormat?: boolean | string
118
+ ) {
119
+ return this.addUrlParams({ [name]: value }, arrayFormat);
120
+ };
121
+
122
+ String.prototype.addUrlParams = function (
123
+ this: string,
124
+ data: DataTypes.SimpleObject | string,
125
+ arrayFormat?: boolean | string
126
+ ) {
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
+ if (typeof URL === 'undefined') {
139
+ const params = Object.entries(data)
140
+ .map(([key, value]) => {
141
+ let v: string;
142
+ if (Array.isArray(value)) {
143
+ if (arrayFormat == null || arrayFormat === false) {
144
+ return value
145
+ .map(
146
+ (item) =>
147
+ `${key}=${encodeURIComponent(`${item}`)}`
148
+ )
149
+ .join('&');
150
+ } else {
151
+ v = value.join(arrayFormat ? ',' : arrayFormat);
152
+ }
153
+ } else if (value instanceof Date) {
154
+ v = value.toJSON();
155
+ } else {
156
+ v = value == null ? '' : `${value}`;
157
+ }
158
+
159
+ return `${key}=${encodeURIComponent(v)}`;
160
+ })
161
+ .join('&');
162
+
163
+ return this.addUrlParams(params);
164
+ } else {
165
+ const urlObj = new URL(this);
166
+ Object.entries(data).forEach(([key, value]) => {
167
+ if (Array.isArray(value)) {
168
+ if (arrayFormat == null || arrayFormat === false) {
169
+ value.forEach((item) => {
170
+ urlObj.searchParams.append(key, `${item}`);
171
+ });
172
+ } else {
173
+ urlObj.searchParams.append(
174
+ key,
175
+ value.join(arrayFormat ? ',' : arrayFormat)
176
+ );
177
+ }
178
+ } else if (value instanceof Date) {
179
+ urlObj.searchParams.append(key, value.toJSON());
180
+ } else {
181
+ urlObj.searchParams.append(
182
+ key,
183
+ `${value == null ? '' : value}`
184
+ );
185
+ }
186
+ });
187
+ return urlObj.toString();
188
+ }
189
+ };
190
+
77
191
  String.prototype.containChinese = function (this: string): boolean {
78
192
  const regExp =
79
193
  /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/g;