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

databob

v1.5.0

Published

Random JS object generation for tests

Downloads

21

Readme

#databob NPM version Build Status Coverage Status Dependency Status devDependency Status

(Also available in Scala and Kotlin flavours)

Given an example JS object, generates random examples of JavaScript objects for usage in tests. Think automatic builder objects where you only supply an example object.

Supports generation of object trees containing all of the primitive JS types, plus:

  • NaN, ±Infinity, undefined, null
  • native Dates
  • arrays and nested arrays
  • child objects
  • GUIDs
  • common timestamp formats, such as ISO8601
  • pluggable custom formats
  • "format safe" overriding of values

This is useful for a number of reasons:

  • reduces need for boilerplate test-code/duplication
  • increases resiliency of tests by enforcing explicit reliance only on important properties (rather than implicit properties of a commonly-built data object)
  • simple "cut and paste" updating of data formats (which means you only need to update the example models in one place)
  • strict-mode overriding will break should the template suddenly becomes inconsistent with example model

###Installation Via npm, simply run: npm install databob

Then instantiate a new databob with: var databob = require('databob')();

###Features

Given an example object:

> var book = {
    name: 'lord of the rings',
    pages: 500,
    simpleNames: false,
    movie: [ 180.52, 'elijah wood', true],
    author: {
        name: 'tolkien',
        dead: true
    }
};

Make a randomised object from the passed model:

> databob.make(book)
{
     name: 'diam praesent',
     pages: 373,
     simpleNames: false,
     movie: [ 140.47, 'nunc metus', true ],
     author: { name: 'dictum in', dead: true }
}

Override any number of the values of the generated instance. By default, strict-mode is enabled so overriding non-existent values will blow up:

> databob.make(book, { name: 'Harry Potter and the English Accent' }, { pages: 999 });
{
     name: 'Harry Potter and the English Accent',
     pages: 999,
     simpleNames: false,
     movie: [ 121.42, 'lorem ipsum', true ],
     author: { name: 'ullamcorper', dead: false }
}

Merge additional values into the generated instance:

> databob.make(book, { ibsn: '978-3-16-148410-0' }, true);

{
     name: 'orci',
     pages: 26,
     simpleNames: true,
     movie: [ 54.55, 'purus', true ],
     author: { name: 'elementum', dead: false },
     ibsn: '978-3-16-148410-0'
}

Register a example model under a name:

> databob.register({
    Book: book
});

...then recall it repeatedly under that name to generate new instances:

> databob.Book();

...or use it as a part of another model:

> databob.register({
    Librarian: {
        FavouriteBook: databob.Book
    }
});

Override (or merge extra) properties of a generated instance using the same mechanism as above:

> databob.Book({ name: 'Harry Potter and the English Accent' }, { ibsn: '978-3-16-148410-1' } , true);

###Extent of randomness By default, generated instances will use the following rules for random values:

  • Integers: The model value is used as a maximum
  • Doubles: The ceil() of the model value is used as a maximum, and the number of decimal places is retained
  • Strings (w/o spaces): Single words
  • Strings (w spaces): A sentence with the number of words in the model value used as a maximum
  • Strings (w newlines): Paragraph of text using the number of lines in the model value as a maximum
  • String GUIDs: 5 Strings seperated by '-'
  • Arrays: Like-for-like element generation to an identical length of array as the model value
  • Dates & Timestamps: Uses current date & time

###General Acks