HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/node_modules/apollo-language-server/src/format.ts
import moment from "moment";

export function formatMS(
  ms: number,
  d: number,
  allowMicros = false,
  allowNanos = true
) {
  if (ms === 0 || ms === null) return "0";
  const bounds = [
    moment.duration(1, "hour").asMilliseconds(),
    moment.duration(1, "minute").asMilliseconds(),
    moment.duration(1, "second").asMilliseconds(),
    1,
    0.001,
    0.000001
  ];
  const units = ["hr", "min", "s", "ms", "μs", "ns"];

  const makeSmallNumbersNice = (f: number) => {
    if (f >= 100) return f.toFixed(0);
    if (f >= 10) return f.toFixed(1);
    if (f === 0) return "0";
    return f.toFixed(2);
  };

  const bound = bounds.find(b => b <= ms) || bounds[bounds.length - 1];
  const boundIndex = bounds.indexOf(bound);
  const unit = boundIndex >= 0 ? units[boundIndex] : "";

  if ((unit === "μs" || unit === "ns") && !allowMicros) {
    return "< 1ms";
  }
  if (unit === "ns" && !allowNanos) {
    return "< 1µs";
  }
  const value =
    typeof d !== "undefined"
      ? (ms / bound).toFixed(d)
      : makeSmallNumbersNice(ms / bound);

  // if something is rounded to 1000 and not reduced this will catch and reduce it
  if ((value === "1000" || value === "1000.0") && boundIndex >= 1) {
    return `1${units[boundIndex - 1]}`;
  }

  return `${value}${unit}`;
}