From 69dbc7bc9c0ad7b58873c7830827668f2f01f448 Mon Sep 17 00:00:00 2001 From: Icebob Date: Fri, 13 May 2016 16:41:28 +0200 Subject: [PATCH] Add more helper functions --- lib/index.js | 73 ++++++++++++++++++++-------- lib/locales/default/company/index.js | 11 +++++ lib/locales/default/index.js | 2 +- lib/locales/default/names/index.js | 54 ++++++++++++++++---- lib/locales/default/phone/index.js | 23 ++++++++- lib/locales/hu-HU/index.js | 17 +++++++ test/test.js | 18 +++++-- 7 files changed, 164 insertions(+), 34 deletions(-) create mode 100644 lib/locales/hu-HU/index.js diff --git a/lib/index.js b/lib/index.js index 7eddf60f..4f932486 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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"; @@ -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 { @@ -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) { @@ -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) { @@ -82,9 +113,11 @@ module.exports = function(localeID, seed) { }); } - // TODO item = self.replaceSymbols(item); + // Replace symbols + res = self.replaceSymbol(res); + return res; - } + }; diff --git a/lib/locales/default/company/index.js b/lib/locales/default/company/index.js index 1010043c..8b6263f0 100644 --- a/lib/locales/default/company/index.js +++ b/lib/locales/default/company/index.js @@ -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" ] } \ No newline at end of file diff --git a/lib/locales/default/index.js b/lib/locales/default/index.js index 849926a6..3aed74e7 100644 --- a/lib/locales/default/index.js +++ b/lib/locales/default/index.js @@ -1,6 +1,6 @@ module.exports = { _meta: { - id: "en", + id: "default", fallback: null, language: "English", country: "Great Britain" diff --git a/lib/locales/default/names/index.js b/lib/locales/default/names/index.js index 841fdff4..46fd1dbd 100644 --- a/lib/locales/default/names/index.js +++ b/lib/locales/default/names/index.js @@ -1,6 +1,26 @@ 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.", @@ -8,6 +28,7 @@ module.exports = { "Miss", "Dr." ], + suffix: [ "Jr.", "Sr.", @@ -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}" + ] } \ No newline at end of file diff --git a/lib/locales/default/phone/index.js b/lib/locales/default/phone/index.js index 8bc4d61d..30874aeb 100644 --- a/lib/locales/default/phone/index.js +++ b/lib/locales/default/phone/index.js @@ -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#####" + ] } \ No newline at end of file diff --git a/lib/locales/hu-HU/index.js b/lib/locales/hu-HU/index.js new file mode 100644 index 00000000..7ab0e223 --- /dev/null +++ b/lib/locales/hu-HU/index.js @@ -0,0 +1,17 @@ +module.exports = { + _meta: { + id: "hu-HU", + fallback: null, + language: "Hungarian", + country: "Hungary" + }, + + company: { + suffix: [ + "Kft.", + "Bt.", + "Zrt", + "Nyrt" + ] + } +} \ No newline at end of file diff --git a/test/test.js b/test/test.js index 7012cf1e..8e0e058d 100644 --- a/test/test.js +++ b/test/test.js @@ -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");