@etsoo/shared 1.1.8 → 1.1.9

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
@@ -151,6 +151,7 @@ String and other related utilities
151
151
 
152
152
  |Name|Description|
153
153
  |---:|---|
154
+ |addBlankItem|Add blank item to collection|
154
155
  |charsToNumber|Base64 chars to number|
155
156
  |correctTypes|Correct object's property value type|
156
157
  |equals|Two values equal|
@@ -1,5 +1,16 @@
1
1
  import { Utils } from '../src/Utils';
2
2
 
3
+ test('Tests for addBlankItem', () => {
4
+ const options = [
5
+ { id: 1, name: 'a' },
6
+ { id: 2, name: 'b' }
7
+ ];
8
+ Utils.addBlankItem(options, 'id', 'name');
9
+ expect(options.length).toBe(3);
10
+ expect(options[0].id).toBe('');
11
+ expect(options[0].name).toBe('---');
12
+ });
13
+
3
14
  test('Tests for correctTypes', () => {
4
15
  const input = {
5
16
  id: '1',
@@ -35,6 +35,14 @@ declare global {
35
35
  * Utilities
36
36
  */
37
37
  export declare namespace Utils {
38
+ /**
39
+ * Add blank item to collection
40
+ * @param options Options
41
+ * @param idField Id field, default is id
42
+ * @param labelField Label field, default is label
43
+ * @param blankLabel Blank label, default is ---
44
+ */
45
+ function addBlankItem<T extends {}>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
38
46
  /**
39
47
  * Base64 chars to number
40
48
  * @param base64Chars Base64 chars
package/lib/cjs/Utils.js CHANGED
@@ -29,6 +29,21 @@ String.prototype.removeNonLetters = function () {
29
29
  */
30
30
  var Utils;
31
31
  (function (Utils) {
32
+ /**
33
+ * Add blank item to collection
34
+ * @param options Options
35
+ * @param idField Id field, default is id
36
+ * @param labelField Label field, default is label
37
+ * @param blankLabel Blank label, default is ---
38
+ */
39
+ function addBlankItem(options, idField, labelField, blankLabel) {
40
+ const blankItem = {
41
+ [idField !== null && idField !== void 0 ? idField : 'id']: '',
42
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
43
+ };
44
+ options.unshift(blankItem);
45
+ }
46
+ Utils.addBlankItem = addBlankItem;
32
47
  /**
33
48
  * Base64 chars to number
34
49
  * @param base64Chars Base64 chars
@@ -35,6 +35,14 @@ declare global {
35
35
  * Utilities
36
36
  */
37
37
  export declare namespace Utils {
38
+ /**
39
+ * Add blank item to collection
40
+ * @param options Options
41
+ * @param idField Id field, default is id
42
+ * @param labelField Label field, default is label
43
+ * @param blankLabel Blank label, default is ---
44
+ */
45
+ function addBlankItem<T extends {}>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
38
46
  /**
39
47
  * Base64 chars to number
40
48
  * @param base64Chars Base64 chars
package/lib/mjs/Utils.js CHANGED
@@ -26,6 +26,21 @@ String.prototype.removeNonLetters = function () {
26
26
  */
27
27
  export var Utils;
28
28
  (function (Utils) {
29
+ /**
30
+ * Add blank item to collection
31
+ * @param options Options
32
+ * @param idField Id field, default is id
33
+ * @param labelField Label field, default is label
34
+ * @param blankLabel Blank label, default is ---
35
+ */
36
+ function addBlankItem(options, idField, labelField, blankLabel) {
37
+ const blankItem = {
38
+ [idField !== null && idField !== void 0 ? idField : 'id']: '',
39
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
40
+ };
41
+ options.unshift(blankItem);
42
+ }
43
+ Utils.addBlankItem = addBlankItem;
29
44
  /**
30
45
  * Base64 chars to number
31
46
  * @param base64Chars Base64 chars
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -57,7 +57,7 @@
57
57
  "@types/jest": "^27.4.0",
58
58
  "@typescript-eslint/eslint-plugin": "^5.10.1",
59
59
  "@typescript-eslint/parser": "^5.10.1",
60
- "eslint": "^8.7.0",
60
+ "eslint": "^8.8.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
63
  "jest": "^27.4.7",
package/src/Utils.ts CHANGED
@@ -77,6 +77,27 @@ String.prototype.removeNonLetters = function (this: string) {
77
77
  * Utilities
78
78
  */
79
79
  export namespace Utils {
80
+ /**
81
+ * Add blank item to collection
82
+ * @param options Options
83
+ * @param idField Id field, default is id
84
+ * @param labelField Label field, default is label
85
+ * @param blankLabel Blank label, default is ---
86
+ */
87
+ export function addBlankItem<T extends {}>(
88
+ options: T[],
89
+ idField?: string | keyof T,
90
+ labelField?: unknown,
91
+ blankLabel?: string
92
+ ) {
93
+ const blankItem: any = {
94
+ [idField ?? 'id']: '',
95
+ [typeof labelField === 'string' ? labelField : 'label']:
96
+ blankLabel ?? '---'
97
+ };
98
+ options.unshift(blankItem);
99
+ }
100
+
80
101
  /**
81
102
  * Base64 chars to number
82
103
  * @param base64Chars Base64 chars