Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

glum

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

glum

JS enums with type safety

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

glum

styled with prettier Travis Coveralls Dev Dependencies Donate

Summary

glum is a super lightweight Enum library that makes use of Symbols to achieve type-safety.

What if my environment doesn't support ES6?

If you're in a browser, use a polyfill. If you're running in Node, chances are that Symbols are supported: they're supported in all versions after 5.12.0.

Why use Glum?

  • Type-safety. When you declare an Enum, members are unique and can't be mimicked by strings, numbers, etc.
  • Allows for passing in polyfills
  • Don't reinvent the wheel

:scroll: Docs

Installation

npm install glum

Or, if in a browser,

<script crossorigin src="https://cdn.jsdelivr.net/npm/glum"></script>

[Note: this will create a global variable called Glum]

Usage

We distribute a UMD build, so you can use it with ES modules, CommonJS, AMD, or in the browser

import Enum from 'glum'
// or var Enum = require 'glum'

let FRUIT = new Enum('APPLE', 'ORANGE', 'PEAR')

function nameFruit (fruit) {
  if ((fruit = FRUIT.APPLE)) {
    console.log("It's an apple")
  }
}

console.log(FRUIT.getName(FRUIT.APPLE)) // APPLE

API

new Enum(... enumNames, [symbolPolyfill])

Create a new Enum. Ex. let fruits = new Enum("APPLE", "ORANGE", "PEAR") Ex. with polyfill let fruits = new Enum("APPLE", "ORANGE", "PEAR", function(name) {return '@@\_Symbol:' + name})

Enum.has(member)

Check if a member is part of an enum. Note: this will primarily be used to accept function parameters, variables, etc. that must be a member of a certain enum. Ex.

let fruits = new Enum('APPLE', 'ORANGE', 'PEAR')
fruits.has(fruits.APPLE) // true
fruits.has(fruits.HI) // false
Enum.getName(member)

Get the name of an enum member. Note: this is usually used to get the name of an Enum member passed as a parameter, variable, etc. Ex.

let fruits = new Enum('APPLE', 'ORANGE', 'PEAR')
fruits.getName(fruits.APPLE) // "APPLE"

:heavy_check_mark: Tests

Tests can be run with npm test. Multiple tests check that things return expected results, formatting follows guidelines, and code coverage is at the expected level.

Projects using glum

Create an issue to add yours!

Credits

Made with :heart: by @nebrelbug and all these wonderful contributors (emoji key):

This project follows the all-contributors specification. Contributions of any kind are welcome!

Keywords

FAQs

Package last updated on 22 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc