{"id":15404340,"url":"https://github.com/nertzy/ts-factory","last_synced_at":"2025-04-16T20:53:10.408Z","repository":{"id":29028635,"uuid":"120045404","full_name":"nertzy/ts-factory","owner":"nertzy","description":"A way to build factories for TypeScript objects","archived":false,"fork":false,"pushed_at":"2025-04-07T12:12:59.000Z","size":496,"stargazers_count":8,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T07:18:21.012Z","etag":null,"topics":["factory","typescript"],"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/nertzy.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2018-02-03T00:20:22.000Z","updated_at":"2025-01-07T15:53:10.000Z","dependencies_parsed_at":"2024-04-05T20:29:52.278Z","dependency_job_id":"6d2db03b-b53b-478a-b281-e2126355a6b6","html_url":"https://github.com/nertzy/ts-factory","commit_stats":{"total_commits":50,"total_committers":7,"mean_commits":7.142857142857143,"dds":0.7,"last_synced_commit":"7683b6ffc131dc4c7bd7c4c2698cd486b68b1a88"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nertzy%2Fts-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nertzy%2Fts-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nertzy%2Fts-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nertzy%2Fts-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nertzy","download_url":"https://codeload.github.com/nertzy/ts-factory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249275774,"owners_count":21242284,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["factory","typescript"],"created_at":"2024-10-01T16:12:23.141Z","updated_at":"2025-04-16T20:53:10.386Z","avatar_url":"https://github.com/nertzy.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-factory\nA way to build factories for TypeScript objects\n\n[![Node.js CI](https://github.com/nertzy/ts-factory/actions/workflows/node.js.yml/badge.svg)](https://github.com/nertzy/ts-factory/actions/workflows/node.js.yml)\n[![npm](https://img.shields.io/npm/v/ts-factory.svg)](https://www.npmjs.com/package/ts-factory)\n[![GitHub license](https://img.shields.io/github/license/nertzy/ts-factory.svg)](https://github.com/nertzy/ts-factory/blob/master/LICENSE)\n\n## Installation\n```\n$ npm install ts-factory\n```\n\n```\n$ yarn add ts-factory\n```\n\n## buildFactory\n```typescript\nbuildFactory\u003cT extends object\u003e(defaultObject: T): (overrides?: Partial\u003cT\u003e) =\u003e T\n```\n`buildFactory` takes a type parameter and a default object of that type. It returns a function that takes an optional partial object of field overrides.\n\n### Example\nConsider the following types:\n\n```typescript\ninterface Author {\n  id?: number;\n  name: string;\n  email: string;\n}\n\ninterface Comment {\n  id?: number;\n  author: Author;\n  text: string;\n}\n\ninterface BlogPost {\n  id?: number;\n  author: Author;\n  title: string;\n  body: string;\n  comments: Comment[];\n}\n```\n\nWe can make a factory for Author by calling `buildFactory` with a default object. Note that optional fields may be omitted.\n\n```typescript\nimport { buildFactory } from \"ts-factory\";\n\nexport const buildAuthor = buildFactory\u003cAuthor\u003e({\n  name: \"Grant Hutchins\",\n  email: \"granthutchins@example.com\"\n});\n```\n\nWe can now use the `buildAuthor()` function to construct Author instances.\n\n```typescript\nbuildAuthor();\n// returns {\n//   name: \"Grant Hutchins\",\n//   email: \"granthutchins@example.com\"\n// }\n\nbuildAuthor({});\n// returns {\n//   name: \"Grant Hutchins\",\n//   email: \"granthutchins@example.com\"\n// }\n\nbuildAuthor({name: \"Mr. Hutchins\"});\n// returns {\n//   name: \"Mr. Hutchins\",\n//   email: \"granthutchins@example.com\"\n// }\n\nbuildAuthor({id: 1});\n// returns {\n//   id: 1,\n//   name: \"Grant Hutchins\",\n//   email: \"granthutchins@example.com\"\n// }\n```\n\nWe can now use `buildAuthor` to make it easier to build a factory for Comment.\n\n```typescript\nimport { buildFactory } from \"ts-factory\";\n\nexport const buildComment = buildFactory\u003cComment\u003e({\n  author: buildAuthor(),\n  text: \"myText\"\n});\n```\n\nNow we can easily build a Comment, knowing that its Author will also be valid.\n\n```typescript\nbuildComment();\n// returns {\n//   author: {\n//     name: \"Grant Hutchins\",\n//     email: \"granthutchins@example.com\"\n//   },\n//   text: \"myText\"\n// }\n\nbuildComment({});\n// returns {\n//   author: {\n//     name: \"Grant Hutchins\",\n//     email: \"granthutchins@example.com\"\n//   },\n//   text: \"myText\"\n// }\n\nbuildComment({id: 1});\n// returns {\n//   id: 1,\n//   author: {\n//     name: \"Grant Hutchins\",\n//     email: \"granthutchins@example.com\"\n//   },\n//   text: \"myText\"\n// }\n\nconst anotherAuthor = buildAuthor({\n  id: 2,\n  name: \"Another Author\",\n  email: \"another_author@example.com\",\n});\n\nbuildComment({author: anotherAuthor});\n// returns {\n//   author: {\n//     id: 2,\n//     name: \"Another Author\",\n//     email: \"another_author@example.com\"\n//   },\n//   text: \"myText\"\n// }\n\n```\n\nWe can use `buildComment` to make it easier to build the default array of comments in `buildBlogPost`.\n\n```typescript\nexport const buildBlogPost = buildFactory\u003cBlogPost\u003e({\n  author: buildAuthor(),\n  title: \"myTitle\",\n  body: \"myBody\",\n  comments: [buildComment()]\n});\n```\n\nNow you can confidently build a BlogPost with as much or as little data as you want.\n\n```typescript\nbuildBlogPost();\n// returns {\n//   author: {\n//     name: \"Grant Hutchins\",\n//     email: \"granthutchins@example.com\"\n//   },\n//   title: \"myTitle\",\n//   body: \"myBody\",\n//   comments: [\n//     {\n//       author: {\n//         name: \"Grant Hutchins\",\n//         email: \"granthutchins@example.com\",\n//         text: \"myText\"\n//       }\n//     }\n//   ]\n// }\n\nbuildBlogPost({\n  id: 3, \n  comments: []\n});\n// returns {\n//   id: 3,\n//   author: {\n//     name: \"Grant Hutchins\",\n//     email: \"granthutchins@example.com\"\n//   },\n//   title: \"myTitle\",\n//   body: \"myBody\",\n//   comments: []\n// }\n\nbuildBlogPost({\n  id: 3, \n  author: buildAuthor({id: 4}),\n  comments: [\n    buildComment({\n      id: 5,\n      author: buildAuthor({\n        id: 6\n      })\n    }), \n    buildComment({\n      author: buildAuthor({\n        id: 7\n      })\n    }), \n  ]\n});\n// returns {\n//   id: 3,\n//   author: {\n//     id: 4,\n//     name: \"Grant Hutchins\",\n//     email: \"granthutchins@example.com\"\n//   },\n//   title: \"myTitle\",\n//   body: \"myBody\",\n//   comments: [\n//     {\n//       id: 5,\n//       author: {\n//         id: 6,\n//         name: \"Grant Hutchins\",\n//         email: \"granthutchins@example.com\",\n//       },\n//       text: \"myText\"\n//     },\n//     {\n//       author: {\n//         id: 7,\n//         name: \"Grant Hutchins\",\n//         email: \"granthutchins@example.com\",\n//       },\n//       text: \"myText\"\n//     }\n//   ]\n// }\n```\n\n## FAQs\n\nIsn't this just currying one argument to Object.assign?\n\n\u003e Yes. \n\nWhy can't I just do that myself?\n\n\u003e You can.\n\nThen what's the point?\n\n\u003e The parameterized type argument helps guide you to get the types of the default objects and the overrides correct. It's not much, but it does help a small bit. \n\nOK, how does it help?\n\n\u003e One example: if you add a required field to an interface that is heavily used throughout your test suite, you can just go in and add a value for this new field to its factory's default object and now all of your tests that use the factory will compile.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnertzy%2Fts-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnertzy%2Fts-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnertzy%2Fts-factory/lists"}