diff --git a/src/shared/formatters/index.ts b/src/shared/formatters/index.ts index 4daf8d0a..defa21e5 100644 --- a/src/shared/formatters/index.ts +++ b/src/shared/formatters/index.ts @@ -1,2 +1,3 @@ export * from './numberformat'; +export * from './pairFormat'; export * from './timeformat'; diff --git a/src/shared/formatters/pairFormat.ts b/src/shared/formatters/pairFormat.ts new file mode 100644 index 00000000..3b28d7c3 --- /dev/null +++ b/src/shared/formatters/pairFormat.ts @@ -0,0 +1,10 @@ +/** + * Split a trade pair into stake and quote currency + * @param pair pair to split, either / or /: + * @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 }; +} diff --git a/tests/unit/formatPair.spec.ts b/tests/unit/formatPair.spec.ts new file mode 100644 index 00000000..d70e28fa --- /dev/null +++ b/tests/unit/formatPair.spec.ts @@ -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', + }); + }); +});