Skip to content

Commit

Permalink
Add more helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 13, 2016
1 parent d192d6c commit 69dbc7b
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 34 deletions.
73 changes: 53 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var _ = require("lodash");
var mersenne = require("../vendor/mersenne");

var alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

module.exports = function(localeID, seed) {
var self = this;
localeID = localeID || "default";
Expand All @@ -11,12 +13,13 @@ module.exports = function(localeID, seed) {
var locale = require("./locales/" + localeID);

if (locale) {
if (locale.fallback) {
var fallback = require("./locales/" + locale.fallback);
if (fallback) {
console.log("Fallback " + locale.fallback + "loaded.");

locale = _.defaults(locale, fallback);
if (locale._meta.id != "default") {
var fallbackID = locale._meta.fallback || "default"
var fbLocale = require("./locales/" + fallbackID);
if (fbLocale) {
console.log("Fallback " + fallbackID + " loaded.");
// Merge locale and fallback
locale = _.defaultsDeep(locale, fbLocale);
}
}
} else {
Expand All @@ -35,19 +38,17 @@ module.exports = function(localeID, seed) {
mersenne.seed(seed);
}

function randomNumber(min, max, precision) {
min = min || 0;
max = max || 99999;
precision = precision || 1; // TODO initial value in es6

return precision * Math.floor( mersenne.rand(max / precision, min / precision) );
}

self.random = {
number: randomNumber,
number: function(min, max, precision) {
min = min || 0;
max = max || 99999;
precision = precision || 1; // TODO initial value in es6

return precision * Math.floor( mersenne.rand(max / precision, min / precision) );
},

arrayElement: function(array) {
return array[randomNumber(0, array.length - 1)];
return array[self.random.number(0, array.length - 1)];
},

objectElement: function(obj) {
Expand All @@ -59,10 +60,40 @@ module.exports = function(localeID, seed) {
}*/
}

}
};

self.get = function(format) {
self.slugify = function (str) {
if (str)
return str.replace(/ /g, '-').replace(/[^\w\.\-]+/g, '');
};

self.replaceSymbol = function (format, numberSymbol, alphaSymbol) {
if (!format) return;
debugger;
numberSymbol = numberSymbol || "#";
alphaSymbol = alphaSymbol || "?";

var str = '';
for (var i = 0; i < format.length; i++) {
if (format.charAt(i) == numberSymbol)
str += self.random.number(0, 9);

else if (format.charAt(i) == alphaSymbol)
str += self.random.arrayElement(alpha);

else
str += format.charAt(i);
}
return str;
};

self.shuffle = function (o) {
for (var j, x, i = o.length-1; i; j = self.random.number(0, i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};


self.get = function(format) {
var res = format;
if (format.indexOf("#{") != -1) {
res = format.replace(/\#\{([A-Za-z_\.]+)\}/g, function(match, cap) {
Expand All @@ -82,9 +113,11 @@ module.exports = function(localeID, seed) {
});
}

// TODO item = self.replaceSymbols(item);
// Replace symbols
res = self.replaceSymbol(res);

return res;
}
};



Expand Down
11 changes: 11 additions & 0 deletions lib/locales/default/company/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
module.exports = {
name: [
"#{names.lastName} #{company.suffix}",
"#{names.lastName}-#{names.lastName} #{company.suffix}",
"#{names.lastName}, #{names.lastName} and #{names.lastName} #{company.suffix}"
],

suffix: [
"Ltd.",
"Inc.",
"Corp.",
"LLC",
"Group"
]
}
2 changes: 1 addition & 1 deletion lib/locales/default/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
_meta: {
id: "en",
id: "default",
fallback: null,
language: "English",
country: "Great Britain"
Expand Down
54 changes: 45 additions & 9 deletions lib/locales/default/names/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
module.exports = {
firstName: require("./firstName"),
lastName: require("./lastName"),
// Male first name
firstNameM: require("./firstName"),
// Femail first name
firstNameF: require("./firstName"),

// First name
firstName: [
"#{names.firstNameM}",
"#{names.firstNameF}",
],

// Male last name
lastNameM: require("./lastName"),
// Female last name
lastNameF: require("./lastName"),

// Last name
lastName: [
"#{names.lastNameM}",
"#{names.lastNameF}",
],

prefix: [
"Mr.",
"Mrs.",
"Ms.",
"Miss",
"Dr."
],

suffix: [
"Jr.",
"Sr.",
Expand All @@ -21,12 +42,27 @@ module.exports = {
"PhD",
"DVM"
],
name: [
"#{names.prefix} #{names.firstName} #{names.lastName}",
"#{names.firstName} #{names.lastName} #{names.suffix}",
"#{names.firstName} #{names.lastName}",
"#{names.firstName} #{names.lastName}",
"#{names.firstName} #{names.lastName}",
"#{names.firstName} #{names.lastName}"

nameM: [
"#{names.prefix} #{names.firstNameM} #{names.lastNameM}",
"#{names.firstNameM} #{names.lastNameM} #{names.suffix}",
"#{names.firstNameM} #{names.lastNameM}",
"#{names.firstNameM} #{names.lastNameM}",
"#{names.firstNameM} #{names.lastNameM}",
"#{names.firstNameM} #{names.lastNameM}"
],

nameF: [
"#{names.prefix} #{names.firstNameF} #{names.lastNameF}",
"#{names.firstNameF} #{names.lastNameF} #{names.suffix}",
"#{names.firstNameF} #{names.lastNameF}",
"#{names.firstNameF} #{names.lastNameF}",
"#{names.firstNameF} #{names.lastNameF}",
"#{names.firstNameF} #{names.lastNameF}"
],

name: [
"#{names.nameM}",
"#{names.nameF}"
]
}
23 changes: 22 additions & 1 deletion lib/locales/default/phone/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
module.exports = {
number: []
number: [
"###-###-####",
"(###) ###-####",
"1-###-###-####",
"###.###.####",
"###-###-####",
"(###) ###-####",
"1-###-###-####",
"###.###.####",
"###-###-#### x###",
"(###) ###-#### x###",
"1-###-###-#### x###",
"###.###.#### x###",
"###-###-#### x####",
"(###) ###-#### x####",
"1-###-###-#### x####",
"###.###.#### x####",
"###-###-#### x#####",
"(###) ###-#### x#####",
"1-###-###-#### x#####",
"###.###.#### x#####"
]
}
17 changes: 17 additions & 0 deletions lib/locales/hu-HU/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
_meta: {
id: "hu-HU",
fallback: null,
language: "Hungarian",
country: "Hungary"
},

company: {
suffix: [
"Kft.",
"Bt.",
"Zrt",
"Nyrt"
]
}
}
18 changes: 15 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
var Fakerator = require("../lib");

var fakerator = new Fakerator("default");
var fakerator = new Fakerator("hu-HU");

/*console.log(fakerator);
for(var i = 0; i < 10; i++) {
console.log(fakerator.random.number(1, 100));
}*/

}
console.log("----------------\r\n");
*/

for(var i = 0; i < 10; i++) {
console.log(fakerator.get("#{names.name}"));
}
console.log("----------------\r\n");

for(var i = 0; i < 10; i++) {
console.log(fakerator.get("#{company.name}"));
}
console.log("----------------\r\n");

for(var i = 0; i < 10; i++) {
console.log(fakerator.get("#{phone.number}"));
}
console.log("----------------\r\n");

0 comments on commit 69dbc7b

Please sign in to comment.