{"id":18557388,"url":"https://github.com/prestaul/enum-props","last_synced_at":"2025-05-15T14:09:47.305Z","repository":{"id":33988008,"uuid":"37739906","full_name":"Prestaul/enum-props","owner":"Prestaul","description":"Enums with support for additional metadata","archived":false,"fork":false,"pushed_at":"2015-09-03T15:49:54.000Z","size":140,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-15T14:09:30.623Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Prestaul.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-19T18:48:23.000Z","updated_at":"2015-06-19T18:48:49.000Z","dependencies_parsed_at":"2022-07-13T22:42:45.036Z","dependency_job_id":null,"html_url":"https://github.com/Prestaul/enum-props","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fenum-props","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fenum-props/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fenum-props/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Prestaul%2Fenum-props/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Prestaul","download_url":"https://codeload.github.com/Prestaul/enum-props/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254355296,"owners_count":22057354,"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":[],"created_at":"2024-11-06T21:35:28.258Z","updated_at":"2025-05-15T14:09:42.282Z","avatar_url":"https://github.com/Prestaul.png","language":"JavaScript","readme":"# Introduction\n\nenum-props wraps the excellent enum implementation [adrai/enum](https://github.com/adrai/enum). It exposes an Enum factory that allows additional metadata to be added to each enum member.\n\n\n## Installation\n```bash\nnpm install -S enum-props\n```\n\n\n## Usage\n```js\nvar Enum = require('enum-props');\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;\nvar myEnum = new Enum(['A', 'B', 'C']);\n\n//define a flagged enum object to create a multicolor option; just pass an array\nvar 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(var 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\nvar myEnum = new Enum({'A': 1, 'B': 2, 'C': 4});\n\n//define enum type without flag\nvar 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\nfor(var 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// define an enum with own custom properties\nvar myEnum = new Enum({\n    'A': { value: 1, description: 'first' }, \n    'B': { value: 2, description: 'second' }, \n    'C': { value: 4, description: 'third' }\n});\n\nmyEnum.A.value // =\u003e 1\nmyEnum.A.description // =\u003e 'first'\n\n// enums and their members are frozen\nmyEnum.A.description = 'this is ignored'\nmyEnum.A.description // =\u003e 'first'\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\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 == 'A'\n\n// or\nmyEnum.A == myEnum.A\n\n// or\nmyEnum.A === myEnum.A\n\n\n// check flag\nvar 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\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```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprestaul%2Fenum-props","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprestaul%2Fenum-props","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprestaul%2Fenum-props/lists"}