Skip to content

Commit

Permalink
[api] Added hacker module. Added default values for all methods. Clea…
Browse files Browse the repository at this point in the history
…ned up errant methods. Added mustache helper.
  • Loading branch information
Marak committed Sep 21, 2014
1 parent 7fb1370 commit 970be2e
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 37 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports.tree = require('./lib/tree');
exports.date = require('./lib/date');
exports.random = require('./lib/random');
exports.finance = require('./lib/finance');
exports.hacker = require('./lib/hacker');

var locales = exports.locales = require('./lib/locales');

Expand All @@ -45,6 +46,7 @@ var _definitions = {
"address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "state"],
"company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb"],
"lorem": ["words"],
"hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb"],
"phone_number": ["formats"],
"internet": ["avatar_uri", "domain_suffix", "free_email"],
"finance": ["account_type", "transaction_type", "currency"],
Expand Down
4 changes: 0 additions & 4 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ var address = {
return Helpers.replaceSymbolWithNumber(faker.random.array_element(["#####", '#####-####']));
},

zipCodeFormat: function (format) {
return Helpers.replaceSymbolWithNumber(["#####", '#####-####'][format]);
},

city: function () {
var result;
switch (faker.random.number(3)) {
Expand Down
52 changes: 52 additions & 0 deletions lib/hacker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var faker = require('../index');

var hacker = {

abbreviation : function () {
return faker.random.array_element(faker.definitions.hacker.abbreviation);
},

adjective : function () {
return faker.random.array_element(faker.definitions.hacker.adjective);
},

noun : function () {
return faker.random.array_element(faker.definitions.hacker.noun);
},

verb : function () {
return faker.random.array_element(faker.definitions.hacker.verb);
},

ingverb : function () {
return faker.random.array_element(faker.definitions.hacker.ingverb);
},

phrase : function () {

var data = {
abbreviation: hacker.abbreviation(),
adjective: hacker.adjective(),
ingverb: hacker.ingverb(),
noun: hacker.noun(),
verb: hacker.verb()
};

var phrase = faker.random.array_element([ "If we {{verb}} the {{noun}}, we can get to the {{abbreviation}} {{noun}} through the {{adjective}} {{abbreviation}} {{noun}}!",
"We need to {{verb}} the {{adjective}} {{abbreviation}} {{noun}}!",
"Try to {{verb}} the {{abbreviation}} {{noun}}, maybe it will {{verb}} the {{adjective}} {{noun}}!",
"You can't {{verb}} the {{noun}} without {{ingverb}} the {{adjective}} {{abbreviation}} {{noun}}!",
"Use the {{adjective}} {{abbreviation}} {{noun}}, then you can {{verb}} the {{adjective}} {{noun}}!",
"The {{abbreviation}} {{noun}} is down, {{verb}} the {{adjective}} {{noun}} so we can {{verb}} the {{abbreviation}} {{noun}}!",
"{{ingverb}} the {{noun}} won't do anything, we need to {{verb}} the {{adjective}} {{abbreviation}} {{noun}}!",
"I'll {{verb}} the {{adjective}} {{abbreviation}} {{noun}}, that should {{noun}} the {{abbreviation}} {{noun}}!"
]);

return faker.helpers.mustache(phrase, data);

},


};

module.exports = hacker;
16 changes: 15 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ exports.randomNumber = function (range) {

// backword-compatibility
exports.randomize = function (array) {
array = array || ["a", "b", "c"];
return faker.random.array_element(array);
};

// slugifies string
exports.slugify = function (string) {
string = string || "";
return string.replace(/ /g, '-').replace(/[^\w\.\-]+/g, '');
};

// parses string for a symbol and replace it with a random number from 1-10
exports.replaceSymbolWithNumber = function (string, symbol) {
string = string || "";
// default symbol is '#'
if (symbol === undefined) {
symbol = '#';
Expand All @@ -35,10 +38,19 @@ exports.replaceSymbolWithNumber = function (string, symbol) {

// takes an array and returns it randomized
exports.shuffle = function (o) {
o = o || ["a", "b", "c"];
for (var j, x, i = o.length; i; j = faker.random.number(i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};

exports.mustache = function (str, data) {
for(var p in data) {
var re = new RegExp('{{' + p + '}}', 'g')
str = str.replace(re, data[p]);
}
return str;
};

exports.createCard = function () {
return {
"name": faker.name.findName(),
Expand Down Expand Up @@ -95,7 +107,10 @@ exports.contextualCard = function () {
return {
"name": name,
"username": userName,
"avatar": faker.internet.avatarUri(),
"email": faker.internet.email(userName),
"dob": faker.date.past(50, new Date("Sat Sep 20 1992 21:35:02 GMT+0200 (CEST)")),
"phone": faker.phone.phoneNumber(),
"address": {
"street": faker.address.streetName(true),
"suite": faker.address.secondaryAddress(),
Expand All @@ -106,7 +121,6 @@ exports.contextualCard = function () {
"lng": faker.address.longitude()
}
},
"phone": faker.phone.phoneNumber(),
"website": faker.internet.domainName(),
"company": {
"name": faker.company.companyName(),
Expand Down
3 changes: 3 additions & 0 deletions lib/internet.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ var internet = {
},

color: function (baseRed255, baseGreen255, baseBlue255) {
baseRed255 = baseRed255 || 0;
baseGreen255 = baseGreen255 || 0;
baseBlue255 = baseBlue255 || 0;
// based on awesome response : http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette
var red = Math.floor((faker.random.number(256) + baseRed255) / 2);
var green = Math.floor((faker.random.number(256) + baseRed255) / 2);
Expand Down
9 changes: 0 additions & 9 deletions lib/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ var _name = {
return faker.random.array_element(faker.definitions.name.first_name)
},

//Working as intended
firstNameFemale: function () {
return faker.name.firstName();
},
//Working as intended
firstNameMale: function () {
return faker.name.firstName();
},

lastName: function () {
return faker.random.array_element(faker.definitions.name.last_name)
},
Expand Down
1 change: 1 addition & 0 deletions lib/phone_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var phone = {

// FIXME: this is strange passing in an array index.
phoneNumberFormat: function (phoneFormatsArrayIndex) {
phoneFormatsArrayIndex = phoneFormatsArrayIndex || 0;
return faker.helpers.replaceSymbolWithNumber(faker.definitions.phone_number.formats[phoneFormatsArrayIndex]);
},

Expand Down
1 change: 1 addition & 0 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var random = {

// takes an array and returns the array randomly sorted
array_element: function (array) {
array = array || ["a", "b", "c"];
var r = faker.random.number({ max: array.length - 1 });
return array[r];
},
Expand Down
4 changes: 4 additions & 0 deletions lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var tree = {
},

createTree: function (depth, width, obj) {
depth = depth || 0;
width = width || 0;
obj = obj || {};

if (!obj) {
throw {
name: "ObjectError",
Expand Down
23 changes: 0 additions & 23 deletions test/name.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,6 @@ describe("name.js", function () {
});
});

describe("firstNameFemale()", function () {
it("returns a random name", function () {
sinon.stub(faker.name, 'firstName').returns('foo');
var first_name = faker.name.firstNameFemale();

assert.equal(first_name, 'foo');

faker.name.firstName.restore();
});
});

describe("firstNameMale()", function () {
it("returns a random name", function () {
sinon.stub(faker.name, 'firstName').returns('foo');
var first_name = faker.name.firstNameMale();

assert.equal(first_name, 'foo');

faker.name.firstName.restore();
});
});


describe("lastName()", function () {
it("returns a random name", function () {
sinon.stub(faker.name, 'lastName').returns('foo');
Expand Down

0 comments on commit 970be2e

Please sign in to comment.