Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

899 B
Raw Blame
export function nFormatter(
  number?: number | bigint,
  opts: { digits?: number; full?: boolean } = {
    digits: 1,
  },
) {
  const num = number !== undefined ? Number(number) : undefined;

  if (!num) {
    return "0";
  }

  if (opts.full) {
    return Intl.NumberFormat("en-US").format(num);
  }

  const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;

  if (num < 1) {
    return num.toFixed(opts.digits).replace(rx, "$1");
  }

  const lookup = [
    { value: 1, symbol: "" },
    { value: 1e3, symbol: "K" },
    { value: 1e6, symbol: "M" },
    { value: 1e9, symbol: "G" },
    { value: 1e12, symbol: "T" },
    { value: 1e15, symbol: "P" },
    { value: 1e18, symbol: "E" },
  ];
  var item = lookup
    .slice()
    .reverse()
    .find(function (item) {
      return num >= item.value;
    });
  return item
    ? (num / item.value).toFixed(opts.digits).replace(rx, "$1") + item.symbol
    : "0";
}