Move MaybeBigNumber functions into helper
This commit is contained in:
		@@ -4,6 +4,7 @@ import * as React from 'react';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import { ColorOption } from '../style/theme';
 | 
					import { ColorOption } from '../style/theme';
 | 
				
			||||||
import { MaybeBigNumber } from '../types';
 | 
					import { MaybeBigNumber } from '../types';
 | 
				
			||||||
 | 
					import { maybeBigNumberUtil } from '../util/maybe_big_number';
 | 
				
			||||||
import { util } from '../util/util';
 | 
					import { util } from '../util/util';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { ScalingInput } from './scaling_input';
 | 
					import { ScalingInput } from './scaling_input';
 | 
				
			||||||
@@ -21,23 +22,7 @@ interface ScalingAmountInputState {
 | 
				
			|||||||
    stringValue: string;
 | 
					    stringValue: string;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const stringToMaybeBigNumber = (stringValue: string): MaybeBigNumber => {
 | 
					const { stringToMaybeBigNumber, areMaybeBigNumbersEqual } = maybeBigNumberUtil;
 | 
				
			||||||
    let maybeBigNumber: MaybeBigNumber;
 | 
					 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
        maybeBigNumber = new BigNumber(stringValue);
 | 
					 | 
				
			||||||
    } catch {
 | 
					 | 
				
			||||||
        maybeBigNumber = undefined;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return _.isNaN(maybeBigNumber) ? undefined : maybeBigNumber;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const areMaybeBigNumbersEqual = (val1: MaybeBigNumber, val2: MaybeBigNumber): boolean => {
 | 
					 | 
				
			||||||
    if (!_.isUndefined(val1) && !_.isUndefined(val2)) {
 | 
					 | 
				
			||||||
        return val1.equals(val2);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return _.isUndefined(val1) && _.isUndefined(val2);
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export class ScalingAmountInput extends React.Component<ScalingAmountInputProps, ScalingAmountInputState> {
 | 
					export class ScalingAmountInput extends React.Component<ScalingAmountInputProps, ScalingAmountInputState> {
 | 
				
			||||||
    public static defaultProps = {
 | 
					    public static defaultProps = {
 | 
				
			||||||
        onChange: util.boundNoop,
 | 
					        onChange: util.boundNoop,
 | 
				
			||||||
@@ -57,6 +42,8 @@ export class ScalingAmountInput extends React.Component<ScalingAmountInputProps,
 | 
				
			|||||||
        if (!areMaybeBigNumbersEqual(parsedStateValue, currentValue)) {
 | 
					        if (!areMaybeBigNumbersEqual(parsedStateValue, currentValue)) {
 | 
				
			||||||
            // we somehow got into the state in which the value passed in and the string value
 | 
					            // we somehow got into the state in which the value passed in and the string value
 | 
				
			||||||
            // in state have differed, reset state
 | 
					            // in state have differed, reset state
 | 
				
			||||||
 | 
					            // we dont expect to ever get into this state, but let's make sure
 | 
				
			||||||
 | 
					            // we reset if we do since we're dealing with important numbers
 | 
				
			||||||
            this.setState({
 | 
					            this.setState({
 | 
				
			||||||
                stringValue: _.isUndefined(currentValue) ? '' : currentValue.toString(),
 | 
					                stringValue: _.isUndefined(currentValue) ? '' : currentValue.toString(),
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										25
									
								
								packages/instant/src/util/maybe_big_number.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								packages/instant/src/util/maybe_big_number.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					import { BigNumber } from '@0x/utils';
 | 
				
			||||||
 | 
					import * as _ from 'lodash';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { MaybeBigNumber } from '../types';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const maybeBigNumberUtil = {
 | 
				
			||||||
 | 
					    // converts a string to a MaybeBigNumber
 | 
				
			||||||
 | 
					    // if string is a NaN, considered undefined
 | 
				
			||||||
 | 
					    stringToMaybeBigNumber: (stringValue: string): MaybeBigNumber => {
 | 
				
			||||||
 | 
					        let validBigNumber: BigNumber;
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            validBigNumber = new BigNumber(stringValue);
 | 
				
			||||||
 | 
					        } catch {
 | 
				
			||||||
 | 
					            return undefined;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return validBigNumber.isNaN() ? undefined : validBigNumber;
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    areMaybeBigNumbersEqual: (val1: MaybeBigNumber, val2: MaybeBigNumber): boolean => {
 | 
				
			||||||
 | 
					        if (!_.isUndefined(val1) && !_.isUndefined(val2)) {
 | 
				
			||||||
 | 
					            return val1.equals(val2);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return _.isUndefined(val1) && _.isUndefined(val2);
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Reference in New Issue
	
	Block a user