# object-factory-bot

> Building objects using proxies

Latest version **0.8.2** (published 2021-03-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-factory-bot
pnpm add object-factory-bot
yarn add object-factory-bot
bun add object-factory-bot
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.8.2 |
| Published | 2021-03-16 |
| First published | 2018-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 70.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Nathan Stitt |
| Maintainers | nathanstitt |

## Links

- npm: https://www.npmjs.com/package/object-factory-bot
- Repository: https://github.com/nathanstitt/object-factory-bot
- npm.io page: https://npm.io/package/object-factory-bot

## Recent versions

- 0.8.2 (latest) — 2021-03-16
- 0.8.1 — 2021-03-16
- 0.8.0 — 2021-03-11
- 0.7.4 — 2021-03-11
- 0.7.3 — 2021-03-10
- 0.7.2 — 2021-03-10
- 0.7.1 — 2021-03-10
- 0.7.0 — 2021-03-10
- 0.6.1 — 2019-03-27
- 0.6.0 — 2019-03-27
- 0.5.4 — 2018-04-11
- 0.5.3 — 2018-04-11
- 0.5.2 — 2018-04-11
- 0.5.1 — 2018-04-06
- 0.5.0 — 2018-03-19
- … 8 more at https://npm.io/package/object-factory-bot/versions

## README

## A simple object factory

[![Build Status](https://travis-ci.org/nathanstitt/object-factory-bot.svg?branch=master)](https://travis-ci.org/nathanstitt/object-factory-bot)

Uses Proxy objects to create a DSL for object construction.  Requires Proxy support, which is supported by modern browsers and Node 6 and above.  https://caniuse.com/#search=Proxy http://node.green/#ES2015-built-ins-Proxy

```javascript

import { sample } from 'lodash';
import faker from 'faker'; // https://github.com/marak/Faker.js
Factory.defaults = { // counld also be a function
  driver: 'Speedy',
};
Factory.define('manufacturer')
    .name(() => sample(['Ford', 'GM', 'Mercedes']))

Factory.define('wheel')
    .diameter(Math.round(Math.random() * 6)+ 15)
    .brand(({ siblings }) => siblings && siblings.length ? siblings[0].brand : sample(['Firestone', 'Cooper', 'Bridgestone']));

Factory.define('car')
    .id(Factory.sequence) // driver is set in defaults
    .owner(({ diver }) => driver || faker.name.findName())
    .manufacturer(Factory.reference('manufacturer'))
    .wheels(Factory.reference('wheel', { count: 4 }));

const car = Factory.create('car', { owner: 'Jillian West', manufacturer: { name: 'Tesla' } });
```


`car` will be a plain object looking something like:
```
    { id: 1,
      owner: 'Jillian West',
      manufacturer: { name: 'Tesla' },
      wheels:
       [ { diameter: 20, brand: 'Bridgestone' },
         { diameter: 20, brand: 'Bridgestone' },
         { diameter: 20, brand: 'Bridgestone' },
         { diameter: 20, brand: 'Bridgestone' } ] }
```

## References support options:
 * *count*: can be either a function or number.  If not provided, the reference will be a single object.  If provided, the reference will be an array with _count_ elements.
 * *defaults*: Also can be either a function or number.  The default values will override whatever is specified by the referenced child's property values, but will not override the context that is passed into the factory itself.

## See also:

 * [Rosie](https://github.com/rosiejs/rosie)
 * [FactoryGirl](https://github.com/aexmachina/factory-girl)


Both of the above seem to be fairly complex and oriented towards createing classes.

For my purposes I just wanted a way to create plain objects.

I also wanted the property name to appear first in the object specification so that it's easier to read.

---
_Source: https://npm.io/package/object-factory-bot · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
