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

object-randomizer

v1.0.4

Published

## Overview

Downloads

7

Readme

Let's Randomize JSON Array/Object

Overview

This is a simple and customizable tool for generating random JSON arrays within specified ranges. It utilizes JavaScript and functional programming concepts to create structured data for testing, prototyping, or any other purpose requiring randomized data generation.

Features

  • Generates random JSON arrays with customizable ranges.
  • Supports various data types including strings, numbers, dates, and predefined sets.
  • Provides flexibility to define complex object structures.
  • Easy to use and integrate into existing projects.

Installation

You can install the generator via npm:

npm install object-randomizer

Usage

  1. Require or import the generator module into your JavaScript project:
const R = require('object-randomizer'); // For Node.js
// or
import R from 'object-randomizer'; // For ES6 modules` 
  1. Customize the generator to fit your data requirements by modifying the provided template.

  2. Generate random JSON arrays using the generate() method of the generator object.

  3. Access the generated data for further processing within your application.

Example

const R = require('object-randomizer');

// Example of generating random data
const dataGenerator = R.Array(
    R.Object({
        // Generate a unique identifier for each object
        id: R.Guid(),

        // Generate a random string for the name
        name: "Item " + R.Between(1, 100),

        // Generate a random value for the price between 10 and 100
        price: R.Between(10, 100),

        // Generate a random value for the category
        category: R.AnyOf("Electronics", "Clothing", "Books"),

        // Generate a random value for the color
        color: R.AnyOf("Red", "Blue", "Green"),

        // Generate a random quantity between 1 and 10
        quantity: R.Between(1, 10),

        // Generate a random date for the manufacture date within the last year
        manufactureDate: R.Date(
            new Date(new Date().setFullYear(new Date().getFullYear() - 1)), // Start date (1 year ago)
            new Date(), // End date (today)
            "YYYY-MM-DD"
        ),

        // Generate a random expiration date within the next year
        expirationDate: R.Date(
            new Date(), // Start date (today)
            new Date(new Date().setFullYear(new Date().getFullYear() + 1)), // End date (1 year from now)
            "YYYY-MM-DD"
        ),

        // Generate a random barcode based on the object's id
        barcode: R.SameAs(p => p.id).mappedInto(barcode => `BAR${barcode}`),

        // Generate a string representation of the price
        priceString: R.SameAs(p => p.price).asString()
    })
).withLengthBetween(2, 5); // Generate an array with 2 to 5 objects

const generatedData = dataGenerator.generate();

console.log(generatedData);

It gives output

[
    {
        "id": "d0dc9c9c-9c04-42ff-bd79-418c2826bdf7",
        "name": "Item 57",
        "price": 82,
        "category": "Electronics",
        "color": "Red",
        "quantity": 5,
        "manufactureDate": "2023-06-12",
        "expirationDate": "2024-03-22",
        "barcode": "BARd0dc9c9c-9c04-42ff-bd79-418c2826bdf7",
        "priceString": "82"
    },
    {
        "id": "b61b682a-9bd5-493d-9c15-8f30bfbbcb67",
        "name": "Item 87",
        "price": 47,
        "category": "Books",
        "color": "Blue",
        "quantity": 2,
        "manufactureDate": "2023-09-28",
        "expirationDate": "2024-05-28",
        "barcode": "BARb61b682a-9bd5-493d-9c15-8f30bfbbcb67",
        "priceString": "47"
    },
    {
        "id": "4a5f0d9e-f9ed-487d-9c5d-6dbb89159024",
        "name": "Item 17",
        "price": 36,
        "category": "Clothing",
        "color": "Green",
        "quantity": 10,
        "manufactureDate": "2023-08-10",
        "expirationDate": "2024-02-09",
        "barcode": "BAR4a5f0d9e-f9ed-487d-9c5d-6dbb89159024",
        "priceString": "36"
    }
]

Certainly! In the provided code, R.Array() and R.Object() are helper functions provided by the object-randomizer library. They allow you to define arrays of objects (R.Array()) and nested object structures (R.Object()), respectively, for generating random data.

Here's an explanation of how they work and how they can be cascaded to create complex data structures:

R.Array()

R.Array() is used to define an array of objects or values. It takes another generator function or a value as an argument. When a generator function is provided, it generates an array of values according to the defined generator function.

R.Object()

R.Object() is used to define an object with nested properties. It takes a configuration object where each key represents a property name, and the value is either a generator function or a static value.

Cascading Example

You can cascade R.Array() and R.Object() to define complex data structures with nested arrays and objects. For example:

javascriptCopy code

const complexDataGenerator = R.Array(
    R.Object({
        id: R.Guid(),
        name: "Category " + R.Between(1, 10),
        products: R.Array(
            R.Object({
                productId: R.Guid(),
                productName: "Product " + R.Between(1, 20),
                price: R.Between(10, 100)
            })
        ).withLengthBetween(1, 5)
    })
).withLengthBetween(2, 5);

In this example:

  • complexDataGenerator generates an array of objects, each representing a category.
  • Each category object has an id, a dynamically generated name, and an array of products.
  • The products array contains objects representing individual products with properties like productId, productName, and price.
  • The length of the products array is randomly generated between 1 and 5 for each category.
  • The length of the categories array is randomly generated between 2 and 5.

Description

  • R.Guid(): Generates a random GUID.
  • R.Between(): Generates a random number within a specified range.
  • R.AnyOf(): Selects a random value from a provided set.
  • R.SameAs(): Generates a value based on the properties of the same object.
  • mappedInto(): Maps the selected value into another value based on a mapping object.
  • asString(): Converts the generated number into a string.
  • Nested objects and arrays of objects can be defined using R.Object() and R.Array() respectively.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Feedback and Contributions

Feedback and contributions are welcome! Feel free to open an issue or submit a pull request on GitHub.