Installation
npm
npm install @anilkumarthakur/matchyarn
yarn add @anilkumarthakur/matchpnpm
pnpm add @anilkumarthakur/matchbun
bun add @anilkumarthakur/matchFrom CDN
The package's unpkg and jsdelivr fields both point at dist/index.global.js, so the bare specifier resolves to the browser build and everything lands on the JsMatch global:
<script src="https://unpkg.com/@anilkumarthakur/match"></script>
<script>
const { match } = JsMatch
</script>Browser Support
Works in all modern browsers, and the build targets ES2022. The published engines.node range is >=22; installing on Node 20 or older fails with EBADENGINE (npm) or ERR_PNPM_UNSUPPORTED_ENGINE (pnpm). The floor tracks Node.js LTS — it is the oldest LTS line still in maintenance and rises as older lines reach end-of-life — so the supported runtimes are:
- Chrome/Edge (latest)
- Firefox (latest)
- Safari (latest)
- Node.js 22+ (active LTS lines)
Module Systems
The package supports multiple module systems:
ESM (Recommended)
import { match } from '@anilkumarthakur/match'CommonJS
const { match } = require('@anilkumarthakur/match')IIFE / browser global
There is no UMD build — the browser bundle is a bare IIFE with no AMD or CommonJS detection. It ships as dist/index.global.js and assigns the JsMatch global:
<script src="https://unpkg.com/@anilkumarthakur/match/dist/index.global.js"></script>
<script>
const { match, Matcher, UnhandledMatchError } = JsMatch
</script>TypeScript Setup
The package comes with complete TypeScript definitions. No additional installation needed!
import { match, type Handler, type MatchChain } from '@anilkumarthakur/match'
// Result type inferred from the handlers — `result` is number
const result = match('test')
.on('test', () => 123)
.otherwise(() => 456)
// Or pin it explicitly, and every handler is checked against that one type
const pinned = match<string, number>('test')
.on('test', () => 123)
.otherwise(() => 456)