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

pattern-mock

v3.0.3

Published

Generates mock data based on a specified object pattern.

Downloads

17

Readme

Pattern mock · Build Status GitHub npm npm

Generates mock data based on a specified object pattern.

Content

Installing

You can install it using Node Package Manager (npm):

$ npm install --save-dev pattern-mock

Then in the console:

const patternMock = require('pattern-mock');

Downloading

The source is available for download on GitHub.

CDN

The source can also be found on the following CDN:

<script src="https://unpkg.com/[email protected]/dist/pattern-mock.min.js"></script>

Syntax

patternMock(pattern[, config]);

Parameters

  • pattern - an object which specifies the structure of the result.

    • key - string - property name.
    • value - any - pattern to be mocked (e.g. name: 'NAME').
      • In case there is a need for passing a config object to a key, the following structure is required:
        • __pattern__ - any - pattern to be mocked (just like value).
        • __config__ - object - config (see below)
  • config - a configuration object which sets configuration and ranges of numbers for generating random data:

    • numbersInPhoneNumber - array - default [10, 10]
    • sentencesInParagraph - array - default [3, 6]
    • wordsInSentence - array - default [5, 10]
    • wordsInFullName - array - default [2, 4]
    • lettersInWord - array - default [4, 8]
    • lettersInString - array - default [5, 10]
    • spaceIndexInPhoneNumber - array - default [4, 6, 8]
    • counterStart - number - default 0
    • range - array - default [3, 8]
    • length - number - sets the length of a list.
    • decorateEach - function - calls each element from a list and returns the output.
    • decorate - function - calls the value and returns the output.
    • shouldPickOne - boolean - picks a single value from a list.

API

Supported types

WORD

patternMock({
	place: 'WORD'
}); 

// { 
// 	place: "jezug" 
// }

FULL_NAME

patternMock({
	name: 'FULL_NAME'
}); 

// { 
// 	name: "Xolapu Moreni Rupuhixe" 
// }

NAME

patternMock({
	name: 'NAME'
}); 

// {
// 	name: "Dunaf" 
// }

SENTENCE

patternMock({
	description: 'SENTENCE'
}); 

// {
// 	description: "Hivilonu jowux wogo zilaxexa hijo vocefe fuzar."
// }

PARAGRAPH

patternMock({
	article: 'PARAGRAPH'
});

// {
// 	article: "Hudehifu qatu hanel peboviw nepuxut miges vibocu zatipe retuq. Bihu safa juzu tufuho bojap rimapo hovuqasi faha vezu. Dicewama yivusuhu valuy qunowaci jixun. Xoyuvo befo rorer tudo qabac butap pezu tebawup cedow. Puvasar tote xeqer cuxeduzo wico zequrefi lirabad periniw. Jamopofe cucojuh menu cawete denipivu."
// }

STRING

patternMock({
	randomId: 'STRING'
});

// {
// 	randomId: "co5dd6bGs"
// }

URL

patternMock({
	website: 'URL'
});

// {
// 	website: "http://zat.wa"
// }

EMAIL

patternMock({
	email: 'EMAIL'
});

// {
// 	email: "[email protected]"
// }

COLOR

patternMock({
	color: 'COLOR'
});

// {
// 	color: "#747978"
// }

BOOLEAN

patternMock({
	isOffline: 'BOOLEAN'
}); 

// {
// 	isOffline: true
// }

DATE

patternMock({
	meetingDate: 'DATE'
}); 

// {
// 	meetingDate: Date Tue Dec 03 1974 22:03:35 GMT+0200 (Eastern European Standard Time)
// }

NUMBER

patternMock({
	id: 'NUMBER'
}); 

// {
// 	id: 7111080744192283
// }

COUNTER

patternMock({
	id: ['COUNTER']
}); 

// {
// 	id: [ 0, 1, 2, 3, 4 ]
// }

PHONE_NUMBER

patternMock({
	phoneNumber: 'PHONE_NUMBER'
}); 

// {
// 	phoneNumber: "3346 80 78 99"
// }

CUSTOM_NUMBER_<MIN_NUMBER>-<MAX_NUMBER>

patternMock({
	magicNumber: 'CUSTOM_NUMBER_23-167'
}); 

// { 
// 	magicNumber: 42
// }

Nesting types

When creating a list, all we need to do is to add a type as first argument such as:

patternMock({
	names: ['FULL_NAME']
});

// {
// 	names: [ "Vavobur Qeloc", "Dame Hogicebu Qosudet", "Lowo Welu" ]
// }

Nesting more types:

patternMock({
	person: {
		name: 'FULL_NAME',
		age: 'CUSTOM_NUMBER_17-26',
		hobbies: ['WORD'],
		isCool: 'BOOLEAN'
	}
});

// {
// 	person": {
// 		"name": "Guya Caro Sacusip Datig",
// 		"age": 22,
// 		"hobbies": [
// 			"jaxen",
// 			"ragodutu",
// 			"noxefu"
// 		],
// 		"isCool": true
// 	}
// }

For a more complex and custom structure, we can use different __config__ properties, followed by __pattern__ which will be the value for our property:

patternMock({
	yearOfBirth: {
		__pattern__: 'DATE',
		__config__: {
			decorate: date => date.getFullYear()
		}
	}
});

// {
// 	"yearOfBirth": 1985
// }

Or a more nested and complex structure:

patternMock({
	person: {
		name: 'FULL_NAME',
		hobbies: ['WORD'],
		bestAt: {
			__pattern__: ['losing at games', 'being rejected'],
			__config__: {
				decorate: list => list.join(', ')
			}
		},
		friends: {
			__pattern__: [{
				name: 'FULL_NAME',
				age: 'CUSTOM_NUMBER_17-26',
				favouriteGame: {
					__pattern__: ['LEAGUE OF LEGENDS', 'DOTA', 'SMITE'],
					__config__: {
						shouldPickOne: true
					}
				}
			}],
			__config__: {
				length: 3,
				decorateEach: ({age, ...rest}) => ({age: `${age} years old`, ...rest})
			}
		},
		randomStuff: ['NUMBER', {email: 'EMAIL'}, 'COLOR', ['whatever', 'BOOLEAN']]
	}
});

// {
// 	"person": {
// 		"name": "Yihoruhi Ruqu Jevimi",
// 		"hobbies": [
// 			"febo",
// 			"nowo",
// 			"wemoxox",
// 			"coquna",
// 			"rere",
// 			"sujof",
// 			"gage",
// 			"pizamic"
// 		],
// 		"bestAt": "losing at games, being rejected",
// 		"friends": [
// 			{
// 				"age": "17 years old",
// 				"name": "Wuvew Dusuto",
// 				"favouriteGame": "LEAGUE OF LEGENDS"
// 			},
// 			{
// 				"age": "22 years old",
// 				"name": "Pexujuba Wuwizur",
// 				"favouriteGame": "DOTA"
// 			},
// 			{
// 				"age": "24 years old",
// 				"name": "Buwed Mazopaze",
// 				"favouriteGame": "SMITE"
// 			}
// 		],
// 		"randomStuff": [
// 			4658847147811904,
// 			{
// 				"email": "[email protected]"
// 			},
// 			"#ADC0DB",
//			[
//				"whatever",
//				true
//			]
// 		]
// 	}
// }

Authors

See also the list of contributors who participated in this project.

License

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