{"id":15471957,"url":"https://github.com/thadeu/ts-utility","last_synced_at":"2026-02-25T01:32:03.337Z","repository":{"id":210107263,"uuid":"724876393","full_name":"thadeu/ts-utility","owner":"thadeu","description":"🖇️ Typescript Utility ","archived":false,"fork":false,"pushed_at":"2025-02-24T14:39:24.000Z","size":1057,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-08T23:16:15.637Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thadeu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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,"zenodo":null}},"created_at":"2023-11-29T01:06:25.000Z","updated_at":"2025-02-24T14:39:54.000Z","dependencies_parsed_at":"2023-12-12T17:57:20.154Z","dependency_job_id":"8299c063-dd69-44c3-a95c-38ee7cbdacd0","html_url":"https://github.com/thadeu/ts-utility","commit_stats":{"total_commits":49,"total_committers":2,"mean_commits":24.5,"dds":0.08163265306122447,"last_synced_commit":"9b254eed7e79dfa1df6a68c5851bf91351df2349"},"previous_names":["thadeu/ts-utility"],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/thadeu/ts-utility","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fts-utility","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fts-utility/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fts-utility/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fts-utility/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thadeu","download_url":"https://codeload.github.com/thadeu/ts-utility/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thadeu%2Fts-utility/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29807897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-24T22:43:48.403Z","status":"ssl_error","status_checked_at":"2026-02-24T22:43:18.536Z","response_time":75,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-10-02T02:22:40.595Z","updated_at":"2026-02-25T01:32:03.289Z","avatar_url":"https://github.com/thadeu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ch1 align=\"center\"\u003e🖇️ ts-utility\u003c/h1\u003e\n  \u003cp align=\"center\"\u003e\u003ci\u003eA simple and lightweight way to create a safe result in Typescript\u003c/i\u003e\u003c/p\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"Build Status\" src=\"https://github.com/thadeu/ts-utility/actions/workflows/ci.yml/badge.svg?cacheSeconds=0\"\u003e  \n  \u003cimg alt=\"GitHub package.json version (subfolder of monorepo)\" src=\"https://img.shields.io/github/package-json/v/thadeu/ts-utility?cacheSeconds=0\"\u003e\n  \u003cimg alt=\"GitHub repo size\" src=\"https://img.shields.io/bundlephobia/min/%40thadeu%2Fts-utility?cacheSeconds=0\"\u003e\n\u003c/p\u003e\n\n\n## Motivation\n\nBecause in sometimes, we need to use safe conditions with try/catch, but we not want use blocks\n\n## Documentation \u003c!-- omit in toc --\u003e\n\nVersion    | Documentation\n---------- | -------------\nunreleased | https://github.com/thadeu/ts-utility/blob/main/README.md\n\n## Table of Contents \u003c!-- omit in toc --\u003e\n  - [Installation](#installation)\n  - [Configuration](#configuration)\n  - [Availables Predicates](#availables-predicates)\n  - [Usage](#usage)\n  - [Utilities](#utilities)\n\n## Compatibility\n\n| kind           | branch  | javascript         |\n| -------------- | ------- | ------------------ |\n| unreleased     | main    | \u003e= 14.x, \u003c= 21.x |\n\n## Installation\n\nUse Yarn\n\n```bash\nyarn add @thadeu/ts-utility\n```\n\nor use NPM\n\n```bash\nnpm i @thadeu/ts-utility\n```\n\nor use PNPM\n\n```bash\npnpm add @thadeu/ts-utility\n```\n\nor use Github directly\n\n```bash\npnpm add github:thadeu/ts-utility\n```\n\nand then, enjoy!\n\nFor example:\n\n**Imagine that you have an class like this:**\n\n```ts\nclass User {\n  static async all() {\n    return [{ id: 1 }, { id: 2 }]\n  }\n\n  static async throwed() {\n    throw new Error(`User not found`)\n  }\n}\n```\n\nNow, look it.\n\n```ts\nimport { Try } from '@thadeu/ts-utility'\n```\n\nor \n\n```ts\nimport { safeTry } from '@thadeu/ts-utility'\n```\n\nThe difference between them, is that safeTry always returns a tupple [error, value]. Look at this.\n\n```ts\ntype Data = {\n  data: {\n    user: {\n      name: string\n    }\n  }\n}\n\nconst obj = { data: { user: { name: 'Thadeu' } } }\nconst fn = () =\u003e JSON.parse(JSON.stringify(obj))\nconst [error, value] = safeTry\u003cError, Data\u003e(fn)\n\nexpect(error).toBeNull()\nexpect(value).toEqual(obj)\nexpect(obj.data.user.name).toEqual('Thadeu')\n```\n\nAnd when we have an async function, like this:\n\n```ts\nconst obj = { data: { user: { name: 'Thadeu' } } }\n\nconst promise = obj =\u003e async () =\u003e JSON.parse(JSON.stringify(obj))\nconst [error, value] = await safeTry(promise(obj))\n\nexpect(error).toBeNull()\nexpect(value).toEqual(obj)\n```\n\n\u003e When success, you receive promise resolved.\n\n```ts\n let tryOptions = {\n  onError: error =\u003e {\n    // console.log(error)\n    return []\n  },\n}\n\nlet result = await Try(User.all, tryOptions)\n// or\nlet result = await Try(_ =\u003e User.all(), tryOptions)\n// result =\u003e [{ id: 1 }, { id: 2 }]\n```\n\n\u003e When fail you receive onError callback OR null\n\n```ts\nlet tryOptions = {\n  onError: async error =\u003e {\n    // console.log(error)\n    return []\n  },\n}\n\nlet result = await Try(User.throwed, tryOptions)\n// result =\u003e []\n```\n\n```ts\nlet tryOptions = {\n  onError: async error =\u003e {\n    // console.log(error)\n    return []\n  },\n}\n\nlet result = await Try(async () =\u003e User.throwed(params), tryOptions)\n// result =\u003e []\n```\n\n**Others examples.**\n\n```ts\nlet counter = 0\nlet tryOptions = { max: 3, onError: {}, onRetry: (count, isReached) =\u003e (counter = count) }\n\nawait Try(_ =\u003e JSON.parse('{'), tryOptions)\n\nexpect(counter).toEqual(3)\n```\n\n```ts\nlet counter = 0\n\nlet tryOptions = {\n  max: 3,\n  onError: {},\n  onRetry: count =\u003e (counter = count)\n}\n\nlet result = await Try(_ =\u003e JSON.parse('{ \"user\": \"1\" }'), tryOptions)\n\nexpect(counter).toEqual(0)\nexpect(result).toEqual({ user: '1' })\n```\n\n**Using exponential parameter**\n\n```ts\nlet result = await Try(_ =\u003e JSON.parse('{'), {\n  onError: {},\n  max: 3,\n  exponential: 2 \n})\n\nexpect(result).toEqual({})\n```\n\n## Configuration\n\nWithout configuration, because we use only JavaScript. ❤️\n\n## Usage Try\n\n[⬆️ \u0026nbsp;Back to Top](#table-of-contents-)\n\n## Development\n\nAfter checking out the repo, install dependencies. Then, run `test` to run the tests.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/thadeu/ts-utility. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thadeu/ts-utility/blob/master/CODE_OF_CONDUCT.md).\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthadeu%2Fts-utility","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthadeu%2Fts-utility","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthadeu%2Fts-utility/lists"}