Building objects using proxies
npm install 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' } ] }
* Rosie
* FactoryGirl
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.