{"id":20565739,"url":"https://github.com/phenax/enum-fp","last_synced_at":"2025-04-14T15:35:32.711Z","repository":{"id":33047156,"uuid":"150369791","full_name":"phenax/enum-fp","owner":"phenax","description":"Functional Enum type / Sum type for javascript with simple pattern matching","archived":false,"fork":false,"pushed_at":"2023-09-28T04:50:17.000Z","size":3293,"stargazers_count":29,"open_issues_count":21,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T07:18:08.685Z","etag":null,"topics":["algebraic-data-types","enum","functional-programming","js","sum-types"],"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/phenax.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-26T04:44:44.000Z","updated_at":"2024-06-04T05:00:50.000Z","dependencies_parsed_at":"2023-01-14T23:15:45.412Z","dependency_job_id":null,"html_url":"https://github.com/phenax/enum-fp","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fenum-fp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fenum-fp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fenum-fp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fenum-fp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phenax","download_url":"https://codeload.github.com/phenax/enum-fp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248906931,"owners_count":21181245,"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":["algebraic-data-types","enum","functional-programming","js","sum-types"],"created_at":"2024-11-16T04:38:54.753Z","updated_at":"2025-04-14T15:35:32.691Z","avatar_url":"https://github.com/phenax.png","language":"JavaScript","readme":"\n# Enum-FP\nFunctional Enum type / Sum type for javascript with simple pattern matching\n\n[![CircleCI](https://img.shields.io/circleci/project/github/phenax/enum-fp/master.svg?style=for-the-badge)](https://circleci.com/gh/phenax/enum-fp)\n[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/enum-fp.svg?style=for-the-badge)](https://www.npmjs.com/package/enum-fp)\n[![Codecov](https://img.shields.io/codecov/c/github/phenax/enum-fp.svg?style=for-the-badge)](https://codecov.io/gh/phenax/enum-fp)\n\n\n[Checkout the docs for more information](./docs)\n\n[Medium article on SumTypes using EnumFP](https://medium.com/@phenax5/writing-cleaner-and-safer-javascript-with-sum-types-bec9c68ba7aa)\n\n## Install\n\n#### To add the project to your project\n```bash\nyarn add enum-fp\n```\n\n## Usage\n\n#### Import it to your file\n```js\nimport Enum from 'enum-fp';\n```\n\n#### Create an enum type\n```js\nconst Action = Enum([ 'Add', 'Edit', 'Delete', 'Get' ]);\n\n// Or with a fixed number of arguments\nconst Maybe = Enum({\n    Just: [ 'value' ],\n    Nothing: [],\n});\n```\n\n#### Create an instance of the type using one of the contructors\n```js\nconst action = Action.Edit(2, 'Hello world and India');\n```\n\n#### Pattern matching\n```js\nconst Action = Enum([ 'Add', 'Edit', 'Delete', 'DeleteAll', 'Get' ]);\n\nconst logMessage = action =\u003e console.log('\u003e\u003e', \n  Action.match(action, {\n    Edit: (id, message) =\u003e `Editing [${id}] to \"${message}\"`,\n    Add: message =\u003e `Adding \"${message}\"`,\n    Delete: id =\u003e `Deleting [${id}]`,\n    DeleteAll: () =\u003e 'Deleting all entries',\n    _: () =\u003e 'Unknown action', // To handle default cases, use _\n  })\n);\n\nlogMessage(Action.Add('Earth'));      // \u003e\u003e Adding \"Earth\"\nlogMessage(Action.Add('Earth 2'));    // \u003e\u003e Adding \"Earth 2\"\nlogMessage(Action.Add('Pluto'));\nlogMessage(Action.Add('Pluto'));       // \u003e\u003e Adding \"Pluto1\"\nlogMessage(Action.Edit(1, 'Mars'));   // \u003e\u003e Editing [2] to \"Mars\"\nlogMessage(Action.Delete(2));         // \u003e\u003e Deleting [3]\nlogMessage(Action.Add('Pluto'));      // \u003e\u003e Adding \"Pluto\"\nlogMessage(Action.DeleteAll());       // \u003e\u003e Deleting all entries\n\n// As Get action is not handled in the pattern, it will execute the default\nlogMessage(Action.Get());             // \u003e\u003e Unknown action\n```\n\n#### Type validation\nYou can add strict type validation instead of argument descriptions. You can read more about types module [here](./docs/react.md)\n\n```js\nimport T from 'enum-fp/types';\n\nconst TodoAction = Enum({\n  Add: [ T.String('message') ],\n  SetChecked: [ T.Number('id'), T.Bool('isChecked') ],\n  Delete: [ T.Number('id') ],\n  Edit: [ T.Number('id'), T.String('message') ],\n  DeleteAll: [],\n});\n```\n\nNOTE: The string passed to the functions are just for documentation purposes and are optional. It won't affect the behavior of the type in any way.\n\n\n\n\n### Enum use cases\n\n#### In the react world\n`You can use it to manage react component state!` [Checkout the documentation](./docs/react.md)\n\n\n#### Safely work with empty/invalid states\n\n* Working with invalid values\n```js\n// Just an example. You should use `Maybe` functor in cases like these\nconst Value = Enum({ Invalid: [], Valid: ['value'] });\n\nconst extractName = user =\u003e user \u0026\u0026 user.name\n  ? Value.Valid(user.name)\n  : Value.Invalid();\n\nconst splitBySpace = Value.cata({\n  Valid: name =\u003e name.split(' '),\n  Invalid: () =\u003e [],\n});\n\nconst getNameSplit = compose(splitBySpace, extractName);\n\nconst [ firstName, lastName ] = getNameSplit({ name: 'Akshay Nair' }); // \u003e\u003e returns ['Akshay','Nair']\n```\n\n\n#### In the functional world\nIf you are unfamiliar with `functors`, you can read [Functors in JS](https://hackernoon.com/functors-in-javascript-20a647b8f39f) blog post.\n\n* **Maybe**\n\n`Maybe` functor is used to handle null.\n\n```js\nconst Maybe = Enum({ Just: ['value'], Nothing: [] });\n\nconst fmap = fn =\u003e Maybe.cata({\n  Just: compose(Maybe.Just, fn),\n  Nothing: Maybe.Nothing,\n});\n```\n\n* **Either**\n\n`Either` functor is used for handling exceptions\n\n```js\nconst Either = Enum({ Left: ['error'], Right: ['value'] });\n\nconst fmap = fn =\u003e Either.cata({\n  Left: Either.Left,\n  Right: compose(Either.Right, fn),\n});\nconst fmapFail = fn =\u003e Either.cata({\n  Left: compose(Either.Left, fn),\n  Right: Either.Right,\n});\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphenax%2Fenum-fp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphenax%2Fenum-fp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphenax%2Fenum-fp/lists"}