import { type InputOf, type OutputOf, type Type, identity } from "./type"; /** * A number type to be used with `option` * * Throws an error when the provided string is not a number */ export const number: Type = { async from(str) { const decoded = Number.parseFloat(str); if (Number.isNaN(decoded)) { throw new Error("Not a number"); } return decoded; }, displayName: "number", description: "a number", }; /** * A string type to be used with `option`. */ export const string: Type = { ...identity(), description: "a string", displayName: "str", }; /** * A boolean type to be used with `flag`. */ export const boolean: Type = { ...identity(), description: "a boolean", displayName: "true/false", defaultValue() { return false; }, }; /** * Makes any type optional, by defaulting to `undefined`. */ export function optional>( t: T, ): Type, OutputOf | undefined> { return { ...t, defaultValue(): OutputOf | undefined { return undefined; }, }; } /** * Transforms any type into an array, useful for `multioption` and `multiflag`. */ export function array>( t: T, ): Type[], OutputOf[]> { return { ...t, async from(inputs: InputOf[]): Promise[]> { return Promise.all(inputs.map((input) => t.from(input))); }, }; }