{"id":18657770,"url":"https://github.com/lightsofapollo/object-factory","last_synced_at":"2025-11-05T22:30:20.926Z","repository":{"id":57312593,"uuid":"13850667","full_name":"lightsofapollo/object-factory","owner":"lightsofapollo","description":"Node friendly object factories (like factory girl)","archived":false,"fork":false,"pushed_at":"2013-12-31T23:17:03.000Z","size":122,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-09T01:36:13.975Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lightsofapollo.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}},"created_at":"2013-10-25T03:29:46.000Z","updated_at":"2013-12-31T23:17:04.000Z","dependencies_parsed_at":"2022-09-10T20:21:03.809Z","dependency_job_id":null,"html_url":"https://github.com/lightsofapollo/object-factory","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fobject-factory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fobject-factory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fobject-factory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fobject-factory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lightsofapollo","download_url":"https://codeload.github.com/lightsofapollo/object-factory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239475960,"owners_count":19645041,"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":[],"created_at":"2024-11-07T07:29:49.729Z","updated_at":"2025-11-05T22:30:20.893Z","avatar_url":"https://github.com/lightsofapollo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Factory\n\nCreate and distribute test fixtures/factories.\n\n## Usage\n\nAll examples assume you have required object-factory like this:\n\n```js\nvar Factory = require('object-factory');\n```\n\n## Basic task syntax\n\n```js\nfunction Event(props) {\n  this.title = props.title;\n  this.location = props.location;\n}\n\nvar EventFactory = new Factory({\n  object: Event\n  properties: {\n    // define defaults\n    title: 'Amazing Event',\n    location: 'Bahamas'\n  }\n});\n\n// create an object with the attributes of the factory but not an\n// instance of the Event class\nvar event = EventFactory.build({ \n  title: 'xxx' \n});\n\n// Create an instance of the event class\nvar event = EventFactory.create({ \n  title: 'xxx' \n});\n\n```\n\n## Options for factories\n\nWhen creating factories there are various options that can be passed.\n\n### `.properties`\n\n```js\nnew Factory({ \n  properties: {\n    key: 'default value\n    '\n  } \n});\n```\n\nThe `.properties` property (sorry) specify the default values for a\ngiven factory.\n\n\n\n### `.object`\n\n```js\n\nvar MyThing = new Factory({ \n  object: ThingWithConstructorThatAcceptsObjects\n});\n```\n\nAs the fictional object might suggest object is the object that the\nfactories properties are passed into...\n\n```js\n// This operation\n\nMyThing.create({ xfoo: true });\n\n// Translates to this\nnew ThingWithConstructorThatAcceptsObjects({ xfoo: true })\n```\n\n### `.onbuild`\n\nThe `onbuild` property will be called if given _before_ the generated\nproperties are passed to the constructor `.object`.\n\n```js\nvar BuildMe = new Factory({\n  onbuild: function(builtObject) {\n    // use this to customize the output of your factory for dynamic\n    // values, etc...\n  }\n})\n```\n\n### `.oncreate`\n\nThe `oncreate` property will be called if given _after_ the generated\nproperties are passed to the constructor `.object`.\n\n```js\nvar BuildMe = new Factory({\n  object: Xfoo\n  oncreate: function(object) {\n    // (object instanceof Xfoo) === true\n  }\n})\n```\n\n## Composing factories\n\nYou can't create abritrarty depth in a factory. Each factory must be\none object deep but multiple factories can be referenced as properties\nto create this nesting.\n\n```js\n\nvar Person = new Factory({\n  properties: {\n    name: 'James Lal'  \n  }\n});\n\nvar Event = new Factory({\n  properties: {\n    // define defaults\n    title: 'Amazing Event',\n    location: 'Bahamas',\n    person: Person\n  }\n});\n```\n## Inheritance\n\nFactories can inherit from other factories:\n\n```js\n\nvar Developer = Person.extend({\n  properties: {\n    OCD: true  \n  }\n});\n```\n\n## Testing Factories\n\nobject factory ships with a `object-factory-viewer` binary which will\npretty print the output of your factory given a module.\n\n```js\n// xfoo.js\nmodule.exports = new Factory({\n  properties: { xfoo: 'foo' }\n});\n```\n\n```sh\n./node_modules/.bin/object-factory-viewer xfoo.js\n# will output the pretty printed (util.inspect) version of the factory.\n```\nIf your not using .onbuild or .oncreate then this is a great way to test\nthe output of your factories. This serves as a good sanity check (and\ncould be used as documentation too).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightsofapollo%2Fobject-factory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flightsofapollo%2Fobject-factory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightsofapollo%2Fobject-factory/lists"}