Skip to content

Commit 7c27c34

Browse files
committed
Merge pull request #28 from cloudfour/feat-new_random
New random helper
2 parents 40c0132 + cf9da4f commit 7c27c34

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

lib/random.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
var Chance = require('chance');
5+
var chance;
6+
7+
/**
8+
* Generate a random integer or any other type of random content supported by
9+
* [Chance.js](http://chancejs.com).
10+
*
11+
* @since v0.4.0
12+
* @param {String} [method=integer] - Chance method to use.
13+
* @param {Object} options
14+
* @param {Object} options.hash - Additional options to pass to method.
15+
* @return {*}
16+
* @see {@link http://chancejs.com|Chance.js}
17+
* @example
18+
*
19+
* {{random}} //=> 1839473434
20+
* {{random min=5 max=10}} //=> 7
21+
* {{random "state"}} //=> WA
22+
* {{random "dollar" max=20}} //=> $17.42
23+
*/
24+
25+
module.exports = function random () {
26+
var options = R.last(arguments);
27+
var hash = options.hash || {};
28+
var method = arguments.length > 1 ? arguments[0] : 'integer';
29+
30+
if (!R.is(String, method)) {
31+
throw new Error('The "random" helper\'s first argument must be a String.');
32+
}
33+
34+
chance = chance || new Chance();
35+
36+
if (!R.propIs(Function, method, chance)) {
37+
throw new Error('The "random" helper does not support the "' + method + '" method.');
38+
}
39+
40+
return chance[method](hash);
41+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "core-hbs-helpers",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "Handlebars helpers that we usually need.",
55
"main": "index.js",
66
"files": [
@@ -27,6 +27,7 @@
2727
},
2828
"homepage": "https://github.com/cloudfour/core-hbs-helpers",
2929
"dependencies": {
30+
"chance": "^1.0.3",
3031
"handlebars": "^4.0.3",
3132
"moment": "^2.10.6",
3233
"ramda": "^0.18.0",

test/random.spec.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
var random = require('../').random;
4+
var tape = require('tape');
5+
var R = require('ramda');
6+
var Handlebars = require('handlebars');
7+
var Chance = require('chance');
8+
var chance = new Chance();
9+
10+
Handlebars.registerHelper(random.name, random);
11+
12+
tape('random', function (test) {
13+
var template;
14+
var result;
15+
var parsed;
16+
17+
test.plan(6);
18+
19+
template = Handlebars.compile('{{random}}');
20+
result = template();
21+
parsed = parseFloat(result);
22+
test.ok(R.is(Number, parsed) && parsed === parseInt(result, 10), 'Works');
23+
24+
template = Handlebars.compile('{{random min=5 max=10}}');
25+
result = template();
26+
parsed = parseInt(result, 10);
27+
test.ok(parsed >= 5 && parsed <= 10, 'Works with hash');
28+
29+
template = Handlebars.compile('{{random "state"}}');
30+
result = template();
31+
test.ok(R.find(R.propEq('abbreviation', result))(chance.states()), 'Works with method');
32+
33+
template = Handlebars.compile('{{random "dollar" max=20}}');
34+
result = template();
35+
parsed = parseFloat(result.substr(1));
36+
test.ok(result[0] === '$' && parsed <= 20, 'Works with method and hash');
37+
38+
template = Handlebars.compile('{{random 42}}');
39+
test.throws(
40+
function () {
41+
template();
42+
},
43+
/first argument must be a String\.$/,
44+
'Errors when method is not a String'
45+
);
46+
47+
template = Handlebars.compile('{{random "whatever"}}');
48+
test.throws(
49+
function () {
50+
template();
51+
},
52+
/does not support the "whatever" method\.$/,
53+
'Errors when method does not exist'
54+
);
55+
56+
});

0 commit comments

Comments
 (0)