Skip to content

Commit

Permalink
feat!: support tree-shaking (faker-js#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored and pkuczynski committed Jan 21, 2022
1 parent 2c8e368 commit 81d7d5e
Show file tree
Hide file tree
Showing 38 changed files with 122 additions and 120 deletions.
31 changes: 15 additions & 16 deletions build/gulp-tasks/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@
*/

const browserify = require('browserify');
const source = require('vinyl-source-stream');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const { src, dest } = require('gulp');

const files = {
jsMain: './index.js',
jsOutput: 'faker.js'
}
jsMain: './lib/index.js',
jsOutput: 'faker.js',
};

module.exports = function browser () {
module.exports = function browser() {
return browserify(files.jsMain, {
standalone: 'faker',
debug: true
debug: true,
})
.bundle()
.pipe(source(files.jsOutput))
.pipe(buffer())
.pipe(dest("examples/browser/js"))
.pipe(dest('dist/'))
.pipe(rename({ extname: ".min.js" }))
.pipe(uglify())
.pipe(dest("examples/browser/js"))
.pipe(dest('dist/'));
.bundle()
.pipe(source(files.jsOutput))
.pipe(buffer())
.pipe(dest('examples/browser/js'))
.pipe(dest('dist/'))
.pipe(rename({ extname: '.min.js' }))
.pipe(uglify())
.pipe(dest('examples/browser/js'))
.pipe(dest('dist/'));
};

70 changes: 39 additions & 31 deletions examples/node/generateMultiLevelMultiLocaleJSON.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
// This example shows the generation of a multilevel object and JSON document using various faker.js features
// including name, address, company, date and commerce namespaces, moustache expressions and random element production
// Using the helper function arr, randomly sized collections of elements are produced in the document.

var faker = require('../../index');
var fs = require('fs');
// produce array with random number of empty elements
const arr = (maxNumberOfElements) => new Array(faker.datatype.number({min: 1, max: maxNumberOfElements})).fill()

const locales = ["nl","es","de","fr","en_AU"]
const company =
{ "name" : faker.company.companyName()
, "country" : faker.address.country()
, "departments" : arr(8).map(() => { faker.locale = faker.random.arrayElement(locales)
return { "name" : faker.commerce.department()
, "location" : faker.fake("{{address.city}} ({{address.country}})")
, "employees": arr(20).map(() => {
return { "name" : faker.fake("{{name.firstName}} {{name.lastName}}")
, "job" : faker.name.jobTitle()
, "hiredate" : faker.date.past(12).toISOString().split('T')[0]
, "salary" : faker.datatype.number(700, 9000)
}
})
}
})
}

console.log(JSON.stringify(company))
fs.writeFile(__dirname + '/companyDataSet.json', JSON.stringify(company), function() {
console.log("dataSet generated successfully!");
});
// This example shows the generation of a multilevel object and JSON document using various faker.js features
// including name, address, company, date and commerce namespaces, moustache expressions and random element production
// Using the helper function arr, randomly sized collections of elements are produced in the document.

var faker = require('../../lib').faker;
var fs = require('fs');
// produce array with random number of empty elements
const arr = (maxNumberOfElements) =>
new Array(faker.datatype.number({ min: 1, max: maxNumberOfElements })).fill();

const locales = ['nl', 'es', 'de', 'fr', 'en_AU'];
const company = {
name: faker.company.companyName(),
country: faker.address.country(),
departments: arr(8).map(() => {
faker.locale = faker.random.arrayElement(locales);
return {
name: faker.commerce.department(),
location: faker.fake('{{address.city}} ({{address.country}})'),
employees: arr(20).map(() => {
return {
name: faker.fake('{{name.firstName}} {{name.lastName}}'),
job: faker.name.jobTitle(),
hiredate: faker.date.past(12).toISOString().split('T')[0],
salary: faker.datatype.number(700, 9000),
};
}),
};
}),
};

console.log(JSON.stringify(company));
fs.writeFile(
__dirname + '/companyDataSet.json',
JSON.stringify(company),
function () {
console.log('dataSet generated successfully!');
}
);
27 changes: 17 additions & 10 deletions examples/node/generateSet.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
var fs = require('fs');

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

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

// generate dataSet as example
fs.writeFile(__dirname + '/dataSet.json', JSON.stringify(faker.helpers.userCard()), function() {
console.log("dataSet generated successfully!");
});
fs.writeFile(
__dirname + '/dataSet.json',
JSON.stringify(faker.helpers.userCard()),
function () {
console.log('dataSet generated successfully!');
}
);
// generate bigDataSet as example
var bigSet = [];

for(var i = 20; i >= 0; i--){
for (var i = 20; i >= 0; i--) {
bigSet.push(faker.helpers.userCard());
};
}

fs.writeFile(__dirname + '/bigDataSet.json', JSON.stringify(bigSet), function() {
console.log("bigDataSet generated successfully!");
});
fs.writeFile(
__dirname + '/bigDataSet.json',
JSON.stringify(bigSet),
function () {
console.log('bigDataSet generated successfully!');
}
);
9 changes: 3 additions & 6 deletions examples/node/generators.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
var faker = require('../../index');
var faker = require('../../lib').faker;

faker.locale = "en";
faker.locale = 'en';

console.log(faker.fake('{{random.uuid}}, {{name.firstName}} {{name.suffix}}'));


return;


console.log(faker.fake('{{finance.currencyName}} - {{finance.amount}}'));


console.log(faker.fake('{{name.firstName}} {{name.lastName}}'));
console.log(faker.fake('{{name.firstName}} {{name.lastName}}'));
14 changes: 7 additions & 7 deletions examples/node/minimal-usage.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env node

var faker = require('../../index');
faker.locale = "fi";
var faker = require('../../lib').faker;
faker.locale = 'fi';

//console.log(faker.lorem.sentences())

console.log(faker.name.findName())
console.log(faker.name.findName());
return;
//console.log(faker.address)
console.log(faker.internet.email())
console.log(faker.date.recent())
console.log(faker.internet.email());
console.log(faker.date.recent());
console.log(faker.helpers.contextualCard());

faker.locale = "uk";
faker.locale = 'uk';

console.log(faker.helpers.contextualCard());
console.log(faker.helpers.contextualCard());
9 changes: 4 additions & 5 deletions examples/node/unique-values.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
var faker = require('../../index');
var faker = require('../../lib').faker;

var emails = {};
var conflicts = 0;
// emails estimated: 1,055,881
// full names estimated: 1,185,139
for (var i = 0; i < 100000; i++) {

// call function with no arguments
var email = faker.unique(faker.internet.email);

// or with function arguments as argument array
// var email = faker.unique(faker.internet.email, [null, null, 'marak.com']);
// var email = faker.unique(faker.internet.email, [null, null, 'marak.com']);

// or with custom options for maxTime as milliseconds or maxRetries
// var email = faker.unique(faker.internet.email, [null, null, 'marak.com'], { maxRetries: 1, maxTime: 50 });
// var email = faker.unique(faker.internet.email, [null, null, 'marak.com'], { maxRetries: 1, maxTime: 50 });

if (typeof emails[email] === 'undefined') {
// found a unique new item
Expand All @@ -26,4 +25,4 @@ for (var i = 0; i < 100000; i++) {
console.log('total conflicts', conflicts); // should be zero using faker.unique()
console.log('total uniques generated', Object.keys(emails).length);

// console.log(emails);
// console.log(emails);
3 changes: 0 additions & 3 deletions index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions index.js

This file was deleted.

4 changes: 0 additions & 4 deletions index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"url": "https://github.com/faker-js/faker.git"
},
"license": "MIT",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"browser": "gulp browser",
"build": "tsc",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,9 @@ export class Faker {
}
}

export default Faker;
module.exports = Faker;
// since we are requiring the top level of faker, load all locales by default
export const faker: Faker = new Faker({
locales: require('./locales'),
});

export default faker;
2 changes: 1 addition & 1 deletion test/address.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('address.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/all.functional.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

var functionalHelpers = require('./support/function-helpers.js');
Expand Down
2 changes: 1 addition & 1 deletion test/animal.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('animal.js', function () {
Expand Down
6 changes: 3 additions & 3 deletions test/commerce.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert'),
sinon = require('sinon'),
faker = require('../index');
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../lib').faker;
}

describe('commerce.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/company.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('company.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/database.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('database.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/datatype.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var _ = require('lodash');
var faker = require('../index');
var faker = require('../lib').faker;
var mersenne = require('../vendor/mersenne');
}

Expand Down
2 changes: 1 addition & 1 deletion test/date.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('date.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/fake.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('fake.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/finance.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

faker.seed(1234);
Expand Down
2 changes: 1 addition & 1 deletion test/finance_iban.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var faker = require('../index');
var faker = require('../lib').faker;
}

function getAnIbanByCountry(countryCode) {
Expand Down
2 changes: 1 addition & 1 deletion test/git.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('git.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('helpers.js', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/image.unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
var faker = require('../lib').faker;
}

describe('image.js', function () {
Expand Down
Loading

0 comments on commit 81d7d5e

Please sign in to comment.