{"id":19707727,"url":"https://github.com/thecodrr/whatifjs","last_synced_at":"2025-10-27T09:16:15.580Z","repository":{"id":66253906,"uuid":"207463198","full_name":"thecodrr/WhatIfJS","owner":"thecodrr","description":"WhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.","archived":false,"fork":false,"pushed_at":"2019-09-14T17:13:46.000Z","size":29,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-26T03:17:23.374Z","etag":null,"topics":["conditional-statements","expressions","extension","functions","helper","if-else","javascript","nodejs","whatif"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/thecodrr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":"thecodrr","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-09-10T04:10:59.000Z","updated_at":"2024-12-06T06:58:56.000Z","dependencies_parsed_at":"2023-02-20T21:30:56.657Z","dependency_job_id":null,"html_url":"https://github.com/thecodrr/WhatIfJS","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/thecodrr/WhatIfJS","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodrr%2FWhatIfJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodrr%2FWhatIfJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodrr%2FWhatIfJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodrr%2FWhatIfJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodrr","download_url":"https://codeload.github.com/thecodrr/WhatIfJS/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodrr%2FWhatIfJS/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265900051,"owners_count":23845898,"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":["conditional-statements","expressions","extension","functions","helper","if-else","javascript","nodejs","whatif"],"created_at":"2024-11-11T21:39:43.379Z","updated_at":"2025-10-27T09:16:10.547Z","avatar_url":"https://github.com/thecodrr.png","language":"JavaScript","readme":"# WhatIfJS\n\n\u003cp align=\"center\"\u003e\n \u003ca href=\"https://www.npmjs.com/package/whatif-js\"\u003e\u003cimg alt=\"total downloads of WhatIfJS\" src=\"https://img.shields.io/npm/dt/whatif-js.svg\"/\u003e\u003c/a\u003e\n \u003ca href=\"https://www.npmjs.com/package/whatif-js\"\u003e\u003cimg alt=\"WhatIfJS's License\" src=\"https://img.shields.io/npm/l/whatif-js.svg\"/\u003e\u003c/a\u003e\n \u003ca href=\"https://www.npmjs.com/package/whatif-js\"\u003e\u003cimg alt=\"latest version of WhatIfJS\" src=\"https://img.shields.io/npm/v/whatif-js.svg\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\nWhatIf is an extremely simple and easy to use JavaScript extension for expressing a single if-else statement anywhere, anytime.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://i.imgur.com/3N2bap4.png\" width=\"844\" height=\"341\"/\u003e\n\u003c/p\u003e\n\n## Installation:\n\nInstall using `yarn` or `npm`\n\n```bash\nnpm i --save whatif-js\nyarn add whatif-js\n```\n\nThen in the `entryPoint` of your `app`/`module`just ` require``whatIf `:\n\n```javascript\nrequire(\"whatif-js\");\n```\n\nAnd that's it, nothing more needed for setup. Everything is handled by the module itself.\n\n## Usage\n\n### WhatIf\n\n`WhatIf` is an expression for invoking an `action` when the given value is not-`null`, not-`undefined` and `true`.\n\nThe `whatIf`function has **three parameters**:\n\n1. `given` - the value\n\n2. `action` - the action to perform if the `given` value is not-`null`, not-`undefined` and `true`.\n\n3. `actionWhatIfNot` - (optional) - the action to perform in case the `given` value returns `false`.\n\n#### Examples:\n\nThe most basic (and useless) example would be:\n\n```javascript\nlet obj = { name: \"Awesome App\", type: \"App\" };\nobj = obj.whatIf(obj.type === \"App\", function() {\n  this[\"description\"] = \"I am an awesome app.\";\n});\n//output =\n//{\n//  name: 'Awesome App',\n//  type: 'App',\n//  description: 'I am an awesome app.'\n//}\n```\n\nNow let's get a **little bit** more complex. Usage in a `builder` like an `Array` or `String`also works.\n\n```javascript\nlet array = [0, 1, 2, 3, 4, 5];\narray = array\n  .whatIf(array.length === 6, ctx =\u003e {\n    return ctx.concat(6, 7, 8, 9);\n  })\n  .slice(2, 4)\n  .join(\",\");\n//output = 2,3,4,5,6,7,8,9\n```\n\n`WhatIf` can also be used directly with `booleans`like so:\n\n```javascript\nfunction workingWithBoolean(stopWork) {\n  stopWork.whatIf(function() {\n    console.log(\"Stop all the work!\");\n  });\n}\n```\n\nNow coming to the **the more complex** example. Using `WhatIf` with functions.\n\nThere are two ways of using `WhatIf` with `functions`, the first is more simple:\n\n```javascript\nfunction calculateSimple(doMultiplication, doDivision) {\n  //three methods for three mathematical operations\n  function add(...params) {\n    return params.reduce((p, c) =\u003e p + c);\n  }\n  function multiply(...params) {\n    return params.reduce((p, c) =\u003e p * c);\n  }\n  function divide(...params) {\n    return params.reduce((p, c) =\u003e p / c);\n  }\n\n  //the values to work with\n  let paramaters = [21, 23, 41, 51, 5, 1];\n\n  //the result\n  let result = add //perform add if both parameters are false\n    .whatIf(doMultiplication, multiply) //this will only be executed if doDivision=false \u0026 doMultiplication=true\n    .whatIf(doDivision, divide) //if doDivision=true, it will always take precedence no matter the value of doMultiplication\n    .call(this, ...paramaters); //the final function call\n  return result;\n}\n```\n\nWe can use the above `function` `calculateSimple`in the following three ways:\n\n```javascript\ncalculateSimple(false, false); //142\ncalculateSimple(false, true); //0.0000873307965816231\ncalculateSimple(true, false); //5049765\ncalculateSimple(true, true); //0.0000873307965816231\n```\n\n**More complex example** includes performing \u003cu\u003eDMAS (Division, Multiplication, Addition, Subtraction)\u003c/u\u003e on a `string`using only the power of`WhatIf`. See the [full example here](https://github.com/thecodrr/WhatIfJS/tree/master/examples/index.js)\n\n### WhatIfNotNull\n\n`whatIfNotNull` is an expression for invoking an `action` when the target object is not null.\n\n```kotlin\nlet person = getPersonByUsername(\"John Doe\");\nperson.whatIfNotNull(\n    function() {\n      console.log(\"person is not null.\");\n    },\n    function() {\n      console.log(\"person is null\");\n    }\n);\n```\n\n### WhatIfNotUndefined\n\n`whatIfNotUndefined` is exactly like `whatIfNotNull`but it checks whether the object is `undefined` or not.\n\n### WhatIf With Different Return Type\n\nIn cases where you want a different return type then the input you can use `whatIf` like this:\n\n```javascript\nlet stringLength = \"i am a string\".whatIf(getLength, function() {\n  return this.length;\n});\n```\n\n### WhatIfNotNullOrEmpty\n\nAn expression for invoking an `action` when the given `array` is not null and not empty.\nIf the `array` is null or empty, `actionWhatIfNot` will be invoked instead of the `action`.\n\n```kotlin\nanyArray.whatIfNotNullOrEmpty(\n  function() {\n    console.log(\"array is not null or empty.\");\n  },\n  function() {\n    console.log(\"array is null or empty\");\n  }\n);\n```\n\n### Using Arrow Functions\n\nAll `function`s of `whatIf`are usable with `arrow functions` like so:\n\n```javascript\nanyArray.whatIfNotNullOrEmpty(\n  ctx =\u003e console.log(\"array is not null or empty.\"),\n  ctx =\u003e console.log(\"array is null or empty\")\n);\n```\n\n## Find this library useful? :heart:\n\nSupport it by joining **[stargazers](https://github.com/thecodrr/WhatIfJS/stargazers)** for this repository. :star:or [buy me a cup of coffee](https://ko-fi.com/thecodrr)\nAnd **[follow](https://github.com/thecodrr)** me for my next creations! 🤩\n\n# License\n\n```xml\nCopyright 2019 thecodrr (Abdullah Atta)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","funding_links":["https://ko-fi.com/thecodrr"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodrr%2Fwhatifjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodrr%2Fwhatifjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodrr%2Fwhatifjs/lists"}