Skip to content

`nano-match` is a tiny utility simplifying key-based matching in TypeScript, bringing elegance to your code.

License

Notifications You must be signed in to change notification settings

jay-es/nano-match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jan 30, 2024
8bed58d Β· Jan 30, 2024

History

2 Commits
Jan 30, 2024
Jan 30, 2024
Jan 29, 2024
Jan 29, 2024
Jan 30, 2024
Jan 30, 2024
Jan 30, 2024
Jan 30, 2024
Jan 30, 2024

Repository files navigation

nano-match 🎯

nano-match is a tiny utility simplifying key-based matching in TypeScript, bringing elegance to your code.

Installation πŸš€

npm install @jay-es/nano-match

Usage πŸ› 

match ✨

import { match } from '@jay-es/nano-match';

type Language = 'en' | 'es' | 'fr';

const lang: Language = 'en';

// Strict matching, only allows valid keys of type 'Language'.
const greeting = match(lang, {
  en: "Hello.",
  es: "Hola.",
  fr: "Bonjour.",
});

console.log(greeting); // Output: Hello. (Type: "Hello." | "Hola." | "Bonjour.")

Type Safety πŸ›‘

  • match enforces type safety by allowing only valid keys. It ensures that the provided key is of the specified type.
// Compilation error: 'de' is not a valid key of type 'Language'.
const strictGreeting = match('de', {
  en: "Hello.",
  es: "Hola.",
  fr: "Bonjour.",
});

Additional Example πŸš€

// Using `match` with numeric keys
const message = match(200, {
  200: "OK",
  404: "Not Found",
  500: "Internal Server Error",
});

console.log(message); // Output: OK (Type: "OK" | "Not Found" | "Internal Server Error")

Additional Feature 🌟

nano-match also provides the following function:

looseMatch 🎈

As an additional feature, looseMatch allows for more flexible matching that accepts keys outside the specified type, returning undefined for keys that do not match.

import { looseMatch } from '@jay-es/nano-match';

// Loose matching, allows keys outside the specified type, returning undefined for non-matching keys.
const nonMatchingKey: string = 'de';
const looseGreeting = looseMatch(nonMatchingKey, {
  en: "Hello.",
  es: "Hola.",
  fr: "Bonjour.",
});

console.log(looseGreeting); // Output: undefined (Type: "Hello." | "Hola." | "Bonjour." | undefined)

License πŸ“„

This project is licensed under the MIT License. See the LICENSE file for details.

About

`nano-match` is a tiny utility simplifying key-based matching in TypeScript, bringing elegance to your code.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published