Skip to content

Commit 636e690

Browse files
authored
Merge pull request #3 from tzellman/chore/random-helper
chore: add random helper
2 parents 3b30d34 + 2b6b3ad commit 636e690

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ ember install ember-fakerjs
2727

2828
### Helpers
2929

30-
The list of helpers available are:
31-
- `faker-fake` use this to generate content by directly using the `faker` API
32-
- `random` utility that provides parameterized access to the `random` subsection of the `faker` API
33-
- `faker-text`, `faker-sentence`, `faker-sentences` - generate lorem content
30+
The main helper is `faker-fake`. You can use this to generate content using the entire faker API.
31+
32+
The addon ships with the following helpers: `faker-fake`, `faker-sentence`, `faker-sentences`, `faker-text`.
3433

3534
Example usage:
3635

3736
```hbs
38-
{{random "number" max=100}}
39-
{{random "arrayelement" array("Link" "Ganon" "Zelda")}}
4037
{{faker-sentence 10}} {{!-- create a 10-word sentence --}}
4138
{{faker-sentences 5}} {{!-- create a 5-sentence string --}}
4239
<img src={{faker-fake "{{image.avatar}}"}} alt="avatar">

addon/helpers/random.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { helper } from '@ember/component/helper';
2+
import faker from 'faker';
3+
4+
export default helper(function random(params, hash) {
5+
const method = params.length > 0 ? params[0].toLowerCase() : undefined;
6+
const options = { ...(hash ?? {}) };
7+
switch (method) {
8+
case 'number':
9+
return faker.datatype.number(options);
10+
case 'float':
11+
return faker.datatype.float(options);
12+
case 'arrayelement':
13+
return faker.random.arrayElement(params.length > 1 ? params[1] : undefined);
14+
case 'arrayelements':
15+
return faker.random.arrayElements(params.length > 1 ? params[1] : undefined, options?.count);
16+
case 'objectelement':
17+
return faker.random.objectElement(params.length > 1 ? params[1] : undefined, options?.field);
18+
case 'uuid':
19+
return faker.datatype.uuid();
20+
case 'boolean':
21+
return faker.datatype.boolean();
22+
case 'word':
23+
return faker.random.word(options?.type);
24+
case 'words':
25+
return faker.random.words(options?.count);
26+
case 'image':
27+
return faker.random.image();
28+
case 'locale':
29+
return faker.random.locale();
30+
case 'alpha':
31+
return faker.random.alpha(options);
32+
case 'alphanumeric':
33+
return faker.random.alphaNumeric(options?.count, { bannedChars: options?.bannedChars });
34+
case 'hexadecimal':
35+
return faker.datatype.hexaDecimal(options);
36+
}
37+
return undefined;
38+
});

app/helpers/random.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-fakerjs/helpers/random';
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { render } from '@ember/test-helpers';
4+
import { hbs } from 'ember-cli-htmlbars';
5+
6+
module('Integration | Helper | random', function (hooks) {
7+
setupRenderingTest(hooks);
8+
9+
test('all options render with defaults', async function (assert) {
10+
const methods = [
11+
'number',
12+
'float',
13+
'arrayElement',
14+
'arrayElements',
15+
'objectElement',
16+
'uuid',
17+
'boolean',
18+
'word',
19+
'words',
20+
'image',
21+
'locale',
22+
'alpha',
23+
'alphaNumeric',
24+
'hexaDecimal'
25+
];
26+
for (const method of methods) {
27+
this.set('method', method);
28+
await render(hbs`{{random method}}`);
29+
assert.ok(this.element.textContent.trim(), 'renders content');
30+
}
31+
});
32+
33+
test('it renders specific values', async function (assert) {
34+
await render(hbs`{{random "arrayelement" (array 1 2 3)}}`);
35+
assert.ok(/[1-3]/.test(this.element.textContent.trim()), 'arrayelement');
36+
await render(hbs`{{random "notsupported"}}`);
37+
assert.notOk(this.element.textContent.trim(), 'should be empty');
38+
});
39+
});

0 commit comments

Comments
 (0)