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

api-test-data-gen-lib

v0.0.60

Published

- [Overview](#overview) - [Key features](#key-features) - [Example JSON](#example-json) - [Example usage (Typescript)](#example-usage-typescript) - [Generators](#generators) - [Numbers](#numbers) - [Strings](#strings) - [randomRecord(options, va

Downloads

64

Readme

Overview

Nodejs lib to generate JSON test data from JSON

:construction::construction::construction::construction::construction::construction:

This is very much a work in progress. Examples below are rather nonsensical and documention is lacking. Having said that the basic stuff works :smile:

:construction::construction::construction::construction::construction::construction:

Key features

  • Built in generators for random numbers, dates, places, strings, bank account etc.
  • Generate fields from user defined arrays
  • Generate multiple JSON records from a single source
  • Generate multiple nested arrays
  • Flexible
  • Localisation support

Example JSON

{
  "vars": {
      "firstName":"Frank",
      "firstNames": ["Fred", "John", "Zara", "Sam", "Tom", "Claire", "Louise"],
      "version":1,
      "iterators": {
        "APIDATAGEN_root": "${randomInt(min:1,max:10)}",
        "addresses": "${randomInt(min:0,max:2)}",
        "creditCards": 6
      }
  },
  "seed": {
    "randomInt": "${randomInt({min:1,max:120})}",
    "timestamp": "{{$timestamp}}",
    "timestamp2": "${timestamp()}",
    "version": "${getField(source: version)}",
    "testDate":"${randomDate(start:1975/02/01, end:2022-02-04T16:00:00.000Z, format:DD-MMM-YYYY)}",
    "today": "${dateNow(format:MM-DD-YYYY)}",
    "yesterday":"${dateTPlus(format:MM-DD-YYYY, offset: -1)}",
    "tomorrow":"${dateTPlus(format:MM-DD-YYYY, offset: 1)}",
    "firstName":"${randomFirstname()}",
    "middleNme":"${randomMiddlename()}",
    "lastName":"${randomLastname()}",
    "name": "${randomString(source: firstNames)} ${randomInt()} / ${randomDecimal( min:200,  max:230, style:currency,currency:EUR)}",
    "age": "${randomInt(min:1,max:120):number}/${randomInt(min:200,max:230)}",
    "addresses": [
      {
        "seq": "${seq(name:addressId,start : 10,   step: 20)}",
        "address_line_1": "${randomInt(min:1,max:30,style:currency,currency:EUR,minimumFractionDigits:2,maximumFractionDigits:2)} abc street",
        "address_line_2": "abc",
        "country":"${randomCountry()}",
        "country2":"${randomCountry(field:alpha2)}",
        "country3":"${randomCountry(field:alpha3)}",
        "country4":"${randomCountry(field:regionCode)}",
        "currency":"${randomCurrency()}",
        "currency2":"${randomCurrency(field:name)}"
      }
    ],
    "creditCards": [
      {
        "cardNumber": "${randomInt(min:10000,max:2000000)}",
        "bank": "abc",
        "amount":"${randomDecimal(locale:de-DE, maximumFractionDigits: 2, min:0, max:1000000)}",
        "accountNumber":"${randomIntFixedLength(length:9)}",
        "accountNumber2":"${randomBankAccount()}",
                "sortCode":"${randomSortCode()}",
        "bankCode": "${randomBank(field:code)}",
        "bankName": "${randomBank(field:name)}"
      }
    ]
  }
}

Example usage (Typescript)

import { parse } from "api-test-data-gen-lib";

const parseRequest = (req, res) => {
  const vars = req.body.vars;
  const seed = req.body.seed;

  const parsedData: any = parse(seed, vars);
  if (parsedData.length === 0) {
    return [];
  }
  const response = JSON.parse(parsedData);

  return response;
};
{
  "randomProduct": {
    "code": "001",
    "desc": "Sponge"
  },
  "randomProductDesc": "Sponge",
  "randomProductCode": "001",
  "randomInt": 18,
  "timestamp": "1650736291",
  "timestamp2": "1650736291",
  "version": 1,
  "testDate": "12-Feb-2010",
  "today": "04-23-2022",
  "yesterday": "04-22-2022",
  "tomorrow": "04-24-2022",
  "firstName": "Theo",
  "middleNme": "Massimo",
  "lastName": "Loni",
  "name": "Claire ${randomInt()} / €204.27",
  "age": "${randomInt(min:1,max:120):number}/${randomInt(min:200,max:230)}",
  "addresses": [
    {
      "seq": 10,
      "address_line_1": "€8.00 abc street",
      "address_line_2": "abc",
      "countryName": {
        "name": "Norfolk Island",
        "alpha2": "NF",
        "alpha3": "NFK",
        "countryCode": "574",
        "iso3166_2": "ISO 3166-2:NF",
        "region": "Oceania",
        "subRegion": "Australia and New Zealand",
        "intermediateRegion": "",
        "regionCode": "009",
        "subRegionCode": "053",
        "intermediateregionCode": ""
      },
      "countryRegion": "Oceania",
      "country2": "CA",
      "country3": "SOM",
      "country4": "002",
      "currency": "TOP",
      "currency2": "Mauritian Rupee"
    }
  ],
  "creditCards": [
    {
      "cardNumber": 1166040,
      "amount": "26.703,15",
      "accountNumber": "764934816",
      "accountNumber2": "439998711",
      "sortCode": "65-57-72",
      "bankCode": "MPCX:",
      "bankName": "Sankheda Nagarik Sahakari Bank",
      "bankCode2": "LATX:",
      "bankName2": "Latur Urban Co-operative Bank Latur",
      "bankCode3": "LATX:"
    }
  ]
}

Generators

Numbers

| name | params | Description | example | | -------------------- | ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- | ------------------------------------------------ | | randomInt | min - lowest value max - highest value | Generate a random int | ${randomInt(min:1,max:120)} | | randomDecimal | min - lowest value max - highest value | Generate a random decimal | ${randomDecimal(min:1,max:120)} | | randomIntFixedLength | min - lowest value max - highest value length - max length of resulting int in chars | Generate a random int with a fixed length | ${randomIntFixedLength(min:1,max:120,length:2)} | | seq | name - name of the sequence start - start int of sequence step - the amount that the sequence is incremented | Generate a random int with a fixed length | ${seq(name:addressId,start:10,step: 20)} |

Strings


randomRecord(options, vars) ⇒ string

Kind: global function
Returns: string - a random string

| Param | Type | Description | | --------------- | ------------------- | ------------------------------------------------ | | options | Object | | | options.source | string | property name in vars of the source array | | [options.field] | string | field name within the source to select | | vars | Object | object containing the source array |

INPUT

  {
    "vars": {
        "products": [{"code":"001","desc":"Sponge"},{"code":"002","desc":"Hat"},{"code":"003","desc":"shell Suit"}],
        "iterators": {
          "APIDATAGEN_root": 2
        }
    },
    "seed": {
      "randomProduct":"${randomRecord(source:products)}"
    }
  }

OUPUT

  [
    {
      "randomProduct": {
        "code": "003",
        "desc": "shell Suit"
      }
    },
    {
      "randomProduct": {
        "code": "002",
        "desc": "Hat"
      }
    }
  ]

Dates

Finance

Names

Places