The Fake Name, Fake EMail, Fake Everything Content Generator - Fakerjs

fake content generatorAn important part of my front-end development workflow is working against a data source. The problem I encounter is the availability of reliable test data. You don't want to create the application against live, production data, which means you need a test data source and fake data.

Over the years I have struggled to find a solid tool to generate fake data like names, emails, phone numbers, credit card numbers and product information. I have spent hours writing my own logic to randomly generate random characters for content. But that recently changed when I found fakerjs.

I have several demonstration progressive web apps, but Fast Furniture is my primary demonstration app. I hastily wrote the first version a week before a major conference presentation. Most of the time I invested in the application was creatign fake data. At the time I was still using ASP.NET and C#, but did not find a good tool for fake data (since then I have found several). So I just used gibberish strings.

fast-furniture-bad-fake-data

Recently I decided it was time to rebuild the data behind my Fast Furniture site and wanted to script the data creation using a node module, Fakerjs.

Faker includes a battery of data modules and methods to create large amounts of valid data, not just random gibberish. It is also helpful because it generates data in desired formats, saving you time crafting logic to create common data types like emails, phone numbers and accounts. It can also create locatoin specific data like country specific phone numbers.

Fake Name Generator

One of the most popular fake data types are names. Just about every object in any application's data structure includes a name property. fakerjs provides several ways to generate fake names.

Some of the common fake name types fakerjs can generate:

  • Person Name
  • Product Name
  • Company Name
  • Street Name
  • Account Name
  • User Name

Generating a Fake Person's Name

The faker library has an array of functions to help not only manage a person's first and last names, but also their job. You can create just a first name, a last name or a combination of both.

 let firstName = faker.name.firstName(); let lastName = faker.name.lastName(); let fullName = faker.name.findName(); //returns a full name 

You can also add a suffix or prefix to the name:

 let prefix = faker.name.prefix(); let suffix = faker.name.suffix();  

You can also generate job related content with the following faker.name methods:

  • jobTitle or title
  • jobDescriptor
  • jobArea
  • jobType

With these methods you can generate not only a fake person's name, but you can also give them a fake job.

The real question do you have to file fake taxes for the fake person?

Fake EMail Address Generator

After you create a fake person and give them a fake job you should create a fake email address. Faker has two methods to create fake email address values.

Note: these are not real email addresses, just placeholder text of a valid email address.

The faker.internet module has two e-mail generation methods:

  • exampleEmail
  • email

The difference between the two methods is simple: exampleEmail creates a fake account for the example.org, example.net or example.com domain. The email method creates a fake address for common public email services like gmail, yahoo or hotmail.

 console.log(faker.internet.exampleEmail()); console.log(faker.internet.email()); /* Laurel26@example.net Brisa27@hotmail.com */ 

Pretty nice it can generate random fake email address values. But when you are creating a fake person you probably want to sync the email address with the name you generated in the previous section.

Faker lets you supply a first and last name and will use those values to generate the email address:

 console.log(faker.internet.exampleEmail(faker.name.firstName(), faker.name.lastName())); console.log(faker.internet.email(faker.name.firstName(), faker.name.lastName())); /* Brooklyn*Senger@example.org Una*Breitenberg87@gmail.com */ 

In addition to generating fake email address values the internet module can generate values using the following methods:

  • avatar
  • userName
  • protocol
  • url
  • domainName
  • domainSuffix
  • domainWord
  • ip
  • ipv6
  • userAgent
  • color
  • mac
  • password

Fake Address Generator

Now that our fake person has a name, a job and even an email address you can generate a fake address. Addresses come in many forms, which is why the faker.address module includes many methods to create a custom combination of values.

  • zipCode
  • city
  • cityPrefix
  • citySuffix
  • streetName
  • streetAddress
  • streetSuffix
  • streetPrefix
  • secondaryAddress
  • county
  • country
  • countryCode
  • state
  • stateAbbr
  • latitude
  • longitude

Fake ID Generator

Data records need unique identifiers. UUIDs are a common way to generate a truly random value and faker has a random UUID generator.

 console.log(faker.random.uuid()); // 947a710a-7e7d-4c08-8002-9f40a894455c 

There are other random value generation methods:

  • number
  • arrayElement
  • objectElement
  • uuid
  • boolean
  • word
  • words
  • image
  • locale
  • alphaNumeric
  • hexaDecimal

I find the random number generator very helpful. Of course I could use the built in JavaScript Math.random method, but it only generates a random value between 0 and 1. The faker random number method allows you to control the min and max range.

 faker.random.number({ min: 1, max: 5 }) 

Fake Phone Number Generator

Our fake person is really starting to take shape. But they need a phone number and faker has what you need.

 console.log(faker.phone.phoneNumber()); console.log(faker.phone.phoneNumberFormat()); /* (941) 596-0155 x7443 555-886-2188 */ 

The default location is the US, but you can change the local to generate a country specific phone number.

 faker.locale = "de"; console.log(faker.phone.phoneNumber()); console.log(faker.phone.phoneNumberFormat()); /* +49-3662-68807167 (0076) 787683108 */

More on localization later.

Fake Text Generator

While faker has all sorts of great methods to generate specific data types, but sometimes you just need filler text. There is an entire array of text helper methods in the lorem module:

  • word
  • words
  • sentence
  • slug
  • sentences
  • paragraph
  • paragraphs
  • text
  • lines

You should note the module is called lorem, which means it does not generate English but Latin, the default Lorem Ipsum language. If you are not familiar with Lorem Ipsum it is how we have filled text for design purposes for years. There are many popular variations of Ipsum available, my favorite is bacon ipsum!

 console.log(faker.lorem.paragraphs()); /* Doloribus ullam est reiciendis vel. Nihil dolores quia soluta et ad molestias. Eaque veniam hic numquam odio sit mollitia voluptatem. Nostrum recusandae quis et dolorum. Itaque porro quia et nihil. Facilis dicta ex voluptatibus. Aut accusamus pariatur ut quisquam quisquam. Excepturi qui voluptas veniam. Ea laudantium iure magnam fuga cum. Omnis vitae molestiae qui ducimus laboriosam. In dolorum debitis perferendis. Natus dolorum maiores eos. Et possimus sed reprehenderit odio harum eum omnis. Eligendi repellat odit quidem sint cupiditate atque dolores quo eligendi. Sit nemo suscipit eos quos rerum cupiditate enim. */ 

The other lorem methods create the desired content type filler text. You are not limited to just a random number of paragraphs, you can generate just words if you want.

Localization Setting

English is the default fakerjs language, but you can change the language by setting the locale:

 // sets locale to de faker.setLocale("es"); // or faker.locale = "es"; 

Faker supports a range of languages:

  • az
  • cz
  • de
  • de_AT
  • de_CH
  • en
  • en_AU
  • en_BORK
  • en_CA
  • en_GB
  • en_IE
  • en_IND
  • en_US
  • en*au*ocker
  • es
  • es_MX
  • fa
  • fr
  • fr_CA
  • ge
  • id_ID
  • it
  • ja
  • ko
  • nb_NO
  • nep
  • nl
  • pl
  • pt_BR
  • ru
  • sk
  • sv
  • tr
  • uk
  • vi
  • zh_CN
  • zh_TW

By default all locals are loaded, which can be fine when using a node modules. If you were to load faker in the client you want a leaner instance. To help you can incrementally load desired locals only.

 // loads only es locale var faker = require('faker/locale/es'); 

Summary

I have building a variety of applicationa lately. Most do not have an API prepared for me to build the UI, at least initially. Fakerjs has been a valuable part of my development workflow, allowing me to focus on the front-end application.

I have a pair of follow up articles in the works to demonstrate how to utilize fakerjs in the Fast Furniture demonstration app. There you will see how to form real, fake, objects that can be used to drive your front-end. Albeit in a test or development environment.

Fake data generation utilities like Fakerjs are also helpful when you need to create application test suites, both unit and integration tests. But mostly I enjoy Fakerjs because it frees me from worrying about the state of the back-end. As long as you know the application's data model you can create a script to reliably generate the data you need to make the front-end.

Share This Article With Your Friends!

We use cookies to give you the best experience possible. By continuing, we'll assume you're cool with our cookie policy.

Install Love2Dev for quick, easy access from your homescreen or start menu.

Googles Ads Bing Pixel LinkedIn Pixel