npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dashingmodels

v1.0.6

Published

Generate pre-configured objects from presets like it's nothing. (Model Factories just like in Laravel)

Downloads

13

Readme

Dashing

Generate pre-configured objects from presets like it's nothing. (Model Factories just like in Laravel)

Try it on Code Sandbox

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 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:

// 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

// 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

// 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 😎

// 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 😎

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

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

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

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)
// 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.

// 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