mocker-data-generator
Advanced tools
Comparing version
@@ -15,4 +15,6 @@ import { Schema } from './Schema'; | ||
restart(): Mocker; | ||
private _buildSync; | ||
buildSync(): IDB; | ||
build(cb?: (error: Error | null, _?: any) => void): Promise<any>; | ||
build(cb?: (error: Error | null, _?: any) => void): void; | ||
} |
@@ -32,25 +32,32 @@ "use strict"; | ||
}; | ||
Mocker.prototype._buildSync = function () { | ||
this.schemas.reduce(function (acc, schema) { | ||
var instances; | ||
try { | ||
instances = schema.build(acc); | ||
} | ||
catch (e) { | ||
throw new Error('Schema: "' + schema.name + '" ' + e); | ||
} | ||
// Clean virtuals | ||
if (schema.virtualPaths.length > 0) { | ||
instances.forEach(function (x) { | ||
return utils_1.cleanVirtuals(schema.virtualPaths, x, { | ||
strict: true, | ||
symbol: ',' | ||
}); | ||
}); | ||
} | ||
// Add to db | ||
acc[schema.name] = instances; | ||
return acc; | ||
}, this.DB); | ||
}; | ||
Mocker.prototype.buildSync = function () { | ||
this._buildSync(); | ||
return this.DB; | ||
}; | ||
Mocker.prototype.build = function (cb) { | ||
try { | ||
this.schemas.reduce(function (acc, schema) { | ||
var instances; | ||
try { | ||
instances = schema.build(acc); | ||
} | ||
catch (e) { | ||
throw new Error('Schema: "' + schema.name + '" ' + e); | ||
} | ||
// Clean virtuals | ||
if (schema.virtualPaths.length > 0) { | ||
instances.forEach(function (x) { | ||
return utils_1.cleanVirtuals(schema.virtualPaths, x, { | ||
strict: true, | ||
symbol: ',' | ||
}); | ||
}); | ||
} | ||
// Add to db | ||
acc[schema.name] = instances; | ||
return acc; | ||
}, this.DB); | ||
this._buildSync(); | ||
} | ||
@@ -57,0 +64,0 @@ catch (e) { |
@@ -114,6 +114,12 @@ "use strict"; | ||
supportedLocales = Object.keys(fakerJS.locales); | ||
supportedLocales.forEach(function (locale) { | ||
var res = gen.faker({ faker: 'address.streetAddress', locale: locale }); | ||
t.true(typeof res === 'string'); | ||
}); | ||
try { | ||
supportedLocales.forEach(function (locale) { | ||
var res = gen.faker({ faker: 'address.streetAddress', locale: locale }); | ||
t.true(typeof res === 'string'); | ||
}); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
console.log('Pull request maded to fakerJS repo.'); | ||
} | ||
return [2 /*return*/]; | ||
@@ -120,0 +126,0 @@ }); |
@@ -169,2 +169,30 @@ "use strict"; | ||
}); }); | ||
ava_1.default('Should build synchronously', function (t) { | ||
var result = { | ||
users: [ | ||
{ | ||
hello: 'world' | ||
} | ||
] | ||
}; | ||
var mock = new __1.Mocker(); | ||
var db = mock.schema('users', { hello: { static: 'world' } }, 1).buildSync(); | ||
t.deepEqual(db, result); | ||
}); | ||
ava_1.default('Should throw synchronously', function (t) { | ||
var result = { | ||
users: [ | ||
{ | ||
hello: 'world' | ||
} | ||
] | ||
}; | ||
var mock = new __1.Mocker(); | ||
var error = t.throws(function () { | ||
return mock | ||
.schema('users', { hello: { faker: 'worldrqwerqw' } }, 1) | ||
.buildSync(); | ||
}); | ||
t.is(error.message, 'Schema: "users" Error: "faker" This faker method doesnt exists \'worldrqwerqw\'.'); | ||
}); | ||
//# sourceMappingURL=Mocker.build.spec.js.map |
@@ -29,25 +29,32 @@ import { Schema } from './Schema'; | ||
}; | ||
Mocker.prototype._buildSync = function () { | ||
this.schemas.reduce(function (acc, schema) { | ||
var instances; | ||
try { | ||
instances = schema.build(acc); | ||
} | ||
catch (e) { | ||
throw new Error('Schema: "' + schema.name + '" ' + e); | ||
} | ||
// Clean virtuals | ||
if (schema.virtualPaths.length > 0) { | ||
instances.forEach(function (x) { | ||
return cleanVirtuals(schema.virtualPaths, x, { | ||
strict: true, | ||
symbol: ',' | ||
}); | ||
}); | ||
} | ||
// Add to db | ||
acc[schema.name] = instances; | ||
return acc; | ||
}, this.DB); | ||
}; | ||
Mocker.prototype.buildSync = function () { | ||
this._buildSync(); | ||
return this.DB; | ||
}; | ||
Mocker.prototype.build = function (cb) { | ||
try { | ||
this.schemas.reduce(function (acc, schema) { | ||
var instances; | ||
try { | ||
instances = schema.build(acc); | ||
} | ||
catch (e) { | ||
throw new Error('Schema: "' + schema.name + '" ' + e); | ||
} | ||
// Clean virtuals | ||
if (schema.virtualPaths.length > 0) { | ||
instances.forEach(function (x) { | ||
return cleanVirtuals(schema.virtualPaths, x, { | ||
strict: true, | ||
symbol: ',' | ||
}); | ||
}); | ||
} | ||
// Add to db | ||
acc[schema.name] = instances; | ||
return acc; | ||
}, this.DB); | ||
this._buildSync(); | ||
} | ||
@@ -54,0 +61,0 @@ catch (e) { |
@@ -5,2 +5,15 @@ # Changelog | ||
## [2.9.0](https://github.com/danibram/mocker-data-generator/compare/v2.8.0...v2.9.0) (2020-09-04) | ||
### Features | ||
* add buildSync method to Mocker class ([772b20d](https://github.com/danibram/mocker-data-generator/commit/772b20da6f9ccaba98f20a1361babe5f55810d78)) | ||
### Bug Fixes | ||
* **appveyor:** update matrix config to install defined node version ([703aa41](https://github.com/danibram/mocker-data-generator/commit/703aa41411b3813f3d1af6e79b6d2643a36dec56)) | ||
* **docs-website:** added seed example ([d1fa143](https://github.com/danibram/mocker-data-generator/commit/d1fa143f20467cf69c87e5cc4a74657429550384)) | ||
## [2.8.0](https://github.com/danibram/mocker-data-generator/compare/v2.7.0...v2.8.0) (2020-05-13) | ||
@@ -7,0 +20,0 @@ |
{ | ||
"name": "mocker-data-generator", | ||
"version": "2.8.0", | ||
"version": "2.9.0", | ||
"description": "A simplified way to generate mock data, builds using a simple schema and with the FakerJs", | ||
@@ -90,28 +90,28 @@ "main": "build/main/index.js", | ||
"@types/chance": "^1.1.0", | ||
"@types/faker": "^4.1.11", | ||
"@types/node": "^14.0.1", | ||
"ava": "^3.8.2", | ||
"codecov": "^3.6.5", | ||
"conventional-github-releaser": "^3.1.3", | ||
"gh-pages": "^2.2.0", | ||
"@types/faker": "^4.1.12", | ||
"@types/node": "^14.6.4", | ||
"ava": "^3.12.1", | ||
"codecov": "^3.7.2", | ||
"conventional-github-releaser": "^3.1.5", | ||
"gh-pages": "^3.1.0", | ||
"http-server": "^0.12.3", | ||
"multiview": "^3.0.0", | ||
"multiview": "^3.0.1", | ||
"npm-scripts-info": "^0.3.9", | ||
"nyc": "^15.0.1", | ||
"nyc": "^15.1.0", | ||
"opn-cli": "^5.0.0", | ||
"prettier": "^2.0.5", | ||
"standard-version": "^8.0.0", | ||
"trash-cli": "^3.0.0", | ||
"ts-node": "^8.10.1", | ||
"tslint": "^6.1.2", | ||
"prettier": "^2.1.1", | ||
"standard-version": "^9.0.0", | ||
"trash-cli": "^3.1.0", | ||
"ts-node": "^9.0.0", | ||
"tslint": "^6.1.3", | ||
"tslint-config-standard": "^9.0.0", | ||
"typedoc": "^0.17.6", | ||
"typescript": "^3.9.2" | ||
"typedoc": "^0.19.0", | ||
"typescript": "^4.0.2" | ||
}, | ||
"dependencies": { | ||
"casual-browserify": "^1.5.19", | ||
"chance": "^1.1.5", | ||
"faker": "^4.1.0", | ||
"chance": "^1.1.7", | ||
"faker": "^5.1.0", | ||
"randexp": "^0.5.3", | ||
"tslib": "^1.12.0" | ||
"tslib": "^2.0.1" | ||
}, | ||
@@ -118,0 +118,0 @@ "nyc": { |
@@ -127,2 +127,18 @@ # mocker-data-generator [](https://twitter.com/intent/tweet?text=Im%20testing%20mocker-data-generator%20is%20awesome!&via=danibram&url=http://bit.ly/2ziE8qT&hashtags=mock,javascript,developers) | ||
) | ||
// Synchronously | ||
// This returns an object | ||
// { | ||
// user:[array of users], | ||
// group: [array of groups], | ||
// conditionalField: [array of conditionalFields] | ||
// } | ||
var data = mocker() | ||
.schema('user', user, 2) | ||
.schema('group', group, 2) | ||
.schema('conditionalField', conditionalField, 2) | ||
.buildSync() | ||
console.log(util.inspect(data, { depth: 10 })) | ||
``` | ||
@@ -148,2 +164,3 @@ | ||
* **_build(callback)_**: This methods start to produce the data and wrap it to the callback function, the callback funtion have 2 parameters, error and data generated. | ||
- **_buildSync()_**: Synchronous version of `build(callback)`. Returns generated data or throws an error. | ||
@@ -150,0 +167,0 @@ ### Schema definition |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
259827
1.36%3422
1.48%582
3.01%+ Added
+ Added
- Removed
- Removed
Updated
Updated
Updated