@jslibrary/enumeration

0.0.5 • Public • Published

Enumeration

An useful concept of Enumeration in JavaScript!


🌟 About the Project

This project was created in order to provide an useful and helpful JavaSciprt Enumeration for developers.

You can use it for comparisons in code and also are able to generate an array that can be easily binded in a select component, for example.

🧰 Getting Started

⚙️ Installation

Install enumeration with npm

  npm install @jslibrary/enumeration

👀 Usage

First of all, you have to create your enumerations. There are two suggested ways for doing it:

1 - Extending Enumeration class.

or

2 - Using a new instance of Enumeration.

1 - Extending Enumeration class ...

import { Enumeration } from '@jslibrary/enumeration'

Class Gender extends Enumeration {
  constructor() {
    super()

    this.add('M', 'Male', 'MALE')
    this.add('F', 'Female', 'FEMALE')
  }
}

export default new Gender()

2 - Using a new Instance of Enumeration ...

import { Enumeration } from '@jslibrary/enumeration'

const enumeration = new Enumeration()

enumeration.add('M', 'Male', 'MALE')
enumeration.add('F', 'Female', 'FEMALE')

export default enumeration

After this, you are ready to use your own enumeration in your code

Using my own Enumeration ...

import Gender from './Gender'

if (person.gender === Gender.Female) {
  ...
}

If you need a JSON to populate a select with this options, you can call .toJSON() method. The result is something like this:

[
  {value: 'M', variable: 'Male', text: 'MALE'},
  {value: 'F', variable: 'Female', text: 'FEMALE'}
]

Binding this JOSN on a select component the result is this:

<v-select
  label="GENDER"
  v-model="person.gender"
  :items="Gender.toJSON()"
>
</v-select>



It's ok if you need to use another type of value like number, for example. This class is flexible!

...
  this.add(1, 'Male', 'MALE')
  this.add(2, 'Female', 'FEMALE')
...

Readme

Keywords

Package Sidebar

Install

npm i @jslibrary/enumeration

Weekly Downloads

0

Version

0.0.5

License

ISC

Unpacked Size

15.1 kB

Total Files

15

Last publish

Collaborators

  • carlosalexandre1985