@etsoo/shared 1.1.66 → 1.1.68

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.
@@ -9,11 +9,14 @@ test('Tests for addBlankItem', () => {
9
9
  expect(options.length).toBe(3);
10
10
  expect(options[0].id).toBe('');
11
11
  expect(options[0].name).toBe('---');
12
+ Utils.addBlankItem(options, 'id', 'name');
13
+ expect(options.length).toBe(3);
12
14
  });
13
15
 
14
16
  test('Tests for correctTypes', () => {
15
17
  const input = {
16
18
  id: '1',
19
+ ignore: '2',
17
20
  price: '6.0',
18
21
  amount: '',
19
22
  date: '2022/01/28',
@@ -53,7 +53,7 @@ export declare namespace Utils {
53
53
  * @param labelField Label field, default is label
54
54
  * @param blankLabel Blank label, default is ---
55
55
  */
56
- function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
56
+ function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
57
57
  /**
58
58
  * Base64 chars to number
59
59
  * @param base64Chars Base64 chars
@@ -66,7 +66,7 @@ export declare namespace Utils {
66
66
  * @param fields Fields to correct
67
67
  */
68
68
  function correctTypes<T extends object, F extends {
69
- [P in keyof T]: DataTypes.BasicNames;
69
+ [P in keyof T]?: DataTypes.BasicNames;
70
70
  }>(input: T, fields: F): void;
71
71
  /**
72
72
  * Two values equal
package/lib/cjs/Utils.js CHANGED
@@ -60,11 +60,16 @@ var Utils;
60
60
  * @param blankLabel Blank label, default is ---
61
61
  */
62
62
  function addBlankItem(options, idField, labelField, blankLabel) {
63
- const blankItem = {
64
- [idField !== null && idField !== void 0 ? idField : 'id']: '',
65
- [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
66
- };
67
- options.unshift(blankItem);
63
+ // Avoid duplicate blank items
64
+ idField !== null && idField !== void 0 ? idField : (idField = 'id');
65
+ if (options.length === 0 || Reflect.get(options[0], idField) !== '') {
66
+ const blankItem = {
67
+ [idField]: '',
68
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
69
+ };
70
+ options.unshift(blankItem);
71
+ }
72
+ return options;
68
73
  }
69
74
  Utils.addBlankItem = addBlankItem;
70
75
  /**
@@ -88,8 +93,11 @@ var Utils;
88
93
  */
89
94
  function correctTypes(input, fields) {
90
95
  for (const field in fields) {
96
+ const type = fields[field];
97
+ if (type == null)
98
+ continue;
91
99
  const value = Reflect.get(input, field);
92
- const newValue = DataTypes_1.DataTypes.convertByType(value, fields[field]);
100
+ const newValue = DataTypes_1.DataTypes.convertByType(value, type);
93
101
  if (newValue !== value) {
94
102
  Reflect.set(input, field, newValue);
95
103
  }
@@ -53,7 +53,7 @@ export declare namespace Utils {
53
53
  * @param labelField Label field, default is label
54
54
  * @param blankLabel Blank label, default is ---
55
55
  */
56
- function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
56
+ function addBlankItem<T extends object>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): T[];
57
57
  /**
58
58
  * Base64 chars to number
59
59
  * @param base64Chars Base64 chars
@@ -66,7 +66,7 @@ export declare namespace Utils {
66
66
  * @param fields Fields to correct
67
67
  */
68
68
  function correctTypes<T extends object, F extends {
69
- [P in keyof T]: DataTypes.BasicNames;
69
+ [P in keyof T]?: DataTypes.BasicNames;
70
70
  }>(input: T, fields: F): void;
71
71
  /**
72
72
  * Two values equal
package/lib/mjs/Utils.js CHANGED
@@ -57,11 +57,16 @@ export var Utils;
57
57
  * @param blankLabel Blank label, default is ---
58
58
  */
59
59
  function addBlankItem(options, idField, labelField, blankLabel) {
60
- const blankItem = {
61
- [idField !== null && idField !== void 0 ? idField : 'id']: '',
62
- [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
63
- };
64
- options.unshift(blankItem);
60
+ // Avoid duplicate blank items
61
+ idField !== null && idField !== void 0 ? idField : (idField = 'id');
62
+ if (options.length === 0 || Reflect.get(options[0], idField) !== '') {
63
+ const blankItem = {
64
+ [idField]: '',
65
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
66
+ };
67
+ options.unshift(blankItem);
68
+ }
69
+ return options;
65
70
  }
66
71
  Utils.addBlankItem = addBlankItem;
67
72
  /**
@@ -85,8 +90,11 @@ export var Utils;
85
90
  */
86
91
  function correctTypes(input, fields) {
87
92
  for (const field in fields) {
93
+ const type = fields[field];
94
+ if (type == null)
95
+ continue;
88
96
  const value = Reflect.get(input, field);
89
- const newValue = DataTypes.convertByType(value, fields[field]);
97
+ const newValue = DataTypes.convertByType(value, type);
90
98
  if (newValue !== value) {
91
99
  Reflect.set(input, field, newValue);
92
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.66",
3
+ "version": "1.1.68",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,13 +55,13 @@
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
57
  "@types/jest": "^29.2.0",
58
- "@typescript-eslint/eslint-plugin": "^5.40.1",
59
- "@typescript-eslint/parser": "^5.40.1",
60
- "eslint": "^8.25.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.41.0",
59
+ "@typescript-eslint/parser": "^5.41.0",
60
+ "eslint": "^8.26.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.26.0",
63
- "jest": "^29.2.1",
64
- "jest-environment-jsdom": "^29.2.1",
63
+ "jest": "^29.2.2",
64
+ "jest-environment-jsdom": "^29.2.2",
65
65
  "ts-jest": "^29.0.3",
66
66
  "typescript": "^4.8.4"
67
67
  }
package/src/Utils.ts CHANGED
@@ -125,12 +125,18 @@ export namespace Utils {
125
125
  labelField?: unknown,
126
126
  blankLabel?: string
127
127
  ) {
128
- const blankItem: any = {
129
- [idField ?? 'id']: '',
130
- [typeof labelField === 'string' ? labelField : 'label']:
131
- blankLabel ?? '---'
132
- };
133
- options.unshift(blankItem);
128
+ // Avoid duplicate blank items
129
+ idField ??= 'id';
130
+ if (options.length === 0 || Reflect.get(options[0], idField) !== '') {
131
+ const blankItem: any = {
132
+ [idField]: '',
133
+ [typeof labelField === 'string' ? labelField : 'label']:
134
+ blankLabel ?? '---'
135
+ };
136
+ options.unshift(blankItem);
137
+ }
138
+
139
+ return options;
134
140
  }
135
141
 
136
142
  /**
@@ -156,11 +162,13 @@ export namespace Utils {
156
162
  */
157
163
  export function correctTypes<
158
164
  T extends object,
159
- F extends { [P in keyof T]: DataTypes.BasicNames }
165
+ F extends { [P in keyof T]?: DataTypes.BasicNames }
160
166
  >(input: T, fields: F) {
161
167
  for (const field in fields) {
168
+ const type = fields[field];
169
+ if (type == null) continue;
162
170
  const value = Reflect.get(input, field);
163
- const newValue = DataTypes.convertByType(value, fields[field]);
171
+ const newValue = DataTypes.convertByType(value, type);
164
172
  if (newValue !== value) {
165
173
  Reflect.set(input, field, newValue);
166
174
  }