mocker-data-generator
Advanced tools
Comparing version
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(require("faker")); | ||
module.exports = factory(require("faker"), require("immutable")); | ||
else if(typeof define === 'function' && define.amd) | ||
define(["faker"], factory); | ||
define(["faker", "immutable"], factory); | ||
else if(typeof exports === 'object') | ||
exports["MockerData"] = factory(require("faker")); | ||
exports["MockerData"] = factory(require("faker"), require("immutable")); | ||
else | ||
root["MockerData"] = factory(root["faker"]); | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__) { | ||
root["MockerData"] = factory(root["faker"], root["immutable"]); | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_3__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
@@ -81,6 +81,6 @@ /******/ // The module cache | ||
var faker = __webpack_require__(2); | ||
var utils = __webpack_require__(3); | ||
var Immutable = __webpack_require__(3); | ||
var utils = __webpack_require__(4); | ||
var Mocker = (function () { | ||
function Mocker(config) { | ||
this.config = config; | ||
this.data = {}; | ||
@@ -90,2 +90,3 @@ this.entity = {}; | ||
this.path = []; | ||
this.config = Immutable.fromJS(config); | ||
} | ||
@@ -100,3 +101,4 @@ Mocker.prototype.generate = function (entity, options) { | ||
utils.repeatFN(options, function (nxt) { | ||
_this.generateEntity(_this.config[entity], function (data) { | ||
var cfg = _this.config.toJS(); | ||
_this.generateEntity(cfg[entity], function (data) { | ||
d.push(data); | ||
@@ -111,5 +113,5 @@ nxt(); | ||
else { | ||
var cfg = _this.config[entity]; | ||
var cfg = _this.config.toJS(); | ||
var f = options.uniqueField; | ||
var possibleValues = cfg[f].values; | ||
var possibleValues = cfg[entity][f].values; | ||
var length_1 = possibleValues.length; | ||
@@ -222,2 +224,8 @@ utils.eachSeries(possibleValues, function (k, nxt) { | ||
module.exports = require("immutable"); | ||
/***/ }, | ||
/* 4 */ | ||
/***/ function(module, exports) { | ||
exports.each = function (arr, fn) { | ||
@@ -224,0 +232,0 @@ for (var i = 0; i < arr.length; ++i) { |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("faker")):"function"==typeof define&&define.amd?define(["faker"],e):"object"==typeof exports?exports.MockerData=e(require("faker")):t.MockerData=e(t.faker)}(this,function(__WEBPACK_EXTERNAL_MODULE_2__){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={exports:{},id:i,loaded:!1};return t[i].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function i(t){return t&&t.__esModule?t:{"default":t}}Object.defineProperty(e,"__esModule",{value:!0});var r=n(1),o=i(r),a=function(t){return new o["default"](t)};e["default"]=a,t.exports=e["default"]},function(t,e,n){var i=n(2),r=n(3),o=function(){function t(t){this.config=t,this.data={},this.entity={},this.initialData=null,this.path=[]}return t.prototype.generate=function(t,e){var n=this,i=[];return this.data[t+"s"]=[],this.initialData={},new Promise(function(o,a){if(Number.isInteger(e))r.repeatFN(e,function(e){n.generateEntity(n.config[t],function(t){i.push(t),e()})},function(){n.data[t+"s"]=i,o(n.data)});else{var u=n.config[t],c=e.uniqueField,f=u[c].values;f.length;r.eachSeries(f,function(e,r){n.initialData[c]={"static":e},n.generateEntity(n.config[t],function(t){i.push(t),r()})},function(){n.data[t+"s"]=i,o(n.data)})}})},t.prototype.generateEntity=function(t,e){this.entity=Object.assign({},t),this.initialData&&(this.entity=Object.assign({},t,this.initialData)),this.iterator(this.entity,function(t){e(t)})},t.prototype.iterator=function(t,e){var n=this;r.overObject(t,function(t,e,i){var o=e[t];r.iamLastChild(o)?n.generateField(o,function(o){if(r.isConditional(t)){var a=t.split(",");r.evalWithContextData(a[0],n.entity)?(e[a[1]]=o,delete n.entity[a]):delete n.entity[a]}else e[t]=o;i()}):n.iterator(o,function(){i()})},function(){e(t)})},t.prototype.generateField=function(t,e){e(r.isArray(t)?this.generateArrayField(t[0],t[1]):this.generateNormalField(t))},t.prototype.generateArrayField=function(t,e){for(var n=[],i=r.fieldArrayCalcLength(e),o=0;i>o;o++)n.push(this.generateNormalField(t));return n},t.prototype.generateNormalField=function(t){var e=this.entity,n=this.data;if(t.faker){var r=t.faker.split(".");return i[r[0]][r[1]].call()}return t.values?i.random.arrayElement(t.values):t["function"]?t["function"].call({object:e,faker:i,db:n}):t["static"]?t["static"]:null},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o},function(t,e){t.exports=require("faker")},function(module,exports){exports.each=function(t,e){for(var n=0;n<t.length;++n)e(t[n])},exports.iamLastChild=function(t){var e=this;if(this.isObject(t)){var n=Object.keys(t),i=null;return n.map(function(n){return e.isObject(t[n])?void(i=!1):void(i=!0)}),i}return!0},exports.iamLastParent=function(t){for(var e=Object.keys(t),n=null,i=0;i<e.length;i++){var r=e[i];if(t[r]&&this.iamLastChild(t[r])){n=!0;break}n=!1;break}return n},exports.isConditional=function(t){var e=t.split(",");return e.length>1?!0:!1},exports.evalWithContextData=function(key,object){return eval(key)},exports.fieldArrayCalcLength=function(t){return t.fixedLength?t.length:Math.floor(Math.random()*t.length+1)},exports.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)?!0:!1},exports.isObject=function(t){return"[object Object]"===Object.prototype.toString.call(t)?!0:!1},exports.repeatFN=function(t,e,n){var i=0,r=function(){e(function(){i+=1,i>=t?n():r()})};r()},exports.eachSeries=function(t,e,n){if(n=n||function(){},!t.length)return n();var i=0,r=function(){e(t[i],function(e){e?(n(e),n=function(){}):(i+=1,i>=t.length?n():r())})};r()},exports.overObject=function(t,e,n){n=n||function(){};var i=Object.keys(t);if(!i.length)return n();var r=0,o=function(){var a=i[r];e(a,t,function(t){t?(n(t),n=function(){}):(r+=1,r>=i.length?n():o())})};o()}}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("faker"),require("immutable")):"function"==typeof define&&define.amd?define(["faker","immutable"],e):"object"==typeof exports?exports.MockerData=e(require("faker"),require("immutable")):t.MockerData=e(t.faker,t.immutable)}(this,function(__WEBPACK_EXTERNAL_MODULE_2__,__WEBPACK_EXTERNAL_MODULE_3__){return function(t){function e(i){if(n[i])return n[i].exports;var r=n[i]={exports:{},id:i,loaded:!1};return t[i].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function i(t){return t&&t.__esModule?t:{"default":t}}Object.defineProperty(e,"__esModule",{value:!0});var r=n(1),a=i(r),o=function(t){return new a["default"](t)};e["default"]=o,t.exports=e["default"]},function(t,e,n){var i=n(2),r=n(3),a=n(4),o=function(){function t(t){this.data={},this.entity={},this.initialData=null,this.path=[],this.config=r.fromJS(t)}return t.prototype.generate=function(t,e){var n=this,i=[];return this.data[t+"s"]=[],this.initialData={},new Promise(function(r,o){if(Number.isInteger(e))a.repeatFN(e,function(e){var r=n.config.toJS();n.generateEntity(r[t],function(t){i.push(t),e()})},function(){n.data[t+"s"]=i,r(n.data)});else{var u=n.config.toJS(),c=e.uniqueField,f=u[t][c].values;f.length;a.eachSeries(f,function(e,r){n.initialData[c]={"static":e},n.generateEntity(n.config[t],function(t){i.push(t),r()})},function(){n.data[t+"s"]=i,r(n.data)})}})},t.prototype.generateEntity=function(t,e){this.entity=Object.assign({},t),this.initialData&&(this.entity=Object.assign({},t,this.initialData)),this.iterator(this.entity,function(t){e(t)})},t.prototype.iterator=function(t,e){var n=this;a.overObject(t,function(t,e,i){var r=e[t];a.iamLastChild(r)?n.generateField(r,function(r){if(a.isConditional(t)){var o=t.split(",");a.evalWithContextData(o[0],n.entity)?(e[o[1]]=r,delete n.entity[o]):delete n.entity[o]}else e[t]=r;i()}):n.iterator(r,function(){i()})},function(){e(t)})},t.prototype.generateField=function(t,e){e(a.isArray(t)?this.generateArrayField(t[0],t[1]):this.generateNormalField(t))},t.prototype.generateArrayField=function(t,e){for(var n=[],i=a.fieldArrayCalcLength(e),r=0;i>r;r++)n.push(this.generateNormalField(t));return n},t.prototype.generateNormalField=function(t){var e=this.entity,n=this.data;if(t.faker){var r=t.faker.split(".");return i[r[0]][r[1]].call()}return t.values?i.random.arrayElement(t.values):t["function"]?t["function"].call({object:e,faker:i,db:n}):t["static"]?t["static"]:null},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o},function(t,e){t.exports=require("faker")},function(t,e){t.exports=require("immutable")},function(module,exports){exports.each=function(t,e){for(var n=0;n<t.length;++n)e(t[n])},exports.iamLastChild=function(t){var e=this;if(this.isObject(t)){var n=Object.keys(t),i=null;return n.map(function(n){return e.isObject(t[n])?void(i=!1):void(i=!0)}),i}return!0},exports.iamLastParent=function(t){for(var e=Object.keys(t),n=null,i=0;i<e.length;i++){var r=e[i];if(t[r]&&this.iamLastChild(t[r])){n=!0;break}n=!1;break}return n},exports.isConditional=function(t){var e=t.split(",");return e.length>1?!0:!1},exports.evalWithContextData=function(key,object){return eval(key)},exports.fieldArrayCalcLength=function(t){return t.fixedLength?t.length:Math.floor(Math.random()*t.length+1)},exports.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)?!0:!1},exports.isObject=function(t){return"[object Object]"===Object.prototype.toString.call(t)?!0:!1},exports.repeatFN=function(t,e,n){var i=0,r=function(){e(function(){i+=1,i>=t?n():r()})};r()},exports.eachSeries=function(t,e,n){if(n=n||function(){},!t.length)return n();var i=0,r=function(){e(t[i],function(e){e?(n(e),n=function(){}):(i+=1,i>=t.length?n():r())})};r()},exports.overObject=function(t,e,n){n=n||function(){};var i=Object.keys(t);if(!i.length)return n();var r=0,a=function(){var o=i[r];e(o,t,function(t){t?(n(t),n=function(){}):(r+=1,r>=i.length?n():a())})};a()}}])}); |
{ | ||
"name": "mocker-data-generator", | ||
"version": "0.1.2", | ||
"version": "0.1.5", | ||
"description": "A simplified way to generate mock data, build over Faker", | ||
@@ -29,3 +29,5 @@ "main": "./build/mocker.min.js", | ||
"dev:watch": "webpack --watch --config webpack.config.base.js lib/main.js --target node --output-library-target umd --output-library MockerData build/mocker.js", | ||
"test": "./node_modules/.bin/mocha ./test/mocker.test.js" | ||
"test": "./node_modules/.bin/mocha ./test/mocker.test.js", | ||
"precommit": "npm test", | ||
"prepush": "npm test" | ||
}, | ||
@@ -51,4 +53,5 @@ "devDependencies": { | ||
"dependencies": { | ||
"faker": "^3.0.1" | ||
"faker": "^3.0.1", | ||
"immutable": "^3.7.5" | ||
} | ||
} |
@@ -116,5 +116,6 @@ # mocker-data-generator | ||
####(0.1.2) | ||
####(0.1.5) | ||
- Fix an error: (Clean initial data field) | ||
- Add a test to check it | ||
- Fix some memory errors adding inmutableJS for the model | ||
- Add new tests | ||
@@ -127,3 +128,3 @@ ####(0.1.1) | ||
####(0.0.1) | ||
####(0.0.4) | ||
- First release i will update soon with tests and more examples, stay tuned! | ||
@@ -130,0 +131,0 @@ |
import faker = require('faker') | ||
import Immutable = require('immutable'); | ||
import * as utils from './utils.ts' | ||
///MAKE the config object the final object!!!! | ||
export default class Mocker { | ||
public config: Immutable.Map<string, number> | ||
public data = {} | ||
@@ -12,4 +13,7 @@ public entity = {} | ||
public path = [] | ||
constructor(private config: any) {} | ||
constructor(config: any) { | ||
this.config = Immutable.fromJS(config) | ||
} | ||
generate(entity: string, options: any) { | ||
@@ -21,7 +25,8 @@ let d = [] | ||
return new Promise((resolve, reject) => { | ||
if ((Number as any).isInteger(options)){ | ||
if ((Number as any).isInteger(options)){ | ||
utils.repeatFN( options, | ||
(nxt) => { | ||
this.generateEntity(this.config[entity], function (data) { | ||
let cfg = this.config.toJS() | ||
this.generateEntity(cfg[entity], function (data) { | ||
d.push(data) | ||
@@ -37,5 +42,5 @@ nxt() | ||
} else { | ||
let cfg = this.config[entity] | ||
let cfg = this.config.toJS() | ||
let f = options.uniqueField | ||
let possibleValues = cfg[f].values | ||
let possibleValues = cfg[entity][f].values | ||
let length = possibleValues.length | ||
@@ -62,2 +67,3 @@ | ||
generateEntity(entityConfig: Object, cb) { | ||
this.entity = (Object as any).assign({}, entityConfig) | ||
@@ -97,3 +103,3 @@ | ||
} else { | ||
this.iterator(lvl, function (){ | ||
this.iterator(lvl, () => { | ||
nxt() | ||
@@ -100,0 +106,0 @@ }) |
@@ -5,2 +5,3 @@ var mocker = require('../build/mocker.js') | ||
var faker = require('faker') | ||
var util = require('util') | ||
@@ -18,3 +19,3 @@ var config = { | ||
it('Should load config correctly', function() { | ||
conf = m.config | ||
conf = m.config.toJS() | ||
expect(conf).to.deep.equal(config) | ||
@@ -291,16 +292,12 @@ }) | ||
it('Should generate prefixed valued data', function(done) { | ||
var length = 10 | ||
var scheemas = { | ||
request: { | ||
type: { | ||
values: [] | ||
request: { | ||
type: { | ||
values: [] | ||
} | ||
} | ||
}, | ||
request2: { | ||
type:{ | ||
static: 'staticValue' | ||
} | ||
} | ||
} | ||
var expectedResult = [] | ||
@@ -316,16 +313,15 @@ | ||
m.generate('request', {uniqueField: 'type'}) | ||
.then(m.generate('request2', 1)) | ||
.then(function(data) { | ||
try { | ||
expect(data.requests).to.deep.equal(expectedResult) | ||
expect(data.requests.length).to.equal(length) | ||
done() | ||
} catch (x) { | ||
done(x) | ||
} | ||
}) | ||
.then(function(data) { | ||
try { | ||
expect(data.requests).to.deep.equal(expectedResult) | ||
expect(data.requests.length).to.equal(length) | ||
done() | ||
} catch (x) { | ||
done(x) | ||
} | ||
}) | ||
}) | ||
it('Should not affect init values to next entity', function(done) { | ||
this.timeout(15000) | ||
var length = 10 | ||
@@ -378,2 +374,35 @@ | ||
it('Should generate more entities', function(done) { | ||
var length = 10 | ||
var model = { | ||
request:{ | ||
id: { | ||
faker: 'random.number' | ||
}, | ||
title: { | ||
faker: 'lorem.sentence' | ||
}, | ||
number: { | ||
faker: 'random.number' | ||
} | ||
} | ||
} | ||
var m = mocker({ act: model}) | ||
m.generate('act', length) | ||
.then(function(data) { | ||
//expect(data.requests).to.deep.equal(expectedResult) | ||
expect(data.acts.length).to.equal(length) | ||
for (var i = 0; i < length; i++) { | ||
var a = data.acts[i] | ||
expect(a).to.have.property('request') | ||
expect(a.request).to.have.property('id').not.to.be.null | ||
expect(a.request).to.have.property('title').not.to.be.null | ||
expect(a.request).to.have.property('number').not.to.be.null | ||
} | ||
done() | ||
}) | ||
}) | ||
it('Should be awesome', function(done) { | ||
@@ -380,0 +409,0 @@ done() |
/// <reference path="es6-promise/es6-promise.d.ts" /> | ||
/// <reference path="globals.d.ts" /> | ||
///<reference path='../node_modules/immutable/dist/immutable.d.ts'/> |
54360
3.56%1189
3.03%136
0.74%2
100%+ Added
+ Added