{"_id":"whatif","_rev":"30-d3fd40686540ff5346c8a2d924c9cd72","name":"whatif","dist-tags":{"latest":"1.0.6"},"versions":{"1.0.0":{"name":"whatif","version":"1.0.0","author":{"name":"HomyeeKing"},"license":"MIT","_id":"whatif@1.0.0","maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"homepage":"http://git@github.com:HomyeeKing/whatif.git","dist":{"shasum":"6f8650b0ea8dac3f4eaa14c0b2bac3f7621adab8","tarball":"https://registry.npmjs.org/whatif/-/whatif-1.0.0.tgz","fileCount":3,"integrity":"sha512-fW1gx7pVI8P1YXO/T6MVwiogY/xYb8JxDdl6gz8Oa56evcD7a+3V4K8JtPmhLLcOdPFmQIC1n6KEA7K1Oz9SzA==","signatures":[{"sig":"MEQCIGNavlsR0aZ9HAhKtnK66VDLifVOSQTkQP2jesJKxUtgAiA+/+dtn04T3SvCDNcDGrOsTeSXLpYhDTknVljVsFYBlA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":2457},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{"import":"./dist/index.mjs","require":"./dist/index.js"},"gitHead":"ac6ba797e0b8853d7ed63e853ba093de5ddf38de","scripts":{"cov":"vitest --coverage","dev":"npm run build-fast --  --watch","test":"vitest","build":"tsup","preversion":"npm run test -- --run && npm run build","f2elint-fix":"f2elint fix","postversion":"git push","publish:pre":"sh scripts/publish.sh pre","f2elint-scan":"f2elint scan","publish:prod":"sh scripts/publish.sh prod"},"_npmUser":{"name":"homy","email":"HomyeeKing@gmail.com"},"repository":{"url":"git+ssh://git@github.com/HomyeeKing/whatif.git","type":"git"},"_npmVersion":"10.9.0","description":"Smarter If","directories":{},"_nodeVersion":"20.16.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"7.1.0","vitest":"0.33.0","prettier":"3.0.0","typescript":"5.1.6","@types/node":"^20.4.2","@egoist/prettier-config":"1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/whatif_1.0.0_1740640208775_0.23441175220662713","host":"s3://npm-registry-packages-npm-production"}},"1.0.2":{"name":"whatif","version":"1.0.2","author":{"name":"HomyeeKing"},"license":"MIT","_id":"whatif@1.0.2","maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"homepage":"http://git@github.com:HomyeeKing/whatif.git","dist":{"shasum":"06d29b3e8a34a01beba168e44bf5327a50000eda","tarball":"https://registry.npmjs.org/whatif/-/whatif-1.0.2.tgz","fileCount":7,"integrity":"sha512-wiH6YIJ937uqZTG8hnfDpyAmjz49yoHR9d8lJxApl/Uvd8vu99lJyiLzYJ80Ivr02C8bWXnrG8YJW4PsCnxDkA==","signatures":[{"sig":"MEUCIQCb4O809Kd6GBq+2D0fIjtdxx/UiN8UL13CP2I/hRetcAIgUF7RkUGygGoZnZ6Vr+hIpdZ9Ahan6UDhI3I38ob6yVA=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":6768},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{"import":"./dist/index.mjs","require":"./dist/index.js"},"gitHead":"3bdda68315dfa93adfdb4f8fcf5e5bf92c8e0ff4","scripts":{"cov":"vitest --coverage","dev":"npm run build-fast --  --watch","test":"vitest","build":"tsup","preversion":"npm run test -- --run && npm run build","f2elint-fix":"f2elint fix","postversion":"git push","publish:pre":"sh scripts/publish.sh pre","f2elint-scan":"f2elint scan","publish:prod":"sh scripts/publish.sh prod"},"_npmUser":{"name":"homy","email":"HomyeeKing@gmail.com"},"repository":{"url":"git+ssh://git@github.com/HomyeeKing/whatif.git","type":"git"},"_npmVersion":"10.9.0","description":"Smarter If","directories":{},"_nodeVersion":"20.16.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"7.1.0","vitest":"0.33.0","prettier":"3.0.0","typescript":"5.1.6","@types/node":"^20.4.2","@egoist/prettier-config":"1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/whatif_1.0.2_1740643654992_0.6489964438237577","host":"s3://npm-registry-packages-npm-production"}},"1.0.3":{"name":"whatif","version":"1.0.3","author":{"name":"HomyeeKing"},"license":"MIT","_id":"whatif@1.0.3","maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"homepage":"http://git@github.com:HomyeeKing/whatif.git","dist":{"shasum":"acd182cbd01e82c30045a3d1f9cde57fb2ef1dd3","tarball":"https://registry.npmjs.org/whatif/-/whatif-1.0.3.tgz","fileCount":7,"integrity":"sha512-FvK6CMUalwMCUxsRGZVOl7R/ypCDkQZTNbVC8QgYHlWvIppCMei1X86V/Faocr6LLOn2cl0WQ1Yy4jo1oXrhdg==","signatures":[{"sig":"MEUCICINi1zO5s0sRwNvVOeuaXa9A64A/bwp4Tj80f+n0G3gAiEA6Fkm7Q2j59qtye/xemvBDsVbET+/eiV+baUo/dGukac=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":6832},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{"import":"./dist/index.mjs","require":"./dist/index.js"},"gitHead":"1cd2564bc6645003bd2caa190e84fbb6c0f3abd3","scripts":{"cov":"vitest --coverage","dev":"npm run build-fast --  --watch","test":"vitest","build":"tsup","preversion":"npm run test -- --run && npm run build","f2elint-fix":"f2elint fix","postversion":"git push","publish:pre":"sh scripts/publish.sh pre","f2elint-scan":"f2elint scan","publish:prod":"sh scripts/publish.sh prod"},"_npmUser":{"name":"homy","email":"HomyeeKing@gmail.com"},"repository":{"url":"git+ssh://git@github.com/HomyeeKing/whatif.git","type":"git"},"_npmVersion":"10.9.0","description":"Smarter If","directories":{},"_nodeVersion":"20.16.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"7.1.0","vitest":"0.33.0","prettier":"3.0.0","typescript":"5.1.6","@types/node":"^20.4.2","@egoist/prettier-config":"1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/whatif_1.0.3_1740644760619_0.7787229109240821","host":"s3://npm-registry-packages-npm-production"}},"1.0.4":{"name":"whatif","version":"1.0.4","author":{"name":"HomyeeKing"},"license":"MIT","_id":"whatif@1.0.4","maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"homepage":"http://git@github.com:HomyeeKing/whatif.git","dist":{"shasum":"f9514a2325b37a7e02a541c0355f2a892a5b1d94","tarball":"https://registry.npmjs.org/whatif/-/whatif-1.0.4.tgz","fileCount":7,"integrity":"sha512-vpaFg8qZKY6GR2/VLi1ZsmMBjUHzsiiaXLV3cFZ2L7eit8J54RYWjl9GPnLMHBhvselU5tkiO+OuZ3mjQVA6hw==","signatures":[{"sig":"MEYCIQCQXg2m56rRROdvTXEgCDc4+s89/Eh881kk6nqCjHIeygIhAPaRD8TYHVI16DNWJW2dLxH+ZPZgpxh4XFZy+TBU70f+","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":6832},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{"import":"./dist/index.mjs","require":"./dist/index.js"},"gitHead":"6da166a6ee7195f1674b3be79e4638b3dc2c47b3","scripts":{"cov":"vitest --coverage","dev":"npm run build-fast --  --watch","test":"vitest","build":"tsup","preversion":"npm run test -- --run && npm run build","f2elint-fix":"f2elint fix","postversion":"git push","publish:pre":"sh scripts/publish.sh pre","f2elint-scan":"f2elint scan","publish:prod":"sh scripts/publish.sh prod"},"_npmUser":{"name":"homy","email":"HomyeeKing@gmail.com"},"repository":{"url":"git+ssh://git@github.com/HomyeeKing/whatif.git","type":"git"},"_npmVersion":"10.9.0","description":"Smarter If","directories":{},"_nodeVersion":"20.16.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"7.1.0","vitest":"0.33.0","prettier":"3.0.0","typescript":"5.1.6","@types/node":"^20.4.2","@egoist/prettier-config":"1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/whatif_1.0.4_1740645102608_0.536994677245509","host":"s3://npm-registry-packages-npm-production"}},"1.0.5":{"name":"whatif","version":"1.0.5","author":{"name":"HomyeeKing"},"license":"MIT","_id":"whatif@1.0.5","maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"homepage":"http://git@github.com:HomyeeKing/whatif.git","dist":{"shasum":"d136f315cbc49e2aacbc9d3ed0dc16bbbe1920b3","tarball":"https://registry.npmjs.org/whatif/-/whatif-1.0.5.tgz","fileCount":7,"integrity":"sha512-Tt/m2RG8gQe9GNGfFBloM95Glh8JHiam742bVUo+3ZsT91KlFZ671lkkdIB4kkKfg5a/xYfMmONERUfkSsDo6A==","signatures":[{"sig":"MEQCIBy2Kq+b6hX0NQ3ACp/GjI4SKSsYGTkOc5CUey+SxyMEAiAD39QnFXbSGk1szd2/IISrjP0HMQwZaFtubcQKD6JnQA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":8002},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{"import":"./dist/index.mjs","require":"./dist/index.js"},"gitHead":"7c2eff298ca774a09455d35c23fef07f1faf754c","scripts":{"cov":"vitest --coverage","dev":"npm run build-fast --  --watch","test":"vitest","build":"tsup","preversion":"npm run test -- --run && npm run build","f2elint-fix":"f2elint fix","postversion":"git push","publish:pre":"sh scripts/publish.sh pre","f2elint-scan":"f2elint scan","publish:prod":"sh scripts/publish.sh prod"},"_npmUser":{"name":"homy","email":"HomyeeKing@gmail.com"},"repository":{"url":"git+ssh://git@github.com/HomyeeKing/whatif.git","type":"git"},"_npmVersion":"10.9.0","description":"Smarter If","directories":{},"_nodeVersion":"20.16.0","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"7.1.0","vitest":"0.33.0","prettier":"3.0.0","typescript":"5.1.6","@types/node":"^20.4.2","@egoist/prettier-config":"1.0.0"},"_npmOperationalInternal":{"tmp":"tmp/whatif_1.0.5_1742283205801_0.3040193083245646","host":"s3://npm-registry-packages-npm-production"}},"1.0.6":{"name":"whatif","version":"1.0.6","description":"Smarter If","publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+ssh://git@github.com/HomyeeKing/whatif.git"},"license":"MIT","author":{"name":"HomyeeKing"},"homepage":"http://git@github.com:HomyeeKing/whatif.git","main":"./dist/index.js","module":"./dist/index.mjs","exports":{"require":"./dist/index.js","import":"./dist/index.mjs"},"types":"./dist/index.d.ts","scripts":{"dev":"npm run build-fast --  --watch","build":"tsup","test":"vitest","cov":"vitest --coverage","f2elint-scan":"f2elint scan","f2elint-fix":"f2elint fix","preversion":"npm run test -- --run && npm run build","postversion":"git push","publish:pre":"sh scripts/publish.sh pre","publish:prod":"sh scripts/publish.sh prod"},"devDependencies":{"@egoist/prettier-config":"1.0.0","@types/node":"^20.4.2","prettier":"3.0.0","tsup":"7.1.0","typescript":"5.1.6","vitest":"0.33.0"},"_id":"whatif@1.0.6","gitHead":"ced956f580868200881d1793b459185ca3d74a61","_nodeVersion":"20.16.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-SF18aP4CxmFUa5l5GrkT75BVEdnqSXU1jryPc6Buk1iYlICYwHHw8U/MepzkVKZoK77ZSutHwCMskf3GafYdxw==","shasum":"9e6efaa69387c8c4dcf4edf710cd8aca1d639cd3","tarball":"https://registry.npmjs.org/whatif/-/whatif-1.0.6.tgz","fileCount":7,"unpackedSize":8114,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCICqMXvuTA+XxrRcYgNc+O9Uz0Luqg1OYglfdtl6yiNWEAiAfHQFgYS3h1DWy0qBnF8XJ4B0ucdZcuQyoGS1rdQFnnw=="}]},"_npmUser":{"name":"homy","email":"HomyeeKing@gmail.com"},"directories":{},"maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/whatif_1.0.6_1742286106299_0.5433497227969928"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-27T08:07:34.901Z","modified":"2025-03-18T08:21:46.661Z","0.1.0":"2016-07-11T09:32:34.779Z","0.1.1":"2016-11-15T08:26:39.267Z","0.1.2":"2016-11-15T08:45:17.690Z","0.1.3":"2017-02-20T10:08:01.885Z","1.0.0":"2025-02-27T07:10:08.930Z","1.0.2":"2025-02-27T08:07:35.173Z","1.0.3":"2025-02-27T08:26:00.841Z","1.0.4":"2025-02-27T08:31:42.832Z","1.0.5":"2025-03-18T07:33:25.976Z","1.0.6":"2025-03-18T08:21:46.513Z"},"author":{"name":"HomyeeKing"},"license":"MIT","homepage":"http://git@github.com:HomyeeKing/whatif.git","repository":{"type":"git","url":"git+ssh://git@github.com/HomyeeKing/whatif.git"},"description":"Smarter If","maintainers":[{"name":"homy","email":"HomyeeKing@gmail.com"}],"readme":"# whatif\n\n[![npm version](https://badgen.net/npm/v/whatif)](https://npm.im/whatif) [![npm downloads](https://badgen.net/npm/dm/whatif)](https://npm.im/whatif)\n\n## Install\n\n```bash\nnpm i whatif\n```\n\n## Usage\n\n### orSome\n\nImagine you have a list of conditions and you want to find the which condition cause the branch to be taken.\n\n```js\nif (a || b || c) {\n  // can't tell which condition is true\n  // especially when you want to give users more exact log\n  console.log(\"reason:\", \"?\")\n}\n```\n\nUsually we may try to use `if` statement again to check the exact condition, but it's too verbose and too ugly.\n\n```js\n// 😕\nif (a || b || c) {\n  if (a) {\n    console.log(\"Due to a ...\")\n  }\n  if (b) {\n    console.log(\"Due to b ...\")\n  }\n}\n```\n\nNow, you can use `orSome` to do this. i.e.\n\n```js\nimport { orSome } from \"whatif\"\n\n// simple boolean array\norSome([true, false], (p) => {\n  console.log(p.key) // 0.  when pass a boolean array, the key will be the index\n})\n\n// with object\norSome(\n  [\n    { condition: false, key: \"one\" },\n    { condition: true, key: \"two\", message: \"condition two pass\" },\n  ],\n  (p) => {\n    console.log(p.key) // two\n    console.log(p.message) // condition two pass\n  },\n)\n\n// mix boolean and object\norSome([false, { condition: true, key: \"two\" }], (p) => {\n  console.log(p.key) // two.\n  console.log(p.message) // condition two pass\n})\n\n// callback won't be called\norSome([false, false], () => {\n  // won't be called\n})\n```\n\nBut there's a difference you should notice, which is that _\\*\\*EVERY CONDITIONs_ will be evaluated right away. So if you want to make use of the short circuit, you may wrap a function to lazy the evaluation or just use `if` statement to make judgement in advance to avoid the Uncaught error:\n\n```ts\n// ❌ bad case\norSome([\n  { condition: !foo.bar, key: \"one\" },\n  { condition: foo.bar.baz, key: \"two\" }, // foo.bar.baz will be evaluated right now, so it will throw an error here\n])\n\n// ✅ good case with if statement in advance:\nif (!foo.bar) {\n  return\n}\n\norSome([{ condition: foo.bar.baz, key: \"one\" }], () => {})\n\n// ✅ good case with lazy evaluation:\norSome([{ condition: () => foo.bar.baz, key: \"one\" }], () => {})\n```\n","readmeFilename":"README.md"}