@etsoo/shared 1.1.23 → 1.1.26

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
@@ -152,6 +152,7 @@ Numbers related utilities
152
152
  |format|Format number|
153
153
  |formatMoney|Format money number|
154
154
  |parse|Parse float value|
155
+ |toExact|To the exact precision number avoiding precision lost|
155
156
 
156
157
  ## StorageUtils
157
158
  Storage related utilities
@@ -1,6 +1,13 @@
1
1
  import { EColor } from '../src/types/EColor';
2
2
  import { ColorUtils } from '../src/ColorUtils';
3
3
 
4
+ test('Tests for format', () => {
5
+ expect(EColor.format('transparent')).toBe('transparent');
6
+ expect(EColor.format('RED')).toBe('red');
7
+ expect(EColor.format('RGB(226, 24, 33)', true)).toBe('#e21821');
8
+ expect(EColor.format('RGB(226, 24, 33)')).toBe('RGB(226, 24, 33)');
9
+ });
10
+
4
11
  test('Tests for parse', () => {
5
12
  // Arrange & act
6
13
  const colorShort = EColor.parse('#000');
@@ -23,3 +23,10 @@ test('Tests for parseWithUnit', () => {
23
23
  expect(NumberUtils.parseWithUnit('16')).toStrictEqual([16, '']);
24
24
  expect(NumberUtils.parseWithUnit('a16')).toBeUndefined();
25
25
  });
26
+
27
+ test('Tests for toExact', () => {
28
+ // 0.7000000000000001
29
+ const result = 0.8 - 0.1;
30
+ expect(result).not.toBe(0.7);
31
+ expect(result.toExact()).toBe(0.7);
32
+ });
@@ -1,3 +1,12 @@
1
+ declare global {
2
+ interface Number {
3
+ /**
4
+ * To the exact precision number avoiding precision lost
5
+ * @param precision Precision
6
+ */
7
+ toExact(precision?: number): number;
8
+ }
9
+ }
1
10
  export declare namespace NumberUtils {
2
11
  /**
3
12
  * Format number
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NumberUtils = void 0;
4
+ Number.prototype.toExact = function (precision) {
5
+ if (precision == null || precision < 0)
6
+ precision = 2;
7
+ if (precision === 0)
8
+ return Math.round(this);
9
+ const p = Math.pow(10, precision);
10
+ return Math.round(this * p) / p;
11
+ };
4
12
  var NumberUtils;
5
13
  (function (NumberUtils) {
6
14
  /**
@@ -13,6 +13,13 @@ export declare class EColor {
13
13
  * @returns Adjusted value
14
14
  */
15
15
  static adjust(value: number, adjust?: number): number;
16
+ /**
17
+ * Format color
18
+ * @param input Input color text
19
+ * @param hexColor Format as Hex color
20
+ * @returns Result
21
+ */
22
+ static format(input: string | undefined | null, hexColor?: boolean): string | undefined;
16
23
  /**
17
24
  * HEX string to integer value
18
25
  * @param hex HEX string
@@ -32,6 +32,24 @@ class EColor {
32
32
  return value % 255;
33
33
  return value;
34
34
  }
35
+ /**
36
+ * Format color
37
+ * @param input Input color text
38
+ * @param hexColor Format as Hex color
39
+ * @returns Result
40
+ */
41
+ static format(input, hexColor) {
42
+ // Null
43
+ if (input == null)
44
+ return undefined;
45
+ // Like transparent, black, red
46
+ if (/^[a-zA-Z]+$/gi.test(input))
47
+ return input.toLowerCase();
48
+ const color = EColor.parse(input);
49
+ if (color == null)
50
+ return undefined;
51
+ return hexColor ? color.toHEXColor() : color.toRGBColor();
52
+ }
35
53
  /**
36
54
  * HEX string to integer value
37
55
  * @param hex HEX string
@@ -1,3 +1,12 @@
1
+ declare global {
2
+ interface Number {
3
+ /**
4
+ * To the exact precision number avoiding precision lost
5
+ * @param precision Precision
6
+ */
7
+ toExact(precision?: number): number;
8
+ }
9
+ }
1
10
  export declare namespace NumberUtils {
2
11
  /**
3
12
  * Format number
@@ -1,3 +1,11 @@
1
+ Number.prototype.toExact = function (precision) {
2
+ if (precision == null || precision < 0)
3
+ precision = 2;
4
+ if (precision === 0)
5
+ return Math.round(this);
6
+ const p = Math.pow(10, precision);
7
+ return Math.round(this * p) / p;
8
+ };
1
9
  export var NumberUtils;
2
10
  (function (NumberUtils) {
3
11
  /**
@@ -13,6 +13,13 @@ export declare class EColor {
13
13
  * @returns Adjusted value
14
14
  */
15
15
  static adjust(value: number, adjust?: number): number;
16
+ /**
17
+ * Format color
18
+ * @param input Input color text
19
+ * @param hexColor Format as Hex color
20
+ * @returns Result
21
+ */
22
+ static format(input: string | undefined | null, hexColor?: boolean): string | undefined;
16
23
  /**
17
24
  * HEX string to integer value
18
25
  * @param hex HEX string
@@ -29,6 +29,24 @@ export class EColor {
29
29
  return value % 255;
30
30
  return value;
31
31
  }
32
+ /**
33
+ * Format color
34
+ * @param input Input color text
35
+ * @param hexColor Format as Hex color
36
+ * @returns Result
37
+ */
38
+ static format(input, hexColor) {
39
+ // Null
40
+ if (input == null)
41
+ return undefined;
42
+ // Like transparent, black, red
43
+ if (/^[a-zA-Z]+$/gi.test(input))
44
+ return input.toLowerCase();
45
+ const color = EColor.parse(input);
46
+ if (color == null)
47
+ return undefined;
48
+ return hexColor ? color.toHEXColor() : color.toRGBColor();
49
+ }
32
50
  /**
33
51
  * HEX string to integer value
34
52
  * @param hex HEX string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.23",
3
+ "version": "1.1.26",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -1,3 +1,22 @@
1
+ declare global {
2
+ interface Number {
3
+ /**
4
+ * To the exact precision number avoiding precision lost
5
+ * @param precision Precision
6
+ */
7
+ toExact(precision?: number): number;
8
+ }
9
+ }
10
+
11
+ Number.prototype.toExact = function (this: number, precision?: number) {
12
+ if (precision == null || precision < 0) precision = 2;
13
+
14
+ if (precision === 0) return Math.round(this);
15
+
16
+ const p = Math.pow(10, precision);
17
+ return Math.round(this * p) / p;
18
+ };
19
+
1
20
  export namespace NumberUtils {
2
21
  /**
3
22
  * Format number
@@ -15,6 +15,25 @@ export class EColor {
15
15
  return value;
16
16
  }
17
17
 
18
+ /**
19
+ * Format color
20
+ * @param input Input color text
21
+ * @param hexColor Format as Hex color
22
+ * @returns Result
23
+ */
24
+ static format(input: string | undefined | null, hexColor?: boolean) {
25
+ // Null
26
+ if (input == null) return undefined;
27
+
28
+ // Like transparent, black, red
29
+ if (/^[a-zA-Z]+$/gi.test(input)) return input.toLowerCase();
30
+
31
+ const color = EColor.parse(input);
32
+ if (color == null) return undefined;
33
+
34
+ return hexColor ? color.toHEXColor() : color.toRGBColor();
35
+ }
36
+
18
37
  /**
19
38
  * HEX string to integer value
20
39
  * @param hex HEX string