@synerise/ds-input-number 0.8.13 → 0.8.14
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/CHANGELOG.md
CHANGED
@@ -3,6 +3,17 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
5
|
|
6
|
+
## [0.8.14](https://github.com/Synerise/synerise-design/compare/@synerise/ds-input-number@0.8.13...@synerise/ds-input-number@0.8.14) (2023-07-12)
|
7
|
+
|
8
|
+
|
9
|
+
### Bug Fixes
|
10
|
+
|
11
|
+
* **input-number:** input number edge cases fix ([6615b12](https://github.com/Synerise/synerise-design/commit/6615b1220fb740d6a5efe53621736e8cf4a1a5b8))
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
6
17
|
## [0.8.13](https://github.com/Synerise/synerise-design/compare/@synerise/ds-input-number@0.8.12...@synerise/ds-input-number@0.8.13) (2023-07-12)
|
7
18
|
|
8
19
|
**Note:** Version bump only for package @synerise/ds-input-number
|
@@ -1,4 +1,4 @@
|
|
1
1
|
import { ReactText } from 'react';
|
2
2
|
import { NumberToFormatOptions, Delimiter } from '@synerise/ds-data-format';
|
3
|
-
export declare const formatNumber: (value: string | number | undefined, formatValue: (value: number, options: NumberToFormatOptions) => string,
|
4
|
-
export declare const parseFormattedNumber: (value: string | undefined, formatValue: (value: number, options: NumberToFormatOptions) => string,
|
3
|
+
export declare const formatNumber: (value: string | number | undefined, formatValue: (value: number, options: NumberToFormatOptions) => string, notationThousandDelimiter: Delimiter, notationDecimalDelimiter: Delimiter, valueFormatOptions?: NumberToFormatOptions | undefined) => string;
|
4
|
+
export declare const parseFormattedNumber: (value: string | undefined, formatValue: (value: number, options: NumberToFormatOptions) => string, notationThousandDelimiter: Delimiter, notationDecimalDelimiter: Delimiter) => ReactText;
|
@@ -4,52 +4,62 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
4
4
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
6
|
|
7
|
-
import { MAXIMUM_FRACTION_DIGITS, NUMBER_DELIMITER } from '../constants/inputNumber.constants'; // input 1: not formatted number string (on input change)
|
8
|
-
// input 2: not formatted number (on blur)
|
7
|
+
import { MAXIMUM_FRACTION_DIGITS, MAXIMUM_NUMBER_DIGITS, NUMBER_DELIMITER } from '../constants/inputNumber.constants'; // input case 1: not formatted number string (on input change)
|
8
|
+
// input case 2: not formatted number (on blur)
|
9
9
|
// output: formatted number string with decimal char
|
10
10
|
|
11
|
-
export var formatNumber = function formatNumber(value, formatValue,
|
11
|
+
export var formatNumber = function formatNumber(value, formatValue, notationThousandDelimiter, notationDecimalDelimiter, valueFormatOptions) {
|
12
|
+
var _value$match, _value$match2;
|
13
|
+
|
12
14
|
if (value === undefined || value === '') return '';
|
13
15
|
|
14
16
|
var formatOptions = _objectSpread({
|
15
17
|
maximumFractionDigits: MAXIMUM_FRACTION_DIGITS
|
16
18
|
}, valueFormatOptions);
|
17
19
|
|
18
|
-
var notationDecimalChar = '';
|
19
|
-
var result = '';
|
20
|
-
|
21
20
|
if (typeof value === 'number') {
|
22
|
-
|
23
|
-
}
|
24
|
-
var lastChar = value == null ? void 0 : value.slice(-1);
|
25
|
-
|
26
|
-
if (lastChar === NUMBER_DELIMITER) {
|
27
|
-
notationDecimalChar = decimalDelimiter;
|
28
|
-
}
|
21
|
+
return formatValue(value, formatOptions);
|
22
|
+
}
|
29
23
|
|
30
|
-
|
24
|
+
var result = '';
|
25
|
+
var lastChar = value == null ? void 0 : value.slice(-1);
|
26
|
+
var numberResult = parseFloat(value);
|
27
|
+
var notationDecimalChar = lastChar === NUMBER_DELIMITER ? notationDecimalDelimiter : '';
|
28
|
+
var zerosAtTheEnd = (_value$match = value.match(new RegExp('0+$'))) == null ? void 0 : _value$match[0];
|
29
|
+
var zerosWithDecimalDelimiterAtTheEnd = (_value$match2 = value.match(new RegExp("\\" + NUMBER_DELIMITER + "0+$"))) == null ? void 0 : _value$match2[0];
|
30
|
+
var numberDelimiterExists = new RegExp("\\" + NUMBER_DELIMITER).test(value);
|
31
|
+
|
32
|
+
if (Number.isNaN(numberResult)) {
|
33
|
+
return '';
|
34
|
+
}
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
}
|
36
|
+
result = formatValue(numberResult, formatOptions);
|
37
|
+
result = "" + result + notationDecimalChar;
|
35
38
|
|
36
|
-
|
37
|
-
result = "" + result +
|
39
|
+
if (zerosWithDecimalDelimiterAtTheEnd) {
|
40
|
+
result = "" + result + zerosWithDecimalDelimiterAtTheEnd;
|
41
|
+
} else if (zerosAtTheEnd && numberDelimiterExists) {
|
42
|
+
result = "" + result + zerosAtTheEnd;
|
38
43
|
}
|
39
44
|
|
40
45
|
return result;
|
41
46
|
}; // input: formatted number string
|
42
47
|
// output: not formatted number string with decimal char
|
43
48
|
|
44
|
-
export var parseFormattedNumber = function parseFormattedNumber(value, formatValue,
|
49
|
+
export var parseFormattedNumber = function parseFormattedNumber(value, formatValue, notationThousandDelimiter, notationDecimalDelimiter) {
|
45
50
|
if (value === undefined || value === '') {
|
46
51
|
return '';
|
47
52
|
}
|
48
53
|
|
49
|
-
var result = value
|
54
|
+
var result = value;
|
55
|
+
result = result.split(notationThousandDelimiter).join('');
|
56
|
+
|
57
|
+
if (result.length > MAXIMUM_NUMBER_DIGITS) {
|
58
|
+
result = result.slice(0, MAXIMUM_NUMBER_DIGITS);
|
59
|
+
}
|
50
60
|
|
51
|
-
if (
|
52
|
-
result = result.replace(
|
61
|
+
if (notationDecimalDelimiter !== NUMBER_DELIMITER) {
|
62
|
+
result = result.replace(notationDecimalDelimiter, NUMBER_DELIMITER);
|
53
63
|
}
|
54
64
|
|
55
65
|
if ((result.match(new RegExp("\\" + NUMBER_DELIMITER, 'g')) || []).length > 1) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@synerise/ds-input-number",
|
3
|
-
"version": "0.8.
|
3
|
+
"version": "0.8.14",
|
4
4
|
"description": "Input-Number UI Component for the Synerise Design System",
|
5
5
|
"license": "ISC",
|
6
6
|
"repository": "Synerise/synerise-design",
|
@@ -45,5 +45,5 @@
|
|
45
45
|
"devDependencies": {
|
46
46
|
"@types/uuid": "7.0.0"
|
47
47
|
},
|
48
|
-
"gitHead": "
|
48
|
+
"gitHead": "9bbc4614a5e2cee2d9de07ee190fd4b467310d7a"
|
49
49
|
}
|