MCPcopy Create free account
hub / github.com/ICBNetwork/web3-Wallet / createKeypadRule

Function createKeypadRule

app/components/Base/Keypad/createKeypadRule.js:4–63  ·  view source on GitHub ↗
({
  decimalSeparator = null,
  decimals = null,
} = {})

Source from the content-addressed store, hash-verified

2import { KEYS } from './constants';
3
4export default function createKeypadRule({
5 decimalSeparator = null,
6 decimals = null,
7} = {}) {
8 return function handler(currentAmount, inputKey) {
9 if (!currentAmount) {
10 currentAmount = '0';
11 }
12
13 switch (inputKey) {
14 case KEYS.PERIOD: {
15 if (!decimalSeparator || decimals === 0 || decimals === false) {
16 return currentAmount;
17 }
18
19 if (currentAmount.includes(decimalSeparator)) {
20 return currentAmount;
21 }
22
23 return `${currentAmount}${decimalSeparator}`;
24 }
25 case KEYS.BACK: {
26 if (currentAmount === '0') {
27 return currentAmount;
28 }
29 if (regex.hasOneDigit.test(currentAmount)) {
30 return '0';
31 }
32
33 return currentAmount.slice(0, -1);
34 }
35 case KEYS.INITIAL: {
36 return '0';
37 }
38 case KEYS.DIGIT_0:
39 case KEYS.DIGIT_1:
40 case KEYS.DIGIT_2:
41 case KEYS.DIGIT_3:
42 case KEYS.DIGIT_4:
43 case KEYS.DIGIT_5:
44 case KEYS.DIGIT_6:
45 case KEYS.DIGIT_7:
46 case KEYS.DIGIT_8:
47 case KEYS.DIGIT_9: {
48 if (currentAmount === '0') {
49 return inputKey;
50 }
51
52 if (hasDecimals(decimalSeparator, decimals).test(currentAmount)) {
53 return currentAmount;
54 }
55
56 return `${currentAmount}${inputKey}`;
57 }
58 default: {
59 return currentAmount;
60 }
61 }

Callers 3

useCurrencyFunction · 0.85
constants.jsFile · 0.85

Calls 2

hasDecimalsFunction · 0.90
testMethod · 0.80

Tested by

no test coverage detected