{"id":18401522,"url":"https://github.com/ddoronin/monas","last_synced_at":"2025-04-07T07:31:35.394Z","repository":{"id":57301404,"uuid":"129474236","full_name":"ddoronin/monas","owner":"ddoronin","description":"🦋 Scala monads for javascript","archived":false,"fork":false,"pushed_at":"2019-03-26T04:30:22.000Z","size":303,"stargazers_count":22,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T14:11:51.680Z","etag":null,"topics":["either","monad","option","typescript"],"latest_commit_sha":null,"homepage":"https://ddoronin.github.io/monas/","language":"TypeScript","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/ddoronin.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":"2018-04-14T02:35:00.000Z","updated_at":"2024-11-21T21:25:26.000Z","dependencies_parsed_at":"2022-09-13T06:12:25.431Z","dependency_job_id":null,"html_url":"https://github.com/ddoronin/monas","commit_stats":null,"previous_names":["ddoronin/nifty-types","ddoronin/ninja-types"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddoronin%2Fmonas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddoronin%2Fmonas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddoronin%2Fmonas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddoronin%2Fmonas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ddoronin","download_url":"https://codeload.github.com/ddoronin/monas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247612074,"owners_count":20966660,"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":["either","monad","option","typescript"],"created_at":"2024-11-06T02:39:03.852Z","updated_at":"2025-04-07T07:31:35.128Z","avatar_url":"https://github.com/ddoronin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \u003ca href='https://ddoronin.github.io/monas/'\u003e\u003cimg src=\"https://github.com/ddoronin/monas/raw/master/assets/Monas.png\" alt=\"μονάς - Scala like monads for javascript\" height=\"80px\"/\u003e\u003c/a\u003e\n\nMonas (from Greek μονάς - \"singularity\") is a monad library for JavaScript apps. It's been inspired by [Scala](https://www.scala-lang.org) and developed with [TypeScript](http://www.typescriptlang.org). Monas introduces two fundamental monads: `Option\u003cA\u003e` and `Either\u003cA, B\u003e`.\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ddoronin/monas/blob/master/LICENSE) \n[![npm version](https://img.shields.io/npm/v/monas.svg?style=flat)](https://www.npmjs.com/package/monas) \n[![Build Status](https://travis-ci.org/ddoronin/monas.svg?branch=master)](https://travis-ci.org/ddoronin/monas) \n[![Coverage Status](https://coveralls.io/repos/github/ddoronin/monas/badge.svg?branch=master)](https://coveralls.io/github/ddoronin/monas?branch=master)\n[![Slack chat](https://now-examples-slackin-fpiresrxzs.now.sh/badge.svg)](https://now-examples-slackin-fpiresrxzs.now.sh) \n\n```bash\n$ yarn add monas\n```\n\n## Option\u0026lt;A\u003e\n\nRepresents optional values. Instances of Option are either an instance of `Some` or the object `None`.\n\nThe idea is to get rid of `null` and `undefined` and, thus, eliminate null pointer exceptions, reduce branching (if statement) and produce better code.\n\n| \u0026nbsp;Exported\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;| Description |\n| ------ | ----------- |\n| `Option\u003cA\u003e`  | the base **abstract** class that implements utility functions for instances of classes `Some` and `None`. It's primarily used to indicate that a type has an optional value. |\n| `Some\u003cA\u003e` |  one of the possible implementations of `Option\u003cA\u003e` that wraps a value. The incapsulated value is available by the `get()` method. |\n| `None\u003cA\u003e` | another implementation of `Option\u003cA\u003e` indicating absence of value.|\n| `some\u003cA\u003e(x: A): Option\u003cA\u003e` | a helper function instantiating objects of type `Some` or `None` based on a provided value.|\n| `none: Option\u003cA\u003e` | a single instance of `None\u003cA\u003e`. |\n\n\n```typescript\nlet greeting: Option\u003cstring\u003e = some('Hello world');\ngreeting = some(null); // will be none\nassert(greeting === none);\n```\n`some()` will return `none` if a given argument is `null` or `undefined`:\n\nReading data from `Option`:\n\na) `getOrElse()`:\n```typescript\nlet str: string = greeting.getOrElse(''); \n// Returns greeting or empty string if none.\n```\n\nb) `get()`:\n```typescript\nlet str: string = greeting.get(); \n// Returns greeting or throws an exception.\n```\n\nc) Using `Symbol.Iterable`:\n```typescript\nlet [str]: string = [...greeting]; \n// returns [\"something\"] or empty array [] if none.\n```\nOR\n```typescript\nfor(let str: string of greeting) {\n    assert(str, \"something\");\n}\n```\n\n### Examples\n\nThe most idiomatic way to use an `Option` instance is to treat it as a collection or monad and use map, flatMap, filter, or foreach.\n\nLet's consider an example where for a given country code we need to find the country name or print \"N/A\" if it's not found. \n\n```typescript\nimport { Option, none, some } from './Option';\n\ntype Country = { name: string, code: number };\nlet countries: Country[] = [{ name: 'United States', code: 1 }, { name: 'United Kingdom', code: 44 }];\n\nfunction getNameByCode(code: number): string {\n    // find a country by code\n    const country = countries.find(c =\u003e c.code === code);\n\n    // if the country is not null return the name or N/A\n    return some(country).map(_ =\u003e _.name).getOrElse('N/A');\n    //     ^^^           ^^^ select name   ^^^ get a value if exists\n    //     create Option\u003cCountry\u003e              otherwise use 'N/A'\n}\n```\n\nMore examples could be found [here](https://github.com/ddoronin/monas/blob/master/test/Option.examples.spec.ts).\n\n## Either\u0026lt;A, B\u003e\n\nRepresents a value of one of two possible types (a disjoint union). \nAn instance of Either is an instance of either `Left` or `Right`.\nConvention dictates that Left is used for failure and Right is used for success.\n\n| \u0026nbsp;Exported\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; | Description |\n| ------ | ----------- |\n| `Either\u003cA, B\u003e`| the base **abstract** class that implements utility functions for instances of classes `Left` and `Right`. |\n| `Right\u003cA, B\u003e` | a right \"good\" part. |\n| `Left\u003cA, B\u003e` | a left \"fail over\" part, e.g. Error. |\n| `right\u003cA, B\u003e(x: B): Either\u003cA, B\u003e` | a helper function instantiating `Right` objects. |\n| `left\u003cA, B\u003e(x: B): Either\u003cA, B\u003e` | a helper function instantiating `Left` objects. |\n\nGenerally `Either` can be considered as an alternative to `Option` where instead of \n`None` a useful information could be encapsulated into `Left`. \nIt turns out that `Either` is a power-full type for validations \nsince it can return either a successfully parsed value, or a validation error.\n\n`Either\u003cA, B\u003e` can be instantiated by calling `right(something)` or `left(error)`:\n\n```typescript\nlet eitherNumber: Either\u003cError, number\u003e = right(42); // equivalent to new Right(42)\n```\n\nOR\n\n```typescript\nlet eitherNumber: Either\u003cError, number\u003e = left(Error('Not a number')); // equivalent to new Left('Not a number')\n```\n\n`Either` is a right-biased monad:\n```typescript\nlet eitherNum: Either\u003cnumber, Error\u003e = right\u003cnumber, Error\u003e(42);\neitherNum.map(num =\u003e num * 2).getOrElse(-1);\n// returns 84\n```\nAnother example:\n```typescript\nlet eitherNum: Either\u003cnumber, Error\u003e = left\u003cnumber, Error\u003e(Error('Not a number.'));\neitherNum.map(_ =\u003e _ * 2).getOrElse(-1);\n// returns -1\n```\nUse `mapLeft()`, `foreachLeft()` to handle `Left` values:\n\n```typescript\nfunction print(numberOrError: Either\u003cError, number\u003e) {\n    numberOrError\n        .map(num =\u003e num)\n        .foreach(printNumber)\n        .mapLeft(err =\u003e err.message)\n        .foreachLeft(printError);\n}\n```\n\nUse `swap()` function to swap `Left` and `Right`.\n\n```typescript\nleft\u003cnumber, string\u003e('Not a number').swap().getOrElse('');\n// returns 'Not a number'\n```\n\n`Either` implements `Symbol.Iterable`:\n```typescript\nlet eitherNumber = right\u003cnumber, Error\u003e(42).map(_ =\u003e _ * 2);\n\nlet [n] = [...eitherNumber]; // n === 42\n\nfor(let num of eitherNumber) {\n    assert(num, 42);\n}\n```\n\nMore examples could be found [here](https://github.com/ddoronin/monas/blob/master/test/Either.examples.spec.ts).\n\n## Docs\n\n1. Medium\n  [Monas — Scala monads for javascript](https://medium.com/@dmitrydoronin/monas-scala-monads-for-javascript-1e9cd7e82113)\n\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddoronin%2Fmonas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fddoronin%2Fmonas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddoronin%2Fmonas/lists"}