|
12 | 12 | */ |
13 | 13 | function isDecimal(value: string | number): boolean { |
14 | 14 | let getValued: string | number = value; |
15 | | - if (typeof getValued === "number" && Number.isNaN(getValued)) { |
16 | | - throw new TypeError("Input value must not be NaN."); |
17 | | - } |
18 | | - |
19 | | - if (typeof getValued === "number" && !isFinite(getValued)) { |
20 | | - throw new TypeError("Input value must not be Infinity, -Infinity or NaN."); |
21 | | - } |
| 15 | + validateInput(getValued); |
22 | 16 |
|
23 | | - if (typeof getValued !== "string") { |
24 | | - if (typeof getValued === "number") { |
25 | | - if (Number.isInteger(getValued)) { |
26 | | - return false; |
27 | | - } |
28 | | - getValued = getValued.toString(); |
29 | | - } else { |
30 | | - throw new TypeError("Input value must be a string or a number."); |
| 17 | + if (typeof getValued === "number") { |
| 18 | + if (Number.isInteger(getValued)) { |
| 19 | + return false; |
31 | 20 | } |
| 21 | + getValued = getValued.toString(); |
32 | 22 | } |
| 23 | + |
33 | 24 | if (getValued.trim().length === 0) { |
34 | 25 | throw new Error("Input value must not be an empty string."); |
35 | 26 | } |
36 | 27 |
|
37 | | - const integerRegex: RegExp = /^\d+$/; |
38 | | - if (integerRegex.test(getValued)) { |
| 28 | + if (isInteger(getValued)) { |
39 | 29 | return false; |
40 | 30 | } |
41 | 31 |
|
42 | | - // Regular expression to validate decimal numbers |
43 | | - const decimalRegex: RegExp = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/; |
44 | | - if (!decimalRegex.test(getValued)) { |
| 32 | + if (!isValidDecimal(getValued)) { |
45 | 33 | return false; |
46 | 34 | } |
47 | | - // Check for multiple decimal separators |
48 | | - const decimalSeparator: Separators = getValued.includes(".") ? "." : ","; |
49 | | - const otherSeparator: Separators = decimalSeparator === "." ? "," : "."; |
50 | | - if ( |
51 | | - getValued.includes(decimalSeparator) && |
52 | | - getValued.includes(otherSeparator) |
53 | | - ) { |
| 35 | + |
| 36 | + if (hasMultipleSeparators(getValued)) { |
54 | 37 | return false; |
55 | 38 | } |
56 | | - // Additional checks for negative sign |
57 | | - if (getValued.startsWith("-")) { |
58 | | - // Ensure the negative sign is only at the beginning and not elsewhere |
59 | | - if (getValued.lastIndexOf("-") > 0) { |
60 | | - return false; |
61 | | - } |
| 39 | + |
| 40 | + if (hasInvalidNegativeSign(getValued)) { |
| 41 | + return false; |
62 | 42 | } |
| 43 | + |
63 | 44 | return true; |
64 | 45 | } |
| 46 | + |
| 47 | +function validateInput(value: string | number): void { |
| 48 | + if (typeof value === "number" && Number.isNaN(value)) { |
| 49 | + throw new TypeError("Input value must not be NaN."); |
| 50 | + } |
| 51 | + |
| 52 | + if (typeof value === "number" && !isFinite(value)) { |
| 53 | + throw new TypeError("Input value must not be Infinity, -Infinity or NaN."); |
| 54 | + } |
| 55 | + |
| 56 | + if (typeof value !== "string" && typeof value !== "number") { |
| 57 | + throw new TypeError("Input value must be a string or a number."); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function isInteger(value: string): boolean { |
| 62 | + const integerRegex: RegExp = /^\d+$/; |
| 63 | + return integerRegex.test(value); |
| 64 | +} |
| 65 | + |
| 66 | +function isValidDecimal(value: string): boolean { |
| 67 | + const decimalRegex: RegExp = /^[-+]?(?:\d+(?:[,.]\d*)?|\d*[,.]\d+)$/; |
| 68 | + return decimalRegex.test(value); |
| 69 | +} |
| 70 | + |
| 71 | +function hasMultipleSeparators(value: string): boolean { |
| 72 | + const decimalSeparator: Separators = value.includes(".") ? "." : ","; |
| 73 | + const otherSeparator: Separators = decimalSeparator === "." ? "," : "."; |
| 74 | + return value.includes(decimalSeparator) && value.includes(otherSeparator); |
| 75 | +} |
| 76 | + |
| 77 | +function hasInvalidNegativeSign(value: string): boolean { |
| 78 | + return value.startsWith("-") && value.lastIndexOf("-") > 0; |
| 79 | +} |
| 80 | + |
65 | 81 | export default isDecimal; |
66 | 82 |
|
67 | 83 | type Separators = "." | ","; |
0 commit comments