Effortlessly Generate Realistic Mock Data with Ease using Handlebars and Fakerjs
- Utilize the power of declarative templates using Handlebars.
- Seamlessly integrate with Faker.js for generating realistic and randomized data.
- Enjoy the simplicity of a command-line interface for streamlined usage.
- Incorporate the tool into your development process and easily version your templates.
- Promote code reuse and maintainability by creating DRY (Don't Repeat Yourself) templates, allowing you to reuse template sections across multiple templates.
- Leverage custom Handlebars helpers to simplify data creation and manipulation, further enhancing the flexibility and functionality of your templates.
Create a template file named sample.json.hbs
with the following content
{
"id": {{faker 'number.int' 10}},
"node_id": "{{faker 'string.alpha' 25}}",
"name": "{{faker 'word.noun'}}",
"language": "{{randomize 'javascript' 'ruby' 'golang' 'c++'}}",
"forks_count": {{faker 'number.int' max=10000}},
"size": {{faker 'number.float' max=10000 precision=0.2}},
"default_branch": "{{randomize 'main' 'master'}}",
"open_issues_count": {{faker 'number.int' max=50}},
"is_template": {{randomize false true}},
"description": "{{faker 'lorem.sentences' max=3}}",
"topics": [
{{#repeat 5}}
"{{faker 'string.alpha' 5}}"
{{/repeat}}
]
}
Run the phake-cli
command to compile the template into user.json
npx @sayjava/phake-cli compile -t sample.json.hbs
{
"id": 7,
"node_id": "HSUWMxwXsVKxycdaVOiJFnlNe",
"name": "factory",
"language": "javascript",
"forks_count": 4558,
"size": 8800,
"default_branch": "master",
"open_issues_count": 9,
"is_template": true,
"description": "Aliquam sunt eveniet quam",
"topics": [
"UiqiJ",
"GdyVl",
"LUiDO",
"QEHha",
"Ksvjv"
]
}
The Phake CLI uses Handlebars and FakerJS to generate sample data for development purposes. It allows you to define data templates using Handlebars syntax and generate realistic sample data based on those templates, incorporating fake data generated by FakerJS.
npm i -g @sayjava/phake-cli
Flag | Default | Description |
---|---|---|
-t, --template | - | file/directory containing .hbs files |
-o, --output | cwd | The directory to write the output files |
Improve the functionality of the phake
CLI with the following enhancements:
- The
phake
CLI now intelligently uses file extensions to name the output files that are written to the output directory. - If no file extension is supplied, the output file will still be created but without any file extension.
- This flexible approach allows you to specify the desired file extension for the output, providing compatibility with various file formats and ensuring clarity in file naming.
- You can now generate output files with the appropriate extensions, making it easier to identify and work with the generated files.
- This enhancement adds versatility to the
phake
CLI, catering to a wider range of use cases and preferences.
Examples
Input | Output |
---|---|
sample.json.hbs | sample.json |
sample.yaml.hbs | sample.yaml |
sample.hbs | sample |
phake compile -t name-of-template.hbs
Compile a folder containing .hbs
files
phake -t dir/containing-hbs-files -o folder/to/write/output
Compile a single [name].hbs
file
phake -t dir/containing/single.hbs -o folder/to/write/output
Compile a single [name].json.hbs
file with an explicit file extension. This
will create [name].json
phake -t dir/containing/single.json.hbs -o folder/to/write/output
- Enable easy reuse of template sections by naming them as
[name].partial.hbs
. - Partial files are not compiled individually but instead registered as Handlebar partials. This allows them to be referenced and included within other template files seamlessly.
- By adopting this approach, you can efficiently organize and manage your templates, promoting modular and reusable code.
- The registration of partials ensures that changes made to a partial file reflect automatically across all templates that reference it.
- This method of partial registration enhances maintainability and reduces duplication of code within your project.
Example
The partial can then be referenced from other template files
phake
ships with some handy helpers that eases data generation. All
Handlebar Helpers are
also supported.
Helper | Description | Example | options |
---|---|---|---|
repeat | Generates a comma separated list for creating arrays | [{{#repeat 5}} index-{{@index}} {{/repeat}}] |
number Iteration count |
for | Repeat items for the specified range | {{#for 1 5}} index-{{@index}} {{/for}} |
number Iteration count |
randomize | Randomize an array | {{randomize "german shepard" "golden retriever" "pug"}} |
values to be randomized |
var | Register a variable to be used later | {{#var 'myVar' 5 }} then accessed later as {{myVar}} |
name of the variable, value of the variable |
imageURI | Generate a URI encoded random patterned image using a seed string | {{imageURI 'seedString' }} |
string to use to seed the generated pattern, color to use for the pattern |
imageURL | Generate a URL encoded random patterned image using a seed string | {{imageURL 'seedString' #00FF00 }} |
string to use to seed the generated pattern, color to use for the pattern |
imageSVG | Generate a SVG random patterned image using a seed string | {{imageSVG 'seedString' }} |
string to use to seed the generated pattern, color to use for the pattern |
imageBase64 | Generate a base64 encoded random patterned image | {{imageBase64 'seedString' }} |
string to use to seed the generated pattern, color to use for the pattern |