Skip to content

Commit

Permalink
Merge pull request #128 from ST-DDT/feat/update/faker
Browse files Browse the repository at this point in the history
feat: update faker-js
  • Loading branch information
danibram authored Sep 24, 2024
2 parents 9085985 + d9be8f6 commit 820a8c0
Show file tree
Hide file tree
Showing 18 changed files with 4,357 additions and 4,349 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[![Support link][paypal-badge]][paypal-link]

A simplified way to generate massive mock data based on a schema, using the awesome fake/random data generators like (FakerJs, ChanceJs, CasualJs and RandExpJs), all in one tool to generate your fake data for testing.
A simplified way to generate massive mock data based on a schema, using the awesome fake/random data generators like ([Faker-JS](https://fakerjs.dev/), ChanceJs, CasualJs and RandExpJs), all in one tool to generate your fake data for testing.

Now the library has been migrated 100% to typescript typing are included.

Expand All @@ -17,33 +17,33 @@ You can test online here: [https://danibram.github.io/mocker-data-generator/](ht
## Getting started

Install the module and the awesome generator you want:
`npm install mocker-data-generator faker`
`npm install mocker-data-generator @faker-js/faker`

Import it

```javascript
var mocker = require('mocker-data-generator').default // (vanilla way)
var faker = require('faker')
var { faker } = require('@faker-js/faker')


// or

import mocker from 'mocker-data-generator' // (ES6 or Typescript way)
import * as faker from 'faker'
import { faker } from '@faker-js/faker'
```

Then use it:

```javascript
var user = {
firstName: {
faker: 'name.firstName()'
faker: 'person.firstName()'
},
lastName: {
faker: 'name.lastName()'
faker: 'person.lastName()'
},
country: {
faker: 'address.country()'
faker: 'location.country()'
},
createdAt: {
faker: 'date.past()'
Expand All @@ -65,7 +65,7 @@ var group = {
users: [
{
function: function () {
return this.generators.faker.random.arrayElement(this.db.user)
return this.generators.faker.helpers.arrayElement(this.db.user)
.username
},
length: 10,
Expand All @@ -78,10 +78,10 @@ var conditionalField = {
values: ['HOUSE', 'CAR', 'MOTORBIKE']
},
'object.type=="HOUSE",location': {
faker: 'address.city()'
faker: 'location.city()'
},
'object.type=="CAR"||object.type=="MOTORBIKE",speed': {
faker: 'random.number()'
faker: 'number.int()'
}
}

Expand Down Expand Up @@ -364,7 +364,7 @@ Data generation goes with model based composed by generators, the generators can
// Faker
[name of the generator injected]: 'path inside the generator'
// If faker is injected with .addGenerator('faker', faker) then you can use:
faker: 'random.arrayElement(db.users).userId'
faker: 'helpers.arrayElement(db.users).userId'
// Function that has included index, length and self that refers at the actual array generation
function: function (index, length, self){ return /**/ }

Expand All @@ -384,13 +384,13 @@ It happens!Mocker now is independant of the generators so I hope this will give
```javascript
var mocker = require('../build/main').default
var faker = require('faker')
var { faker } = require('@faker-js/faker')
var Randexp = require('randexp')
var util = require('util')

var user = {
firstName: {
faker: 'name.firstName()'
faker: 'person.firstName()'
},
notes: {
randexp: /hello+ (world|to you)/
Expand Down Expand Up @@ -421,7 +421,7 @@ It happens!Mocker now is independant of the generators so I hope this will give
// Faker
[name of the generator injected]: 'path inside the generator'
// If faker is injected with .addGenerator('faker', faker) then you can use:
faker: 'random.arrayElement(db.users).userId'
faker: 'helpers.arrayElement(db.users).userId'
// Static
static: 'any static field'
// Function
Expand Down Expand Up @@ -523,7 +523,7 @@ json-schema-faker is awesome and works really nice, but i need a simplified and
I couldn't do this without this awesome libraries, so thanks to all:
* Faker: [[https://github.com/Marak/faker.js](https://github.com/Marak/faker.js)]
* Faker: [[https://github.com/faker-js/faker](https://github.com/faker-js/faker)]
* Chance: [[https://github.com/victorquinn/chancejs](https://github.com/victorquinn/chancejs)]
* Casual: [[https://github.com/boo1ean/casual](https://github.com/boo1ean/casual)]
* RandExpJs: [[https://github.com/fent/randexp.js](https://github.com/fent/randexp.js)]
Expand All @@ -535,7 +535,7 @@ Licensed under the MIT license. 2022
[paypal-badge]: https://img.shields.io/badge/❤%20support-paypal-blue.svg?style=flat-square
[paypal-link]: https://www.paypal.me/danibram
[https://github.com/marak/faker.js]: https://github.com/Marak/faker.js
[https://github.com/faker-js/faker]: https://github.com/faker-js/faker
[https://github.com/victorquinn/chancejs]: https://github.com/victorquinn/chancejs
[https://github.com/boo1ean/casual]: https://github.com/boo1ean/casual
[https://github.com/fent/randexp.js]: https://github.com/fent/randexp.js
Expand Down
8 changes: 4 additions & 4 deletions _docs_website/examples/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module.exports = `var person_with_pseudonyms = {
chance: 'guid'
},
real_name: {
faker: "name.firstName"
faker: "person.firstName"
},
pseudonyms: [{
faker: 'name.firstName',
faker: 'person.firstName',
length: 10,
fixedLength: true
}]
}]
};
mocker()
.schema('person_with_pseudonyms', person_with_pseudonyms, 5)
`
`
4 changes: 2 additions & 2 deletions _docs_website/examples/condition.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = `var conditional = {
condition: {
faker: 'helpers.randomize(["email", "user"])'
faker: 'helpers.arrayElement(["email", "user"])'
},
'object.condition==="email",show': {
static: 'email'
Expand All @@ -16,7 +16,7 @@ module.exports = `var conditional = {
}
}
var user = {faker: 'name.findName'}
var user = {faker: 'person.findName'}
var email = {faker: 'internet.email'}
mocker()
Expand Down
2 changes: 1 addition & 1 deletion _docs_website/examples/initial.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = `var cat = {
chance: 'guid'
},
age: {
faker: 'random.number({"min": 1, "max": 17})'
faker: 'number.int({"min": 1, "max": 17})'
},
country: {
casual: 'country'
Expand Down
4 changes: 2 additions & 2 deletions _docs_website/examples/unique.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = `var user = {
chance: 'guid'
},
name: {
faker: 'name.findName'
faker: 'person.findName'
},
email: {
faker: 'internet.email'
Expand All @@ -14,7 +14,7 @@ var house = {
chance: 'guid'
},
address: {
faker: 'address.streetAddress'
faker: 'location.streetAddress'
},
householder: {
hasOne: 'users'
Expand Down
4 changes: 2 additions & 2 deletions _docs_website/examples/withDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = `var user = {
chance: 'guid'
},
name: {
faker: 'name.findName'
faker: 'person.findName'
},
email: {
faker: 'internet.email'
Expand All @@ -14,7 +14,7 @@ var house = {
chance: 'guid'
},
address: {
faker: 'address.streetAddress'
faker: 'location.streetAddress'
},
householder: {
hasOne: 'users'
Expand Down
4 changes: 2 additions & 2 deletions _docs_website/examples/withSeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var cat = {
chance: 'guid'
},
age: {
faker: 'random.number({"min": 1, "max": 17})'
faker: 'number.int({"min": 1, "max": 17})'
},
country: {
casual: 'country'
Expand All @@ -29,4 +29,4 @@ var cat = {
mocker()
.seed('cats', seedCats)
.schema('cats', cat, 1)
`
`
14 changes: 7 additions & 7 deletions _docs_website/public/examples.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"array": "var person_with_pseudonyms = {\n id: {\n chance: 'guid'\n },\n real_name: {\n faker: \"name.firstName\"\n },\n pseudonyms: [{\n faker: 'name.firstName',\n length: 10,\n fixedLength: true\n }] \n};\n\nmocker()\n .schema('person_with_pseudonyms', person_with_pseudonyms, 5)\n",
"condition": "var conditional = {\n condition: {\n faker: 'helpers.randomize([\"email\", \"user\"])'\n },\n 'object.condition===\"email\",show': {\n static: 'email'\n },\n 'object.condition===\"user\",show': {\n static: 'user'\n },\n 'object.condition===\"email\",email': {\n hasOne: 'emails'\n },\n 'object.condition===\"user\",user': {\n hasOne: 'users'\n }\n}\n\nvar user = {faker: 'name.findName'}\nvar email = {faker: 'internet.email'}\n\nmocker()\n .schema('users', user, 2)\n .schema('emails', email, 2)\n .schema('situation', conditional, {min:2, max: 5})\n",
"initial": "var cat = {\n id: {\n chance: 'guid'\n },\n age: {\n faker: 'random.number({\"min\": 1, \"max\": 17})'\n },\n country: {\n casual: 'country'\n },\n favorite_greeting: {\n randexp: /feed (me|you and me|he and me|she and me)/\n },\n name: {\n values: ['txuri', 'pitxi', 'kitty', 'obi']\n }\n};\n\nmocker()\n .schema('cats', cat, 1)\n",
"unique": "var user = {\n id: {\n chance: 'guid'\n },\n name: {\n faker: 'name.findName'\n },\n email: {\n faker: 'internet.email'\n }\n};\nvar house = {\n id: {\n chance: 'guid'\n },\n address: {\n faker: 'address.streetAddress'\n },\n householder: {\n hasOne: 'users'\n },\n mateIds: {\n hasMany: 'users',\n max: 2,\n min: 2,\n unique: true\n }\n};\n\nmocker()\n .schema('users', user, 3)\n .schema('houses', house, 1)\n",
"array": "var person_with_pseudonyms = {\n id: {\n chance: 'guid'\n },\n real_name: {\n faker: \"person.firstName\"\n },\n pseudonyms: [{\n faker: 'person.firstName',\n length: 10,\n fixedLength: true\n }] \n};\n\nmocker()\n .schema('person_with_pseudonyms', person_with_pseudonyms, 5)\n",
"condition": "var conditional = {\n condition: {\n faker: 'helpers.arrayElement([\"email\", \"user\"])'\n },\n 'object.condition===\"email\",show': {\n static: 'email'\n },\n 'object.condition===\"user\",show': {\n static: 'user'\n },\n 'object.condition===\"email\",email': {\n hasOne: 'emails'\n },\n 'object.condition===\"user\",user': {\n hasOne: 'users'\n }\n}\n\nvar user = {faker: 'person.fullName'}\nvar email = {faker: 'internet.email'}\n\nmocker()\n .schema('users', user, 2)\n .schema('emails', email, 2)\n .schema('situation', conditional, {min:2, max: 5})\n",
"initial": "var cat = {\n id: {\n chance: 'guid'\n },\n age: {\n faker: 'number.int({\"min\": 1, \"max\": 17})'\n },\n country: {\n casual: 'country'\n },\n favorite_greeting: {\n randexp: /feed (me|you and me|he and me|she and me)/\n },\n name: {\n values: ['txuri', 'pitxi', 'kitty', 'obi']\n }\n};\n\nmocker()\n .schema('cats', cat, 1)\n",
"unique": "var user = {\n id: {\n chance: 'guid'\n },\n name: {\n faker: 'person.fullName'\n },\n email: {\n faker: 'internet.email'\n }\n};\nvar house = {\n id: {\n chance: 'guid'\n },\n address: {\n faker: 'location.streetAddress'\n },\n householder: {\n hasOne: 'users'\n },\n mateIds: {\n hasMany: 'users',\n max: 2,\n min: 2,\n unique: true\n }\n};\n\nmocker()\n .schema('users', user, 3)\n .schema('houses', house, 1)\n",
"virtuals": "var model = {\n exampleVirtual: {\n incrementalId: 0,\n virtual: true\n },\n\n id: {\n function: function() {\n return this.object.exampleVirtual\n }\n }\n}\n\nmocker()\n .schema('situation', model, 3)\n",
"withDB": "var user = {\n id: {\n chance: 'guid'\n },\n name: {\n faker: 'name.findName'\n },\n email: {\n faker: 'internet.email'\n }\n};\nvar house = {\n id: {\n chance: 'guid'\n },\n address: {\n faker: 'address.streetAddress'\n },\n householder: {\n hasOne: 'users'\n },\n mateIds: {\n hasMany: 'users',\n max: 3,\n min: 1,\n get: 'id'\n }\n};\n\nmocker()\n .schema('users', user, 3)\n .schema('houses', house, 1)\n",
"withSeed": "var seedCats = [{\n \"id\": \"bde09c8b-7d66-5818-a5bd-d46d780e7256\",\n \"age\": 32,\n \"country\": \"Vietnam\",\n \"favorite_greeting\": \"feed you and me\",\n \"name\": \"kitty bond\"\n}]\n\n\n\nvar cat = {\n id: {\n chance: 'guid'\n },\n age: {\n faker: 'random.number({\"min\": 1, \"max\": 17})'\n },\n country: {\n casual: 'country'\n },\n favorite_greeting: {\n randexp: /feed (me|you and me|he and me|she and me)/\n },\n name: {\n values: ['txuri', 'pitxi', 'kitty', 'obi']\n }\n};\n\nmocker()\n .seed('cats', seedCats)\n .schema('cats', cat, 1)\n"
}
"withDB": "var user = {\n id: {\n chance: 'guid'\n },\n name: {\n faker: 'person.fullName'\n },\n email: {\n faker: 'internet.email'\n }\n};\nvar house = {\n id: {\n chance: 'guid'\n },\n address: {\n faker: 'location.streetAddress'\n },\n householder: {\n hasOne: 'users'\n },\n mateIds: {\n hasMany: 'users',\n max: 3,\n min: 1,\n get: 'id'\n }\n};\n\nmocker()\n .schema('users', user, 3)\n .schema('houses', house, 1)\n",
"withSeed": "var seedCats = [{\n \"id\": \"bde09c8b-7d66-5818-a5bd-d46d780e7256\",\n \"age\": 32,\n \"country\": \"Vietnam\",\n \"favorite_greeting\": \"feed you and me\",\n \"name\": \"kitty bond\"\n}]\n\n\n\nvar cat = {\n id: {\n chance: 'guid'\n },\n age: {\n faker: 'number.int({\"min\": 1, \"max\": 17})'\n },\n country: {\n casual: 'country'\n },\n favorite_greeting: {\n randexp: /feed (me|you and me|he and me|she and me)/\n },\n name: {\n values: ['txuri', 'pitxi', 'kitty', 'obi']\n }\n};\n\nmocker()\n .seed('cats', seedCats)\n .schema('cats', cat, 1)\n"
}
6 changes: 3 additions & 3 deletions example/bench.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as fakerJS from 'faker'
import { faker } from '@faker-js/faker'
var Generator = require('../build/main').Generator

const gen = new Generator()

let res
console.time('faker new')
res = gen.custom({ generator: fakerJS, input: 'lorem.words' })
res = gen.custom({ generator: faker, input: 'lorem.words' })
console.timeEnd('faker new')

console.time('faker eval')
res = gen.custom({ generator: fakerJS, input: 'lorem.words', eval: true })
res = gen.custom({ generator: faker, input: 'lorem.words', eval: true })
console.timeEnd('faker eval')
4 changes: 2 additions & 2 deletions example/mocker.example.randexp.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var mocker = require('../build/main').default
var faker = require('faker')
var { faker } = require('@faker-js/faker')
var Randexp = require('randexp')
var util = require('util')

var user = {
firstName: {
faker: 'name.firstName()'
faker: 'person.firstName()'
},
notes: {
randexp: /hello+ (world|to you)/
Expand Down
14 changes: 7 additions & 7 deletions example/mocker.example.readme.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var mocker = require('../build/main').default
var faker = require('faker')
var { faker } = require('@faker-js/faker')
var util = require('util')

var user = {
firstName: {
faker: 'name.firstName()'
faker: 'person.firstName()'
},
lastName: {
faker: 'name.lastName()'
faker: 'person.lastName()'
},
country: {
faker: 'address.country()'
faker: 'location.country()'
},
createdAt: {
faker: 'date.past()'
Expand All @@ -32,7 +32,7 @@ var group = {
users: [
{
function: function () {
return this.generators.faker.random.arrayElement(this.db.user)
return this.generators.faker.helpers.arrayElement(this.db.user)
.username
},
length: 10,
Expand All @@ -45,10 +45,10 @@ var conditionalField = {
values: ['HOUSE', 'CAR', 'MOTORBIKE']
},
'object.type=="HOUSE",location': {
faker: 'address.city()'
faker: 'location.city()'
},
'object.type=="CAR"||object.type=="MOTORBIKE",speed': {
faker: 'random.number()'
faker: 'number.int()'
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/mocker.example.vainilla.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var mocker = require('..').default
var util = require('util')
var faker = require('faker')
var { faker } = require('@faker-js/faker')

var cat2 = {
name: { static: 'txuri' },
Expand Down
37 changes: 24 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 820a8c0

Please sign in to comment.