npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@taipescripeto/enum-type-guard

v1.0.0

Published

Check if an string or number is compatible to an TypeScript enum structure

Downloads

17

Readme

TypeScript Enum Type Guard

Some simple tools for TypeScript, it will help your daily development

taipescripeto custos autem enumeratio

npm version Build Status Open Source Love

Installation

npm install @taipescripeto/enum-type-guard --save

Usage

A type guard is a method or keyword with power of something like cast in TypeScript. When a type guard is used to check the type of an information in a conditional structure, the following code assume the validated type as the information type.

Type Guard example

This feature is basically the Type Guard to identify enum data.

import { enumTypeGuard } from '@taipescripeto/enum-type-guard';

/**
 * The application domain
 */
enum PokemonType {
  FIRE = 'FIRE',
  GRASS = 'GRASS',
  WATER = 'WATER'
}

interface Pokemon {
  name: string;
  type: PokemonType;
}

/**
 * External data collect (an api for example)
 */
const externalDataAboutPokemon = JSON.parse('{"name":"Charmander","type":"FIRE"}');

/**
 * Type Guard function
 */
function isPokemon(pokemonIGuess: unknown): pokemonIGuess is Pokemon {
  //  If it is null or isn't an object, isn't a Pokémon model
  if (!pokemonIGuess || !(pokemonIGuess instanceof Object)) {
    return false;
  }

  //  ignore that
  const someObject = pokemonIGuess as { [prop: string]: unknown };

  //  the type check
  if (
    !(typeof someObject.name === 'string' &&
      enumTypeGuard(someObject.type, PokemonType))
  ) {
    return false;
  }

  return true;
}

if (isPokemon(externalDataAboutPokemon)) {
  externalDataAboutPokemon
}

You can see in this print the TypeScript recognizing the external data as an enum: Type Guard example

Contributing

1. Create an issue

No one feature will be implemented without it having an open issue and without which the proposed has been accepted by the team responsible for the project. After the issue is approved, the applicant, a team member or anyone else can open a pull request associated with that issue (just paste the issue link in the pull request).

2. Did you find a bug?

When logging a bug, please be sure to include the following:

  • The library version;
  • If at all possible, an isolated way to reproduce the behavior;
  • The behavior you expect to see, and the actual behavior.

You can try to update the library to the last version to see if the bug has already been fixed.

3. Do not create a duplicate issue

Search the existing issues before logging a new one.

Some search tips:

  • Don't restrict your search to only open issues. An issue with a title similar to yours may have been closed as a duplicate of one with a less-findable title.
  • Check for synonyms. For example, if your bug involves an interface, it likely also occurs with type aliases or classes.

4. Create a Pull Request

Follow the steps:

  • Create a fork from our repository, install node, and run npm install in the application folder;
  • Create a branch in your forked repository, then code the feature or fix the bug;
  • Run npm run lint, npm run test and npm run build in the repository;
  • Create a Pull Request from your repository to this one, with the issue in the body and some information you think could be usefull to the reviewer (print or a gif of it working will be appreciated);
  • The reviewer can ask some changes, don't be mad, this is the GIT Flow process;
  • You get approved and your branch with the feature / fix