Skip to content

Commit c089ed3

Browse files
committed
fake-file-generator
1 parent a3d0341 commit c089ed3

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
files/*
2+
node_modules/

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
# Linnia JSON File Generator
1+
# Linnia JSON File Generator
2+
3+
Welcome to the fake file generator! The Linia Team threw this together so you
4+
could spend less time making fake data and more time building! In order to get
5+
this working for you, you should change three things:
6+
7+
1. Change `numberOfFiles` to equal the amount of files you want to generate
8+
2. Change the `fileSchema` to make that of the data you want to produce.
9+
3. Change the `fileName` function to product the name for the file that you want.
10+
11+
To get started, head over to `index.js` and follow the steps provided in the comments.

index.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const fs = require('fs');
2+
const faker = require('faker');
3+
4+
/*
5+
Welcome to the fake file generator! The Linia Team threw this together so you
6+
could spend less time making fake data and more time building! In order to get
7+
this working for you, you should change three things:
8+
9+
1. Change `numberOfFiles` to equal the amount of files you want to generate
10+
11+
2. Change the `fileSchema` to make that of the data you want to produce. More explantion
12+
on how to do that in the comment above it.
13+
14+
3. Change the fileName function to product the name for the file that you want.
15+
*/
16+
17+
/*
18+
The number of files that the script will generator. Pretty self explanatory.
19+
*/
20+
21+
const numberOfFiles = 10;
22+
23+
/*
24+
The schema for the files you want to generate. Keys added to the object will be
25+
the name of the property that appears in the JSON, and the value will be a function
26+
that produces the fake data.
27+
28+
For instance:
29+
30+
const fileSchema = {
31+
name: () => 'Sammy'
32+
}
33+
34+
Will set translate to:
35+
36+
{
37+
"name": "Sammy"
38+
}
39+
40+
The included `faker` library provides great methods to produce fake data. Documentation
41+
can be found here: https://github.com/marak/Faker.js
42+
*/
43+
44+
const fileSchema = {
45+
// will provide a fake name provided by the library
46+
name: faker.name.findName,
47+
// will increment age by 1 per file
48+
age: (() => {
49+
let age = 0;
50+
return () => {
51+
age++;
52+
return age;
53+
};
54+
})(),
55+
// will always set favoritePet to cats
56+
favoritePet: () => 'cats'
57+
};
58+
59+
/*
60+
A function that produces the name for each file. It will receive the generated data
61+
for the file as an argument
62+
*/
63+
64+
const generateFileName = (fileData) => fileData.name;
65+
66+
/*
67+
***********************************************************
68+
YOU PROBABLY DONT NEED TO CHANGE ANYTHING BELOW THESE LINES
69+
***********************************************************
70+
*/
71+
72+
const generateFileData = () => {
73+
const fileData = {};
74+
75+
Object.entries(fileSchema).forEach((pair) => {
76+
const [ key, value ] = pair;
77+
fileData[key] = value();
78+
});
79+
80+
return fileData;
81+
};
82+
83+
const generateFiles = async () => {
84+
for (let i = 0; i < numberOfFiles; i++) {
85+
const fileData = generateFileData();
86+
const fileName = generateFileName(fileData);
87+
const json = JSON.stringify(fileData);
88+
fs.writeFileSync(`./files/${fileName}.json`, json);
89+
}
90+
};
91+
92+
generateFiles();
93+

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "linnia-json-files-generator",
3+
"version": "1.0.0",
4+
"description": "A library to generator dummy json files",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Max Blaushild",
10+
"license": "ISC",
11+
"dependencies": {
12+
"faker": "^4.1.0"
13+
}
14+
}

0 commit comments

Comments
 (0)