Dashing
Generate pre-configured objects from presets like it's nothing. (Model Factories just like in Laravel)
How do I install this thing ?
# Using npm $ npm install dashing --save-dev # Using yarn $ yarn add dashing --save-dev
How do I use this thing ?
Importing
// Import with typescript / ES6 // Import with nodeconst makeDashing =
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:
// someFile.js const dashing = // will return a unique instance of dashing // someOtherFile.js let freshPreConfiguredUserObject = // have fun
Defining a model factory
// someFile.js let dashing = // will return a unique instance of dashing // Creating the default state for every User generated by dashingdashing // new User(Bruce, Wayne, 🥞) // Creating the default state for every Product generated by dashing with dynamic data 🦄 (see at the end for generators)dashing // equals new Product(9.99, "philospher stone") // or if multiple exports
But wait, dashing is way more powerful than that. You can haz presets 😍
Create presets
// someFile.js let dashing = dashing // new User(Bruce, Wayne, 🥞) // new User(Catwoman, "🥛" ) // new User( "twoface", "🍉" ) // new User( "Bruce Wayne", "🍕" ) dashing // equals new Product(9.99, 10, "philospher stone") // equals new Product(9.99, 0, "philospher stone") // equals new Product(1, 100, "Crappy product)
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 😎
// someFile.js let dashing = const myDefaultCallback = { instance} const myCallbackForAState = { return instance // If you return the object (in case immutable or something, we will use it for the next process)} dashing // Will apply this cllback to every created instance // Will apply this callback to instance generated with this state
Use the model factory
Now that you have instanciated dahsing and defined your presets, let's make some 😎
const instance = ; // By the way, if you registered a callback it will have been applied to the resulting instance 😁
Last minute overrides
const instance = ; // Considering defaults ["Catwoman", "something"], will make new User("catwoman", "else")
Using a/multiple presets
const instance = // Each state params & it's callback will be applied on top of the other in the oreder you asked for // (aka here moodyAlfred on top of hungryAlfred which is applied on top of tiredAlfred ; // orconst instance = ;
Make multiple instances
const instanceS = ; // 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)
// someFile.js let dashing = // pass it to makeDashing and, voila! 🤑 dashing // someRandomFile.js // For overrides too 😍
Write your own
I just pass you the generator, so you can pass whatever object you want as generator.
// someFile.js // This is a pointless but valid generatorconst quotesGenerator = "I like sweaters. I have a sweater obsession, I guess. -Drake" let dashing = // pass it to makeDashing and, voila! 🤑 dashing