From 620f439816b8c62edb5f0e2e140647176b8702c8 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 2 Nov 2018 13:22:10 -0700 Subject: Move MaybeBigNumber functions into helper --- .../src/components/scaling_amount_input.tsx | 21 ++++-------------- packages/instant/src/util/maybe_big_number.ts | 25 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 packages/instant/src/util/maybe_big_number.ts (limited to 'packages') diff --git a/packages/instant/src/components/scaling_amount_input.tsx b/packages/instant/src/components/scaling_amount_input.tsx index 5d06c5242..a30c64b3a 100644 --- a/packages/instant/src/components/scaling_amount_input.tsx +++ b/packages/instant/src/components/scaling_amount_input.tsx @@ -4,6 +4,7 @@ import * as React from 'react'; import { ColorOption } from '../style/theme'; import { MaybeBigNumber } from '../types'; +import { maybeBigNumberUtil } from '../util/maybe_big_number'; import { util } from '../util/util'; import { ScalingInput } from './scaling_input'; @@ -21,23 +22,7 @@ interface ScalingAmountInputState { stringValue: string; } -const stringToMaybeBigNumber = (stringValue: string): MaybeBigNumber => { - 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); -}; - +const { stringToMaybeBigNumber, areMaybeBigNumbersEqual } = maybeBigNumberUtil; export class ScalingAmountInput extends React.Component { public static defaultProps = { onChange: util.boundNoop, @@ -57,6 +42,8 @@ export class ScalingAmountInput extends React.Component { + 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); + }, +}; -- cgit v1.2.3