0.5.5 • Published 5 years ago

ape-mock v0.5.5

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

Ape

The Ape API is how I thought a data-mocker API should look like. I needed it to create similar chunks of raw data for projects and tests, and thought it could be a good idea to share it with the rest of the world. I believe that the API I proposed here makes it simple and easy to create complex template-based JSON objects.

Why Ape?

Because apes like to mimic (or "mock") real human behavior... ;) Also, API sounds like Ape-PI.

Usage

Use the Ape function to generate a new mock object. Ape receives a template object that is consisted of string keys and primitive or Ape operators as values:

import Ape, { name, age } from 'ape-mock';

const template = Ape({
  firstName: name().male(),
  lastName: name().lastName(),
  age: age().adult(),
});

// call the generate method to create a new object out of the template above
template.generate();

Ape

This is the template builder. It can receive an object or an array and returns a template object.

MethodArgumentsDescription
generateNoneCreates an object with mock data, based on the template supplied to Ape

name

Generate random names. The name value can be mutated with the following methods:

MethodArgumentsDescription
maleNoneSet the first name to be male
femaleNoneSet the first name to be female
noFirstNameNoneDon't generate a first name
middleNameNoneGenerate a middle name
abbrMiddleNameNoneGenerate an abbreviated middle name (W. instead of William)
lastNameNoneGenerate a last name
lastNameFirstNonePlace the last name first in this format: lastName, firstName middleName

If not specified, the gender will be random

name().female().middleName().abbrMiddleName().lastNameFirst()
// will generate a female name such as Cooper, Whitney E.

age

Generate a random age. Mutation methods:

MethodArgumentsDescription
babyNoneGenerates an age between 0 and 4
childNoneGenerates an age between 4 and 14
teenagerNoneGenerates an age between 14 and 20
adultNoneGenerates an age between 20 and 67
seniorNoneGenerates an age between 67 and 120
setMinNumberSets the minimum age
setMaxNumberSets the maximum age

If not specified, a random age between 0 and 120 will be generated

age().teenager()
// will generate an age between 14 and 20

text

Manipulate a supplied string.

MethodArgumentsDescription
randomizeLengthNumberGet a random number of words between 1 and the string's length
minLengthNumberSet the minimum length of the randomLength method
maxLengthNumberSet the maximum length of the randomLength method
randomizeOrderNoneRandomize the order of the result string
randomSequenceNoneRandomize the string before exctracting the text
text('We are the Borg. Your biological and technological distinctiveness will be added to our own. Resistance is futile.').maxLength(4)

loremIpsum

Generate a string extract from a lorem ipsum paragraph. Mutation methods:

MethodArgumentsDescription
randomizeLengthNumberGet a random number of words between 1 and the paragraph's length
minLengthNumberSet the minimum length of the randomLength method
maxLengthNumberSet the maximum length of the randomLength method
randomizeOrderNoneRandomize the order of the result lorem ipsum string
randomSequenceNoneRandomize the lorem ipsum paragraph before exctracting the text
loremIpsum().maxLength(5)
// result: Lorem ipsum dolor sit amet,

image

Generate a placeholder image (a link to a real image). Mutation methods:

MethodArgumentsDescription
widthNumberImage width
heightNumberImage height
specificStringIf not specified, a random image will be generated each time

If not specified, the default width and height are 420x320

image().width(100).height(50);

alt result

fromValues

Takes an array of values. The output will be a random value from the array.

fromValues(['Manager', 'Developer', 'IT', 'QA'])

color

Generate a color. Mutation methods:

MethodArgumentsDescription
specificStringReturns only the specified color. Requires a hex-formatted color
asRGBNoneThe value will be in RGB format
removeHashNoneIf the result is in hexadecimal, the # symbol will be removed from it
setAlphaNumberSet an alpha channel. This will automatically set the format to RGB
color().setAlpha(0.5)
// rgb(r, g, b, a)

date

Generates a date. Mutation methods:

MethodArgumentsDescription
randomNoneGenerates a random date between Jan 1, 1970 and now
startMinutesAgoNumberSets the minimum date to x minutes ago
startDaysAgoNumberSets the minimum date to x days ago
startYearsAgoNumberSets the minimum date to x years ago
endMinutesAgoNumberSets the maximum date to x minutes ago
endDaysAgoNumberSets the maximum date to x days ago
endYearsAgoNumberSets the maximum date to x years ago
startAtDateSets the start time
endAtDateSets the end time
date().random().startYearsAgo(30)
// a date between 30 years ago and now

arrayOf

Generate an array of a specific value. The value can also be an Ape object (see example). Mutation methods:

MethodArgumentsDescription
repeatNumberThe number of times the supplied object will be cloned
randomNumber, Number?Clone the supplied object x number of times. The second argument is the minimum number of clones, which defaults to 1 if not passed.
Ape({
  blogPosts: arrayOf({
    title: loremIpsum().minLength(5).randomizeLength(10).randomizeOrder(),
    thumbnail: image(),
    publishDate: date().startYearsAgo(5).random()
  })
}.random(10, 3))

// an array of 3-10 blogPost items 

email

Generate an email address:

MethodArgumentsDescription
gmailNoneSet gmail as the email provider
outlookNoneSet outlook as the email provider
icloudNoneSet icloud as the email provider
yahooNoneSet yahoo as the email provider
aolNoneSet aol as the email provider
setProviderStringSet your own email provider
setUserStringSet the email's user
maleNoneUse a random male user
femaleNoneUse a random female user
useDotNoneWill format the email address with a dot in the user field (test.t@gmail.com)
useEmailNoneUse a static email address

If no user was provided, a random one will be selected setEmail and setProvider do not validate the data they receive

email: email().gmail().male().useDot()
// will generate a random email like this: clarence.stanley@gmail.com
0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago