Skip to content

Commit

Permalink
[refactor] [dist] Allow for node to require individual locales ( to a…
Browse files Browse the repository at this point in the history
…void the default behavior of requiring all locale data. #125 #167
  • Loading branch information
Marak committed Jul 7, 2015
1 parent 5414b79 commit eaf5c65
Show file tree
Hide file tree
Showing 52 changed files with 266 additions and 104 deletions.
20 changes: 18 additions & 2 deletions build/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var rename = require('gulp-rename');
var mustache = require('gulp-mustache');
var browserify = require('browserify');
var transform = require('vinyl-transform');

var path = require('path');
var fs = require('fs');

gulp.task('browser-package', function() {

Expand Down Expand Up @@ -88,4 +89,19 @@ gulp.task('documentation', function(cb) {

});

gulp.task('default', ['browser-package', 'documentation']);
gulp.task('nodeLocalRequires', function (cb){
var locales = require('../lib/locales');
for (var locale in locales) {
var localeFile = path.normalize(__dirname + "/../locale/" + locale + ".js");
var localeRequire = '';
localeRequire += "var faker = require('../lib');\n";
localeRequire += 'faker.locale = "' + locale + '";\n';
localeRequire += "faker.locales['" + locale + "'] = require('../lib/locales/" + locale + "');\n";
localeRequire += "module['exports'] = faker;\n";
console.log(localeRequire);
fs.writeFileSync(localeFile, localeRequire);
}
cb();
});

gulp.task('default', ['nodeLocalRequires', 'browser-package', 'documentation']);
7 changes: 7 additions & 0 deletions examples/singleLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var faker = require('../locale/en');

console.log(faker.name.findName());

var faker = require('../locale/uk');

console.log(faker.name.findName());
87 changes: 3 additions & 84 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,4 @@
/*
module['exports'] = require('./lib');

this index.js file is used for including the faker library as a CommonJS module, instead of a bundle
you can include the faker library into your existing node.js application by requiring the entire /faker directory
var faker = require(./faker);
var randomName = faker.name.findName();
you can also simply include the "faker.js" file which is the auto-generated bundled version of the faker library
var faker = require(./customAppPath/faker);
var randomName = faker.name.findName();
if you plan on modifying the faker library you should be performing your changes in the /lib/ directory
*/
exports.random = require('./lib/random');
exports.fake = require('./lib/fake');

exports.name = require('./lib/name');
exports.address = require('./lib/address');
exports.phone = require('./lib/phone_number');
exports.internet = require('./lib/internet');
exports.company = require('./lib/company');
exports.image = require('./lib/image');
exports.lorem = require('./lib/lorem');
exports.helpers = require('./lib/helpers');
exports.date = require('./lib/date');
exports.finance = require('./lib/finance');
exports.hacker = require('./lib/hacker');
exports.commerce = require('./lib/commerce');

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

// default locale
exports.locale = "en";

// in case a locale is missing a definition, fallback to this locale
exports.localeFallback = "en";

exports.definitions = {};


var _definitions = {
"name": ["first_name", "last_name", "prefix", "suffix", "title", "male_first_name", "female_first_name", "male_middle_name", "female_middle_name", "male_last_name", "female_last_name"],
"address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "zipFormat"],
"company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"],
"lorem": ["words"],
"hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb"],
"phone_number": ["formats"],
"finance": ["account_type", "transaction_type", "currency"],
"internet": ["avatar_uri", "domain_suffix", "free_email", "password"],
"commerce": ["color", "department", "product_name", "price", "categories"],
"title": "",
"separator": ""
};

// Create a Getter for all definitions.foo.bar propetries
Object.keys(_definitions).forEach(function(d){
if (typeof exports.definitions[d] === "undefined") {
exports.definitions[d] = {};
}

if (typeof _definitions[d] === "string") {
exports.definitions[d] = locales[exports.locale][d];
return;
}

_definitions[d].forEach(function(p){
Object.defineProperty(exports.definitions[d], p, {
get: function () {
if (typeof locales[exports.locale][d] === "undefined" || typeof locales[exports.locale][d][p] === "undefined") {
// certain localization sets contain less data then others.
// in the case of a missing defintion, use the default localeFallback to substitute the missing set data
return locales[exports.localeFallback][d][p];
} else {
// return localized data
return locales[exports.locale][d][p];
}
}
});
});
});
// since we are requiring the top level of faker, load all locales by default
module['exports'].locales = require('./lib/locales');
2 changes: 1 addition & 1 deletion lib/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Helpers = require('./helpers');
var faker = require('../index'),
var faker = require('./index'),
f = faker.fake;

var address = {
Expand Down
6 changes: 4 additions & 2 deletions lib/commerce.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');

var commerce = {

Expand Down Expand Up @@ -56,7 +56,9 @@ var commerce = {
},

mergeCategories: function(categories) {
var separator = faker.definitions.separator;
var separator = faker.definitions.separator || " &";
// TODO: find undefined here
categories = categories || faker.definitions.commerce.categories;
var commaSeparated = categories.slice(0, -1).join(', ');

return [commaSeparated, categories[categories.length - 1]].join(separator);
Expand Down
2 changes: 1 addition & 1 deletion lib/company.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index'),
var faker = require('./index'),
f = faker.fake;

var company = {
Expand Down
2 changes: 1 addition & 1 deletion lib/date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require("../index");
var faker = require("./index");

var date = {

Expand Down
2 changes: 1 addition & 1 deletion lib/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

var faker = require('../');
var faker = require('./');

module['exports'] = function fake (str) {

Expand Down
2 changes: 1 addition & 1 deletion lib/finance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Helpers = require('./helpers'),
faker = require('../index');
faker = require('./index');

var finance = {

Expand Down
2 changes: 1 addition & 1 deletion lib/hacker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');

var hacker = {

Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');

// backword-compatibility
exports.randomNumber = function (range) {
Expand Down
2 changes: 1 addition & 1 deletion lib/image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');

var image = {
image: function () {
Expand Down
90 changes: 90 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
this index.js file is used for including the faker library as a CommonJS module, instead of a bundle
you can include the faker library into your existing node.js application by requiring the entire /faker directory
var faker = require(./faker);
var randomName = faker.name.findName();
you can also simply include the "faker.js" file which is the auto-generated bundled version of the faker library
var faker = require(./customAppPath/faker);
var randomName = faker.name.findName();
if you plan on modifying the faker library you should be performing your changes in the /lib/ directory
*/
exports.random = require('./random');
exports.fake = require('./fake');

exports.name = require('./name');
exports.address = require('./address');
exports.phone = require('./phone_number');
exports.internet = require('./internet');
exports.company = require('./company');
exports.image = require('./image');
exports.lorem = require('./lorem');
exports.helpers = require('./helpers');
exports.date = require('./date');
exports.finance = require('./finance');
exports.hacker = require('./hacker');
//exports.commerce = require('./commerce');

// don't load all locales by default
var locales = {};
exports.locales = locales;
locales = require('./locales');

// default locale
exports.locale = "en";

// in case a locale is missing a definition, fallback to this locale
exports.localeFallback = "en";

exports.definitions = {};


var _definitions = {
"name": ["first_name", "last_name", "prefix", "suffix", "title", "male_first_name", "female_first_name", "male_middle_name", "female_middle_name", "male_last_name", "female_last_name"],
"address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "zipFormat"],
"company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"],
"lorem": ["words"],
"hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb"],
"phone_number": ["formats"],
"finance": ["account_type", "transaction_type", "currency"],
"internet": ["avatar_uri", "domain_suffix", "free_email", "password"],
"commerce": ["color", "department", "product_name", "price", "categories"],
"title": "",
"separator": ""
};

// Create a Getter for all definitions.foo.bar propetries
Object.keys(_definitions).forEach(function(d){
if (typeof exports.definitions[d] === "undefined") {
exports.definitions[d] = {};
}

if (typeof _definitions[d] === "string") {
exports.definitions[d] = _definitions[d];


return;
}

_definitions[d].forEach(function(p){
Object.defineProperty(exports.definitions[d], p, {
get: function () {
if (typeof locales[exports.locale][d] === "undefined" || typeof locales[exports.locale][d][p] === "undefined") {
// certain localization sets contain less data then others.
// in the case of a missing defintion, use the default localeFallback to substitute the missing set data
return locales[exports.localeFallback][d][p];
} else {
// return localized data
return locales[exports.locale][d][p];
}
}
});
});
});
2 changes: 1 addition & 1 deletion lib/internet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index'),
var faker = require('./index'),
password_generator = require('../vendor/password-generator.js'),
random_ua = require('../vendor/user-agent');

Expand Down
2 changes: 1 addition & 1 deletion lib/locales.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');
exports['de'] = require('./locales/de');
exports['de_AT'] = require('./locales/de_AT');
exports['de_CH'] = require('./locales/de_CH');
Expand Down
2 changes: 1 addition & 1 deletion lib/lorem.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');
var Helpers = require('./helpers');

var lorem = {
Expand Down
2 changes: 1 addition & 1 deletion lib/name.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');

var _name = {

Expand Down
2 changes: 1 addition & 1 deletion lib/phone_number.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require('./index');

var phone = {
phoneNumber: function (format) {
Expand Down
2 changes: 1 addition & 1 deletion lib/random.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var mersenne = require('../vendor/mersenne');
var faker = require('../index');
var faker = require('./index');

var random = {
// returns a single random number based on a max number or range
Expand Down
4 changes: 4 additions & 0 deletions locale/de.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "de";
faker.locales['de'] = require('../lib/locales/de');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/de_AT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "de_AT";
faker.locales['de_AT'] = require('../lib/locales/de_AT');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/de_CH.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "de_CH";
faker.locales['de_CH'] = require('../lib/locales/de_CH');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en";
faker.locales['en'] = require('../lib/locales/en');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_AU.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_AU";
faker.locales['en_AU'] = require('../lib/locales/en_AU');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_BORK.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_BORK";
faker.locales['en_BORK'] = require('../lib/locales/en_BORK');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_CA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_CA";
faker.locales['en_CA'] = require('../lib/locales/en_CA');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_GB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_GB";
faker.locales['en_GB'] = require('../lib/locales/en_GB');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_IND.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_IND";
faker.locales['en_IND'] = require('../lib/locales/en_IND');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_US.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_US";
faker.locales['en_US'] = require('../lib/locales/en_US');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/en_au_ocker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "en_au_ocker";
faker.locales['en_au_ocker'] = require('../lib/locales/en_au_ocker');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "es";
faker.locales['es'] = require('../lib/locales/es');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/fa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "fa";
faker.locales['fa'] = require('../lib/locales/fa');
module['exports'] = faker;
4 changes: 4 additions & 0 deletions locale/fr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var faker = require('../lib');
faker.locale = "fr";
faker.locales['fr'] = require('../lib/locales/fr');
module['exports'] = faker;
Loading

0 comments on commit eaf5c65

Please sign in to comment.