@etsoo/shared 1.1.16 → 1.1.19

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
@@ -190,5 +190,6 @@ String and other related utilities
190
190
  |objectUpdated|Get the new object's updated fields contrast to the previous object|
191
191
  |parseString|Parse string (JSON) to specific type|
192
192
  |removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
193
+ |replaceNullOrEmpty|Replace null or empty with default value|
193
194
  |setLabels|Set source with new labels|
194
195
  |snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
@@ -1,4 +1,5 @@
1
1
  import { EColor } from '../src/types/EColor';
2
+ import { ColorUtils } from '../src/ColorUtils';
2
3
 
3
4
  test('Tests for parse', () => {
4
5
  // Arrange & act
@@ -13,6 +14,13 @@ test('Tests for parse', () => {
13
14
  });
14
15
 
15
16
  test('Tests for getColors', () => {
16
- const colors = EColor.getColors(undefined, 128);
17
+ const colors = ColorUtils.getColors(undefined, 128);
17
18
  expect(colors.length).toBe(8);
18
19
  });
20
+
21
+ test('Tests for toRGBColor', () => {
22
+ const color = new EColor(0, 0, 0);
23
+ expect(color.toRGBColor()).toBe('RGB(0, 0, 0)');
24
+ expect(color.toRGBColor(0.1)).toBe('RGBA(0, 0, 0, 0.1)');
25
+ expect(color.alpha).toBeUndefined();
26
+ });
@@ -126,6 +126,12 @@ test('Tests for removeNonLetters', () => {
126
126
  expect(input.removeNonLetters()).toBe(result);
127
127
  });
128
128
 
129
+ test('Tests for replaceNullOrEmpty', () => {
130
+ expect(Utils.replaceNullOrEmpty('a', 's')).toBe('a');
131
+ expect(Utils.replaceNullOrEmpty(null, 's')).toBe('s');
132
+ expect(Utils.replaceNullOrEmpty(' ', 's')).toBe('s');
133
+ });
134
+
129
135
  test('Tests for objectEqual', () => {
130
136
  const obj1 = { a: 1, b: 'abc', c: true, d: null, f: [1, 2] };
131
137
  const obj2 = { a: '1', b: 'abc', c: true, f: [1, 2] };
@@ -0,0 +1,23 @@
1
+ import { EColor } from './types/EColor';
2
+ /**
3
+ * Color utils
4
+ */
5
+ export declare namespace ColorUtils {
6
+ /**
7
+ * Get HEX or RGB colors
8
+ * @param init Initial color
9
+ * @param factor Increase factor
10
+ * @param adjustOrder Adjust order to increase difference
11
+ * @param hex to HEX or not
12
+ * @returns Result
13
+ */
14
+ function getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
15
+ /**
16
+ * Get EColors
17
+ * @param init Initial color
18
+ * @param factor Increase factor
19
+ * @param adjustOrder Adjust order to increase difference
20
+ * @returns Result
21
+ */
22
+ function getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
23
+ }
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ColorUtils = void 0;
4
+ const EColor_1 = require("./types/EColor");
5
+ /**
6
+ * Color utils
7
+ */
8
+ var ColorUtils;
9
+ (function (ColorUtils) {
10
+ /**
11
+ * Get HEX or RGB colors
12
+ * @param init Initial color
13
+ * @param factor Increase factor
14
+ * @param adjustOrder Adjust order to increase difference
15
+ * @param hex to HEX or not
16
+ * @returns Result
17
+ */
18
+ function getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
19
+ return getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
20
+ }
21
+ ColorUtils.getColors = getColors;
22
+ /**
23
+ * Get EColors
24
+ * @param init Initial color
25
+ * @param factor Increase factor
26
+ * @param adjustOrder Adjust order to increase difference
27
+ * @returns Result
28
+ */
29
+ function getEColors(init = '#000', factor = 51, adjustOrder = true) {
30
+ var _a;
31
+ // Init color
32
+ const initColor = (_a = EColor_1.EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor_1.EColor(0, 0, 0);
33
+ // Factors elements
34
+ // 51 = '00', '33', '66', '99', 'cc', 'ff'
35
+ const factors = [];
36
+ let f = 0;
37
+ while (f <= 255) {
38
+ factors.push(f);
39
+ f += factor;
40
+ }
41
+ // RGB loop
42
+ const colors = [initColor];
43
+ for (const r of factors) {
44
+ for (const g of factors) {
45
+ for (const b of factors) {
46
+ colors.push(initColor.clone(r, g, b));
47
+ }
48
+ }
49
+ }
50
+ // Non-nullable colors
51
+ const nColors = colors.filter((color) => color != null);
52
+ // Adjust order
53
+ if (adjustOrder) {
54
+ const firstColor = nColors.shift();
55
+ if (firstColor) {
56
+ let color = firstColor;
57
+ const newColors = [color];
58
+ while (nColors.length > 0) {
59
+ const result = nColors.reduce((p, c, index) => {
60
+ const delta = color.getDeltaValue(c);
61
+ if (delta != null && delta > p.delta) {
62
+ p.delta = delta;
63
+ p.color = c;
64
+ p.index = index;
65
+ }
66
+ return p;
67
+ }, { delta: 0, color, index: -1 });
68
+ if (result.delta > 0) {
69
+ color = result.color;
70
+ newColors.push(color);
71
+ nColors.splice(result.index, 1);
72
+ }
73
+ }
74
+ return newColors;
75
+ }
76
+ }
77
+ return nColors;
78
+ }
79
+ ColorUtils.getEColors = getEColors;
80
+ })(ColorUtils = exports.ColorUtils || (exports.ColorUtils = {}));
@@ -189,6 +189,13 @@ export declare namespace Utils {
189
189
  * @returns Result
190
190
  */
191
191
  const removeNonLetters: (input?: string | undefined) => string | undefined;
192
+ /**
193
+ * Replace null or empty with default value
194
+ * @param input Input string
195
+ * @param defaultValue Default value
196
+ * @returns Result
197
+ */
198
+ const replaceNullOrEmpty: (input: string | null | undefined, defaultValue: string) => string;
192
199
  /**
193
200
  * Set source with new labels
194
201
  * @param source Source
package/lib/cjs/Utils.js CHANGED
@@ -371,6 +371,17 @@ var Utils;
371
371
  Utils.removeNonLetters = (input) => {
372
372
  return input === null || input === void 0 ? void 0 : input.removeNonLetters();
373
373
  };
374
+ /**
375
+ * Replace null or empty with default value
376
+ * @param input Input string
377
+ * @param defaultValue Default value
378
+ * @returns Result
379
+ */
380
+ Utils.replaceNullOrEmpty = (input, defaultValue) => {
381
+ if (input == null || input.trim() === '')
382
+ return defaultValue;
383
+ return input;
384
+ };
374
385
  /**
375
386
  * Set source with new labels
376
387
  * @param source Source
@@ -4,6 +4,7 @@ export * from './types/FormData';
4
4
  export * from './storage/IStorage';
5
5
  export * from './storage/WindowStorage';
6
6
  export * from './DataTypes';
7
+ export * from './ColorUtils';
7
8
  export * from './DateUtils';
8
9
  export * from './DomUtils';
9
10
  export * from './ExtendUtils';
package/lib/cjs/index.js CHANGED
@@ -20,6 +20,7 @@ __exportStar(require("./types/FormData"), exports);
20
20
  __exportStar(require("./storage/IStorage"), exports);
21
21
  __exportStar(require("./storage/WindowStorage"), exports);
22
22
  __exportStar(require("./DataTypes"), exports);
23
+ __exportStar(require("./ColorUtils"), exports);
23
24
  __exportStar(require("./DateUtils"), exports);
24
25
  __exportStar(require("./DomUtils"), exports);
25
26
  __exportStar(require("./ExtendUtils"), exports);
@@ -13,23 +13,6 @@ export declare class EColor {
13
13
  * @returns Adjusted value
14
14
  */
15
15
  static adjust(value: number, adjust?: number): number;
16
- /**
17
- * Get HEX or RGB colors
18
- * @param init Initial color
19
- * @param factor Increase factor
20
- * @param adjustOrder Adjust order to increase difference
21
- * @param hex to HEX or not
22
- * @returns Result
23
- */
24
- static getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
25
- /**
26
- * Get EColors
27
- * @param init Initial color
28
- * @param factor Increase factor
29
- * @param adjustOrder Adjust order to increase difference
30
- * @returns Result
31
- */
32
- static getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
33
16
  /**
34
17
  * HEX string to integer value
35
18
  * @param hex HEX string
@@ -98,8 +81,8 @@ export declare class EColor {
98
81
  toLabValue(): [number, number, number];
99
82
  /**
100
83
  * To RGB color string
101
- * @param includeAlpha Include alpha or not
84
+ * @param alpha Alpha value, false means ignore it
102
85
  * @returns RGB color string
103
86
  */
104
- toRGBColor(includeAlpha?: boolean): string;
87
+ toRGBColor(alpha?: boolean | number): string;
105
88
  }
@@ -32,74 +32,6 @@ class EColor {
32
32
  return value % 255;
33
33
  return value;
34
34
  }
35
- /**
36
- * Get HEX or RGB colors
37
- * @param init Initial color
38
- * @param factor Increase factor
39
- * @param adjustOrder Adjust order to increase difference
40
- * @param hex to HEX or not
41
- * @returns Result
42
- */
43
- static getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
44
- return EColor.getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
45
- }
46
- /**
47
- * Get EColors
48
- * @param init Initial color
49
- * @param factor Increase factor
50
- * @param adjustOrder Adjust order to increase difference
51
- * @returns Result
52
- */
53
- static getEColors(init = '#000', factor = 51, adjustOrder = true) {
54
- var _a;
55
- // Init color
56
- const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
57
- // Factors elements
58
- // 51 = '00', '33', '66', '99', 'cc', 'ff'
59
- const factors = [];
60
- let f = 0;
61
- while (f <= 255) {
62
- factors.push(f);
63
- f += factor;
64
- }
65
- // RGB loop
66
- const colors = [initColor];
67
- for (const r of factors) {
68
- for (const g of factors) {
69
- for (const b of factors) {
70
- colors.push(initColor.clone(r, g, b));
71
- }
72
- }
73
- }
74
- // Non-nullable colors
75
- const nColors = colors.filter((color) => color != null);
76
- // Adjust order
77
- if (adjustOrder) {
78
- const firstColor = nColors.shift();
79
- if (firstColor) {
80
- let color = firstColor;
81
- const newColors = [color];
82
- while (nColors.length > 0) {
83
- const result = nColors.reduce((p, c, index) => {
84
- const delta = color.getDeltaValue(c);
85
- if (delta != null && delta > p.delta) {
86
- p.delta = delta;
87
- p.color = c;
88
- p.index = index;
89
- }
90
- return p;
91
- }, { delta: 0, color, index: -1 });
92
- if (result.delta > 0) {
93
- color = result.color;
94
- newColors.push(color);
95
- nColors.splice(result.index, 1);
96
- }
97
- }
98
- return newColors;
99
- }
100
- }
101
- return nColors;
102
- }
103
35
  /**
104
36
  * HEX string to integer value
105
37
  * @param hex HEX string
@@ -125,7 +57,7 @@ class EColor {
125
57
  // Null
126
58
  if (htmlColor == null)
127
59
  return undefined;
128
- htmlColor = htmlColor.toUpperCase();
60
+ htmlColor = htmlColor.trim().toUpperCase();
129
61
  // HEX color
130
62
  if (htmlColor.startsWith('#')) {
131
63
  htmlColor = htmlColor.substring(1);
@@ -250,15 +182,24 @@ class EColor {
250
182
  }
251
183
  /**
252
184
  * To RGB color string
253
- * @param includeAlpha Include alpha or not
185
+ * @param alpha Alpha value, false means ignore it
254
186
  * @returns RGB color string
255
187
  */
256
- toRGBColor(includeAlpha) {
257
- var _a;
258
- // Default case
259
- includeAlpha !== null && includeAlpha !== void 0 ? includeAlpha : (includeAlpha = this.alpha != null);
188
+ toRGBColor(alpha) {
189
+ // Decide
190
+ let includeAlpha, alphaValue = this.alpha;
191
+ if (typeof alpha === 'number') {
192
+ alphaValue = alpha;
193
+ includeAlpha = true;
194
+ }
195
+ else if (alpha == null) {
196
+ includeAlpha = this.alpha != null;
197
+ }
198
+ else {
199
+ includeAlpha = alpha;
200
+ }
260
201
  if (includeAlpha)
261
- return `RGBA(${this.r}, ${this.g}, ${this.b}, ${(_a = this.alpha) !== null && _a !== void 0 ? _a : 1})`;
202
+ return `RGBA(${this.r}, ${this.g}, ${this.b}, ${alphaValue !== null && alphaValue !== void 0 ? alphaValue : 1})`;
262
203
  return `RGB(${this.r}, ${this.g}, ${this.b})`;
263
204
  }
264
205
  }
@@ -0,0 +1,23 @@
1
+ import { EColor } from './types/EColor';
2
+ /**
3
+ * Color utils
4
+ */
5
+ export declare namespace ColorUtils {
6
+ /**
7
+ * Get HEX or RGB colors
8
+ * @param init Initial color
9
+ * @param factor Increase factor
10
+ * @param adjustOrder Adjust order to increase difference
11
+ * @param hex to HEX or not
12
+ * @returns Result
13
+ */
14
+ function getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
15
+ /**
16
+ * Get EColors
17
+ * @param init Initial color
18
+ * @param factor Increase factor
19
+ * @param adjustOrder Adjust order to increase difference
20
+ * @returns Result
21
+ */
22
+ function getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
23
+ }
@@ -0,0 +1,77 @@
1
+ import { EColor } from './types/EColor';
2
+ /**
3
+ * Color utils
4
+ */
5
+ export var ColorUtils;
6
+ (function (ColorUtils) {
7
+ /**
8
+ * Get HEX or RGB colors
9
+ * @param init Initial color
10
+ * @param factor Increase factor
11
+ * @param adjustOrder Adjust order to increase difference
12
+ * @param hex to HEX or not
13
+ * @returns Result
14
+ */
15
+ function getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
16
+ return getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
17
+ }
18
+ ColorUtils.getColors = getColors;
19
+ /**
20
+ * Get EColors
21
+ * @param init Initial color
22
+ * @param factor Increase factor
23
+ * @param adjustOrder Adjust order to increase difference
24
+ * @returns Result
25
+ */
26
+ function getEColors(init = '#000', factor = 51, adjustOrder = true) {
27
+ var _a;
28
+ // Init color
29
+ const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
30
+ // Factors elements
31
+ // 51 = '00', '33', '66', '99', 'cc', 'ff'
32
+ const factors = [];
33
+ let f = 0;
34
+ while (f <= 255) {
35
+ factors.push(f);
36
+ f += factor;
37
+ }
38
+ // RGB loop
39
+ const colors = [initColor];
40
+ for (const r of factors) {
41
+ for (const g of factors) {
42
+ for (const b of factors) {
43
+ colors.push(initColor.clone(r, g, b));
44
+ }
45
+ }
46
+ }
47
+ // Non-nullable colors
48
+ const nColors = colors.filter((color) => color != null);
49
+ // Adjust order
50
+ if (adjustOrder) {
51
+ const firstColor = nColors.shift();
52
+ if (firstColor) {
53
+ let color = firstColor;
54
+ const newColors = [color];
55
+ while (nColors.length > 0) {
56
+ const result = nColors.reduce((p, c, index) => {
57
+ const delta = color.getDeltaValue(c);
58
+ if (delta != null && delta > p.delta) {
59
+ p.delta = delta;
60
+ p.color = c;
61
+ p.index = index;
62
+ }
63
+ return p;
64
+ }, { delta: 0, color, index: -1 });
65
+ if (result.delta > 0) {
66
+ color = result.color;
67
+ newColors.push(color);
68
+ nColors.splice(result.index, 1);
69
+ }
70
+ }
71
+ return newColors;
72
+ }
73
+ }
74
+ return nColors;
75
+ }
76
+ ColorUtils.getEColors = getEColors;
77
+ })(ColorUtils || (ColorUtils = {}));
@@ -189,6 +189,13 @@ export declare namespace Utils {
189
189
  * @returns Result
190
190
  */
191
191
  const removeNonLetters: (input?: string | undefined) => string | undefined;
192
+ /**
193
+ * Replace null or empty with default value
194
+ * @param input Input string
195
+ * @param defaultValue Default value
196
+ * @returns Result
197
+ */
198
+ const replaceNullOrEmpty: (input: string | null | undefined, defaultValue: string) => string;
192
199
  /**
193
200
  * Set source with new labels
194
201
  * @param source Source
package/lib/mjs/Utils.js CHANGED
@@ -368,6 +368,17 @@ export var Utils;
368
368
  Utils.removeNonLetters = (input) => {
369
369
  return input === null || input === void 0 ? void 0 : input.removeNonLetters();
370
370
  };
371
+ /**
372
+ * Replace null or empty with default value
373
+ * @param input Input string
374
+ * @param defaultValue Default value
375
+ * @returns Result
376
+ */
377
+ Utils.replaceNullOrEmpty = (input, defaultValue) => {
378
+ if (input == null || input.trim() === '')
379
+ return defaultValue;
380
+ return input;
381
+ };
371
382
  /**
372
383
  * Set source with new labels
373
384
  * @param source Source
@@ -4,6 +4,7 @@ export * from './types/FormData';
4
4
  export * from './storage/IStorage';
5
5
  export * from './storage/WindowStorage';
6
6
  export * from './DataTypes';
7
+ export * from './ColorUtils';
7
8
  export * from './DateUtils';
8
9
  export * from './DomUtils';
9
10
  export * from './ExtendUtils';
package/lib/mjs/index.js CHANGED
@@ -4,6 +4,7 @@ export * from './types/FormData';
4
4
  export * from './storage/IStorage';
5
5
  export * from './storage/WindowStorage';
6
6
  export * from './DataTypes';
7
+ export * from './ColorUtils';
7
8
  export * from './DateUtils';
8
9
  export * from './DomUtils';
9
10
  export * from './ExtendUtils';
@@ -13,23 +13,6 @@ export declare class EColor {
13
13
  * @returns Adjusted value
14
14
  */
15
15
  static adjust(value: number, adjust?: number): number;
16
- /**
17
- * Get HEX or RGB colors
18
- * @param init Initial color
19
- * @param factor Increase factor
20
- * @param adjustOrder Adjust order to increase difference
21
- * @param hex to HEX or not
22
- * @returns Result
23
- */
24
- static getColors(init?: string, factor?: number, adjustOrder?: boolean, hex?: boolean): string[];
25
- /**
26
- * Get EColors
27
- * @param init Initial color
28
- * @param factor Increase factor
29
- * @param adjustOrder Adjust order to increase difference
30
- * @returns Result
31
- */
32
- static getEColors(init?: string, factor?: number, adjustOrder?: boolean): EColor[];
33
16
  /**
34
17
  * HEX string to integer value
35
18
  * @param hex HEX string
@@ -98,8 +81,8 @@ export declare class EColor {
98
81
  toLabValue(): [number, number, number];
99
82
  /**
100
83
  * To RGB color string
101
- * @param includeAlpha Include alpha or not
84
+ * @param alpha Alpha value, false means ignore it
102
85
  * @returns RGB color string
103
86
  */
104
- toRGBColor(includeAlpha?: boolean): string;
87
+ toRGBColor(alpha?: boolean | number): string;
105
88
  }
@@ -29,74 +29,6 @@ export class EColor {
29
29
  return value % 255;
30
30
  return value;
31
31
  }
32
- /**
33
- * Get HEX or RGB colors
34
- * @param init Initial color
35
- * @param factor Increase factor
36
- * @param adjustOrder Adjust order to increase difference
37
- * @param hex to HEX or not
38
- * @returns Result
39
- */
40
- static getColors(init = '#000', factor = 51, adjustOrder = true, hex = true) {
41
- return EColor.getEColors(init, factor, adjustOrder).map((c) => hex ? c.toHEXColor() : c.toRGBColor());
42
- }
43
- /**
44
- * Get EColors
45
- * @param init Initial color
46
- * @param factor Increase factor
47
- * @param adjustOrder Adjust order to increase difference
48
- * @returns Result
49
- */
50
- static getEColors(init = '#000', factor = 51, adjustOrder = true) {
51
- var _a;
52
- // Init color
53
- const initColor = (_a = EColor.parse(init)) !== null && _a !== void 0 ? _a : new EColor(0, 0, 0);
54
- // Factors elements
55
- // 51 = '00', '33', '66', '99', 'cc', 'ff'
56
- const factors = [];
57
- let f = 0;
58
- while (f <= 255) {
59
- factors.push(f);
60
- f += factor;
61
- }
62
- // RGB loop
63
- const colors = [initColor];
64
- for (const r of factors) {
65
- for (const g of factors) {
66
- for (const b of factors) {
67
- colors.push(initColor.clone(r, g, b));
68
- }
69
- }
70
- }
71
- // Non-nullable colors
72
- const nColors = colors.filter((color) => color != null);
73
- // Adjust order
74
- if (adjustOrder) {
75
- const firstColor = nColors.shift();
76
- if (firstColor) {
77
- let color = firstColor;
78
- const newColors = [color];
79
- while (nColors.length > 0) {
80
- const result = nColors.reduce((p, c, index) => {
81
- const delta = color.getDeltaValue(c);
82
- if (delta != null && delta > p.delta) {
83
- p.delta = delta;
84
- p.color = c;
85
- p.index = index;
86
- }
87
- return p;
88
- }, { delta: 0, color, index: -1 });
89
- if (result.delta > 0) {
90
- color = result.color;
91
- newColors.push(color);
92
- nColors.splice(result.index, 1);
93
- }
94
- }
95
- return newColors;
96
- }
97
- }
98
- return nColors;
99
- }
100
32
  /**
101
33
  * HEX string to integer value
102
34
  * @param hex HEX string
@@ -122,7 +54,7 @@ export class EColor {
122
54
  // Null
123
55
  if (htmlColor == null)
124
56
  return undefined;
125
- htmlColor = htmlColor.toUpperCase();
57
+ htmlColor = htmlColor.trim().toUpperCase();
126
58
  // HEX color
127
59
  if (htmlColor.startsWith('#')) {
128
60
  htmlColor = htmlColor.substring(1);
@@ -247,15 +179,24 @@ export class EColor {
247
179
  }
248
180
  /**
249
181
  * To RGB color string
250
- * @param includeAlpha Include alpha or not
182
+ * @param alpha Alpha value, false means ignore it
251
183
  * @returns RGB color string
252
184
  */
253
- toRGBColor(includeAlpha) {
254
- var _a;
255
- // Default case
256
- includeAlpha !== null && includeAlpha !== void 0 ? includeAlpha : (includeAlpha = this.alpha != null);
185
+ toRGBColor(alpha) {
186
+ // Decide
187
+ let includeAlpha, alphaValue = this.alpha;
188
+ if (typeof alpha === 'number') {
189
+ alphaValue = alpha;
190
+ includeAlpha = true;
191
+ }
192
+ else if (alpha == null) {
193
+ includeAlpha = this.alpha != null;
194
+ }
195
+ else {
196
+ includeAlpha = alpha;
197
+ }
257
198
  if (includeAlpha)
258
- return `RGBA(${this.r}, ${this.g}, ${this.b}, ${(_a = this.alpha) !== null && _a !== void 0 ? _a : 1})`;
199
+ return `RGBA(${this.r}, ${this.g}, ${this.b}, ${alphaValue !== null && alphaValue !== void 0 ? alphaValue : 1})`;
259
200
  return `RGB(${this.r}, ${this.g}, ${this.b})`;
260
201
  }
261
202
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.16",
3
+ "version": "1.1.19",
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
  "dependencies": {},
56
56
  "devDependencies": {
57
57
  "@types/jest": "^27.4.1",
58
- "@typescript-eslint/eslint-plugin": "^5.13.0",
59
- "@typescript-eslint/parser": "^5.13.0",
60
- "eslint": "^8.10.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.17.0",
59
+ "@typescript-eslint/parser": "^5.17.0",
60
+ "eslint": "^8.12.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
63
  "jest": "^27.5.1",
64
- "ts-jest": "^27.1.3",
65
- "typescript": "^4.6.2"
64
+ "ts-jest": "^27.1.4",
65
+ "typescript": "^4.6.3"
66
66
  }
67
67
  }
@@ -0,0 +1,99 @@
1
+ import { EColor } from './types/EColor';
2
+
3
+ /**
4
+ * Color utils
5
+ */
6
+ export namespace ColorUtils {
7
+ /**
8
+ * Get HEX or RGB colors
9
+ * @param init Initial color
10
+ * @param factor Increase factor
11
+ * @param adjustOrder Adjust order to increase difference
12
+ * @param hex to HEX or not
13
+ * @returns Result
14
+ */
15
+ export function getColors(
16
+ init: string = '#000',
17
+ factor: number = 51,
18
+ adjustOrder: boolean = true,
19
+ hex: boolean = true
20
+ ) {
21
+ return getEColors(init, factor, adjustOrder).map((c) =>
22
+ hex ? c.toHEXColor() : c.toRGBColor()
23
+ );
24
+ }
25
+
26
+ /**
27
+ * Get EColors
28
+ * @param init Initial color
29
+ * @param factor Increase factor
30
+ * @param adjustOrder Adjust order to increase difference
31
+ * @returns Result
32
+ */
33
+ export function getEColors(
34
+ init: string = '#000',
35
+ factor: number = 51,
36
+ adjustOrder: boolean = true
37
+ ): EColor[] {
38
+ // Init color
39
+ const initColor = EColor.parse(init) ?? new EColor(0, 0, 0);
40
+
41
+ // Factors elements
42
+ // 51 = '00', '33', '66', '99', 'cc', 'ff'
43
+ const factors: number[] = [];
44
+ let f = 0;
45
+ while (f <= 255) {
46
+ factors.push(f);
47
+ f += factor;
48
+ }
49
+
50
+ // RGB loop
51
+ const colors: (EColor | undefined)[] = [initColor];
52
+ for (const r of factors) {
53
+ for (const g of factors) {
54
+ for (const b of factors) {
55
+ colors.push(initColor.clone(r, g, b));
56
+ }
57
+ }
58
+ }
59
+
60
+ // Non-nullable colors
61
+ const nColors = colors.filter(
62
+ (color): color is EColor => color != null
63
+ );
64
+
65
+ // Adjust order
66
+ if (adjustOrder) {
67
+ const firstColor = nColors.shift();
68
+ if (firstColor) {
69
+ let color = firstColor;
70
+ const newColors: EColor[] = [color];
71
+
72
+ while (nColors.length > 0) {
73
+ const result = nColors.reduce(
74
+ (p, c, index) => {
75
+ const delta = color.getDeltaValue(c);
76
+ if (delta != null && delta > p.delta) {
77
+ p.delta = delta;
78
+ p.color = c;
79
+ p.index = index;
80
+ }
81
+ return p;
82
+ },
83
+ { delta: 0, color, index: -1 }
84
+ );
85
+
86
+ if (result.delta > 0) {
87
+ color = result.color;
88
+ newColors.push(color);
89
+ nColors.splice(result.index, 1);
90
+ }
91
+ }
92
+
93
+ return newColors;
94
+ }
95
+ }
96
+
97
+ return nColors;
98
+ }
99
+ }
package/src/Utils.ts CHANGED
@@ -516,6 +516,20 @@ export namespace Utils {
516
516
  return input?.removeNonLetters();
517
517
  };
518
518
 
519
+ /**
520
+ * Replace null or empty with default value
521
+ * @param input Input string
522
+ * @param defaultValue Default value
523
+ * @returns Result
524
+ */
525
+ export const replaceNullOrEmpty = (
526
+ input: string | null | undefined,
527
+ defaultValue: string
528
+ ) => {
529
+ if (input == null || input.trim() === '') return defaultValue;
530
+ return input;
531
+ };
532
+
519
533
  /**
520
534
  * Set source with new labels
521
535
  * @param source Source
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export * from './storage/IStorage';
6
6
  export * from './storage/WindowStorage';
7
7
 
8
8
  export * from './DataTypes';
9
+ export * from './ColorUtils';
9
10
  export * from './DateUtils';
10
11
  export * from './DomUtils';
11
12
  export * from './ExtendUtils';
@@ -15,99 +15,6 @@ export class EColor {
15
15
  return value;
16
16
  }
17
17
 
18
- /**
19
- * Get HEX or RGB colors
20
- * @param init Initial color
21
- * @param factor Increase factor
22
- * @param adjustOrder Adjust order to increase difference
23
- * @param hex to HEX or not
24
- * @returns Result
25
- */
26
- static getColors(
27
- init: string = '#000',
28
- factor: number = 51,
29
- adjustOrder: boolean = true,
30
- hex: boolean = true
31
- ) {
32
- return EColor.getEColors(init, factor, adjustOrder).map((c) =>
33
- hex ? c.toHEXColor() : c.toRGBColor()
34
- );
35
- }
36
-
37
- /**
38
- * Get EColors
39
- * @param init Initial color
40
- * @param factor Increase factor
41
- * @param adjustOrder Adjust order to increase difference
42
- * @returns Result
43
- */
44
- static getEColors(
45
- init: string = '#000',
46
- factor: number = 51,
47
- adjustOrder: boolean = true
48
- ): EColor[] {
49
- // Init color
50
- const initColor = EColor.parse(init) ?? new EColor(0, 0, 0);
51
-
52
- // Factors elements
53
- // 51 = '00', '33', '66', '99', 'cc', 'ff'
54
- const factors: number[] = [];
55
- let f = 0;
56
- while (f <= 255) {
57
- factors.push(f);
58
- f += factor;
59
- }
60
-
61
- // RGB loop
62
- const colors: (EColor | undefined)[] = [initColor];
63
- for (const r of factors) {
64
- for (const g of factors) {
65
- for (const b of factors) {
66
- colors.push(initColor.clone(r, g, b));
67
- }
68
- }
69
- }
70
-
71
- // Non-nullable colors
72
- const nColors = colors.filter(
73
- (color): color is EColor => color != null
74
- );
75
-
76
- // Adjust order
77
- if (adjustOrder) {
78
- const firstColor = nColors.shift();
79
- if (firstColor) {
80
- let color = firstColor;
81
- const newColors: EColor[] = [color];
82
-
83
- while (nColors.length > 0) {
84
- const result = nColors.reduce(
85
- (p, c, index) => {
86
- const delta = color.getDeltaValue(c);
87
- if (delta != null && delta > p.delta) {
88
- p.delta = delta;
89
- p.color = c;
90
- p.index = index;
91
- }
92
- return p;
93
- },
94
- { delta: 0, color, index: -1 }
95
- );
96
-
97
- if (result.delta > 0) {
98
- color = result.color;
99
- newColors.push(color);
100
- nColors.splice(result.index, 1);
101
- }
102
- }
103
-
104
- return newColors;
105
- }
106
- }
107
-
108
- return nColors;
109
- }
110
-
111
18
  /**
112
19
  * HEX string to integer value
113
20
  * @param hex HEX string
@@ -134,7 +41,7 @@ export class EColor {
134
41
  static parse(htmlColor?: string | null): EColor | undefined {
135
42
  // Null
136
43
  if (htmlColor == null) return undefined;
137
- htmlColor = htmlColor.toUpperCase();
44
+ htmlColor = htmlColor.trim().toUpperCase();
138
45
 
139
46
  // HEX color
140
47
  if (htmlColor.startsWith('#')) {
@@ -309,15 +216,25 @@ export class EColor {
309
216
 
310
217
  /**
311
218
  * To RGB color string
312
- * @param includeAlpha Include alpha or not
219
+ * @param alpha Alpha value, false means ignore it
313
220
  * @returns RGB color string
314
221
  */
315
- toRGBColor(includeAlpha?: boolean) {
316
- // Default case
317
- includeAlpha ??= this.alpha != null;
222
+ toRGBColor(alpha?: boolean | number) {
223
+ // Decide
224
+ let includeAlpha: boolean,
225
+ alphaValue: number | undefined = this.alpha;
226
+
227
+ if (typeof alpha === 'number') {
228
+ alphaValue = alpha;
229
+ includeAlpha = true;
230
+ } else if (alpha == null) {
231
+ includeAlpha = this.alpha != null;
232
+ } else {
233
+ includeAlpha = alpha;
234
+ }
318
235
 
319
236
  if (includeAlpha)
320
- return `RGBA(${this.r}, ${this.g}, ${this.b}, ${this.alpha ?? 1})`;
237
+ return `RGBA(${this.r}, ${this.g}, ${this.b}, ${alphaValue ?? 1})`;
321
238
 
322
239
  return `RGB(${this.r}, ${this.g}, ${this.b})`;
323
240
  }