Giter Site home page Giter Site logo

jay-es / nano-match Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 25 KB

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

Home Page: https://www.npmjs.com/package/@jay-es/nano-match

License: MIT License

TypeScript 100.00%
pattern-matching switch-case typescript

nano-match's Introduction

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.

nano-match's People

Contributors

jay-es avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.