{"_id":"@axebear/when-otherwise","_rev":"2-5eed84cb7820480af3e493642ff10e0f","name":"@axebear/when-otherwise","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@axebear/when-otherwise","version":"1.0.0","keywords":[],"author":{"name":"Alex Barrett"},"license":"ISC","_id":"@axebear/when-otherwise@1.0.0","maintainers":[{"name":"spleenboy","email":"spleendini@yahoo.com"}],"dist":{"shasum":"10c4b4bba357b296dfca315eb1e009cfe97544dd","tarball":"https://registry.npmjs.org/@axebear/when-otherwise/-/when-otherwise-1.0.0.tgz","fileCount":17,"integrity":"sha512-h6TSKh0/l99Jq9guxtzfHxLIa37YIGsNIEgs9eXwNs5b4pR5Yv6KqwaaKXmrPCapopJu/CcxpjC4ZbYD8l5Ysg==","signatures":[{"sig":"MEQCIDUM/ZvZRcVnm/47S7/ljEr/6sUoqyBeD06Wq5C5TQ1fAiA9AOLphvETiiX9JGfZBL36quPIUMou8taHzW+t2kf7og==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":37416},"main":"dist/index.js","type":"module","types":"dist/index.d.ts","gitHead":"f116889d75c8363ff993b8425a8f9f1c8822a23c","scripts":{"test":"jest","build":"tsc --build","clean":"rm -rf dist","prebuild":"npm run clean","prettier":"prettier 'src/**/*.{ts,js,json,md}' --write"},"_npmUser":{"name":"spleenboy","email":"spleendini@yahoo.com"},"_npmVersion":"9.8.1","description":"Fluently compare values in TypeScript and replace complex switch and if/else statements.","directories":{},"_nodeVersion":"18.18.2","_hasShrinkwrap":false,"devDependencies":{"jest":"^30.2.0","ts-jest":"^29.4.5","ts-node":"^10.9.2","prettier":"3.6.2","typescript":"^5.9.3","@types/jest":"^30.0.0","@types/node":"^24.7.2","@jest/globals":"^30.2.0","@babel/preset-typescript":"^7.27.1"},"_npmOperationalInternal":{"tmp":"tmp/when-otherwise_1.0.0_1760215189185_0.5836710424472162","host":"s3://npm-registry-packages-npm-production"}},"1.1.0":{"name":"@axebear/when-otherwise","version":"1.1.0","description":"Fluently compare values in TypeScript and replace complex switch and if/else statements.","type":"module","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"test":"jest","build":"tsc --build","clean":"rm -rf dist","prettier":"prettier 'src/**/*.{ts,js,json,md}' --write","prebuild":"npm run clean"},"keywords":[],"author":{"name":"Alex Barrett"},"license":"ISC","devDependencies":{"@babel/preset-typescript":"^7.27.1","@jest/globals":"^30.2.0","@types/jest":"^30.0.0","@types/node":"^24.7.2","jest":"^30.2.0","prettier":"3.6.2","ts-jest":"^29.4.5","ts-node":"^10.9.2","typescript":"^5.9.3"},"_id":"@axebear/when-otherwise@1.1.0","gitHead":"bbb61a4bc87ab7e5b0b08bcfe277bcb382d333a4","_nodeVersion":"18.18.2","_npmVersion":"9.8.1","dist":{"integrity":"sha512-D6xJ9Bd7QJRK/ze5adqAT+tgHAc3GuAXhTBEqqKz7dDXFcfnY4FefErc9Z3cPnNAuKdbbMuKuykc38q/cRWTig==","shasum":"f39cc26e62f577c815c5919e700dfff1c2371b48","tarball":"https://registry.npmjs.org/@axebear/when-otherwise/-/when-otherwise-1.1.0.tgz","fileCount":18,"unpackedSize":49314,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIFA8Y+CL9nNnFcNgHhG8xLRxER7+xHXtCTGKucWG5rnkAiEA95w8wjj/i4sfxVG08r/jjdFFasgYUppkJp+nIo/uQo4="}]},"_npmUser":{"name":"spleenboy","email":"spleendini@yahoo.com"},"directories":{},"maintainers":[{"name":"spleenboy","email":"spleendini@yahoo.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/when-otherwise_1.1.0_1760675021085_0.6440441823130338"},"_hasShrinkwrap":false}},"time":{"created":"2025-10-11T20:39:49.111Z","modified":"2025-10-17T04:23:41.513Z","1.0.0":"2025-10-11T20:39:49.366Z","1.1.0":"2025-10-17T04:23:41.288Z"},"author":{"name":"Alex Barrett"},"license":"ISC","keywords":[],"description":"Fluently compare values in TypeScript and replace complex switch and if/else statements.","maintainers":[{"name":"spleenboy","email":"spleendini@yahoo.com"}],"readme":"# when-otherwise\n\nwhen-otherwise is a small TypeScript library that provides a fluent API for comparison logic. It is designed to simplify complex conditional statements by allowing you to chain multiple comparisons together in a readable manner.\n\n## Installation\n\nYou can install the library via npm:\n\n```\nnpm install @axebear/when-otherwise\n```\n\nTest the installation with `npm test`.\n\n## Simple Usage\n\n```typescript\nimport { when } from \"@axebear/when-otherwise\";\n\nconst result = when(type)\n  .is(\"small\", () => this.handleSmall())\n  .is(\"medium\", () => this.handleMedium())\n  .is(\"large\", () => this.handleLarge())\n  .elseWhen(\n    (value) => value.startsWith(\"extra\"),\n    () => this.handleExtra(),\n  )\n  .otherwise(() => this.handleDefault());\n```\n\n## Methods\n\nStart a comparison with either `when` or `whenSomething`. Use `when` for immediate calculation of the result. `whenSomething` defers calculation so that you may re-use a comparison test across multiple values. Both methods create and return a `Comparison` object. Once you have this object, you can start to chain additional tests.\n\nNote: for all of these comparison methods:\n\n- The `comparison` parameter can be a value to compare against or a function that returns a value to compare against. The function will be called with the value being tested when the comparison is executed.\n- The `result` parameter can be a value or a function that returns a value. If it is a function, it will be called with the value being tested when the comparison matches.\n\n### Comparison Methods\n\n- `is(comparison, result)`: Checks for strict equality (`===`) between the compared value and the provided value. If they are equal, it returns the associated result.\n- `isLike(comparison, result)`: Checks for loose equality (`==`) between the compared value and the provided value. If they are loosely equal, it returns the associated result.\n- `isNot(comparison, result)`: Checks for strict inequality (`!==`) between the compared value and the provided value. If they are not equal, it returns the associated result.\n- `isNotLike(comparison, result)`: Checks for loose inequality (`!=`) between the compared value and the provided value. If they are not loosely equal, it returns the associated result.\n- `elseWhen(condition, result)`: Allows you to have more complex comparisons. The `condition` parameter can be either a boolean or a function that accepts the value to test and returns a boolean. If the condition is true, it returns the associated result.\n\n### Handling Default Results\n\nThere are two ways to provide a default result if none of the comparisons match, depending on whether you are deferring the comparison or providing the value upfront:\n\n- `defaultTo(result)`: Used when deferring the comparison. It sets a default result to return if none of the comparisons match when the comparison is later executed.\n- `otherwise(result)`: Used when providing the value upfront. It sets a default result to return if none of the comparisons match immediately.\n\n## Examples\n\n### Immediate Comparison\n\n```typescript\nimport { when } from \"@axebear/when-otherwise\";\n\nconst input = \"a\";\nconst result = when(input)\n  .is(\"a\", () => \"Value is a\")\n  .is(\"b\", \"Value is b\")\n  .is(1, \"Something is 1\")\n  .otherwise(\"Value is something else\");\n\nconsole.log(result); // Output: \"Value is a\"\n```\n\nCalling `when()` without a parameter defaults to `when(true)`. This allows you to test boolean conditions.\n\n```typescript\nimport { when } from \"@axebear/when-otherwise\";\n\nconst firstName = get(\"firstName\");\nconst lastName = get(\"lastName\");\n\nconst result = when()\n  .is(\n    () => firstName.startsWith(\"A\"),\n    () => \"First name starts with A\",\n  )\n  .is(\n    () => lastName.startsWith(\"A\"),\n    () => \"Last name starts with A\",\n  )\n  .otherwise(\"Your name doesn't start with As\");\n\nconsole.log(result); // Output: \"Value is A\"\n```\n\n### Deferred Comparison\n\n```typescript\nimport { whenSomething } from \"@axebear/when-otherwise\";\n\nconst test = whenSomething<string>()\n  .is(\"a\", () => \"Value is A\")\n  .is(\"b\", \"Value is B\")\n  .is(1, \"Something is 1\")\n  .defaultTo((value) => \"Value is \" + value);\n\nconsole.log(test.against(\"a\")); // Output: \"Value is A\"\nconsole.log(test.against(\"b\")); // Output: \"Value is B\"\nconsole.log(test.against(1)); // Output: \"Something is 1\"\nconsole.log(test.against(123)); // Output: \"Value is 123\"\n```\n\n### Async Comparisons\n\nIf you only need the result type to be asynchronous, you can just specify the return type as a Promise:\n\n```typescript\nimport { when } from \"@axebear/when-otherwise\";\n\nconst fetchData = async (id: string) => {\n  return `data-${id}`;\n};\nconst result = await when<string, Promise<string>>(\"1\")\n  .is(\"1\", fetchData)\n  .is(\"2\", fetchData)\n  .otherwise(\"Fetched data is something else\");\nconsole.log(result); // Output: \"Fetched data is data-1\"\n```\n\nIf you need the comparisons themselves to be asynchronous, you need to use the `withPromises()` method. This will ensure that the `otherwise` or `against` methods return a Promise that resolves to the result type.\n\n```typescript\nimport { whenSomething } from \"@axebear/when-otherwise\";\n\nconst fetchData = async (id: string) => {\n  return `data-${id}`;\n};\nconst test = whenSomething<string, string>()\n  .withPromises()\n  .is(async () => \"data-1\", \"Fetched data is data-1\")\n  .is(async () => \"data-2\", \"Fetched data is data-2\")\n  .defaultTo(\"Fetched data is something else\");\n\nconst result1 = await test.against(await fetchData(\"1\"));\nconsole.log(result1); // Output: \"Fetched data is data-1\"\n\nconst result2 = await test.against(await fetchData(\"2\"));\nconsole.log(result2); // Output: \"Fetched data is data-2\"\n\nconst result3 = await test.against(await fetchData(\"3\"));\nconsole.log(result3); // Output: \"Fetched data is something else\"\n```\n","readmeFilename":"readme.md"}