{"_id":"@burakbey/result","_rev":"1-933c7639b4dc6dc34fc354feec4014d1","name":"@burakbey/result","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@burakbey/result","version":"1.0.0","keywords":["result","rust"],"author":{"name":"BurakBey"},"license":"MIT","_id":"@burakbey/result@1.0.0","maintainers":[{"name":"burakbey","email":"rectfc@gmail.com"}],"homepage":"https://github.com/BUR4KBEY/result#readme","bugs":{"url":"https://github.com/BUR4KBEY/result/issues"},"dist":{"shasum":"b19c7f373cd3f1975ccc1041f28c7ddc99ca719f","tarball":"https://registry.npmjs.org/@burakbey/result/-/result-1.0.0.tgz","fileCount":5,"integrity":"sha512-zRTiREWdj4XCxt9wXldlA7mNitcx8B2pTaBLiUOUgdH/6xUAq3oEeCRJBoXDU+rXUSOKa0B8L3+sCCqCTXcd6Q==","signatures":[{"sig":"MEYCIQCSBfCbGVQ6ol8nc69xItmTR20qvv3o1KUBM2sx8K24agIhAJkWcHe3Z8aXmJQr8mNqj+/3jfnUlFpJufm3ShCsliJJ","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@burakbey%2fresult@1.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":16381},"main":"./dist/index.js","types":"./dist/index.d.ts","funding":"https://burakbey.dev","gitHead":"b8bd5d303b108ddb95b141381e1f4bb65e00c4f4","scripts":{"lint":"eslint","test":"jest --coverage","build":"tsup","prepare":"husky","lint:fix":"eslint --fix","build:watch":"tsup --watch"},"_npmUser":{"name":"burakbey","email":"rectfc@gmail.com"},"repository":{"url":"git+https://github.com/BUR4KBEY/result.git","type":"git"},"_npmVersion":"10.8.2","description":"Rust-inspired Result type for TypeScript, ensuring type-safe success and error states.","directories":{},"lint-staged":{"*.{js,jsx,ts,tsx}":"npm run lint:fix"},"_nodeVersion":"20.18.3","_hasShrinkwrap":false,"devDependencies":{"jest":"^29.7.0","tsup":"^8.4.0","husky":"^9.1.7","eslint":"^9.21.0","rimraf":"^6.0.1","ts-jest":"^29.2.6","prettier":"^3.5.2","@eslint/js":"^9.21.0","typescript":"^5.7.3","@types/jest":"^29.5.14","lint-staged":"^15.4.3","pretty-quick":"^4.0.0","jest-extended":"^4.0.2","@commitlint/cli":"^19.7.1","typescript-eslint":"^8.25.0","eslint-plugin-unicorn":"^57.0.0","eslint-config-prettier":"^10.0.2","eslint-plugin-prettier":"^5.2.3","eslint-plugin-perfectionist":"^4.9.0","@commitlint/config-conventional":"^19.7.1"},"_npmOperationalInternal":{"tmp":"tmp/result_1.0.0_1740769856967_0.8660411093687956","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"@burakbey/result","version":"1.0.1","description":"Rust-inspired Result type for TypeScript, ensuring type-safe success and error states.","author":{"name":"BurakBey"},"license":"MIT","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"build":"tsup","build:watch":"tsup --watch","test":"jest --coverage","lint":"eslint","lint:fix":"eslint --fix","prepare":"husky"},"devDependencies":{"@commitlint/cli":"^19.7.1","@commitlint/config-conventional":"^19.7.1","@eslint/js":"^9.21.0","@types/jest":"^29.5.14","eslint":"^9.21.0","eslint-config-prettier":"^10.0.2","eslint-plugin-perfectionist":"^4.9.0","eslint-plugin-prettier":"^5.2.3","eslint-plugin-unicorn":"^57.0.0","husky":"^9.1.7","jest":"^29.7.0","jest-extended":"^4.0.2","lint-staged":"^15.4.3","prettier":"^3.5.2","pretty-quick":"^4.0.0","rimraf":"^6.0.1","ts-jest":"^29.2.6","tsup":"^8.4.0","typescript":"^5.7.3","typescript-eslint":"^8.25.0"},"lint-staged":{"*.{js,jsx,ts,tsx}":"npm run lint:fix"},"keywords":["result","rust"],"repository":{"type":"git","url":"git+https://github.com/BUR4KBEY/result.git"},"bugs":{"url":"https://github.com/BUR4KBEY/result/issues"},"homepage":"https://github.com/BUR4KBEY/result#readme","funding":"https://burakbey.dev","_id":"@burakbey/result@1.0.1","gitHead":"02ec9dcfc123be284a64f26340d08732aad03b19","_nodeVersion":"20.18.3","_npmVersion":"10.8.2","dist":{"integrity":"sha512-2C/6GIryoCY0C3MeM8WILKBcVA4XI6j4g1+lMPmQezkr0n0Vydxmg83mONWSEKoqH4YICopwK8ObnzitAtDO4Q==","shasum":"d97122e492d1bc68f652c22a56519cab62eeb7e9","tarball":"https://registry.npmjs.org/@burakbey/result/-/result-1.0.1.tgz","fileCount":5,"unpackedSize":16851,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@burakbey%2fresult@1.0.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIHOzxmUtrAAiuHFqAvXpJ3RIk4sHXIZ2IAXKcZ/3rO4IAiAVZJNvFO6b+Yg4hFkuxzyOSapM6nLkcyRKld6WxAgdAg=="}]},"_npmUser":{"name":"burakbey","email":"rectfc@gmail.com"},"directories":{},"maintainers":[{"name":"burakbey","email":"rectfc@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/result_1.0.1_1740778560247_0.8351114675545885"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-28T19:10:56.846Z","modified":"2025-02-28T21:36:00.818Z","1.0.0":"2025-02-28T19:10:57.168Z","1.0.1":"2025-02-28T21:36:00.461Z"},"bugs":{"url":"https://github.com/BUR4KBEY/result/issues"},"author":{"name":"BurakBey"},"license":"MIT","homepage":"https://github.com/BUR4KBEY/result#readme","keywords":["result","rust"],"repository":{"type":"git","url":"git+https://github.com/BUR4KBEY/result.git"},"description":"Rust-inspired Result type for TypeScript, ensuring type-safe success and error states.","maintainers":[{"name":"burakbey","email":"rectfc@gmail.com"}],"readme":"[![NPM Version](https://img.shields.io/npm/v/%40burakbey%2Fresult?style=for-the-badge&logo=npm&color=blue&cacheSeconds=3600)](https://npmjs.com/package/@burakbey/result)\n[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/bur4kbey/result/test.yml?style=for-the-badge&label=tests&cacheSeconds=3600)](https://github.com/BUR4KBEY/result/actions/workflows/test.yml)\n[![Codecov](https://img.shields.io/codecov/c/github/bur4kbey/result?style=for-the-badge&cacheSeconds=3600)](https://app.codecov.io/gh/BUR4KBEY/result)\n[![GitHub License](https://img.shields.io/github/license/bur4kbey/result?style=for-the-badge)](https://github.com/BUR4KBEY/result/blob/main/LICENSE)\n[![GitHub Repo stars](https://img.shields.io/github/stars/bur4kbey/result?style=for-the-badge&label=%E2%AD%90%20STARS&color=yellow&cacheSeconds=3600)](https://github.com/BUR4KBEY/result)\n\n# 🎯 [@burakbey/result](https://npmjs.com/package/@burakbey/result)\n\nThis library provides a **Rust-inspired** `Result` type for **TypeScript**, ensuring type-safe success and error states while promoting explicit and predictable error handling.\n\n## 🧠 Why Use This?\n\nIn TypeScript, error handling often relies on throwing exceptions, which can lead to unpredictable control flow and runtime crashes. This library introduces a **Rust-inspired** `Result` type, enabling you to handle success and error cases explicitly and type-safely, without the pitfalls of thrown exceptions.\n\nWhile there are existing libraries like [neverthrow](https://www.npmjs.com/package/neverthrow) that implement the `Result` pattern, they can be complex or overwhelming to use. This package offers a **minimalist and easy-to-use** alternative.\n\n## 🚀 Installation\n\nInstall the package using your preferred package manager. Here's an example using `pnpm`:\n\n```bash\npnpm add @burakbey/result\n```\n\n## 📝 Examples\n\n### Basic Usage\n\n```ts\nimport { type Result, ok, err } from '@burakbey/result';\n\n// A function that returns a `Result`\nfunction divide(a: number, b: number): Result<number, string> {\n  if (b === 0) {\n    return err('Division by zero is not allowed');\n  }\n  return ok(a / b);\n}\n\n// Handle the `Result`\nconst result = divide(10, 2);\n\nif (result.isOk()) {\n  console.log('Result:', result.value); // Result: 5\n} else {\n  console.error('Error:', result.error); // This won't run in this case\n}\n```\n\n### Extract Both Success and Error as a Tuple\n\n```ts\n// Unwrap as a tuple: [successValue, errorValue]\nconst [value, error] = divide(10, 0).unwrapTuple();\n\nif (error) {\n  console.error('Error:', error); // Error: Division by zero is not allowed\n} else {\n  console.log('Value:', value); // This won't run in this case\n}\n```\n\n### Unwrapping the Success Value\n\n```ts\nconst result = divide(10, 2);\n\n// Unwrap the success value (throws if it's `Err`)\nconst value = result.unwrap();\nconsole.log('Unwrapped Value:', value); // Unwrapped Value: 5\n\n// This will throw if the result is `Err`\nconst riskyValue = divide(10, 0).unwrap(); // Throws `ResultError`\n```\n\n### Unwrapping with Fallback\n\n```ts\nconst result = divide(10, 0);\n\n// Unwrap with a fallback value\nconst value = result.unwrapOr(0);\nconsole.log('Value:', value); // Value: 0\n```\n\n### Unwrapping the Error\n\n```ts\nconst result = divide(10, 0);\n\n// Unwrap the error value (throws if it's `Ok`)\nconst error = result.unwrapErr();\nconsole.log('Unwrapped Error:', error); // Unwrapped Error: Division by zero is not allowed\n\n// This will throw if the result is `Ok`\nconst riskyError = divide(10, 2).unwrapErr(); // Throws `ResultError`\n```\n\n### Transform the Success Value\n\n```ts\nconst result = divide(10, 2);\n\n// Map the success value to a new one\nconst mappedResult = result.map(value => value * 2);\n\nif (mappedResult.isOk()) {\n  console.log('Mapped Value:', mappedResult.value); // Mapped Value: 10\n}\n```\n\n### Pattern Matching for Success and Error\n\n```ts\nconst result = divide(10, 0);\n\n// Match on the result to handle both cases\nconst output = result.match({\n  ok: value => `Success: ${value}`, // Called if the result is `Ok`\n  err: error => `Error: ${error}` // Called if the result is `Err`\n});\n\nconsole.log(output); // Error: Division by zero is not allowed\n```\n\n### Working with Asynchronous Operations\n\n```ts\nimport { type AsyncResult, err, ok } from '@burakbey/result';\n\n// Mocking a database function\n// Think of this function as coming from a database library\n// such as Prisma, TypeORM, or Mongoose.\nfunction lookDb(): Promise<string> {\n  return new Promise<string>((resolve, reject) => {\n    setTimeout(() => {\n      if (Math.random() > 0.5) {\n        resolve('data'); // Simulate a successful database query\n      } else {\n        reject(new Error('Database error')); // Simulate a database error\n      }\n    }, 2000); // Simulate a 2-second delay\n  });\n}\n\n// This function represents an implementation inside your codebase.\n// Note that the `AsyncResult<T, E>` is just `Promise<Result<T, E>>`.\nasync function getUser(): AsyncResult<string, Error> {\n  try {\n    const data = await lookDb(); // Await the database query\n    return ok(data); // Wrap the success value in an `Ok`\n  } catch (error) {\n    return err(error as Error); // Wrap the error in an `Err`\n  }\n}\n\n// Example usage\n(async () => {\n  const result = await getUser(); // Await the async result (promise)\n  const [value, error] = result.unwrapTuple(); // Unwrap the result into a tuple\n\n  if (value) {\n    console.log('Success:', value); // Log the success value\n  } else {\n    console.error('Error:', error); // Log the error\n  }\n})();\n```\n\n## 🧪 Code Coverage and Tests\n\nTests are crucial for ensuring that the library functions as expected. You can review the code coverage reports by visiting [**Codecov**](https://app.codecov.io/gh/BUR4KBEY/result). The primary objective is to achieve complete coverage of the entire codebase through rigorous testing.\n\n## ☕ Support\n\nIf you find this project useful and would like to support [me](https://github.com/BUR4KBEY), you can do so by visiting [my website](https://burakbey.dev).\n\n<a href=\"https://burakbey.dev\" target=\"_blank\"><img src=\"https://burakbey.dev/github_support_snippet.png\" style=\"height: 56px !important;width: 200px !important;\" alt=\"Buy me a coffee\"></img></a>\n","readmeFilename":"README.md"}