{"id":20079289,"url":"https://github.com/jbreckmckye/highly-questionable","last_synced_at":"2025-03-02T13:16:39.234Z","repository":{"id":33995124,"uuid":"164155293","full_name":"jbreckmckye/highly-questionable","owner":"jbreckmckye","description":"Result / Option pseudomonad for TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-04T21:01:50.000Z","size":592,"stargazers_count":2,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-13T01:13:05.002Z","etag":null,"topics":["defensive-programming","maybe-monad","result-monad","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jbreckmckye.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":"2019-01-04T21:13:20.000Z","updated_at":"2019-12-01T12:00:32.000Z","dependencies_parsed_at":"2023-01-15T03:51:55.319Z","dependency_job_id":null,"html_url":"https://github.com/jbreckmckye/highly-questionable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbreckmckye%2Fhighly-questionable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbreckmckye%2Fhighly-questionable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbreckmckye%2Fhighly-questionable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbreckmckye%2Fhighly-questionable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jbreckmckye","download_url":"https://codeload.github.com/jbreckmckye/highly-questionable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241509656,"owners_count":19974071,"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":["defensive-programming","maybe-monad","result-monad","typescript"],"created_at":"2024-11-13T15:21:22.517Z","updated_at":"2025-03-02T13:16:39.214Z","avatar_url":"https://github.com/jbreckmckye.png","language":"TypeScript","readme":"# Highly Questionable\n\nA TypeScript / JavaScript library for paranoid developers.\n\n**Caution, experimental!**\n\nHighly Questionable allows you to safely and elegantly handle values that might be null, undefined or Errors, without writing tedious null checks and try-catches everywhere. It is loosely based on the `Option`, `Maybe` and `Result` monads from other languages.\n\nThink of it like a synchronous `Promise`, with `map/catch` methods equivalent to `then/catch`. 'Map' won't be called if the value doesn't exist, and you can handle errors with `catch`.\n\n## Concept\n\nValues are wrapped in a `Perhaps` object. When you want to use the value, you pass perhaps 'mapping' functions that are only run if the value is valid. The output of the 'mapping' gets wrapped in a new `Perhaps`, and so on, so forth, like so:\n\n```typescript\nconst userName = Perhaps.of(userJSON)\n    .map(json =\u003e JSON.parse(json))\n    .map(user =\u003e user.details)\n    .map(details =\u003e details.name);\n```\n\nThere are three things that could normally fail here: JSON.parse might throw an error; the user may not have details; or details may not have a name. `Perhaps` will handle each one gracefully.\n\nWhen you need to actually use a value, there are various methods and checks to make this safe:\n\n```typescript\nuserName.forOne(name =\u003e {\n    // This only runs if userName exists and contains no errors\n    print(name);\n});\n\nif (userName instanceof Something) {\n    // If using TypeScript, the compiler will now know the unwrapped value is safe\n    print(userName.unwrap());\n}\n\nif (userName !== Nothing) {\n    // 'catch' is another way to handle errors, and works like Promise.catch\n    const unwrapped = userName.catch(error =\u003e 'Unretrievable').unwrap();\n    print(unwrapped);\n}\n\nprint(userName.unwrapOr('Anonymous'));\n```\n\nYou can also catch exceptions at your leisure:\n\n```typescript\nuserName.catch(err =\u003e {\n    logException(err);\n    return 'Anonymous'; //  can pass a default to use\n});\n```\n\nAnd throw exceptions when values don't exist:\n\n```typescript\nuserName.unwrapOrThrow(new Error('Could not retrieve user name'));\n```\n\nNote that every `Perhaps` object is immutable: mapping one will create a new instance, unless the output is Nothing (which is a singleton, as all Nothings are the same).\n\nFor more details, consult the [API docs](API.md).\n\n## Setup \u0026 usage\n\nInstalling the package:\n\n```bash\nnpm install highly-questionable\n```\n\nImporting the code:\n\n```typescript\n// TypeScript / ESNext\nimport {Perhaps, Something, Nothing, Problem} from 'highly-questionable';\n```\n\n```javascript\n// Node\nconst {Perhaps, Something, Nothing, Problem} = require('highly-questionable');\n```\n\nTypeScript should work out of the box.\n\n## API\n\nSee [API.md](API.md).\n\n## Due dilligence\n\n### License\n\nThis library is provided under an Apache 2.0 license. For more details, see [LICENSE.md];\n\n### Dependencies\n\nThis project has no production dependencies.\n\n### Library size\n\nAt v1.1, the whole library minified and gzipped amounted to 1018 bytes.\n\n## Contributing / developing\n\n1. Check out the code and install Node\n2. Use `npm install` to install project dependencies\n3. Write your TypeScript\n4. Test with `npm test`\n5. Build the bundle with `npm run build`\n6. Raise a pull request","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbreckmckye%2Fhighly-questionable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbreckmckye%2Fhighly-questionable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbreckmckye%2Fhighly-questionable/lists"}