add memoization to few validators

This commit is contained in:
Mikhail Mikheev 2019-06-27 12:21:47 +04:00
parent f03fde5ffe
commit 23903ba708
2 changed files with 17 additions and 16 deletions

View File

@ -2,9 +2,21 @@
import { type FieldValidator } from 'final-form' import { type FieldValidator } from 'final-form'
import { getWeb3 } from '~/logic/wallets/getWeb3' import { getWeb3 } from '~/logic/wallets/getWeb3'
export const simpleMemoize = (fn: Function) => {
let lastArg
let lastResult
return (arg: any) => {
if (arg !== lastArg) {
lastArg = arg
lastResult = fn(arg)
}
return lastResult
}
}
type Field = boolean | string | null | typeof undefined type Field = boolean | string | null | typeof undefined
export const required = (value: Field) => (value ? undefined : 'Required') export const required = simpleMemoize((value: Field) => (value ? undefined : 'Required'))
export const mustBeInteger = (value: string) => (!Number.isInteger(Number(value)) || value.includes('.') ? 'Must be an integer' : undefined) export const mustBeInteger = (value: string) => (!Number.isInteger(Number(value)) || value.includes('.') ? 'Must be an integer' : undefined)
@ -46,17 +58,17 @@ export const maxValue = (max: number) => (value: string) => {
export const ok = () => undefined export const ok = () => undefined
export const mustBeEthereumAddress = (address: Field) => { export const mustBeEthereumAddress = simpleMemoize((address: Field) => {
const isAddress: boolean = getWeb3().utils.isAddress(address) const isAddress: boolean = getWeb3().utils.isAddress(address)
return isAddress ? undefined : 'Address should be a valid Ethereum address' return isAddress ? undefined : 'Address should be a valid Ethereum address'
} })
export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`) export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`)
export const ADDRESS_REPEATED_ERROR = 'Address already introduced' export const ADDRESS_REPEATED_ERROR = 'Address already introduced'
export const uniqueAddress = (addresses: string[]) => (value: string) => (addresses.includes(value) ? ADDRESS_REPEATED_ERROR : undefined) export const uniqueAddress = (addresses: string[]) => simpleMemoize((value: string) => (addresses.includes(value) ? ADDRESS_REPEATED_ERROR : undefined))
export const composeValidators = (...validators: Function[]): FieldValidator => (value: Field) => validators.reduce((error, validator) => error || validator(value), undefined) export const composeValidators = (...validators: Function[]): FieldValidator => (value: Field) => validators.reduce((error, validator) => error || validator(value), undefined)

View File

@ -3,20 +3,9 @@ import { List } from 'immutable'
import { getWeb3 } from '~/logic/wallets/getWeb3' import { getWeb3 } from '~/logic/wallets/getWeb3'
import { type Token } from '~/logic/tokens/store/model/token' import { type Token } from '~/logic/tokens/store/model/token'
import { sameAddress } from '~/logic/wallets/ethAddresses' import { sameAddress } from '~/logic/wallets/ethAddresses'
import { simpleMemoize } from '~/components/forms/validator'
// import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens' // import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens'
export const simpleMemoize = (fn: Function) => {
let lastArg
let lastResult
return (arg: any) => {
if (arg !== lastArg) {
lastArg = arg
lastResult = fn(arg)
}
return lastResult
}
}
// eslint-disable-next-line // eslint-disable-next-line
export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string) => { export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string) => {
// SECOND APPROACH: // SECOND APPROACH: