yn
Parses yes/no-like values from strings, numbers, and booleans, with an optional lenient typo-tolerant mode.
Repository Health
Technical Analysis
yn is a tiny, dependency-free npm utility for validating and normalizing yes/no-style answers. It recognizes a fixed set of case-insensitive truthy and falsy tokens (‘y’, ‘yes’, ‘t’, ‘true’, 1, ‘on’ vs. ‘n’, ‘no’, ‘f’, ‘false’, 0, ‘off’) and returns a strict boolean or undefined for anything else, so callers can distinguish ‘no answer’ from an explicit false.
Its standout feature is an opt-in lenient mode: a keyboard-distance scoring algorithm (in lenient.js) that tolerates common typos of ‘yes’ and ‘no’ — such as ‘ywa’ or ‘mo’ — by weighting nearby keys on a QWERTY layout. This makes it well suited for validating free-text answers to CLI prompts, where users often mistype short confirmations.
What You Get
- A single default export function,
yn(input, options?), with a fully typed TypeScript signature - Recognition of common truthy/falsy tokens across strings, numbers, and native booleans (‘y’/‘n’, ‘yes’/‘no’, ‘true’/‘false’, 1/0, ‘on’/‘off’)
- An opt-in
lenientmode that uses keyboard-distance scoring to tolerate typos of ‘yes’ and ‘no’ - A
defaultoption to supply a fallback boolean when no match is found, with a runtime type check that throws if it isn’t a boolean - Zero runtime dependencies and a tiny (35KB) footprint, published as a native ESM package
Common Use Cases
- Validating a user’s typed answer to a CLI confirmation prompt (e.g. ‘Continue? (y/n)’)
- Normalizing boolean-like environment variables or config file values (‘true’/‘1’/‘on’) into real booleans
- Coercing loosely-typed API or form input into a strict boolean with a safe fallback via the
defaultoption - Gracefully handling typo’d yes/no answers in interactive prompts using the
lenientoption instead of re-prompting the user
Under The Hood
Architecture
The package is deliberately minimal: index.js exports a single default function that normalizes the input to a string, tests it against two case-insensitive regular expressions for truthy and falsy tokens, and falls through to an optional lenient path in lenient.js before returning the default option. There is no class hierarchy, no internal state, and no configuration beyond the two options — the entire control flow is a short sequence of guard clauses, making the single exported function trivial to reason about and impossible to misuse structurally.
Tech Stack
Written in plain modern JavaScript, published as native ESM ("type": "module") with a hand-written index.d.ts providing full TypeScript types via function overloads (one signature for when default is set, one for when it isn’t). It has zero runtime dependencies; ava and tsd are used for testing and type-testing respectively, and xo (an opinionated ESLint wrapper) enforces linting. No build step is needed since the source ships as-is.
Code Quality
Tests in test.js use the ava runner and cover truthy cases, falsy cases, undefined/unrecognized cases, both lenient-mode edge cases, and the default option’s runtime type-check error path with dedicated assertions — giving the small surface area comprehensive coverage. index.test-d.ts adds compile-time type assertions via tsd. Naming is consistent and terse in the idiomatic style of this ecosystem’s Sindresorhus-maintained packages, error handling is explicit (a thrown TypeError for a malformed default option), and xo plus CI (GitHub Actions, inferred from .github/) enforce style automatically.
What Makes It Unique Most yes/no parsers stop at exact token matching; this package’s lenient mode implements a keyboard-distance scoring heuristic — per-letter proximity maps for the keys around ‘y’, ‘e’, ‘s’, ‘n’, and ‘o’ — to accept common single-key-slip typos (‘ywa’, ‘mo’) as valid answers without a full fuzzy-matching or edit-distance library. It’s a narrow, purpose-built heuristic rather than a general novel algorithm, but it is a genuinely uncommon feature for a package this small.
Used by 2 apps in this directory
Botpress
AI Assistants · AI Development · Customer Support
The open-source hub for building and deploying LLM-powered AI agents with TypeScript-first tooling, 40+ integrations, and a revolutionary code-execution agent framework.
overleaf
Collaboration · Productivity
Open-source, real-time collaborative LaTeX editor with sandboxed compilation and full TeXLive support for self-hosted academic and research teams.