@etsoo/shared 1.0.56 → 1.0.60
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 +1 -0
- package/__tests__/DomUtils.ts +11 -1
- package/__tests__/Utils.ts +16 -0
- package/lib/cjs/DomUtils.js +1 -3
- package/lib/cjs/Utils.d.ts +27 -0
- package/lib/cjs/Utils.js +48 -0
- package/lib/mjs/DomUtils.js +1 -3
- package/lib/mjs/Utils.d.ts +27 -0
- package/lib/mjs/Utils.js +48 -0
- package/package.json +6 -1
- package/src/DomUtils.ts +3 -3
- package/src/Utils.ts +71 -0
package/README.md
CHANGED
|
@@ -132,6 +132,7 @@ String and other related utilities
|
|
|
132
132
|
|mergeFormData|Merge form data to primary one|
|
|
133
133
|
|mergeClasses|Merge class names|
|
|
134
134
|
|newGUID|Create a GUID|
|
|
135
|
+
|objectEqual|Test two objects are equal or not|
|
|
135
136
|
|parseString|Parse string (JSON) to specific type|
|
|
136
137
|
|setLabels|Set source with new labels|
|
|
137
138
|
|snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
|
package/__tests__/DomUtils.ts
CHANGED
|
@@ -133,17 +133,27 @@ test('Tests for dataAs', () => {
|
|
|
133
133
|
formData.append('price', '34.25');
|
|
134
134
|
formData.append('options', '1,2,3,4');
|
|
135
135
|
formData.append('memo', 'Memo');
|
|
136
|
+
formData.append('email', 'a@b');
|
|
137
|
+
formData.append('email', 'c@d');
|
|
138
|
+
formData.append('code', '123');
|
|
139
|
+
formData.append('code', '456');
|
|
136
140
|
|
|
137
141
|
const data = DomUtils.dataAs(formData, {
|
|
138
142
|
id: 'number',
|
|
139
143
|
name: 'string',
|
|
140
144
|
price: 'number',
|
|
141
|
-
options: 'number[]'
|
|
145
|
+
options: 'number[]',
|
|
146
|
+
email: 'string[]',
|
|
147
|
+
code: 'number[]'
|
|
142
148
|
});
|
|
143
149
|
|
|
144
150
|
expect(data.id).toStrictEqual(1234);
|
|
145
151
|
expect(data.options?.length).toStrictEqual(4);
|
|
146
152
|
expect(data.options![0]).toStrictEqual(1);
|
|
153
|
+
|
|
154
|
+
expect(data.email).toEqual(['a@b', 'c@d']);
|
|
155
|
+
expect(data.code?.length).toStrictEqual(2);
|
|
156
|
+
expect(data.code).toEqual([123, 456]);
|
|
147
157
|
expect(data).not.toHaveProperty('memo', 'Memo');
|
|
148
158
|
|
|
149
159
|
const keepSourceData = DomUtils.dataAs(
|
package/__tests__/Utils.ts
CHANGED
|
@@ -23,6 +23,13 @@ test('Tests for getDataChanges', () => {
|
|
|
23
23
|
expect(input.amount).toBeUndefined();
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
+
test('Tests for formatString', () => {
|
|
27
|
+
const template = '{0} is first item, {1} is second item, {0} repeat';
|
|
28
|
+
const result = 'aa is first item, bb is second item, aa repeat';
|
|
29
|
+
expect(Utils.formatString(template, 'aa', 'bb')).toBe(result);
|
|
30
|
+
expect(template.format('aa', 'bb')).toBe(result);
|
|
31
|
+
});
|
|
32
|
+
|
|
26
33
|
test('Tests for formatLowerLetter', () => {
|
|
27
34
|
expect(Utils.formatLowerLetter('HelloWorld')).toBe('helloWorld');
|
|
28
35
|
});
|
|
@@ -51,6 +58,15 @@ test('Tests for removeNonLetters', () => {
|
|
|
51
58
|
expect(Utils.removeNonLetters('1234-5678@abc.')).toBe('12345678abc');
|
|
52
59
|
});
|
|
53
60
|
|
|
61
|
+
test('Tests for objectEqual', () => {
|
|
62
|
+
const obj1 = { a: 1, b: 'abc', c: true, d: null };
|
|
63
|
+
const obj2 = { a: '1', b: 'abc', c: true };
|
|
64
|
+
expect(Utils.objectEqual(obj1, obj2)).toBeFalsy();
|
|
65
|
+
expect(Utils.objectEqual(obj1, obj2, [], 0)).toBeTruthy();
|
|
66
|
+
expect(Utils.objectEqual(obj1, obj2, ['a'])).toBeTruthy();
|
|
67
|
+
expect(Utils.objectEqual(obj1, obj2, ['a'], 2)).toBeFalsy();
|
|
68
|
+
});
|
|
69
|
+
|
|
54
70
|
test('Tests for parseString', () => {
|
|
55
71
|
expect(Utils.parseString('test', '')).toBe('test');
|
|
56
72
|
expect(Utils.parseString('true', false)).toBe(true);
|
package/lib/cjs/DomUtils.js
CHANGED
|
@@ -94,9 +94,7 @@ var DomUtils;
|
|
|
94
94
|
// Properties
|
|
95
95
|
const properties = Object.keys(template);
|
|
96
96
|
// Entries
|
|
97
|
-
const entries = isFormData(source)
|
|
98
|
-
? source.entries()
|
|
99
|
-
: Object.entries(source);
|
|
97
|
+
const entries = Object.entries(isFormData(source) ? formDataToObject(source) : source);
|
|
100
98
|
for (const [key, value] of entries) {
|
|
101
99
|
// Is included or keepSource
|
|
102
100
|
const property = (_a = properties.find((p) => p.localeCompare(key, 'en', { sensitivity: 'base' }) ===
|
package/lib/cjs/Utils.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
|
+
declare global {
|
|
3
|
+
interface String {
|
|
4
|
+
/**
|
|
5
|
+
* Format string
|
|
6
|
+
* @param this Template
|
|
7
|
+
* @param parameters Parameters to fill the template
|
|
8
|
+
* @returns Result
|
|
9
|
+
*/
|
|
10
|
+
format(this: string, ...parameters: string[]): string;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
2
13
|
/**
|
|
3
14
|
* Utilities
|
|
4
15
|
*/
|
|
@@ -8,6 +19,13 @@ export declare namespace Utils {
|
|
|
8
19
|
* @param word Word
|
|
9
20
|
*/
|
|
10
21
|
function formatLowerLetter(word: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Format string
|
|
24
|
+
* @param template Template with {0}, {1}, ...
|
|
25
|
+
* @param parameters Parameters to fill the template
|
|
26
|
+
* @returns Result
|
|
27
|
+
*/
|
|
28
|
+
function formatString(template: string, ...parameters: string[]): string;
|
|
11
29
|
/**
|
|
12
30
|
* Format word's first letter to upper case
|
|
13
31
|
* @param word Word
|
|
@@ -41,6 +59,15 @@ export declare namespace Utils {
|
|
|
41
59
|
* Create a GUID
|
|
42
60
|
*/
|
|
43
61
|
function newGUID(): string;
|
|
62
|
+
/**
|
|
63
|
+
* Test two objects are equal or not
|
|
64
|
+
* @param obj1 Object 1
|
|
65
|
+
* @param obj2 Object 2
|
|
66
|
+
* @param ignoreFields Ignored fields
|
|
67
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
68
|
+
* @returns Result
|
|
69
|
+
*/
|
|
70
|
+
function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
|
|
44
71
|
/**
|
|
45
72
|
* Parse string (JSON) to specific type
|
|
46
73
|
* @param input Input string
|
package/lib/cjs/Utils.js
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Utils = void 0;
|
|
4
4
|
const DataTypes_1 = require("./DataTypes");
|
|
5
|
+
String.prototype.format = function (...parameters) {
|
|
6
|
+
let template = this;
|
|
7
|
+
parameters.forEach((value, index) => {
|
|
8
|
+
template = template.replace(new RegExp(`\\{${index}\\}`, 'g'), value);
|
|
9
|
+
});
|
|
10
|
+
return template;
|
|
11
|
+
};
|
|
5
12
|
/**
|
|
6
13
|
* Utilities
|
|
7
14
|
*/
|
|
@@ -15,6 +22,16 @@ var Utils;
|
|
|
15
22
|
return word.charAt(0).toLowerCase() + word.slice(1);
|
|
16
23
|
}
|
|
17
24
|
Utils.formatLowerLetter = formatLowerLetter;
|
|
25
|
+
/**
|
|
26
|
+
* Format string
|
|
27
|
+
* @param template Template with {0}, {1}, ...
|
|
28
|
+
* @param parameters Parameters to fill the template
|
|
29
|
+
* @returns Result
|
|
30
|
+
*/
|
|
31
|
+
function formatString(template, ...parameters) {
|
|
32
|
+
return template.format(...parameters);
|
|
33
|
+
}
|
|
34
|
+
Utils.formatString = formatString;
|
|
18
35
|
/**
|
|
19
36
|
* Format word's first letter to upper case
|
|
20
37
|
* @param word Word
|
|
@@ -98,6 +115,37 @@ var Utils;
|
|
|
98
115
|
});
|
|
99
116
|
}
|
|
100
117
|
Utils.newGUID = newGUID;
|
|
118
|
+
/**
|
|
119
|
+
* Test two objects are equal or not
|
|
120
|
+
* @param obj1 Object 1
|
|
121
|
+
* @param obj2 Object 2
|
|
122
|
+
* @param ignoreFields Ignored fields
|
|
123
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
124
|
+
* @returns Result
|
|
125
|
+
*/
|
|
126
|
+
function objectEqual(obj1, obj2, ignoreFields = [], strict = 1) {
|
|
127
|
+
// Keys
|
|
128
|
+
const keys = new Set([
|
|
129
|
+
...Object.keys(obj1).filter((item) => !ignoreFields.includes(item)),
|
|
130
|
+
...Object.keys(obj2).filter((item) => !ignoreFields.includes(item))
|
|
131
|
+
]);
|
|
132
|
+
for (const key of keys) {
|
|
133
|
+
// Values
|
|
134
|
+
const v1 = Reflect.get(obj1, key);
|
|
135
|
+
const v2 = Reflect.get(obj2, key);
|
|
136
|
+
// Null and undefined case
|
|
137
|
+
if (v1 == null && v2 == null && strict <= 1)
|
|
138
|
+
continue;
|
|
139
|
+
// 1 and '1' case
|
|
140
|
+
if (strict === 0 && v1 == v2)
|
|
141
|
+
continue;
|
|
142
|
+
// Strict equal
|
|
143
|
+
if (v1 !== v2)
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
Utils.objectEqual = objectEqual;
|
|
101
149
|
/**
|
|
102
150
|
* Parse string (JSON) to specific type
|
|
103
151
|
* @param input Input string
|
package/lib/mjs/DomUtils.js
CHANGED
|
@@ -91,9 +91,7 @@ export var DomUtils;
|
|
|
91
91
|
// Properties
|
|
92
92
|
const properties = Object.keys(template);
|
|
93
93
|
// Entries
|
|
94
|
-
const entries = isFormData(source)
|
|
95
|
-
? source.entries()
|
|
96
|
-
: Object.entries(source);
|
|
94
|
+
const entries = Object.entries(isFormData(source) ? formDataToObject(source) : source);
|
|
97
95
|
for (const [key, value] of entries) {
|
|
98
96
|
// Is included or keepSource
|
|
99
97
|
const property = (_a = properties.find((p) => p.localeCompare(key, 'en', { sensitivity: 'base' }) ===
|
package/lib/mjs/Utils.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
|
+
declare global {
|
|
3
|
+
interface String {
|
|
4
|
+
/**
|
|
5
|
+
* Format string
|
|
6
|
+
* @param this Template
|
|
7
|
+
* @param parameters Parameters to fill the template
|
|
8
|
+
* @returns Result
|
|
9
|
+
*/
|
|
10
|
+
format(this: string, ...parameters: string[]): string;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
2
13
|
/**
|
|
3
14
|
* Utilities
|
|
4
15
|
*/
|
|
@@ -8,6 +19,13 @@ export declare namespace Utils {
|
|
|
8
19
|
* @param word Word
|
|
9
20
|
*/
|
|
10
21
|
function formatLowerLetter(word: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Format string
|
|
24
|
+
* @param template Template with {0}, {1}, ...
|
|
25
|
+
* @param parameters Parameters to fill the template
|
|
26
|
+
* @returns Result
|
|
27
|
+
*/
|
|
28
|
+
function formatString(template: string, ...parameters: string[]): string;
|
|
11
29
|
/**
|
|
12
30
|
* Format word's first letter to upper case
|
|
13
31
|
* @param word Word
|
|
@@ -41,6 +59,15 @@ export declare namespace Utils {
|
|
|
41
59
|
* Create a GUID
|
|
42
60
|
*/
|
|
43
61
|
function newGUID(): string;
|
|
62
|
+
/**
|
|
63
|
+
* Test two objects are equal or not
|
|
64
|
+
* @param obj1 Object 1
|
|
65
|
+
* @param obj2 Object 2
|
|
66
|
+
* @param ignoreFields Ignored fields
|
|
67
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
68
|
+
* @returns Result
|
|
69
|
+
*/
|
|
70
|
+
function objectEqual(obj1: {}, obj2: {}, ignoreFields?: string[], strict?: number): boolean;
|
|
44
71
|
/**
|
|
45
72
|
* Parse string (JSON) to specific type
|
|
46
73
|
* @param input Input string
|
package/lib/mjs/Utils.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
|
+
String.prototype.format = function (...parameters) {
|
|
3
|
+
let template = this;
|
|
4
|
+
parameters.forEach((value, index) => {
|
|
5
|
+
template = template.replace(new RegExp(`\\{${index}\\}`, 'g'), value);
|
|
6
|
+
});
|
|
7
|
+
return template;
|
|
8
|
+
};
|
|
2
9
|
/**
|
|
3
10
|
* Utilities
|
|
4
11
|
*/
|
|
@@ -12,6 +19,16 @@ export var Utils;
|
|
|
12
19
|
return word.charAt(0).toLowerCase() + word.slice(1);
|
|
13
20
|
}
|
|
14
21
|
Utils.formatLowerLetter = formatLowerLetter;
|
|
22
|
+
/**
|
|
23
|
+
* Format string
|
|
24
|
+
* @param template Template with {0}, {1}, ...
|
|
25
|
+
* @param parameters Parameters to fill the template
|
|
26
|
+
* @returns Result
|
|
27
|
+
*/
|
|
28
|
+
function formatString(template, ...parameters) {
|
|
29
|
+
return template.format(...parameters);
|
|
30
|
+
}
|
|
31
|
+
Utils.formatString = formatString;
|
|
15
32
|
/**
|
|
16
33
|
* Format word's first letter to upper case
|
|
17
34
|
* @param word Word
|
|
@@ -95,6 +112,37 @@ export var Utils;
|
|
|
95
112
|
});
|
|
96
113
|
}
|
|
97
114
|
Utils.newGUID = newGUID;
|
|
115
|
+
/**
|
|
116
|
+
* Test two objects are equal or not
|
|
117
|
+
* @param obj1 Object 1
|
|
118
|
+
* @param obj2 Object 2
|
|
119
|
+
* @param ignoreFields Ignored fields
|
|
120
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
121
|
+
* @returns Result
|
|
122
|
+
*/
|
|
123
|
+
function objectEqual(obj1, obj2, ignoreFields = [], strict = 1) {
|
|
124
|
+
// Keys
|
|
125
|
+
const keys = new Set([
|
|
126
|
+
...Object.keys(obj1).filter((item) => !ignoreFields.includes(item)),
|
|
127
|
+
...Object.keys(obj2).filter((item) => !ignoreFields.includes(item))
|
|
128
|
+
]);
|
|
129
|
+
for (const key of keys) {
|
|
130
|
+
// Values
|
|
131
|
+
const v1 = Reflect.get(obj1, key);
|
|
132
|
+
const v2 = Reflect.get(obj2, key);
|
|
133
|
+
// Null and undefined case
|
|
134
|
+
if (v1 == null && v2 == null && strict <= 1)
|
|
135
|
+
continue;
|
|
136
|
+
// 1 and '1' case
|
|
137
|
+
if (strict === 0 && v1 == v2)
|
|
138
|
+
continue;
|
|
139
|
+
// Strict equal
|
|
140
|
+
if (v1 !== v2)
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
Utils.objectEqual = objectEqual;
|
|
98
146
|
/**
|
|
99
147
|
* Parse string (JSON) to specific type
|
|
100
148
|
* @param input Input string
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.60",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
"<rootDir>/__tests__/**/*.ts"
|
|
25
25
|
],
|
|
26
26
|
"testEnvironment": "jsdom",
|
|
27
|
+
"moduleFileExtensions": [
|
|
28
|
+
"js",
|
|
29
|
+
"ts",
|
|
30
|
+
"d.ts"
|
|
31
|
+
],
|
|
27
32
|
"transform": {
|
|
28
33
|
".+\\.ts$": "ts-jest"
|
|
29
34
|
}
|
package/src/DomUtils.ts
CHANGED
|
@@ -108,9 +108,9 @@ export namespace DomUtils {
|
|
|
108
108
|
const properties = Object.keys(template);
|
|
109
109
|
|
|
110
110
|
// Entries
|
|
111
|
-
const entries =
|
|
112
|
-
? source
|
|
113
|
-
|
|
111
|
+
const entries = Object.entries(
|
|
112
|
+
isFormData(source) ? formDataToObject(source) : source
|
|
113
|
+
);
|
|
114
114
|
|
|
115
115
|
for (const [key, value] of entries) {
|
|
116
116
|
// Is included or keepSource
|
package/src/Utils.ts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import { DataTypes } from './DataTypes';
|
|
2
2
|
|
|
3
|
+
declare global {
|
|
4
|
+
interface String {
|
|
5
|
+
/**
|
|
6
|
+
* Format string
|
|
7
|
+
* @param this Template
|
|
8
|
+
* @param parameters Parameters to fill the template
|
|
9
|
+
* @returns Result
|
|
10
|
+
*/
|
|
11
|
+
format(this: string, ...parameters: string[]): string;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
String.prototype.format = function (
|
|
16
|
+
this: string,
|
|
17
|
+
...parameters: string[]
|
|
18
|
+
): string {
|
|
19
|
+
let template = this;
|
|
20
|
+
parameters.forEach((value, index) => {
|
|
21
|
+
template = template.replace(new RegExp(`\\{${index}\\}`, 'g'), value);
|
|
22
|
+
});
|
|
23
|
+
return template;
|
|
24
|
+
};
|
|
25
|
+
|
|
3
26
|
/**
|
|
4
27
|
* Utilities
|
|
5
28
|
*/
|
|
@@ -12,6 +35,16 @@ export namespace Utils {
|
|
|
12
35
|
return word.charAt(0).toLowerCase() + word.slice(1);
|
|
13
36
|
}
|
|
14
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Format string
|
|
40
|
+
* @param template Template with {0}, {1}, ...
|
|
41
|
+
* @param parameters Parameters to fill the template
|
|
42
|
+
* @returns Result
|
|
43
|
+
*/
|
|
44
|
+
export function formatString(template: string, ...parameters: string[]) {
|
|
45
|
+
return template.format(...parameters);
|
|
46
|
+
}
|
|
47
|
+
|
|
15
48
|
/**
|
|
16
49
|
* Format word's first letter to upper case
|
|
17
50
|
* @param word Word
|
|
@@ -115,6 +148,44 @@ export namespace Utils {
|
|
|
115
148
|
});
|
|
116
149
|
}
|
|
117
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Test two objects are equal or not
|
|
153
|
+
* @param obj1 Object 1
|
|
154
|
+
* @param obj2 Object 2
|
|
155
|
+
* @param ignoreFields Ignored fields
|
|
156
|
+
* @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
|
|
157
|
+
* @returns Result
|
|
158
|
+
*/
|
|
159
|
+
export function objectEqual(
|
|
160
|
+
obj1: {},
|
|
161
|
+
obj2: {},
|
|
162
|
+
ignoreFields: string[] = [],
|
|
163
|
+
strict = 1
|
|
164
|
+
) {
|
|
165
|
+
// Keys
|
|
166
|
+
const keys = new Set([
|
|
167
|
+
...Object.keys(obj1).filter((item) => !ignoreFields.includes(item)),
|
|
168
|
+
...Object.keys(obj2).filter((item) => !ignoreFields.includes(item))
|
|
169
|
+
]);
|
|
170
|
+
|
|
171
|
+
for (const key of keys) {
|
|
172
|
+
// Values
|
|
173
|
+
const v1 = Reflect.get(obj1, key);
|
|
174
|
+
const v2 = Reflect.get(obj2, key);
|
|
175
|
+
|
|
176
|
+
// Null and undefined case
|
|
177
|
+
if (v1 == null && v2 == null && strict <= 1) continue;
|
|
178
|
+
|
|
179
|
+
// 1 and '1' case
|
|
180
|
+
if (strict === 0 && v1 == v2) continue;
|
|
181
|
+
|
|
182
|
+
// Strict equal
|
|
183
|
+
if (v1 !== v2) return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
118
189
|
/**
|
|
119
190
|
* Parse string (JSON) to specific type
|
|
120
191
|
* @param input Input string
|