{"id":15631202,"url":"https://github.com/adrai/enum","last_synced_at":"2025-04-12T20:46:01.390Z","repository":{"id":5518092,"uuid":"6719427","full_name":"adrai/enum","owner":"adrai","description":"Enum is a javascript module that introduces the Enum Type. It works for node.js, in the browser and for deno.","archived":false,"fork":false,"pushed_at":"2020-05-13T07:28:51.000Z","size":384,"stargazers_count":199,"open_issues_count":1,"forks_count":29,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-04T00:09:56.411Z","etag":null,"topics":["enum","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adrai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["adrai"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2012-11-16T09:46:23.000Z","updated_at":"2024-12-22T17:34:22.000Z","dependencies_parsed_at":"2022-07-06T22:33:07.496Z","dependency_job_id":null,"html_url":"https://github.com/adrai/enum","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrai%2Fenum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrai%2Fenum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrai%2Fenum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrai%2Fenum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adrai","download_url":"https://codeload.github.com/adrai/enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631696,"owners_count":21136559,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","javascript"],"created_at":"2024-10-03T10:39:30.698Z","updated_at":"2025-04-12T20:46:01.366Z","avatar_url":"https://github.com/adrai.png","language":"JavaScript","readme":"# Introduction\n\n[![Actions](https://github.com/adrai/enum/workflows/node/badge.svg)](https://github.com/adrai/enum/actions?query=workflow%3Anode)\n[![travis](https://img.shields.io/travis/adrai/enum.svg)](https://travis-ci.org/adrai/enum)\n[![npm](https://img.shields.io/npm/v/enum.svg)](https://npmjs.org/package/enum)\n\nEnum is a javascript module that introduces the Enum Type. It works for node.js, in the browser and for deno.\n\n...and [ref](https://github.com/TooTallNate/ref) compatible [Known Types](https://github.com/TooTallNate/ref/wiki/Known-%22types%22)\n\n# Download\nReleases for a browser are available for download from GitHub.\n\n| **Version** | **Description** | **Size** |\n|:------------|:----------------|:---------|\n| `enum-3.0.4.js` | *uncompressed, with comments* | [Download](https://raw.github.com/adrai/enum/master/dist/enum-3.0.4.js) |\n| `enum-3.0.4.min.js` | *compressed, without comments* | [Download](https://raw.github.com/adrai/enum/master/dist/enum-3.0.4.min.js) |\n\n# Installation (node.js)\n\n    $ npm install enum\n\n# Installation (browser)\n\n[download](#download) the standalone file\n\n# Usage\n\n````js\n// use it as module\nconst Enum = require('enum')\n\n// or with import\nimport Enum from 'enum'\n\n// or in deno\nimport Enum from 'https://deno.land/x/enum/index.js'\n\n// or in browser\n\u003cscript src=\"enum.js\"\u003e\u003c/script\u003e\n\n// or extend node.js, deno or browser global/window with this new type\nEnum.register()\n\n// define a simple enum (automatically flaggable -\u003e A: 0x01, B: 0x02, C: 0x04)\n//Uses bitwise 'OR' operation in between the values and creates enumerated constants. For example, if 'Read':1, 'Write':2, then ReadWrite= Read | Write = 1 | 2 = 3\nconst myEnum = new Enum(['A', 'B', 'C'])\n\n//define a flagged enum object to create a multicolor option just pass an array\nconst myEnum = new Enum(['Black', 'Red', 'Green', 'Blue'])\nmyEnum //=\u003e Enum {_options: Object, enums: Array[4], Black: EnumItem, Red: EnumItem, Green: EnumItem….....}\nmyEnum.isFlaggable //=\u003e true\n\nfor (let i=1; i\u003c8; i++){ console.log(myEnum.get(i).value + '=\u003e '+ myEnum.get(i).key)}\n    1=\u003e Black\n    2=\u003e Red\n    3=\u003e Black | Red\n    4=\u003e Green\n    5=\u003e Black | Green\n    6=\u003e Red | Green\n    7=\u003e Black | Red | Green\n\n// define an enum with own values\nconst myEnum = new Enum({'A': 1, 'B': 2, 'C': 4})\n\n// if defining a flaggable enum, you can define your own separator (default is ' | ')\nconst myEnum = new Enum(['A', 'B', 'C'], { separator: ' | ' })\n\n// if you want your enum to have a name define it in the options\nconst myEnum = new Enum(['A', 'B', 'C'], { name: 'MyEnum' })\n\n// or\nconst myEnum = new Enum(['A', 'B', 'C'], 'MyEnum')\n\n// if you want your enum to have an explicit \"endianness\", define it in the options\n// (defaults to `os.endianness()`)\nconst myEnum = new Enum(['A', 'B', 'C'], { endianness: 'BE' })\n\n// if you want your enum to be not case sensitive\n// (defaults to `false`)\nconst myEnum = new Enum(['One', 'tWo', 'ThrEE'], { ignoreCase: true })\nmyEnum.get('one') // =\u003e myEnum.One\nmyEnum.get('TWO') // =\u003e myEnum.tWo\nmyEnum.ThrEE.is('three') // =\u003e true\n\n// this option will make instances of Enum non-extensible\n// (defaults to `false`)\nconst myEnum = new Enum(['ONE', 'TWO', 'THREE'], { freeze: true })\n\n//define enum type without flag\nconst myEnum = new Enum({'None': 0, 'Black':1, 'Red': 2, 'Red2': 3, 'Green': 4, 'Blue': 5})\nmyEnum //=\u003e  Enum {_options: Object, enums: Array[6], None: EnumItem, Black: EnumItem, Red: EnumItem…........}\nmyEnum.isFlaggable //=\u003e false\n\nmyEnum.toJSON() // returns {'None': 0, 'Black':1, 'Red': 2, 'Red2': 3, 'Green': 4, 'Blue': 5}\nJSON.stringify(myEnum) // returns '{\"None\":0,\"Black\":1,\"Red\":2,\"Red2\":3,\"Green\":4,\"Blue\":5}'\n\nfor(const i=0 i\u003c=5 i++){ console.log(myEnum.get(i).value + '=\u003e '+ myEnum.get(i).key)}\n    0=\u003e None\n    1=\u003e Black\n    2=\u003e Red\n    3=\u003e Red2\n    4=\u003e Green\n    5=\u003e Blue\n\n// iterating over an enum\nmyEnum.enums.forEach(function(enumItem) {\n  console.log(enumItem.key)\n})\n// =\u003e None\n// =\u003e Black\n// =\u003e Red\n// =\u003e Red2\n// =\u003e Green\n// =\u003e Blue\n\n// get your item\nmyEnum.A\n\n// or\nmyEnum.get('A')\n\n// or\nmyEnum.get(1)\n\n// or\nmyEnum.get('A | B')\n\n// or\nmyEnum.get(3)\n\n\n// get your value\nmyEnum.A.value\n\n// get your key\nmyEnum.A.key\n\n\n// get all items\nmyEnum.enums // returns all enums in an array\n\n// check if it's defined\nmyEnum.isDefined(myEnum.A) // returns true\nmyEnum.isDefined('A')      // returns true\nmyEnum.isDefined(1)        // returns true\n\n// compare\nmyEnum.A.is(myEnum.A)\n\n// or\nmyEnum.A.is('A')\n\n// or\nmyEnum.A.is(1)\n\n// or\nmyEnum.A == myEnum.A\n\n// or\nmyEnum.A === myEnum.A\n\n\n// check flag\nconst myItem = myEnum.get(3) // or [myEnum.get('A | B')]\nmyItem.has(myEnum.A)\n\n// or\nmyItem.has('A')\n\n// or\nmyItem.has(1)\n\n\n// other functions\nmyItem.toString() // returns 'A | C'\nmyItem.toJSON() // returns '\"A | C\"'\nmyItem.valueOf() // returns 3\n\nJSON.stringify(myItem) // returns '\"A | C\"'\n\n//Type Safety:\n//Newly created enumerable objects are Type-Safe in a way that it's non-configurable and no longer extensible.\n//Each EnumItem has a beack-reference to a constructor and they are implicitly final.\nObject.getOwnPropertyDescriptor(myEnum, 'Red') //=\u003e Object {value: EnumItem, writable: false, enumerable: true, configurable: false}\nObject.isExtensible(myEnum) //=\u003e false\nmyEnum instanceof Enum //=\u003e true\n\n//Instances of Enum created with 'new' from similar objects are not equal\nmyEnum1=new Enum({'A':1, 'B':2, 'C':4})\nmyEnum2=new Enum({'A':1, 'B':2, 'C':4})\nmyEnum1 == myEnum2 //=\u003e false\nmyEnum1.A == myEnum2.A //=\u003e false\nmyEnum1.A.value == myEnum2.A.value //=\u003e true\n\n//This enum object has no properties other than those defined during its creation. Existing Data is 'Persistent' and preserves the original version\nmyEnum.B.value //=\u003e 2\nmyEnum.B = 5 //=\u003e Throws TypeError\ndelete myEnum.B //=\u003e false\nmyEnum.D = 6 //=\u003e doesn't add to the enum object, silently ignores\nmyEnum.D // undefined\n\n//Try to define new property throws TypeError\nObject.defineProperty(myEnum, D, { value:6, writable:false, enumerable:true })\n//=\u003eTypeError: Cannot define property:D, object is not extensible.\n````","funding_links":["https://github.com/sponsors/adrai"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrai%2Fenum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadrai%2Fenum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrai%2Fenum/lists"}