@etsoo/shared 1.1.67 → 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,6 +9,8 @@ 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', () => {
@@ -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
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
  /**
@@ -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
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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.67",
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",
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
  /**