count

tcmts.cli.commands.count · #7s5c5bb7 · term · runs anywhere

A whole number; optional with a default, or with null for none.

import { valid } from "#codebase/tcmts/cli/commands";
import type { Schema } from "#codebase/tcmts/cli/commands";

export function count(fallback: number): Schema<number>;
export function count(fallback: null): Schema<number | undefined>;
export function count(fallback?: number | null): Schema<number | undefined> {
  return valid((v) => {
    if (v === undefined) return fallback === undefined ? { issues: [{ message: "required" }] } : { value: fallback ?? undefined };
    const n = Number(v);
    return Number.isInteger(n) && n >= 0 ? { value: n } : { issues: [{ message: `expected a whole number, not '${String(v)}'` }] };
  });
}

uses

used by