Add pair split function (incl. tests)

This commit is contained in:
Matthias 2023-10-21 17:09:40 +02:00
parent 0b8e134c15
commit 0f6aec7df8
3 changed files with 37 additions and 0 deletions

View File

@ -1,2 +1,3 @@
export * from './numberformat'; export * from './numberformat';
export * from './pairFormat';
export * from './timeformat'; export * from './timeformat';

View File

@ -0,0 +1,10 @@
/**
* Split a trade pair into stake and quote currency
* @param pair pair to split, either <stake>/<quote> or <stake>/<quote>:<settle>
* @returns {stakeCurrency, quoteCurrency}
*/
export function splitTradePair(pair: string): { stakeCurrency: string; quoteCurrency: string } {
const [stakeCurrency, quoteCurrency] = pair.split('/');
const quoteCurrencySplit = quoteCurrency.split(':');
return { stakeCurrency, quoteCurrency: quoteCurrencySplit[0] || quoteCurrency };
}

View File

@ -0,0 +1,26 @@
import { describe, it, expect } from 'vitest';
import { splitTradePair } from '@/shared/formatters';
describe('splitTradePair', () => {
it('Extracts stake and quote currencies from spot pairs', () => {
expect(splitTradePair('BTC/USDT')).toEqual({ stakeCurrency: 'BTC', quoteCurrency: 'USDT' });
expect(splitTradePair('USDT/BTC')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'BTC' });
expect(splitTradePair('USDT/USDT')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'USDT' });
expect(splitTradePair('USDT/USDT')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'USDT' });
});
it('Extracts stake and quote currencies from spot pairs', () => {
expect(splitTradePair('BTC/USDT:USDT')).toEqual({
stakeCurrency: 'BTC',
quoteCurrency: 'USDT',
});
expect(splitTradePair('USDT/BTC:BTC')).toEqual({ stakeCurrency: 'USDT', quoteCurrency: 'BTC' });
expect(splitTradePair('USDT/USDT:USDT')).toEqual({
stakeCurrency: 'USDT',
quoteCurrency: 'USDT',
});
expect(splitTradePair('USDT/USDT:USDT')).toEqual({
stakeCurrency: 'USDT',
quoteCurrency: 'USDT',
});
});
});