Skip to content

Commit 3800516

Browse files
author
Raymond Ottun
committed
feat: new for helper
1 parent a017bad commit 3800516

8 files changed

Lines changed: 273 additions & 2602 deletions

File tree

package-lock.json

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

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"dependencies": {
3131
"@faker-js/faker": "^8.0.2",
3232
"handlebars": "^4.7.7",
33-
"handlebars-helpers": "^0.10.0",
3433
"object-path": "^0.11.8",
3534
"yargs": "^17.7.2"
3635
},
@@ -80,4 +79,4 @@
8079
"src/**/*.test.ts"
8180
]
8281
}
83-
}
82+
}

src/helpers.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ test('repeat', () => {
1414
expect(result).toMatchInlineSnapshot(`"index-0,index-1,index-2"`)
1515
})
1616

17+
test('for', () => {
18+
const result = compile({
19+
template: '{{#for 1 3}} index-{{@index}} {{/for}}'
20+
})
21+
expect(result).toMatchInlineSnapshot(`" index-1 index-2 index-3 "`)
22+
})
23+
1724
test('randomize', () => {
1825
const template =
1926
'My dogs\'s breed is {{randomize "german shepard" "golden retriever" "pug"}}'

src/helpers.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import Handlebars from 'handlebars'
22
import * as allFaker from '@faker-js/faker'
33
import objectPath from 'object-path'
4-
import extraHelpers from 'handlebars-helpers'
54

65
export const createHelpers = ({ locale }: { locale: string }): void => {
7-
extraHelpers()
86
Handlebars.registerHelper('repeat', function (count: number, options: any) {
97
const MAX_COUNT = 100000
108
if (Number.isNaN(count)) {
@@ -56,14 +54,41 @@ export const createHelpers = ({ locale }: { locale: string }): void => {
5654
return `fakerjs does not support ${String(type)}`
5755
})
5856

59-
Handlebars.registerHelper('helperMissing', function (...args: any[]): string {
60-
try {
61-
const [{ name }] = args.concat().reverse()
62-
return `${String(name)} helper is not available`
63-
} catch (error) {
64-
return error.message
57+
Handlebars.registerHelper('for', function (from: any, to: any) {
58+
const options = arguments[arguments.length - 1]
59+
const maxIterations = 100
60+
let output = ''
61+
62+
if (Number.isNaN(parseInt(from, 10)) || Number.isNaN(parseInt(to, 10))) {
63+
throw new Error('Invalid number for for helper')
64+
}
65+
66+
if (to < from) {
67+
throw new Error('Invalid range for for helper')
68+
}
69+
70+
from = parseInt(from, 10)
71+
to = parseInt(to, 10)
72+
73+
if (to - from >= maxIterations) {
74+
to = Number(from) + maxIterations - 1
75+
}
76+
77+
for (let index = from; index <= to; index++) {
78+
output += options.fn(this, { data: { index } })
6579
}
80+
81+
return output
6682
})
6783
}
6884

85+
Handlebars.registerHelper('helperMissing', function (...args: any[]): string {
86+
try {
87+
const [{ name }] = args.concat().reverse()
88+
return `${String(name)} helper is not available`
89+
} catch (error) {
90+
return error.message
91+
}
92+
})
93+
6994
export const registerHelper = Handlebars.registerHelper

web/components/Compile.jsx

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useEffect, useState } from 'react'
22
import Editor from '@monaco-editor/react'
33
import { compile } from '@sayjava/phake-cli/lib/compile'
4+
import { templates } from '../templates'
45

5-
function debounce (func, delay) {
6+
function debounce(func, delay) {
67
let timeoutId
78

89
return function () {
@@ -17,56 +18,58 @@ function debounce (func, delay) {
1718
}
1819

1920
export const Compile = () => {
20-
const initialContent = `
21-
{
22-
"id": {{faker 'number.int' 10}},
23-
"node_id": "{{faker 'string.alpha' 25}}",
24-
"name": "{{faker 'word.noun'}}",
25-
"language": "{{randomize 'javascript' 'ruby' 'golang' 'c++'}}",
26-
"forks_count": {{faker 'number.int' max=10000}},
27-
"size": {{faker 'number.float' max=10000 precision=0.2}},
28-
"default_branch": "{{randomize 'main' 'master'}}",
29-
"open_issues_count": {{faker 'number.int' max=50}},
30-
"is_template": {{randomize false true}},
31-
"description": "{{faker 'lorem.sentences' max=1}}",
32-
"topics": [
33-
{{#repeat 5}}
34-
"{{faker 'string.alpha' 5}}"
35-
{{/repeat}}
36-
]
37-
}
38-
`
3921

22+
const [currTemplate, setCurrTemplate] = useState(templates[0].name)
4023
const [content, setContent] = useState('')
24+
4125
const doCompile = (template) => {
4226
try {
43-
const newContent = JSON.parse(compile({ template }))
44-
setContent(JSON.stringify(newContent, null, 2))
27+
const newContent = compile({ template })
28+
setContent(newContent, null, 2)
4529
} catch (error) {
4630
setContent(error.toString())
4731
}
4832
}
4933

50-
useEffect(() => {
51-
doCompile(initialContent)
52-
}, [])
53-
5434
const handleChange = debounce(doCompile, 500)
35+
const changeTemplate = (e) => {
36+
setCurrTemplate(e.target.value)
37+
doCompile(templates.find((template) => template.name === e.target.value).template)
38+
}
39+
40+
useEffect(() => {
41+
doCompile(
42+
templates.find((template) => template.name === currTemplate).template
43+
)
44+
}, [currTemplate])
5545

5646
return (
5747
<div className='w-full'>
48+
<div className='my-4 flex space-x-4'>
49+
<label className='inline-block' htmlFor="examples">Examples</label>
50+
<select id='examples' className='inline-block'
51+
onChange={(e) => { changeTemplate(e) }}>
52+
{
53+
templates.map((template) => (
54+
<option key={template.name} value={template.name}>{template.label}</option>
55+
))
56+
}
57+
</select>
58+
</div>
59+
60+
5861
<div style={{ display: 'flex' }}>
5962
<Editor
6063
height='70vh' theme='vs-dark'
6164
options={{ minimap: { enabled: false }, readOnly: false }}
62-
language='json'
63-
defaultValue={initialContent}
65+
language={currTemplate}
66+
value={templates.find((template) => template.name === currTemplate).template}
6467
onChange={handleChange}
6568
/>
6669
<Editor
6770
height='70vh' theme='vs-dark'
68-
options={{ minimap: { enabled: false }, readOnly: true }}
69-
language='json'
71+
options={{ minimap: { enabled: false }, readOnly: true, formatOnPaste: true, formatOnType: true, renderControlCharacters: false, renderWhitespace: 'none', renderFinalNewline: true, renderIndentGuides: true, renderLineHighlight: 'none', renderLineNumbers: 'off', renderValidationDecorations: 'off', scrollBeyondLastLine: false, wordWrap: 'on' }}
72+
language={currTemplate}
7073
value={content}
7174
/>
7275
</div>

web/pages/editor.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
---
2+
title: Mock data generator
3+
description: Effortlessly Generate Realistic Mock JSON, CSV, YAML, SQL Data with Ease using Handlebars and Fakerjs
4+
---
5+
16
import { Compile } from '../components/Compile'
27

3-
<h1 className="w-full text-center text-4xl my-4 text-gray-600">JSON Live Data Generator</h1>
8+
<h1 className="w-full text-center text-4xl my-4 text-gray-600">Mock Data Generator</h1>
49

510
<Compile />

web/pages/index.mdx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
---
22
title: Mock data generator
3-
description: Effortlessly Generate Realistic Mock Data with Ease using Handlebars and Fakerjs
3+
description: Effortlessly Generate Realistic Mock JSON, CSV, YAML, SQL Data with Ease using Handlebars and Fakerjs
44
---
55

6-
import { Compile } from '../components/Compile.jsx'
7-
import { Bleed } from 'nextra-theme-docs'
8-
96
<h1 className="text-8xl tracking-tighter font-extrabold mt-8">Phake</h1>
107
<p className="text-2xl my-4 text-gray-600">Effortlessly Generate Realistic Mock Data with Ease using Handlebars and Fakerjs</p>
118
<a href="/editor" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-4 px-6 rounded-full mt-4 inline-block">Try it online →</a>

web/templates.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
export const templates = [
2+
{
3+
name: 'json',
4+
label: 'JSON',
5+
template: `
6+
{
7+
"id": {{faker 'number.int' 10}},
8+
"node_id": "{{faker 'string.alpha' 25}}",
9+
"name": "{{faker 'word.noun'}}",
10+
"language": "{{randomize 'javascript' 'ruby' 'golang' 'c++'}}",
11+
"forks_count": {{faker 'number.int' max=10000}},
12+
"size": {{faker 'number.float' max=10000 precision=0.2}},
13+
"default_branch": "{{randomize 'main' 'master'}}",
14+
"open_issues_count": {{faker 'number.int' max=50}},
15+
"is_template": {{randomize false true}},
16+
"description": "{{faker 'lorem.sentences' max=1}}",
17+
"topics": [
18+
{{#repeat 5}}
19+
"{{faker 'string.alpha' 5}}"
20+
{{/repeat}}
21+
]
22+
}
23+
`,
24+
},
25+
{
26+
name: 'csv',
27+
label: 'CSV',
28+
template: `firstName,lastName,email
29+
{{#for 1 10}}
30+
{{faker 'person.firstName'}},{{faker 'person.lastName'}},{{faker 'internet.email'}}
31+
{{/for}}
32+
`
33+
},
34+
{
35+
name: 'xml',
36+
label: 'XML',
37+
template: `
38+
<root>
39+
<id>{{faker 'number.int' 10}}</id>
40+
<node_id>{{faker 'string.alpha' 25}}</node_id>
41+
<name>{{faker 'word.noun'}}</name>
42+
<language>{{randomize 'javascript' 'ruby' 'golang' 'c++'}}</language>
43+
<forks_count>{{faker 'number.int' max=10000}}</forks_count>
44+
<size>{{faker 'number.float' max=10000 precision=0.2}}</size>
45+
<default_branch>{{randomize 'main' 'master'}}</default_branch>
46+
<open_issues_count>{{faker 'number.int' max=50}}</open_issues_count>
47+
<is_template>{{randomize false true}}</is_template>
48+
<description>{{faker 'lorem.sentences' max=1}}</description>
49+
<topics>
50+
{{#for 1 5}}
51+
<topic>{{faker 'string.alpha' 5}}</topic>
52+
{{/for}}
53+
</topics>
54+
</root>
55+
`
56+
},
57+
{
58+
name: 'yaml',
59+
label: 'YAML',
60+
template: `id: {{faker 'number.int' 10}}
61+
node_id: {{faker 'string.alpha' 25}}
62+
name: {{faker 'word.noun'}}
63+
language: {{randomize 'javascript' 'ruby' 'golang' 'c++'}}
64+
forks_count: {{faker 'number.int' max=10000}}
65+
size: {{faker 'number.float' max=10000 precision=0.2}}
66+
`
67+
},
68+
{
69+
name: 'sql',
70+
label: 'SQL',
71+
template: `
72+
INSERT INTO users (id, node_id, name, language, forks_count, size, default_branch, open_issues_count)
73+
{{#for 1 10}}
74+
VALUES ({{faker 'number.int' 10}},
75+
"{{faker 'string.alpha' 25}}",
76+
"{{faker 'word.noun'}}",
77+
"{{randomize 'javascript' 'ruby' 'golang' 'c++'}}",
78+
{{faker 'number.int' max=10000}},
79+
{{faker 'number.float' max=10000 precision=0.2}},
80+
"{{randomize 'main' 'master'}}",
81+
{{faker 'number.int' max=50}}
82+
)
83+
{{/for}}
84+
`
85+
}
86+
]

0 commit comments

Comments
 (0)