{"id":15953380,"url":"https://github.com/delanni/if-expression","last_synced_at":"2025-03-17T15:31:25.751Z","repository":{"id":43891125,"uuid":"234601731","full_name":"delanni/if-expression","owner":"delanni","description":"$if 💰Expression based conditional.","archived":false,"fork":false,"pushed_at":"2024-07-09T15:36:03.000Z","size":103,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-08T13:11:43.310Z","etag":null,"topics":["expression","functional-js","javascript","library"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/delanni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-17T17:33:43.000Z","updated_at":"2024-07-09T15:36:08.000Z","dependencies_parsed_at":"2024-07-09T19:11:22.546Z","dependency_job_id":"7b24eb4b-b483-4165-8b73-a9966ddebd3f","html_url":"https://github.com/delanni/if-expression","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delanni%2Fif-expression","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delanni%2Fif-expression/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delanni%2Fif-expression/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/delanni%2Fif-expression/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/delanni","download_url":"https://codeload.github.com/delanni/if-expression/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221695584,"owners_count":16865251,"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":["expression","functional-js","javascript","library"],"created_at":"2024-10-07T13:12:13.778Z","updated_at":"2024-10-27T15:11:15.854Z","avatar_url":"https://github.com/delanni.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# $if 💰Expression based conditional.\n_You can read this as dolla'-if._\n\nJavascript's `if` is unfortunately not an expression, which means you can't use it as a right hand side of an expression or in places where you can only put a single value. Luckily, `$if` can function as an expression.\n\n## Installation\n\nFor now, you can only use this with node.js, or using a bundler, because it's not exposing any globals, but who are we kidding, it's 2020, you probably do this daily already.\n\n```bash\nnpm install --save dolla-if\n```\n\n## Examples\n\nThis if-expression works like a regular if-else clause, except, your expression has a return value:\n```javascript\nconst { $if } = require('dolla-if');\n// or to you who live in the future: \n// import $if from 'dolla-if';\n\nconst isPartyTime = new Date().getHours() \u003e 20 || new Date().getHours() \u003c 5;\nconst myFriendsCallMe = $if(isPartyTime)\n                            .then('ßling-ßling John$on')\n                            .else('Gregory Buttsoup');\nconsole.log({myFriendsCallMe});\n```\n\nIt's especially useful when you're trying to use some inline react JSX, because those inline curlies only support expressions, so unless you want to do some IIFE ugliness:\n```jsx\nconst RegularHeader = ({ dayOfTheWeek }) =\u003e {\n    return (\n        \u003cdiv\u003e\n            {\n                $if(dayOfTheWeek === 3).then(\u003ch1 style={partyTheme}\u003eIt's Wednesday my dudes! 🐸\u003c/h1\u003e)\n                .elseIf(dayOfTheWeek === 5).then(\u003ch1 style={partyTheme}\u003eCall Rebecca Black! 👯‍♂️\u003c/h1\u003e)\n                .else(\u003ch1 style={{backgroundColor: 'gray'}}\u003eNo partying allowed. 🙅‍♂️\u003c/h1\u003e)\n            }\n        \u003c/div\u003e\n    )\n}\n```\n\nYou can also use it with unit functions, to short circuit execution of branches where the chain executes to false:\n```javascript\nconst result = $if(false)\n    .thenDo(() =\u003e computeEngine.computeValue())\n    .elseIf(true).thenDo(() =\u003e computeEngine.computeSomethingElse())\n    .elseIf(Math.random() \u003e 0.5).then('bingo!')\n    .elseDo(computeEngine.getDefaultValue);\n```\nIn this above example, only one of the branches gets executed, so you don't need to worry about all the wasted calculation or unwanted side-effects.\n\n---\n\nAnd $if also supports types through [Typescript](http://typescriptlang.org/)! \n\n\u003csmall\u003eContains no meat™!\u003c/small\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdelanni%2Fif-expression","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdelanni%2Fif-expression","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdelanni%2Fif-expression/lists"}