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

python-enum

v1.1.0

Published

An implementation of python-like enums in JavaScript.

Downloads

22

Readme

python-enum

An implementation of python-like enums in JavaScript.

This is the repository for the python-enum npm package. This package was created as a tool to facilitate the migration of projects written in python to javascript. As such, it serves the usecase of providing a python-like api (functional-api) to enum objects in javascript, including the int-enum.

A side-by-side comparison

The following code examples demonstrate the JavaScript analogs of the Python enum module.

Python:

import enum as Enum

Animal = Enum.Enum('Animal', 'ANT BEE CAT DOG')
print(Animal)
# <enum 'Animal'>
print(Animal.ANT)
# <Animal.ANT: 1>
print(Animal.ANT.value)
# 1
list(Animal)
# [<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]


IntAnimal = Enum.IntEnum('IntAnimal','ANT BEE CAT DOG')
list(IntAnimal)
#  [<IntAnimal.ANT: 1>, <IntAnimal.BEE: 2>, <IntAnimal.CAT: 3>, <IntAnimal.DOG: 4>]
print(IntAnimal.ANT.value)
# 1
IntAnimal.ANT + 1
# 2
IntAnimal.ANT.CAT == IntAnimal.CAT
# True

JavaScript:

import Enum from 'python-enum'

const Animal = Enum.Enum('Animal', 'ANT BEE CAT DOG')
console.log(Animal) 
// ƒ [object Function]
console.log(Animal.ANT)
// Animal {val: 1, key: "ANT"}
console.log(Animal.ANT.value)
// 1
Animal.toArray()
// (4) [Animal, Animal, Animal, Animal]


const IntAnimal = Enum.IntEnum('IntAnimal','ANT BEE CAT DOG')
IntAnimal.toArray()
// (4) [IntAnimal, IntAnimal, IntAnimal, IntAnimal]
console.log(IntAnimal.ANT.value)
// 1
IntAnimal.ANT + 1
// 2
IntAnimal.ANT.CAT === IntAnimal.CAT
// true

Note: IntEnum values behave like integers in other ways you’d expect.

Derived Enumerations

IntEnum

The first variation of Enum that is provided is also a subclass of int. Members of an IntEnum can be compared to integers; by extension, integer enumerations of different types can also be compared to each other:

Python

from enum import IntEnum

class Shape(IntEnum):
   CIRCLE = 1
   SQUARE = 2
class Request(IntEnum):
   POST = 1
   GET = 2

Shape == 1
# False
Shape.CIRCLE == 1
# True
Shape.CIRCLE == Request.POST
# True

JavaScript

import IntEnum from 'python-enum'

const Shape = IntEnum('Shape', {
  CIRCLE: 1,
  SQUARE: 2,
})
const Request = IntEnum('Request', {
  POST: 1,
  GET: 2,
 })
Shape === 1
// false
Shape.CIRCLE === 1
// True
Shape.CIRCLE === Request.POST
// True

EnumMeta

You can check class type like so:

Python

foo = new Enum.Enum('foo', { 'a': 1 })
print(isinstance(foo, Enum.EnumMeta))
# True

JavaScript

const foo = new Enum.Enum('foo', { a: 1 })
console.log(foo instanceof Enum.EnumMeta)
// true

Supported _sunder_ names

  • _missing_ – a lookup function used when a value is not found; may be overridden

JavaScript

const foo = Enum.IntEnum('foo', {
  _missing_: function _missing_() { return 10 },
  a: 1,
  b: 2,
  c: 3,
})

Other Enum features

As of right now (3/17/2020) I will only be implementing the necessary Python enum module features for my own use. If you need additional enum features to be ported over from python you can make an issue or make a pull request.

For other feeback not strictly related to bugs or feature requests you can leave a comment here