Like the name implies, gen-data
is a library for generating data, mostly in prototyping and testing scenarios.
$ npm install -D gen-data
Or
$ yarn add --dev gen-data
import gen from 'gen-data';
// Scalars evaluate to themselves
gen(5) // => 5
gen(-98) // => -98
gen("hello") // => "hello"
gen(false) // => false
gen(true) // => true
// Collections are iterated over recursively
gen([
{a: gen.random.number},
{b: gen.random.string}
])
// => [ { a: 39764 }, { b: 'rSI5X0Vwf8O3Cc_b' } ]
// Generators are simply functions
let _id = 1
const incrementingId = () => _id++
const animal = () => ['cat', 'dog', 'hamster'][gen.random.number(2)]
const pet = {
name: gen.person.firstName,
type: animal
}
let users = []
for (let i = 0, user; i < 3; i++) {
user = gen({
id: incrementingId,
username: gen.internet.username,
profileImage: gen.internet.avatar,
pets: [pet, pet]
})
users.push(user)
}
// Results in
[
{
"id": 4,
"username": "Giles.Vandervort50",
"profileImage": "https://s3.amazonaws.com/uifaces/faces/twitter/renbyrd/128.jpg",
"pets": [
{
"name": "Cristina",
"type": "dog"
},
{
"name": "Wanda",
"type": "cat"
}
]
},
{
"id": 5,
"username": "Nellie5",
"profileImage": "https://s3.amazonaws.com/uifaces/faces/twitter/gabrielizalo/128.jpg",
"pets": [
{
"name": "Dariana",
"type": "hamster"
},
{
"name": "Kristopher",
"type": "dog"
}
]
},
{
"id": 6,
"username": "Paolo.Corkery10",
"profileImage": "https://s3.amazonaws.com/uifaces/faces/twitter/mtolokonnikov/128.jpg",
"pets": [
{
"name": "Hailee",
"type": "cat"
},
{
"name": "Cody",
"type": "hamster"
}
]
}
]
gen-data
wraps faker to provide some useful default generators. Note that the full faker
api is not exactly mirrored, it's a rough approximation. You can use faker directly if you need any of its generators. The following API covers quite a bit of what one might need and creating more is simply a matter of defining new functions -- generators are simply functions after all.
Takes a spec and produces data and generators which are used to describe the data that you want to generate. Generators are can be simple types (Number
, String
, Boolean
, null
, and undefined
) which simply evaluate to themselves, collections (arrays and objects) which are iterated over recursively evaluating any nested generators or they are functions which can do a number of operations before returning a value. Essentially, you just pass the shape of what you want your data to look like to gen()
and it takes cares of giving you something usable.
Generates a random address object.
gen.address()
/* =>
{ street: '588 Blick Parkways',
street2: 'Apt. 303',
city: 'Port Enolamouth',
state: 'IA',
zipCode: '40317-5991' }
*/
Generates a random city.
gen.address.city() // => 'East Danikaland'
Returns the name of a randomly selected country.
gen.address.country() // => 'Cote d\'Ivoire'
Generates a random country code.
gen.address.countryCode() // => 'TO'
Generates a random country.
gen.address.county() // => 'Buckinghamshire'
Generates a random latitude (a value ranging from -90 to 90).
gen.address.latitude() // => '48.9096'
Generates a random longitude (a value ranging from -90 to 90).
gen.address.longitude() // => '11.8738'
gen.address.longitude() // => '-163.2458'
Generates a random secondary street address.
gen.address.secondaryAddress() // => 'Suite 264'
Generates the name of a random US state.
gen.address.state() // => 'Georgia'
gen.address.state() // => 'Minnesota'
Generates a random US state in abbreviated form.
gen.address.stateAbbr() // => 'AZ'
gen.address.stateAbbr() // => 'FL'
Generates a random primary street address.
gen.address.street() // => '29878 Ward Stream'
Generates a random secondary street address.
gen.address.street2() // => 'Apt. 601'
Generates the name of a street.
gen.address.streetName() // => 'Felton Ways'
Generates a random street address.
gen.address.streetAddress() // => '5769 Friesen Cape'
Generates a random 5 or 9 digit zip code.
gen.address.zipCode() // => '08719-0159'
gen.address.zipCode() // => '50396'
Generates a random date.
gen.date() // => 2016-09-10T08:07:53.405Z
Generates a date between the two given dates.
const minDate = new Date('1970-01-01')
const maxDate = new Date('1975-12-31')
gen.date.between(minDate, maxDate) // => 1974-05-28T04:47:25.736Z
gen.date.between(minDate, maxDate) // => 1971-05-26T18:10:40.182Z
Generates a date in the future. I.e., a date greater than Date.now()
.
new Date() // => 2016-10-17T01:12:42.830Z
gen.date.future() // => 2017-05-02T05:00:31.965Z
Generates the name of a random month.
gen.date.month() // => 'October'
gen.date.month() // => 'September'
Generates a date in the past. I.e, a date less than Date.now()
.
new Date() // => 2016-10-17T01:14:49.804Z
gen.date.past() // => 2016-04-16T23:25:53.662Z
Generates a date within 24 hours of the current time.
new Date() // => 2016-10-17T01:16:14.117Z
gen.date.recent() // => 2016-10-16T02:08:37.091Z
gen.date.recent() // => 2016-10-16T17:05:13.610Z
Generates the name of a random weekday.
gen.date.weekday() // => 'Wednesday'
gen.date.weekday() // => 'Friday'
Generates a URL for an avatar.
gen.internet.avatar() // => 'https://s3.amazonaws.com/uifaces/faces/twitter/stephcoue/128.jpg'
Generates a random domain name.
gen.internet.domain() // => 'barbara.net'
Generates a valid email address based on optional input criteria.
All the arguments are optional, you can for instance pass in only the first name and provider, letting the generator pick a random last name:
gen.internet.email('Fernando', undefined, 'example.com')
// => 'Fernando_Hills56@example.com'
Generates a random lorempixel url.
gen.internet.image() // => 'http://lorempixel.com/640/480'
Generates a random IPv4 address.
gen.internet.ip() // => '19.204.38.170'
Generates a random mac address.
gen.internet.mac() // => 'e9:f8:e4:32:4f:01'
Generates a random password. When memorable
if true
, it interleaves vowels between consonants.
gen.internet.password() // => '_ugp1jGGHuJbjcg'
gen.internet.password(undefined, true) // => 'gakunujevotubus'
Randomly generates the strings 'http'
or 'https'
.
gen.internet.protocol() // => 'https'
Generates a random domain suffix.
gen.internet.tld() // => 'net'
Generates a random URL. The URL could be secure or insecure.
gen.internet.url() // => 'https://shania.org'
Generates a random user agent.
gen.internet.userAgent()
// => 'Mozilla/5.0 (Windows; U; Windows NT 6.1) AppleWebKit/534.2.2 (KHTML, like Gecko) Chrome/14.0.801.0 Safari/534.2.2'
Generates a username based on one of several patterns. The pattern is chosen randomly.
gen.internet.userName() // => 'Jodie85'
Randomly generates a word, phrase, sentence or paragraph.
gen.lorem()
'Et eaque mollitia reiciendis omnis.\nQuia voluptas fuga voluptas blanditiis.\nIllum voluptas consequatur maiores nemo veritatis eius.\nIste fugiat porro et.'
gen.lorem()
'accusamus enim rem'
Generates one or more random sentences each separated by a newline (\n
) character.
gen.lorem.lines()
'Excepturi maiores quae.\nQuis facilis excepturi asperiores qui adipisci.\nUt quia voluptas quisquam adipisci et eligendi velit odio.'
Generates a random paragraph of text.
gen.lorem.paragraph()
'Aut sint id dolores. Vero totam cupiditate repellendus quia. Ducimus sequi suscipit natus illo eum alias quos eligendi.'
Generates random multiple paragraphs of text separated by \n \r
.
gen.lorem.paragraphs()
'Atque quis aut rerum totam commodi consequuntur. Dolor est eaque. Tempore nobis officia. Non culpa sit vel magnam voluptatem deserunt aut.\n \rRerum explicabo consectetur maxime voluptatem neque illo hic reiciendis cum. Ut aliquam aliquam non explicabo. Aut est corporis et quia occaecati sit pariatur sint iste. Et dolorum eum velit est.\n \rQuia dolores asperiores. Quia qui eius quisquam voluptates praesentium consequuntur. A distinctio veritatis commodi distinctio magni quae sint. Reprehenderit ullam dolorem enim quam et dolor quas et. Est aut non tempora.'
Generates a random sentence.
gen.lorem.sentence()
'Enim commodi voluptas ratione velit dolores nihil eaque quia.'
Generates multiple sentences. Effectively equal to gen.lorem.paragraph
.
gen.lorem.sentences()
'Maxime soluta quae nemo. Et et cum ipsa ut recusandae dicta rerum. Saepe aut omnis quam. Et ut quo voluptatum minus ut assumenda vitae. Omnis itaque omnis est quisquam.'
Randomly generates a word, phrase, sentence or paragraph.
gen.lorem.text()
'Voluptatem minima harum illo pariatur modi non ut.\nAut dolorem consequuntur et enim dolor.\nPraesentium nihil incidunt nemo voluptatem.'
Generates a random word.
gen.lorem.word()
'natus'
Generates a random string of words.
gen.lorem.words()
'excepturi consectetur et'
Generates a generic person
object.
gen.person()
{ username: 'Savannah4',
firstname: 'Maudie',
lastname: 'Runolfsdottir' }
gen.person.firstName() : String
Generates a random first name.
gen.person.firstName() // => 'Kiara'
Generates a random full name (first and last).
gen.person.fullName() // => 'Pinkie Ullrich'
Generates a random last name.
gen.person.lastName() // => 'Monahan'
Generates a random name prefix.
gen.person.namePrefix() // => 'Dr.'
Generates a random name suffix.
gen.person.nameSuffix() // => 'Jr.'
Generates the a random title for a person.
gen.person.title() // => 'Dynamic Assurance Executive'
Generates a username based on one of several patterns. The pattern is chosen randomly.
gen.person.userName() // => 'Dayne1'
Generates a random phone number. Takes an optional format which defaults to '(###)-###-####'
-- #
characters are replaced with numbers in the format string.
gen.phone() // => '(320)-710-6369'
gen.phone('1(800)-###-####') // => '1(800)-720-6242'
gen.phone('+44 #### ### #####') // => '+44 2270 397 29959'
Generates a random value from the generators under the gen.random
module.
gen.random() // => '8d46a487-ff8e-48d9-a32e-ce8fb3e7f56d', uuid
gen.random() // => 'pt_BR', locale
gen.random() // => true, boolean
Generates a boolean value.
gen.random.boolean() // => true
gen.random.boolean() // => false
Generates a random color in hex form.
gen.random.hexColor() // => '#364e07'
Generates a random locale
gen.random.locale() // => 'fr_CA'
Generates a random integer up to an optional max.
gen.random.string(length=15) : String
Generates a random string. Takes an optional length
argument which defaults to 15
.
gen.random.string() // => 'rb7WUna7kOKCHXSw'
gen.random.string(3) // => 'P0a'
gen.random.string(20) // => 'QloEmpKriDgYCcJqZyzH'
Generates a random UUID
.
gen.random.uuid() // => '6bb1c37c-33a5-4398-a9b6-dcb47c04f90e'
Generates a random, yet common, file extension.
gen.system.commonFileExt() // => 'html'
Generates a random, yet common, file name.
gen.system.commonFileName() // => 'fresh.mpeg'
gen.system.commonFileName() // => 'parse.png'
Generates a random, yet common, file type.
gen.system.commonFileType() // => 'audio'
gen.system.commonFileType() // => 'application'
Generates a random file extension.
gen.system.fileExt() // => 'clkt'
gen.system.fileExt() // => 'pdb'
Generates a random file name.
gen.system.fileName() // => 'generic.mk3d'
gen.system.fileName() // => 'maximized_reciprocal.xsl'
Generates a random file type.
gen.system.fileType() // => 'video'
gen.system.fileType() // => 'chemical'
Generates a random mime type.
gen.system.mimeType() // => 'application/vnd.xfdl'
gen.system.mimeType() // => 'audio/vmr-wb'
Generates a random semver string.
gen.system.semver() // => '0.2.6'
The MIT License (MIT)
Copyright © 2016 Seth Bonnie
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.