{"id":26670764,"url":"https://github.com/crbroughton/ts-utils","last_synced_at":"2026-02-11T17:32:32.244Z","repository":{"id":256269858,"uuid":"854781112","full_name":"CRBroughton/ts-utils","owner":"CRBroughton","description":"A collection of helper functions and types","archived":false,"fork":false,"pushed_at":"2025-10-12T21:01:34.000Z","size":476,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-20T00:57:29.206Z","etag":null,"topics":["mocks","testing","typescript","utility","zod"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@crbroughton/ts-utils","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/CRBroughton.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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":"2024-09-09T19:08:25.000Z","updated_at":"2025-10-12T21:01:37.000Z","dependencies_parsed_at":"2024-09-13T07:14:56.758Z","dependency_job_id":"540e8f20-079e-42a5-bb75-b22219fc9323","html_url":"https://github.com/CRBroughton/ts-utils","commit_stats":null,"previous_names":["crbroughton/ts-utils"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/CRBroughton/ts-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CRBroughton%2Fts-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CRBroughton%2Fts-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CRBroughton%2Fts-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CRBroughton%2Fts-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CRBroughton","download_url":"https://codeload.github.com/CRBroughton/ts-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CRBroughton%2Fts-utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29339609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T16:14:43.024Z","status":"ssl_error","status_checked_at":"2026-02-11T16:14:15.258Z","response_time":97,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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":["mocks","testing","typescript","utility","zod"],"created_at":"2025-03-25T22:46:20.048Z","updated_at":"2026-02-11T17:32:32.239Z","avatar_url":"https://github.com/CRBroughton.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-utils\n\nA collection of helper functions and types. To use any of the following,\nsimply import into your project like so:\n\n```typescript\nimport { safeAwait, Ok, Err, Result } from '@crbroughton/ts-utils'\n```\n\n## Installation\n\nTo install `ts-utils` with Bun, run the following command:\n\n```bash\nbun i -D @crbroughton/ts-utils\n```\n\n## result\n\nThe `result` directory contains a Rust-inspired Result type system for explicit error handling:\n\n- `Result\u003cT, E\u003e` - A type representing either success (`Ok\u003cT\u003e`) or failure (`Err\u003cE\u003e`)\n- `Ok(value)` - Creates a successful result containing a value\n- `Err(error)` - Creates a failed result containing an error\n- `safeAwait(promise)` - Safely executes a Promise and returns a Result instead of throwing\n- `isOk(result)` - Type guard to check if a Result is Ok\n- `isErr(result)` - Type guard to check if a Result is Err\n- `ok(result)` - Extracts the value from a Result if Ok, otherwise returns null\n- `err(result)` - Extracts the error from a Result if Err, otherwise returns null\n\nExample usage:\n\n```typescript\n// Basic usage with safeAwait\nconst result = await safeAwait(fetch('/api/data'))\nif (isErr(result)) {\n  console.error('Fetch failed:', result.error)\n  return\n}\nconst response = result.value\n\n// Creating Results manually\nconst success = Ok(42) // { ok: true, value: 42 }\nconst failure = Err('Something went wrong') // { ok: false, error: 'Something went wrong' }\n\n// Type guards for safe access\nif (isOk(result)) {\n  console.log(result.value) // TypeScript knows this is the success type\n}\n\n// Convenient extraction\nconst user = ok(result) // User | null\nconst error = err(result) // Error | null\n```\n\n## enum\n\nThe `enum` directory contains the following:\n\n- `EnumLike` - A helper type to define enum-like objects\n- `createEnum` - A function to create enum-like object\n\nIt is intended that you'll only need to interface with the `createEnum` function,\nhowever the `EnumLike` type has been exported if you find yourself requiring it.\n\n## utils\n\nThe `utils` directory for now only contains the `Prettify` type, which will\nimprove your experience when hovering over type to get their underlying type\ninformation.\n\n## Zod\n\nThe Zod directly currently contains only a single function, the `zodObjectBuilder` function, which is\na helper to generate objects from a Zod object schema.\nUse this function for whenever you need to generate\nobjects but don't want to see the entire object in\nyour code. This function support both full and partial\nschema support. Below is an example of `zodObjectBuilder` in action with it's various\nuse-cases.\n\n``` typescript\n// Define a schema\nconst UserSchema = z.object({\n  id: z.string().default('1'),\n  name: z.string().default(\"Craig R Broughton\"),\n  email: z.string().email().default(\"myemail@gmail.com\")\n  settings: z.object({\n    theme: z.enum(['light', 'dark']),\n    notifications: z.boolean()\n  }).default({\n    theme: 'dark',\n    notifications: true\n  })\n});\n\n// Create default object(nested in an array) with no overrides\nconst defaultUsers = zodObjectBuilder({\n  schema: UserSchema\n});\n\n\n// Create a single object with overrides\nconst user = zodObjectBuilder({\n  schema: UserSchema,\n  overrides: { name: 'John', settings: { theme: 'dark' } }\n});\n\n// Create multiple objects with overrides\nconst users = zodObjectBuilder({\n  schema: UserSchema,\n  overrides: [\n    { name: 'John' },\n    { name: 'Jane', settings: { theme: 'light' } }\n  ]\n});\n\n// Preserve nested defaults with overrides\nconst userWithDefaults = zodObjectBuilder({\n  schema: UserSchema,\n  overrides: { name: 'John', settings: { theme: 'dark' } },\n  config: { preserveNestedDefaults: true }\n});\n\n// Generate multiple objects with sequential values\nconst sequentialUsers = zodObjectBuilder({\n  schema: UserSchema,\n  options: {\n    count: 3,\n    transform: {\n      id: (i) =\u003e `USER-${i + 1}`,\n      name: (i) =\u003e `User ${i + 1}`\n    }\n  }\n});\n\n// Generate multiple batches with different transforms\nconst mixedUsers = zodObjectBuilder({\n  schema: UserSchema,\n  options: {\n    batchTransform: [\n      { \n        count: 2, \n        transform: {\n          id: ({ index }) =\u003e `ADMIN-${index + 1}`,\n          role: () =\u003e 'admin' as const\n        }\n      },\n      { \n        count: 3, \n        transform: {\n          id: ({ index }) =\u003e `USER-${index + 1}`,\n          role: () =\u003e 'user' as const\n        }\n      }\n    ]\n  }\n})\n\n// Combine global transformations with batch transformations\nconst result = zodObjectBuilder({\n  schema: UserSchema,\n  config: {\n    allowOverlappingTransforms: true\n  },\n  options: {\n    transform: {\n      email: ({ index }) =\u003e `global${index + 1}@example.com`\n    },\n    batchTransform: [\n      {\n        count: 2,\n        transform: {\n          id: ({ index }) =\u003e `FIRST-${index + 1}`\n        }\n      },\n      {\n        count: 1,\n        transform: {\n          id: ({ index }) =\u003e `SECOND-${index + 1}`\n        }\n      }\n    ]\n  }\n})\n\n\n```\n## Development Installation\n\n```bash\nbun install\n```\n\nTo run:\n\n```bash\nbun run index.ts\n```\n\nThis project was created using `bun init` in bun v1.1.27. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrbroughton%2Fts-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrbroughton%2Fts-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrbroughton%2Fts-utils/lists"}