GithubHelp home page GithubHelp logo

stein197 / js-enum Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 35 KB

Complex enum implementation for TypeScript

License: MIT License

TypeScript 96.57% Batchfile 1.82% Shell 1.61%
typescript ts enum

js-enum's Introduction

Complex enum implementation for JavaScript & TypeScript

Installation

npm install @stein197/enum

Motivation

TypeScript provides a very simple and primitive implementation of enumerations. All you can do with native implementation is to declare numeric or string enum values. But sometimes there is a need of more complex enumerations - that have properties or even methods. This package provides such an implementation that exists in Java and PHP.

Usage

The package provides only one simple class Enum that all complex enums must extend from. Assume that there is an enum Status which represents HTTP statuses. Status can have status code and a message. In TypeScript it could be reached by creating two distinct enums. Instead, use complex enumerations like this:

import Enum from "@stein197/enum";

class Status extends Enum {

	public static readonly Ok = new Status(200, "Ok");
	public static readonly NotFound = new Status(404, "Not found");
	public static readonly InternalServerError = new Status(500, "Internal server error");

	public constructor(public readonly code: number, public readonly message: string) {}

	// Since it's a regular class, methods can be used as well
	public getMessage(): string {
		return this.message;
	}
}

// Unique properties
Status.NotFound.name; // "NOT_FOUND"

// Custom properties and methods
Status.Ok.message; // "Ok"
Status.Ok.getMessage(); // "Ok"

// Retrieving arbitrary data
Status.values(); // [Status.Ok, Status.NotFound, Status.InternalServerError]
Status.from("NotFound"); // By name: Status.NotFound
Status.from("Nonexistent"); // null

NPM scripts

  • clean Cleans compiled files
  • build, build:dev Builds the project
  • test Runs all unit tests

js-enum's People

Contributors

stein197 avatar

Watchers

 avatar  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.