https://github.com/isthatcentered/dashing
Model factory inspired by laravel
https://github.com/isthatcentered/dashing
Last synced: 12 months ago
JSON representation
Model factory inspired by laravel
- Host: GitHub
- URL: https://github.com/isthatcentered/dashing
- Owner: isthatcentered
- Created: 2018-08-06T05:18:37.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-11-02T04:40:01.000Z (over 5 years ago)
- Last Synced: 2025-05-23T07:42:03.245Z (about 1 year ago)
- Language: TypeScript
- Size: 2.03 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dashing
Generate pre-configured objects from presets like it's nothing. (Model Factories just like in Laravel)
[](https://codesandbox.io/s/209n3k3r1j?expanddevtools=1&view=editor)
## How do I install this thing ?
```sh
# Using npm
$ npm install dashing --save-dev
# Using yarn
$ yarn add dashing --save-dev
```
## How do I use this thing ?
### Importing
```javascript
// Import with typescript / ES6
import makeDashing from "dashing"
// Import with node
const makeDashing = require("dashing")
```
### Initialize dashing
Dashing does not comes as a singleton to give you flexibility.
This means you can have multiple instances if needed,
but you have to instantiate it by yourself.
Fortunately, this is quite simple:
```javascript
// someFile.js
import makeDashing from "dashing"
export const dashing = makeDashing({}) // will return a unique instance of dashing
// someOtherFile.js
import dashing from "./someFile" // if exported as default
import { dashing } from "./someFile" // if not exported as default
let freshPreConfiguredUserObject = dashing(User)
.make() // have fun
```
### Defining a model factory
```javascript
// someFile.js
import makeDashing from "dashing"
let dashing = makeDashing({}) // will return a unique instance of dashing
// Creating the default state for every User generated by dashing
dashing.define( User, ["Bruce", "Wayne", "🥞"] ) // new User(Bruce, Wayne, 🥞)
// Creating the default state for every Product generated by dashing with dynamic data 🦄 (see at the end for generators)
dashing.define( Product, generator => [generator.someMethod(), "Philosopher stone"] ) // equals new Product(9.99, "philospher stone")
export default dashing
// or if multiple exports
export { dashing, otherStuff }
```
But wait, dashing is way more powerful than that. You can haz presets 😍
#### Create presets
```javascript
// someFile.js
import makeDashing from "dashing"
let dashing = makeDashing({})
dashing.define( User, ["Bruce Wayne", "🥞"] ) // new User(Bruce, Wayne, 🥞)
.registerPreset( "superHero", generator => [ "Catwoman", "🥛" ] ) // new User(Catwoman, "🥛" )
.registerPreset( "superVilain", generator => [ "twoface", "🍉" ] ) // new User( "twoface", "🍉" )
.registerPreset( "batman", generator => [ undefined, "🍕" ] ) // new User( "Bruce Wayne", "🍕" )
dashing.define( Product, generator => [generator.someMethod(), 10, "Philosopher stone"] ) // equals new Product(9.99, 10, "philospher stone")
.registerPreset( "soldOut", generator => [ undefined, 0] ) // equals new Product(9.99, 0, "philospher stone")
.registerPreset( "crappy", generator => [1, 100, "Crappy product"] ) // equals new Product(1, 100, "Crappy product)
export default dashing
```
#### Modify the instance after creation
You might not use you constructor to configure your objects. Or use a bit of constructors and methods. Fine with me, here's how you can do that with dashing 😎
```javascript
// someFile.js
import makeDashing from "dashing"
let dashing = makeDashing({})
const myDefaultCallback = (instance, _) => {
instance.setBreakfast("🥞")
}
const myCallbackForAState = (instance, _) => {
return instance.setMood("tired") // If you return the object (in case immutable or something, we will use it for the next process)
}
dashing.define( User, ["Alfred"], myDefaultCallback) // Will apply this cllback to every created instance
.registerPreset( "tiredAlfred", () => [], myCallbackForAState) // Will apply this callback to instance generated with this state
export default dashing
````
### Use the model factory
Now that you have instanciated dahsing and defined your presets, let's make some 😎
```javascript
import dashing from "./someFile"
const instance = dashing(User)
.make(); // By the way, if you registered a callback it will have been applied to the resulting instance 😁
````
#### Last minute overrides
```javascript
import dashing from "./someFile"
const instance = dashing(User)
.make([undefined, "else"]); // Considering defaults ["Catwoman", "something"], will make new User("catwoman", "else")
````
#### Using a/multiple presets
```javascript
import dashing from "./someFile"
const instance = dashing(User)
.preset( "tiredAlfred" ) // Each state params & it's callback will be applied on top of the other in the oreder you asked for
.preset( "hungryAlfred" ) // (aka here moodyAlfred on top of hungryAlfred which is applied on top of tiredAlfred
.preset( "moodyAlfred" )
.make();
// or
const instance = dashing(User)
.preset( ["tiredAlfred", "hungryAlfred", "moodyAlfred"] )
.make();
````
#### Make multiple instances
```javascript
import dashing from "./someFile"
const instanceS = dashing(User)
.preset( "tiredAlfred" )
.times(99)
.make(); // we return an array when you ask for multiple instances 📦
````
### Randomize data
So, I intended to ship it with a default data generator. But frankly the package was 10x th size soooo... 😱
Here are your options:
#### Use one of those
- https://chancejs.com/
- https://github.com/marak/Faker.js/
- (note, there are many others)
```javascript
// someFile.js
import makeDashing from "dashing"
import faker from "faker" // import your generator
let dashing = makeDashing(faker) // pass it to makeDashing and, voila! 🤑
dashing.define(User, generator => {
return [generator.someMethodOfUsedGenerator(), {active: true, id: generator.makeMeSomeId()}]
}, (instance, generator) => { // Available for callback too ❤️
instance.setSomething(generator.generateSomething())
})
export default dashing
// someRandomFile.js
import dashing from "./someFile"
dashing(User)
.make(generator => [generator.generateSomeStuff()]) // For overrides too 😍
````
#### Write your own
I just pass you the generator, so you can pass whatever object you want as generator.
```javascript
// someFile.js
import makeDashing from "dashing"
// This is a pointless but valid generator
const quotesGenerator = {
giveMeADrakeQuote: () => "I like sweaters. I have a sweater obsession, I guess. -Drake"
}
let dashing = makeDashing(quotesGenerator) // pass it to makeDashing and, voila! 🤑
dashing.define(User, generator => [{greeting: generator.giveMeADrakeQuote()}])
export default dashing
````