Skip to content
markpaper

src/markets/symbols.ts

v0.1.0 · 799 B

Download file
export const QFEX_QUOTE = 'USD';
export const isQfexBase = (value: unknown): value is string =>
  typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._]{0,31}$/.test(value);
export function toSymbol(base: string, quote = QFEX_QUOTE): string {
  if (!isQfexBase(base) || !/^[A-Za-z0-9]{2,8}$/.test(quote)) throw new TypeError('Invalid symbol');
  return base + '-' + quote;
}
export function parseSymbol(symbol: unknown): { base: string; quote: string } | null {
  if (typeof symbol !== 'string') return null;
  const parts = symbol.split('-');
  return parts.length === 2 && isQfexBase(parts[0]) && /^[A-Za-z0-9]{2,8}$/.test(parts[1] ?? '')
    ? { base: parts[0], quote: parts[1]! }
    : null;
}
export const isUsdSymbol = (symbol: unknown): boolean => parseSymbol(symbol)?.quote === QFEX_QUOTE;
All files