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

ranson

v1.0.5

Published

Robust random json data generator, with several built in data types, and the ability to implement custom generated data.

Downloads

19

Readme

Ranson (A Random Data Generator)

About

This is an extremely lightweight data generator that gives you a framework to create custom random data sets. It's designed to be extensible, so feel free to use it how you wish.

How to Use

The generator is pretty straightforward. Ranson comes with a prebuilt class "UserData", which can be used for most baseline purposes. Here's an example of a generated UserData object:

  UserData {
    firstName: 'Kevin',
    lastName: 'McGuire',
    phone: '212-631-0810',
    email: '[email protected]',
    dob: 1991-05-22T06:00:00.000Z,
    location: { state: 'Utah', city: 'Salt Lake City', zip: '84101' }
  },

If all you care about is creating some random user data to populate a test db, then all you need to do is the following:

import { Ranson, UserData } from "ranson";

const numEntries = 100; // Or however many you want
const randomData = Ranson.generateData(UserData, numEntries);

RansonData

RansonData is an interface that should be implemented by your class. This allows it to be used with the Ranson class. This is required to be used by the Ranson Class

export interface RansonData {
  generate(): RansonData;
}

The only required method to be implemented is a generate(): RansonData;. This is usually as simple as the following:

  generate(): RansonData {
    return this;
  }

It needs to return the instantiated object.

Ranson

Ranson is a class comprised entirely of static methods, so you don't need to instantiate it as an object. It comes with a number of different functions to generate random pieces of data, or utility functions to work with arrays of data. The primary function you will use with Ranson are -

// Accepts two arguments, a class that implements RansonData, and the number of entries to generate. 
Ranson.generateData(RansonDataClass, numberOfEntries)

There is also a helper function that automatically converts the output array to a JSON data -

Ranson.generateJSONData(RansonDataClass, numberOfEntries)

Custom Data Sets

The way I typically set up custom data is by setting aside an array of the values I want. This can be seen in the /data directory bundled with the package. I then use the Ranson.getRandomArrayValue(dataset[]) to select the item.

Custom Classes

User defined classes need to implement the RansonData interface. Here is how I would set up a custom class with a custom dataset.

import { Ranson, RansonData } from "ranson";

// define the datasets to be used in the generator
const firstNames = ["Frank", "Gary", "Linda"];
const lastNames = ["Smith", "Jones", "Williams"];

class Example implements RansonData {
  // define the properties I want in my class
  firstName: string;
  lastName: string;

  // create a constructor that uses the Ranson.generateFromDataset() function, or a different random-value generator function you define.
  constructor() {
    this.firstName = Ranson.generateFromDataset(firstNames);
    this.lastName = Ranson.generateFromDataset(lastNames);
  }
  // implement the generate function, which should ideally just return the instance of the class.
  generate() {
    return this;
  }
}

const randomData = Ranson.generateData(Example, 3);
/*
Expected Output:
[
  Example { firstName: 'Frank', lastName: 'Jones' },
  Example { firstName: 'Gary', lastName: 'Smith' },
  Example { firstName: 'Frank', lastName: 'Jones' }
]
*/

Built-In

Built into the package is the UserData class, and the following useful arrays / datasets:

  • firstNames
  • lastNames
  • adjectives
  • nouns
  • USAreaCodes
  • locations
    • each entry includes a state, along with three of the major cities in that state, and zip codes for those cities. There are also some defined types / enums that are primarily used with the built-in static methods owned by the Ranson class, such as
  • PhoneFormat
  • USLocation
  • USLocationData (This one has three cities, the USLocation type only has a single city and zip, since a location can be in multiple cities.)

Finally

I want this to be open source, so feel free to add whatever you like to it, or let me know if there are any features / datasets you would like included.