Compare commits

...

4 Commits

Author SHA1 Message Date
Mayukha Vadari
9fa3f73d5a import from lodash/ 2021-10-18 18:30:25 -04:00
Mayukha Vadari
9bf7df868e remove omitBy 2021-10-18 18:30:25 -04:00
Mayukha Vadari
3134dc112f remove flatmap 2021-10-18 18:30:25 -04:00
Mayukha Vadari
915118265f remove flatten 2021-10-18 18:30:25 -04:00
9 changed files with 62 additions and 50 deletions

View File

@@ -62,29 +62,26 @@ function getAgent(url: string, config: ConnectionOptions): Agent | undefined {
const parsedURL = new URL(url) const parsedURL = new URL(url)
const parsedProxyURL = new URL(config.proxy) const parsedProxyURL = new URL(config.proxy)
const proxyOptions = _.omitBy( const proxyOptions = {
{ secureEndpoint: parsedURL.protocol === 'wss:',
secureEndpoint: parsedURL.protocol === 'wss:', secureProxy: parsedProxyURL.protocol === 'https:',
secureProxy: parsedProxyURL.protocol === 'https:', auth: config.proxyAuthorization,
auth: config.proxyAuthorization, ca: config.trustedCertificates,
ca: config.trustedCertificates, key: config.key,
key: config.key, passphrase: config.passphrase,
passphrase: config.passphrase, cert: config.certificate,
cert: config.certificate, href: parsedProxyURL.href,
href: parsedProxyURL.href, origin: parsedProxyURL.origin,
origin: parsedProxyURL.origin, protocol: parsedProxyURL.protocol,
protocol: parsedProxyURL.protocol, username: parsedProxyURL.username,
username: parsedProxyURL.username, password: parsedProxyURL.password,
password: parsedProxyURL.password, host: parsedProxyURL.host,
host: parsedProxyURL.host, hostname: parsedProxyURL.hostname,
hostname: parsedProxyURL.hostname, port: parsedProxyURL.port,
port: parsedProxyURL.port, pathname: parsedProxyURL.pathname,
pathname: parsedProxyURL.pathname, search: parsedProxyURL.search,
search: parsedProxyURL.search, hash: parsedProxyURL.hash,
hash: parsedProxyURL.hash, }
},
(value) => value == null,
)
let HttpsProxyAgent: new (opt: typeof proxyOptions) => Agent let HttpsProxyAgent: new (opt: typeof proxyOptions) => Agent
try { try {
@@ -118,15 +115,12 @@ function createWebSocket(
const base64 = Buffer.from(config.authorization).toString('base64') const base64 = Buffer.from(config.authorization).toString('base64')
options.headers = { Authorization: `Basic ${base64}` } options.headers = { Authorization: `Basic ${base64}` }
} }
const optionsOverrides = _.omitBy( const optionsOverrides = {
{ ca: config.trustedCertificates,
ca: config.trustedCertificates, key: config.key,
key: config.key, passphrase: config.passphrase,
passphrase: config.passphrase, cert: config.certificate,
cert: config.certificate, }
},
(value) => value == null,
)
const websocketOptions = { ...options, ...optionsOverrides } const websocketOptions = { ...options, ...optionsOverrides }
const websocket = new WebSocket(url, websocketOptions) const websocket = new WebSocket(url, websocketOptions)
/* /*

View File

@@ -1,7 +1,7 @@
/* eslint-disable complexity -- verifies 19 tx types hence a lot of checks needed */ /* eslint-disable complexity -- verifies 19 tx types hence a lot of checks needed */
/* eslint-disable max-lines-per-function -- need to work with a lot of Tx verifications */ /* eslint-disable max-lines-per-function -- need to work with a lot of Tx verifications */
import _ from 'lodash' import isEqual from 'lodash/isEqual'
import { encode, decode } from 'ripple-binary-codec' import { encode, decode } from 'ripple-binary-codec'
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
@@ -164,11 +164,23 @@ export function validate(transaction: Record<string, unknown>): void {
} }
if ( if (
!_.isEqual( !isEqual(
decode(encode(tx)), decode(encode(tx)),
_.omitBy(tx, (value) => value == null), omitBy(tx, (value) => value == null),
) )
) { ) {
throw new ValidationError(`Invalid Transaction: ${tx.TransactionType}`) throw new ValidationError(`Invalid Transaction: ${tx.TransactionType}`)
} }
} }
function omitBy(
obj: Record<string, unknown>,
fn: (value: unknown) => unknown,
): Record<string, unknown> {
return (
Object.keys(obj)
.filter((key) => !fn(obj[key]))
// eslint-disable-next-line no-return-assign -- it's fine
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {})
)
}

View File

@@ -1,5 +1,3 @@
import _ from 'lodash'
import type { Client } from '..' import type { Client } from '..'
import { LedgerIndex } from '../models/common' import { LedgerIndex } from '../models/common'
import { AccountInfoRequest } from '../models/methods' import { AccountInfoRequest } from '../models/methods'
@@ -95,7 +93,7 @@ async function getBalances(
// combine results // combine results
await Promise.all([xrpPromise, linesPromise]).then( await Promise.all([xrpPromise, linesPromise]).then(
([xrpBalance, linesResponses]) => { ([xrpBalance, linesResponses]) => {
const accountLinesBalance = _.flatMap(linesResponses, (response) => const accountLinesBalance = linesResponses.flatMap((response) =>
formatBalances(response.result.lines), formatBalances(response.result.lines),
) )
if (xrpBalance !== '') { if (xrpBalance !== '') {

View File

@@ -1,6 +1,4 @@
/* eslint-disable max-lines-per-function -- Needs to process orderbooks. */
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import _ from 'lodash'
import type { Client } from '../client' import type { Client } from '../client'
import { LedgerIndex } from '../models/common' import { LedgerIndex } from '../models/common'
@@ -65,12 +63,10 @@ async function getOrderbook(
request.taker_pays = takerGets request.taker_pays = takerGets
const reverseOfferResults = await this.requestAll(request) const reverseOfferResults = await this.requestAll(request)
// 3. Return Formatted Response // 3. Return Formatted Response
const directOffers = _.flatMap( const directOffers = directOfferResults.flatMap(
directOfferResults,
(directOfferResult) => directOfferResult.result.offers, (directOfferResult) => directOfferResult.result.offers,
) )
const reverseOffers = _.flatMap( const reverseOffers = reverseOfferResults.flatMap(
reverseOfferResults,
(reverseOfferResult) => reverseOfferResult.result.offers, (reverseOfferResult) => reverseOfferResult.result.offers,
) )

View File

@@ -1,5 +1,5 @@
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import _ from 'lodash' import groupBy from 'lodash/groupBy'
import { Amount, IssuedCurrencyAmount } from '../models/common' import { Amount, IssuedCurrencyAmount } from '../models/common'
import TransactionMetadata, { Node } from '../models/transactions/metadata' import TransactionMetadata, { Node } from '../models/transactions/metadata'
@@ -66,7 +66,7 @@ function normalizeNodes(metadata: TransactionMetadata): NormalizedNode[] {
} }
function groupByAccount(balanceChanges: BalanceChange[]): BalanceChanges[] { function groupByAccount(balanceChanges: BalanceChange[]): BalanceChanges[] {
const grouped = _.groupBy(balanceChanges, (node) => node.account) const grouped = groupBy(balanceChanges, (node) => node.account)
return Object.entries(grouped).map(([account, items]) => { return Object.entries(grouped).map(([account, items]) => {
return { account, balances: items.map((item) => item.balance) } return { account, balances: items.map((item) => item.balance) }
}) })
@@ -181,5 +181,5 @@ export default function getBalanceChanges(
} }
return [] return []
}) })
return groupByAccount(_.flatten(quantities)) return groupByAccount(quantities.flat())
} }

View File

@@ -0,0 +1,15 @@
// NOTE: This should not be exported at the top level
export function omitBy(
obj: Record<string, unknown>,
fn: (unknown, string) => unknown,
): Record<string, unknown> {
return (
Object.keys(obj)
.filter((key) => !fn(obj[key], key))
// eslint-disable-next-line no-return-assign -- it's fine
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {})
)
}
export function groupBy(): void {}

View File

@@ -1,5 +1,4 @@
import { assert } from 'chai' import { assert } from 'chai'
import _ from 'lodash'
import { ServerInfoResponse } from '../src' import { ServerInfoResponse } from '../src'

View File

@@ -1,5 +1,4 @@
import { assert } from 'chai' import { assert } from 'chai'
import _ from 'lodash'
import { Client } from 'xrpl-local' import { Client } from 'xrpl-local'

View File

@@ -2,7 +2,6 @@
import net from 'net' import net from 'net'
import { assert } from 'chai' import { assert } from 'chai'
import _ from 'lodash'
import { import {
Client, Client,