{"_id":"hook-conditional","_rev":"2-9fa7288edc056476275f73559366a6d0","name":"hook-conditional","dist-tags":{"latest":"0.2.0"},"versions":{"0.1.0":{"name":"hook-conditional","version":"0.1.0","author":{"url":"https://jp.coffee","name":"JP.Coffee"},"license":"MIT","_id":"hook-conditional@0.1.0","maintainers":[{"name":"jp.coffee","email":"me@jp.coffee"}],"homepage":"https://github.com/jp-coffee/hook-conditional#readme","bugs":{"url":"https://github.com/jp-coffee/hook-conditional/issues"},"dist":{"shasum":"404bc8eb3cc4c8876cfa053fc9284feadca3433c","tarball":"https://registry.npmjs.org/hook-conditional/-/hook-conditional-0.1.0.tgz","fileCount":9,"integrity":"sha512-cchOoAGYJ5FflgwNiRkU4tGnuRDPSFqKjDA+r35AzhhnLpqCStQn1zArgD2ZaNJM0Cnn/+to7WrL3zikyv5eWw==","signatures":[{"sig":"MEUCIQC5gWYqBq4zTkgRaMmb6SdtBba/ARE7WTcInGMto31BiAIgNK7982fke0OywEVB7zqDqkUOetRz1p2zHR0ll3BNhaE=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":13809},"exports":{".":{"types":"./dist/index.d.ts","default":"./dist/index.js"}},"gitHead":"43fa30503dd011d081ed610e17f91a1907d0ef79","scripts":{"lint":"eslint .","test":"vitest run","build":"npm run prebuild && tsc","clean":"rimraf dist","lint:fix":"eslint . --ext .ts,.tsx --fix","prebuild":"npm run clean","typecheck":"tsc --noEmit","test:watch":"vitest --watch"},"_npmUser":{"name":"jp.coffee","email":"me@jp.coffee"},"repository":{"url":"git+https://github.com/jp-coffee/hook-conditional.git","type":"git"},"_npmVersion":"10.9.2","description":"Conditionally run different React hooks at runtime with a clean and safe API that follows the Rules of Hooks.","directories":{},"displayName":"React Hook Conditional","_nodeVersion":"22.14.0","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"devDependencies":{"vite":"^6.2.3","jsdom":"^26.0.0","eslint":"^9.23.0","rimraf":"^6.0.1","vitest":"^3.0.9","typescript":"^5.8.2","@types/node":"^24.0.0","@types/react":"^19.0.12","eslint-plugin-react":"^7.37.4","@vitejs/plugin-react":"^4.3.4","eslint-plugin-import":"^2.31.0","@testing-library/react":"^16.2.0","@testing-library/jest-dom":"^6.6.3","@typescript-eslint/parser":"^8.28.0","eslint-plugin-react-hooks":"^5.2.0","@typescript-eslint/eslint-plugin":"^8.28.0"},"peerDependencies":{"react":"^19.0.0"},"_npmOperationalInternal":{"tmp":"tmp/hook-conditional_0.1.0_1749672851664_0.5271011378017412","host":"s3://npm-registry-packages-npm-production"}},"0.2.0":{"name":"hook-conditional","displayName":"React Hook Conditional","version":"0.2.0","description":"Conditionally run different React hooks at runtime with a clean and safe API that follows the Rules of Hooks.","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"license":"MIT","author":{"name":"JP.Coffee","url":"https://jp.coffee"},"repository":{"type":"git","url":"git+https://github.com/jp-coffee/hook-conditional.git"},"bugs":{"url":"https://github.com/jp-coffee/hook-conditional/issues"},"homepage":"https://github.com/jp-coffee/hook-conditional#readme","keywords":["react","hooks","conditional","runtime","dynamic","state-management","typescript"],"exports":{".":{"types":"./dist/index.d.ts","default":"./dist/index.js"}},"scripts":{"build":"npm run prebuild && tsc","clean":"rimraf dist","lint":"eslint .","lint:fix":"eslint . --ext .ts,.tsx --fix","prebuild":"npm run clean","test":"vitest run","test:watch":"vitest --watch","typecheck":"tsc --noEmit","verify-provenance":"./scripts/verify-provenance.sh"},"devDependencies":{"@testing-library/jest-dom":"^6.6.3","@testing-library/react":"^16.2.0","@types/node":"^24.0.0","@types/react":"^19.0.12","@typescript-eslint/eslint-plugin":"^8.28.0","@typescript-eslint/parser":"^8.28.0","@vitejs/plugin-react":"^4.3.4","eslint":"^9.23.0","eslint-plugin-import":"^2.31.0","eslint-plugin-react":"^7.37.4","eslint-plugin-react-hooks":"^5.2.0","jsdom":"^26.0.0","rimraf":"^6.0.1","typescript":"^5.8.2","vite":"^6.2.3","vitest":"^3.0.9"},"peerDependencies":{"react":"^19.0.0"},"_id":"hook-conditional@0.2.0","gitHead":"185615a3ba1271bcfe6a43a278fc4bf032533026","_nodeVersion":"22.14.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-2mC+ORJFWgxTJEO0NRJgHcyYp4n5sjpLa+GNWhiTygbGogcuV1uAvyeppAVAUdxgJ8nP41vcMRECHphN4dmSEw==","shasum":"0eadfe4b7c4fdbbef8b006e7bc6d4ab09931ec98","tarball":"https://registry.npmjs.org/hook-conditional/-/hook-conditional-0.2.0.tgz","fileCount":9,"unpackedSize":15018,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/hook-conditional@0.2.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCZZJXeXEZJn99FwvjgsHeKkzjhEqJqi/2J6j9E5A4c/gIhAL6wyI9/OvWwL5piu/VNOYmDfqdlCgOsRqd0DfT0m8zt"}]},"_npmUser":{"name":"jp.coffee","email":"me@jp.coffee"},"directories":{},"maintainers":[{"name":"jp.coffee","email":"me@jp.coffee"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/hook-conditional_0.2.0_1754512543175_0.1420450119593013"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-11T20:14:11.546Z","modified":"2025-08-06T20:35:43.912Z","0.1.0":"2025-06-11T20:14:11.856Z","0.2.0":"2025-08-06T20:35:43.354Z"},"bugs":{"url":"https://github.com/jp-coffee/hook-conditional/issues"},"author":{"name":"JP.Coffee","url":"https://jp.coffee"},"license":"MIT","homepage":"https://github.com/jp-coffee/hook-conditional#readme","repository":{"type":"git","url":"git+https://github.com/jp-coffee/hook-conditional.git"},"description":"Conditionally run different React hooks at runtime with a clean and safe API that follows the Rules of Hooks.","maintainers":[{"name":"jp.coffee","email":"me@jp.coffee"}],"readme":"![hook-conditional Banner](https://raw.githubusercontent.com/jp-coffee/hook-conditional/main/media/hook-conditional-banner.png)\n\n**Dynamic Hook Logic in React, Based on Any Runtime Condition** Simplify conditional logic in your components by delegating it to purpose-built hooks mapped to values like roles, flags, environments, or status codes.\n\n[![npm version](https://img.shields.io/npm/v/hook-conditional.svg)](https://www.npmjs.com/package/hook-conditional)\n[![npm downloads](https://img.shields.io/npm/dt/hook-conditional.svg)](https://www.npmjs.com/package/hook-conditional)\n[![License](https://img.shields.io/github/license/jp-coffee/hook-conditional)](LICENSE)\n[![Tests Status](https://img.shields.io/github/actions/workflow/status/jp-coffee/hook-conditional/lint-and-test.yml?branch=main)](https://github.com/jp-coffee/hook-conditional/actions)\n\n## 🚀 Features\n\n- 🔀 **Switch on Any Condition** – Support for strings, numbers, booleans, or any discriminant value.\n- 🧠 **Declarative Hook Selection** – Map conditions to custom hooks and let React do the rest.\n- ⚡ **Fully Compliant** – All hooks are called unconditionally, following the Rules of Hooks.\n- ✨ **Tiny & Typed** – Minimal, type-safe API with full IntelliSense support.\n- 🛡 **Optional Fallback** – Gracefully handle unmatched conditions with a fallback hook.\n\n## 📦 Getting Started\n\n### Installation\n\nInstall via your preferred package manager:\n\n```sh\n# npm\nnpm install hook-conditional\n\n# yarn\nyarn add hook-conditional\n\n# pnpm\npnpm add hook-conditional\n\n# bun\nbun add hook-conditional\n```\n\n### Basic Usage\n\n```tsx\n\"use client\";\n\nimport { useConditionalHook } from \"hook-conditional\";\n\nconst Page = () => {\n  const env = process.env.NODE_ENV;\n\n  const value = useConditionalHook(env, {\n    development: () => useDevHook(),\n    production: () => useProdHook(),\n    test: () => useTestHook(),\n  });\n\n  return <p>{value}</p>;\n};\n```\n\n## 🔍 API Reference\n\n### `useConditionalHook(condition, hookMap, fallbackHook?)` _(TypeScript)_\n\nSwitches between custom hooks based on a condition. All mapped hooks are **always executed** to comply with React’s Rules of Hooks, but **only the result of the matching hook is returned**.\n\n#### Parameters\n\n- `condition` (`string | number | boolean`)  \n  The runtime value that determines which hook result to return.\n\n- `hookMap` (`Record<string | number, () => TResult>`)  \n  An object where each key corresponds to a possible value of `condition`, and the value is a hook function to execute.\n\n- `fallbackHook` (`() => TResult`, optional)  \n  A fallback hook to use when no match is found in the map.\n\n#### Returns\n\n- `TResult` — The result of the matched hook (or fallback hook if no match is found).\n\n## 🔒 Security & Provenance\n\nThis package is published with **NPM Package Provenance**, which provides cryptographic proof that the package was built from the source code in this repository using GitHub Actions.\n\n### Verifying Package Authenticity\n\nYou can verify the package's provenance using:\n\n```bash\n# Install the package\nnpm install hook-conditional\n\n# Verify the provenance\nnpm audit signatures\n\n# Or use the provided verification script\nnpm run verify-provenance\n```\n\n### What This Means\n\n- ✅ **Authentic Source**: The package was built from this exact repository\n- ✅ **Secure Build**: Built in a trusted GitHub Actions environment\n- ✅ **Tamper-Proof**: Cryptographically signed to prevent supply chain attacks\n- ✅ **Transparent**: You can verify the build process and source code\n\nFor more information about NPM Package Provenance, see the [official documentation](https://docs.npmjs.com/generating-provenance-statements).\n\n## 💡 Examples\n\n### Role-based hook switching\n\n```tsx\n\"use client\";\n\nimport useConditionalHook from \"hook-conditional\";\n\nconst Page = () => {\n  const role = useUserRole();\n\n  const permissions: string[] = useConditionalHook(role, {\n    guest: () => useGuestPermissions(),\n    user: () => useUserPermissions(),\n    admin: () => useAdminPermissions(),\n  });\n\n  return <div>Permissions: {permissions.join(\", \")}</div>;\n};\n```\n\n### Boolean feature flag\n\n```tsx\n\"use client\";\n\nimport useConditionalHook from \"hook-conditional\";\n\nconst Page = () => {\n  const isEnabled = useFeatureFlag(\"new-ui\");\n\n  const ui = useConditionalHook(isEnabled, {\n    true: () => useNewUI(),\n    false: () => useOldUI(),\n  });\n\n  return <>{ui}</>;\n};\n```\n\n### Numeric condition\n\n```tsx\n\"use client\";\n\nimport useConditionalHook from \"hook-conditional\";\n\nconst Page = () => {\n  const status = useStatus();\n\n  const content = useConditionalHook(\n    status,\n    {\n      200: () => useSuccessContent(),\n      404: () => useNotFoundContent(),\n      500: () => useErrorContent(),\n    },\n    () => useDefaultContent()\n  );\n\n  return <>{content}</>;\n};\n```\n\n## 🛠 Contributing\n\nContributions are welcome! To contribute:\n\n1. Fork the repository.\n2. Create a feature branch.\n3. Commit your changes.\n4. Open a Pull Request.\n\nPlease ensure your code matches the project style and includes relevant tests if applicable.\n\n## 📜 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n\n## 👨‍💻 Created By\n\nThis package is developed and maintained by [JP.Coffee](https://github.com/jp-coffee). Feel free to reach out or open an issue for any questions or suggestions!\n","readmeFilename":"README.md","keywords":["react","hooks","conditional","runtime","dynamic","state-management","typescript"]}