Skip to content

Commit

Permalink
Create fake functions
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed May 14, 2016
1 parent c6123af commit 5b63720
Show file tree
Hide file tree
Showing 18 changed files with 9,961 additions and 16,604 deletions.
22,793 changes: 6,258 additions & 16,535 deletions demo/fakerator.js

Large diffs are not rendered by default.

97 changes: 92 additions & 5 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,113 @@
var Fakerator = require("./fakerator");

var fakerator = new Fakerator();
//fakerator.seed(123);

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

//console.log(fakerator);
/*
for(var i = 0; i < 10; i++) {
console.log(fakerator.random.number(10, 1, 2));
}
console.log("----------------\r\n");
*/

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

for(var i = 0; i < 10; i++) {
console.log(fakerator.company.name());
}
console.log("----------------\r\n");
/*
for(var i = 0; i < 10; i++) {
console.log(fakerator.phone.number());
}
console.log("----------------\r\n");
*/
/*
for(var i = 0; i < 10; i++) {
console.log({
country: fakerator.address.country(),
countryCode: fakerator.address.countryCode(),
state: fakerator.address.state(),
stateAbbr: fakerator.address.stateAbbr(),
city: fakerator.address.city(),
address: fakerator.address.street(),
zip: fakerator.address.postCode(),
geoLocation: fakerator.address.geoLocation()
});
}
console.log("----------------\r\n");
*/
/*
for(var i = 0; i < 10; i++) {
console.log(fakerator.internet.email("Bob", "Ice"));
}
console.log("----------------\r\n");
*/
/*
for(var i = 0; i < 10; i++) {
console.log(fakerator.lorem.paragraph());
}*/
/*
for(var i = 0; i < 10; i++) {
console.log(fakerator.address.geoLocation());
}
console.log("----------------\r\n");
*/

/*
var a = {
a: 1,
b: 2,
c: 3
}
for(var i = 0; i < 10; i++) {
console.log(fakerator.random.objectElement(a));
}
console.log("----------------\r\n");
*/
/*
for(var i = 0; i < 10; i++) {
console.log(fakerator.random.letter());
}
console.log("----------------\r\n");
for(var i = 0; i < 10; i++) {
console.log(fakerator.random.number(1, 100));
console.log(fakerator.random.digit());
}
console.log("----------------\r\n");
*/


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


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

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


/*
setInterval( function() {
console.log(fakerator.random.number(10, 1));
}, 500);*/
155 changes: 99 additions & 56 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,113 +1,156 @@
//import { defaultsDeep } from "lodash";
var _ = require("lodash");
var mersenne = require("../vendor/mersenne");
import get from "lodash/get";
import each from "lodash/each";
import defaultsDeep from "lodash/defaultsDeep";
import capitalize from "lodash/capitalize";
import isArray from "lodash/isArray";
import isString from "lodash/isString";
import isFunction from "lodash/isFunction";
import isNumber from "lodash/isNumber";
import isObject from "lodash/isObject";

var letters = ['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']
const mersenne = require("../vendor/mersenne");

module.exports = function(localeID, seed) {
var self = this;
localeID = localeID || "default";
const letters = ['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']

console.log("Load " + localeID + " locale");

var locale = require("./locales/" + localeID);
module.exports = function(localeID = "default") {
let self = this;

let locale = require("./locales/" + localeID + "/index");
if (locale) {
if (locale._meta.id != "default") {
var fallbackID = locale._meta.fallback || "default"
var fbLocale = require("./locales/" + fallbackID);
if (localeID != "default") {
let fallbackID = locale._meta.fallback || "default"
let fbLocale = require("./locales/" + fallbackID + "/index");
if (fbLocale) {
console.log("Fallback " + fallbackID + " loaded.");
//console.log("Fallback " + fallbackID + " loaded.");
// Merge locale and fallback
locale = _.defaultsDeep(locale, fbLocale);
locale = defaultsDeep(locale, fbLocale);
}
}
} else {
locale = require("./locales/default");
locale = require("./locales/default/index");
}

self.locale = locale;

// Set seed for random
if (seed) {
if (_.isArray(seed) && seed.length > 0)
self.seed = (seed) => {
if (isArray(seed) && seed.length > 0)
mersenne.seed_array(seed);
else
mersenne.seed(seed);
}

self.random = {
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) );
number(max = 99999, min = 0, precision = 1) {
max /= precision;
min /= precision;
return precision * Math.floor( mersenne.rand(max + 1, min));
},

digit: function() {
return self.random.number(0, 9);
digit() {
return self.random.number(9);
},

letter: function() {
letter() {
return self.random.arrayElement(letters);
},

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

objectElement: function(obj) {
var array = Object.keys(obj);
var key = self.random.arrayElement(array);
/*
objectElement(obj) {
let key = self.random.arrayElement(Object.keys(obj));
return {
[key]: object[key]
}*/
[key]: obj[key]
}
}

};

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

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

self.replaceSymbols = function (format, numberSymbol, alphaSymbol) {
self.replaceSymbols = function (format, numberSymbol = "#", alphaSymbol = "\\?") {
if (format)
return format
.replace(new RegExp(numberSymbol || "#", "g"), self.random.digit)
.replace(new RegExp(alphaSymbol || "\\?", "g"), self.random.letter);
.replace(new RegExp(numberSymbol, "g"), self.random.digit)
.replace(new RegExp(alphaSymbol, "g"), self.random.letter);
};

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);
for (let j, x, i = o.length-1; i; j = self.random.number(i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};

let maskRE = new RegExp(locale._meta.mask || "\#\{([A-Za-z_\.]+)\}", "g");

self.get = function(format) {
var res = format;
self.populate = function(format, ...args) {
let res = format;
if (format.indexOf("#{") != -1) {
res = format.replace(/\#\{([A-Za-z_\.]+)\}/g, function(match, cap) {
res = format.replace(maskRE, function(match, cap) {
// Kikeressük van-e ilyen a locale-ban
var part = _.get(self.locale, cap);
let part = get(self.locale, cap);
if (part) {
if (_.isArray(part))
if (isFunction(part)) {
part = part.call(self, ...args);
}

if (isArray(part))
// Ha tömböt kaptunk, akkor abból random választunk és rekurzívan hívunk,
// mert lehet abban is egy mask van
return self.get(self.random.arrayElement(part));
else (_.isString(part))
return self.populate(self.random.arrayElement(part), ...args);
else if (isString(part))
// Ha szöveg, akkor rekurzívan hívunk mert lehet az is mask
return self.get(part);
} else
// Ha nincs, akkor marad ami volt
return match;
return self.populate(part, ...args);
else if (isNumber(part) || isObject(part))
// Ha szám, vagy objektum akkor visszatérünk
return part;
}
// Ha nincs, akkor marad ami volt
return match;
});
}

// Replace symbols
return self.replaceSymbols(res);
if (isString(res))
res = self.replaceSymbols(res);

return res;
};

// Set helper functions from locale
each(Object.keys(self.locale), (category) => {
if (category === "_meta") return;
each(Object.keys(self.locale[category]), (item) => {
self[category] = self[category] || {};

if (isFunction(self.locale[category][item])) {
// Ha a locale-ban ez is egy függvény, akkor meghívjuk közvetlen.
// Ha a visszatérési érték pedig string, akkor ráhívjuk a populate-et
self[category][item] = function(...args) {
let res = self.locale[category][item].call(self, ...args);

if (isArray(res))
res = self.random.arrayElement(res);

if (isString(res))
res = self.populate(res, ...args);

return res;
}

} else {
// Ha nem, akkor simán a populate-t
self[category][item] = function(...args) {
return self.populate("#{" + category + "." + item + "}", ...args);
}

}
console.log("Set " + category + "." + item);
});
});

return self;
}
Loading

0 comments on commit 5b63720

Please sign in to comment.