Skip to content
markpaper

src/numbers/ints.ts

v0.1.0 · 803 B

Download file
export const U64_MAX = (1n << 64n) - 1n;
export const BID_SEQ_MIN = 1n << 63n;
export const I64_MIN = -(1n << 63n);
export const I64_MAX = (1n << 63n) - 1n;
export const QUOTE_LOTS_PER_USD = 1_000_000n;
export function parseU64(v: unknown): bigint | null {
  if (typeof v !== 'string' || !/^(0|[1-9][0-9]{0,19})$/.test(v)) return null;
  const n = BigInt(v);
  return n <= U64_MAX ? n : null;
}
export function parseI64(v: unknown): bigint | null {
  if (typeof v !== 'string' || !/^-(?:[1-9][0-9]{0,19})$|^(0|[1-9][0-9]{0,19})$/.test(v)) return null;
  const n = BigInt(v);
  return n >= I64_MIN && n <= I64_MAX ? n : null;
}
export function isBidSeq(seq: string | bigint): boolean {
  const n = typeof seq === 'bigint' ? seq : parseU64(seq);
  return n !== null && n >= BID_SEQ_MIN && n <= U64_MAX;
}
All files