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

camusjs

v2.0.1

Published

A extensible random objects generator

Downloads

3

Readme

camusjs

A extensible random objects generator

Build Status

camusjs

What is this?

You can translate templates into objects with full random objects.

This:

{
	"name": {
		"*": "name"
	},
	"age": {
		"*": "integer",
		"args": [
			{
				"min": 12,
				"max": 40
			}
		]
	}
}

Will generate some like this:

{
    "name": "Jordan Barnes",
    "age": 18
}

If you want some more complex:

{
    "name": {
        "*": "name"
    },
    "pets": {
        "*": "array",
        "minLength": 1,
        "maxLength": 3,
        "types": [
            {
				"chanceToAppear": 80,
				"definition": {
					"*": "object",
					"definition": {
						"name": {
							"*": "first"
						},
						"type": "Dog"
					}
				}
			},
			{
				"chanceToAppear": 20,
				"definition": {
					"*": "object",
					"definition": {
						"name": {
							"*": "first"
						},
						"type": "Cat"
					}
				}
			}
        ]
    }
}

It will generate a person, with a name and 1 to 3 pets. Each pet has a name and type. Dogs has 80% of chance to appear. Cats, 20%.

The result is some like this:

{
    "name": "Alvin Ramsey",
    "pets": [
        {
            "name": "Jane",
            "type": "Dog"
        },
        {
            "name": "Harry",
            "type": "Dog"
        },
        {
            "name": "Ina",
            "type": "Cat"
        }
    ]
}

It can handle only certain path if you want:

{
	store: {
		country: "Brazil",
		employees: [
			{
				"*": "name"
			}
		]
	}
}

Is translated to some like:

{
    "store": {
        "country": "Brazil",
        "employees": [
            "Maud Moran"
        ]
    }
}

How can i use this?

First, install it: npm install camusjs

var camusjs = require('camusjs')

var myTemplate = {
  "myProperty": {
    "*": "type of the generator"
  }
}

var generated = camusjs.parse(myTemplate)

// done

ChanceJS

chancejs

You can use any CHANCE JS(VIEW HERE) method to generate values.

Let me show you a sample: The method guid:

chance.guid();
// 'f0d8368d-85e2-54fb-73c4-2d60374295e3'

You can add to your templates using:

{
  "id": {
    "*": "guid"
  }
}

And that's it!

If the function you want to call has arguments:

chance.integer({min: 1, max: 10});
// -7
{
  "myRandomInt": {
    "*": "integer",
    "args": [
      { "min": 1, "max": 10 }
    ]
  }
}

Nested Objects

If you want to generate nested objects, use the object template:

{
    "name": {
        "*": "name"
    },
    "job": {
        "*": "object",
        "definition": {
            "title": {
                "*": "pickone",
                "args": [
                    ["developer", "qa", "manager"]
                ]
            }
        }
    }
}

It will generate some like this:

{
	"name": "Andre Ortega",
	"job": {
		"title": "developer"
	}
}

Array

If you want a collection of objects, use the array template:

{
	"myArray": {
		"*": "array",
		"minLength": 2, // the minimum size of the array
		"maxLength": 6, // the maximum size of the array
		"types": [ // the types of content of the array
			{
				// if you have multimple types of templates in the array,
				// set the percentage of the template appear.
				"chanceToAppear": 90,
				"definition": { // you can use template here.
					"*": "object",
					"definition": {
						"myPropName": {
							"*": "name"
						}
					}
				}
			},
			{
				"chanceToAppear": 10,
				"definition": {
					"*": "integer"
				}
			}
		]
	}
}

In this sample, we generate an array with misc objects and numbers. The result must be some like this:

{
	"myArray": [
		{
			"myPropName": "Georgie Patterson"
		},
		6303461331173376,
		{
			"myPropName": "Anne Jefferson"
		},
		{
			"myPropName": "Lucinda Jordan"
		}
	]
}

Allowing NULLS

If you want to some template returns null in some case, use the chanceToBeNull modifier:

{
	"id": {
		"*": "guid",
		"chanceToBeNull": 15 // in this case, 15% of the times, the id will returns null.
	}
}

All templates accept this modifier =).

Using static values

You can use fixed informed values using the option_value and options:

var template = {
	id: {
		"*": "option_value",
		property: 'myId'
	}
}

var generated = camusjs.parse(template, {
	myId: 'abc123'
})

String Replacing

If your property has a string value and you want to replace some of this string with a random or option value, use the string_replace template:

var template = {
	url: {
		"*": "string_replace",
		value: 'http://api/authentication/%CPF%(/.*)?', // the raw string
		searchFor: '%CPF%', // what will be replaced
		replaceWith: { // the value that be replacer, you can use templates here =).
			"*": "option_value",
			property: "cpf"
		}
	}
}

var generated = camusjs.parse(template, {
	cpf: '00011122244'
})

// 'http://api/authentication/00011122244(/.*)?'

Advanced

Adding new Parsers

You can create parsers and append them to the core. For this, create the following:

var camusjs = require('camusjs')

var newParser = {
	"*": "my_name_of_generator",
	converter: function(template, options) {
		// return your value...
		return null
	}
}

camusjs.registerParser(newParser)

Now you can use:

{
	"myProperty": {
		"*": "my_name_of_generator"
	}
}

Sample of parser that generate a sword:

/*
	want to use:
	{
		"*": "sword"
	}

	and get
	{
		"name": "any name of sword",
		"damage": 12,
		"price": 300.51
	}
*/

var camusjs = require('camusjs')

var swordParser = {
	"*": "sword",
	converter: function(template, options) {
		return camusjs.parse({
			name: {
				"*": "name",
			},
			damage: {
				"*": "integer",
				args: [
					{ min: 5, max: 20 }
				]
			},
			price: {
				"*": "floating",
				args: [
					{min: 150, max: 550, fixed: 2}
				]
			}
		}, options)
	}
}

camusjs.registerParser(swordParser)

var template = {
	"item": {
		"*": "sword"
	}
}

var generated = camusjs.parse(template)

/*
	{
		"item": {
			"name": "Mitchell Mason",
			"damage": 9,
			"price": 257.21
		}
	}
*/

Adding new middlewares

Middlewares are parsers that runs BEFORE the registered parsers (chanceToBeNull is a middleware).

You can register new middlewares:

var camusjs = require('camusjs')

var myMiddleware = {
	'#': 'name_of_my_middleware',
	isGlobal: true, // if must run before ALL transformers
	converter: function(template, options, result) {
		// do what you want here
		var newValue = {} // my new value
		var mustContinue = true //must execute the next middleware or stops here and return value

		result(newValue, mustContinue)
	}
}

camusjs.registerMiddleware(myMiddleware)

For localized middlewares, you can set isGlobal to false and add it manually to your parsers:

var camusjs = require('camusjs')

var myMiddleware = {
	'#': 'name_of_my_middleware',
	isGlobal: false, // if must run before ALL transformers
	converter: function(template, options, result) {
		// do what you want here
		var newValue = {} // my new value
		var mustContinue = true //must execute the next middleware or stops here and return value

		result(newValue, mustContinue)
	}
}

camusjs.registerMiddleware(myMiddleware)

var newParser = {
	"*": "my_name_of_generator",
	middlewares: ['name_of_my_middleware'],
	converter: function(template, options) {
		// return your value...
		return null
	}
}

camusjs.registerParser(newParser)

Changelog

2.0.1

  • Fix: fixing wrong template manipulation in loop generation.

2.0.0

  • Breaking Change: Removing parseValue method. Use parse only for all.

1.3.1

  • Fixing null exception on parse object

1.3.0

  • Improve object parsing

1.2.0

  • Feat: adding string_replace

1.1.0: First Release