About Me
Hello, my name is Dennis and this is my first blog post as a student at Flatiron School. Having just completed the first of five phases here, I am excited to share something new I have learned along the way. Namely, how to use 'Faker' (and other similar gems), a library for generating fake data.
While working on my phase 1 project (a variation of the snake game), I came across the need for data, and a lot of it. I had a leaderboard that would keep track and display the user's score, but the board was empty-it needed players!
I knew it would be too cumbersome to hard code data for each player object in my db.json file, and too time-consuming to play the game enough times to fill out the leaderboard.
The solution: Faker!
What is Faker?
Faker is a library used to generate fake data which can then be used by a developer in their project.
For instance, here is a code snippet of the faker data I used in my db.json file:
{
"players": [
{
"id": 1,
"userName": "Tiffany.Koss",
"score": 2029
},
{
"id": 2,
"userName": "Zoie63",
"score": 625
},
{
"id": 3,
"userName": "Brandon_Beahan52",
"score": 2013
},
No, "Tiffany Koss" is not a real person and no privacy violations are being committed here.
While the data is fictitious, the developer can choose to make each value unique and eliminate the need to worry about instances of repeat-data. In order to do so, you can prefix your method call like so:
Faker::Name.unique.name
How to Use
While I am going to walk through some of the steps necessary to use Faker, you can navigate to https://github.com/faker-ruby/faker and peruse the README.md for more in-depth information on installation and usage.
Step 1. After first making sure you are in your projects local directory, use a package manager (npm, yarn, pnpm) to install Faker as a Dev Dependency:
npm install @faker-js/faker --save-dev
and then:
npm run dev
Note: This post is for the implementation of Fakerjs. The set-up process for Ruby, Java, and Python is slightly different.
Step 2. Create a generateData.js file within your project directory:
dennis@Dennis's-MacBook-Pro my-project % touch generateData.js
Step 3. Within your generateData.js, retrieve the Faker data by providing the program with a route to '@faker-js/faker':
import { faker } from '@faker-js/faker'
Step 4. Create a function that generates your data. Here is what my function looked like:
const { faker } = require('@faker-js/faker');
function generateData() {
const players = [] //create an empty array for player data
for (let i = 1; i < 201; i++) { // iterate through array
const player = { // structure data within array
id: i,
userName: faker.internet.userName(),
score: faker.datatype.number({ min: 10, max: 3000 })
}
players.push(player)
// fills empty players array with data
}
return {
players // returns populated array
};
}
module.exports = generateData; // exports data to be used in your project (in this case it is exported from the module that houses my leaderboard)
As you can see, the function generateData creates an empty array for player data, iterates through the array, structures the data within the array, and then exports it from the module.
Step 5. Now that you have retrieved your data and wrote a function to structure and export it, you are ready to implement the data within your program. Doing so is simply a matter of writing a function(s) to render the data according to your needs.
For instance, here is how I was able to get my data (player names and scores) to display on my leaderboard:
function getPlayers(){
fetch(baseUrl)
.then(res => res.json())
.then(players => players.sort((playerA, playerB) => {
const playerAScore = Number(playerA.score)
const playerBScore = Number(playerB.score)
return playerBScore - playerAScore
}))
.then(renderRankings)
}
function renderRankings(players){
for(let i = 0; i < players.length; i++){
let ranking = i + 1
renderUserRanking(ranking, players[i])
}
}
This code snippet (used to display player data retrieved from Faker) provides a simple demonstration of how one can make use of their fake data.
Hopefully this blog post was helpful and you now have the knowledge necessary to make use of Faker in your own applications and projects.
For more information on implementing Faker and it's different data types (animals, names, vehicles, etc.) refer to https://fakerjs.dev/api/.
Top comments (0)