diff --git a/README.md b/README.md index 4651c2b..aaee4e9 100644 --- a/README.md +++ b/README.md @@ -121,16 +121,19 @@ Inside every value you can put: - ***length***: to know how many values - ***fixedLength***: true to create always same amount of values in the array, false to generate a random number bettwen 0 and 'length' value. ```javascript - [{ - //Any generator - //Faker - faker: 'random.arrayElement(db.users)[userId]' - //Chance - chance: 'integer' - //Function - function: function (){ return /**/ } + [{ + //Any generator + //Faker + faker: 'random.arrayElement(db.users)[userId]' + //Chance + chance: 'integer' + //Function + function: function (){ return /**/ } - }, {length: 10, fixedLength: false}] + //Array config + length: 10, + fixedLength: false + }] ``` #### Optional fields - ***[virtual]***: Boolean, if you pass this option, this mean that this field will not appear at the output entity. But you can use during the generation. @@ -181,6 +184,41 @@ m.generate('user', 2) ## Release History +#### (0.5.0) +- Break Point with array config. Now is more clear. + + Old config: + ```javascript + [{ + //Any generator + //Faker + faker: 'random.arrayElement(db.users)[userId]' + //Chance + chance: 'integer' + //Function + function: function (){ return /**/ } + + }, //Array config + {length: 10, fixedLength: false}] + ``` + + New configuration: + ```javascript + [{ + //Any generator + //Faker + faker: 'random.arrayElement(db.users)[userId]' + //Chance + chance: 'integer' + //Function + function: function (){ return /**/ } + + //Array config + length: 10, + fixedLength: false + }] + ``` + #### (0.4.7) - Add virtual fields diff --git a/build/mocker.js b/build/mocker.js index 679af97..4fd0024 100644 --- a/build/mocker.js +++ b/build/mocker.js @@ -197,7 +197,7 @@ return /******/ (function(modules) { // webpackBootstrap var proccessNode = function proccessNode(obj, k, value, path) { if (path) { if (utils.isArray(value)) { - if (value[1].virtual) { + if (value[0].virtual) { _this2.virtualPaths.push(path.toString()); } } else { @@ -255,9 +255,8 @@ return /******/ (function(modules) { // webpackBootstrap value: function generator(field, cb) { if (utils.isArray(field)) { var fieldConfig = field[0]; - var arrayConfig = field[1]; var array = []; - var length = utils.fieldArrayCalcLength(arrayConfig); + var length = utils.fieldArrayCalcLength(fieldConfig); for (var i = 0; i < length; i++) { array.push(this.generateNormalField(fieldConfig)); } diff --git a/build/mocker.min.js b/build/mocker.min.js index 5ccbae8..1a6f796 100644 --- a/build/mocker.min.js +++ b/build/mocker.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("faker"),require("chance"),require("immutable"),require("babel-polyfill")):"function"==typeof define&&define.amd?define(["faker","chance","immutable","babel-polyfill"],t):"object"==typeof exports?exports.MockerData=t(require("faker"),require("chance"),require("immutable"),require("babel-polyfill")):e.MockerData=t(e.faker,e.chance,e.immutable,e["babel-polyfill"])}(this,function(__WEBPACK_EXTERNAL_MODULE_3__,__WEBPACK_EXTERNAL_MODULE_4__,__WEBPACK_EXTERNAL_MODULE_8__,__WEBPACK_EXTERNAL_MODULE_9__){return function(e){function t(n){if(r[n])return r[n].exports;var a=r[n]={exports:{},id:n,loaded:!1};return e[n].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}var a=r(1),i=n(a);r(9);var o=function(e){return new i["default"](e)};e.exports=o},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function a(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t["default"]=e,t}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function e(e,t){for(var r=0;r0?(0,h["default"])(this.virtualPaths,this.entity,{strict:!0,symbol:","}).then(t):t(this.entity)}},{key:"generator",value:function(e,t){if(u.isArray(e)){for(var r=e[0],n=e[1],a=[],i=u.fieldArrayCalcLength(n),o=0;i>o;o++)a.push(this.generateNormalField(r));t(a)}else t(this.generateNormalField(e))}},{key:"generateNormalField",value:function(e){var t=this.entity,r=this.data;if(e.faker)return u.stringToFn("faker",e.faker,r,t);if(e.chance)return u.stringToFn("chance",e.chance,r,t);if(e.values)return b.random.arrayElement(e.values);if(e["function"])return e["function"].call({object:t,faker:b,chance:y,db:r});if(e["static"])return e["static"];if(e.hasOwnProperty("incrementalId")){var n=0;return r[this.entityOutputName]&&r[this.entityOutputName].length&&(n=r[this.entityOutputName].length),e.incrementalId===!0&&(e.incrementalId=0),n+parseInt(e.incrementalId)}return null}}]),e}();t["default"]=g},function(module,exports,__webpack_require__){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var faker=__webpack_require__(3),Chance=__webpack_require__(4),chance=new Chance,_Math=Math,floor=_Math.floor,_Object=Object,keys=_Object.keys,evalWithContextData=exports.evalWithContextData=function evalWithContextData(key,object){return eval(key)},fieldArrayCalcLength=exports.fieldArrayCalcLength=function(e){var t=void 0;return t=e.fixedLength?e.length:Math.floor(Math.random()*e.length+1)},stringToFn=exports.stringToFn=function stringToFn(moduleName,string,db,object){var re=/(^[a-zA-Z.]*)/,matches=re.exec(string),strFn=void 0;return matches&&2===matches.length&&(strFn=moduleName+"."+string),re=/\((.*?)\)/,matches=re.exec(string),matches||(strFn=moduleName+"."+string+"()"),eval(strFn)},iamLastParent=exports.iamLastParent=function(e){var t=!1;if(this.isObject(e))for(var r=Object.keys(e),n=0;n1?!0:!1},repeatFN=exports.repeatFN=function(e,t,r){var n=0,a=function i(){t(function(){n+=1,n>=e?r():n%2e3==0?setTimeout(i,0):i()})};a()},eachSeries=exports.eachSeries=function(e,t,r){if(r=r||function(){},!e.length)return r();var n=0,a=function i(){t(e[n],function(t){t?(r(t),r=function(){}):(n+=1,n>=e.length?r():i())})};a()},isArray=exports.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)?!0:!1},isObject=exports.isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)?!0:!1},getKeys=exports.getKeys=function(e){var t=keys(e);return this.isArray(e)||(t=this.isArrayLike(e)?t.filter(function(e){return floor(Number(e))==e}):t.sort()),t},isArrayLike=exports.isArrayLike=function(e){if(!this.isObject(e))return!1;if(this.isGlobalObject(e))return!1;if(!("length"in e))return!1;var t=e.length;return 0===t?!0:t-1 in e},GLOBAL_OBJECT=new Function("return this")(),isGlobalObject=exports.isGlobalObject=function(e){return e===GLOBAL_OBJECT}},function(e,t){e.exports=require("faker")},function(e,t){e.exports=require("chance")},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e){var t={"(quiz)$":"$1zes","^(ox)$":"$1en","([m|l])ouse$":"$1ice","(matr|vert|ind)ix|ex$":"$1ices","(x|ch|ss|sh)$":"$1es","([^aeiouy]|qu)y$":"$1ies","(hive)$":"$1s","(?:([^f])fe|([lr])f)$":"$1$2ves","(shea|lea|loa|thie)f$":"$1ves",sis$:"ses","([ti])um$":"$1a","(tomat|potat|ech|her|vet)o$":"$1oes","(bu)s$":"$1ses","(alias)$":"$1es","(octop)us$":"$1i","(ax|test)is$":"$1es","(us)$":"$1es",s$:"s"},r={move:"moves",foot:"feet",goose:"geese",sex:"sexes",child:"children",man:"men",tooth:"teeth",person:"people"},n=["sheep","fish","deer","series","species","money","rice","information","equipment"];if(n.indexOf(e.toLowerCase())>=0)return e;for(var a in r){var i=new RegExp(a+"$","i"),o=r[a];if(i.test(e))return e.replace(i,o)}var s=t;for(var u in s){var i=new RegExp(u,"i");if(i.test(e))return e.replace(i,s[u])}return e+"s"}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e,t,r){return new Promise(function(n){var a=regeneratorRuntime.mark(function v(e,t,r){var n,a,i,o;return regeneratorRuntime.wrap(function(s){for(;;)switch(s.prev=s.next){case 0:if(n=e.split(r.symbol),a=t,n&&0!==n.length){s.next=4;break}return s.abrupt("return");case 4:if(t){s.next=6;break}return s.abrupt("return");case 6:i=0;case 7:if(!(i0)){s.next=24;break}return s.delegateYield(v(n.join(r.symbol),t,r),"t0",22);case 22:s.next=25;break;case 24:return s.abrupt("return");case 25:case"end":return s.stop()}},v,this)}),i=regeneratorRuntime.mark(function d(e,t,r){var n,i,o,s;return regeneratorRuntime.wrap(function(u){for(;;)switch(u.prev=u.next){case 0:n=e.split(r.symbol),i=t,o=0;case 3:if(!(o0?(0,h["default"])(this.virtualPaths,this.entity,{strict:!0,symbol:","}).then(t):t(this.entity)}},{key:"generator",value:function(e,t){if(u.isArray(e)){for(var r=e[0],n=[],a=u.fieldArrayCalcLength(r),i=0;a>i;i++)n.push(this.generateNormalField(r));t(n)}else t(this.generateNormalField(e))}},{key:"generateNormalField",value:function(e){var t=this.entity,r=this.data;if(e.faker)return u.stringToFn("faker",e.faker,r,t);if(e.chance)return u.stringToFn("chance",e.chance,r,t);if(e.values)return b.random.arrayElement(e.values);if(e["function"])return e["function"].call({object:t,faker:b,chance:y,db:r});if(e["static"])return e["static"];if(e.hasOwnProperty("incrementalId")){var n=0;return r[this.entityOutputName]&&r[this.entityOutputName].length&&(n=r[this.entityOutputName].length),e.incrementalId===!0&&(e.incrementalId=0),n+parseInt(e.incrementalId)}return null}}]),e}();t["default"]=g},function(module,exports,__webpack_require__){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var faker=__webpack_require__(3),Chance=__webpack_require__(4),chance=new Chance,_Math=Math,floor=_Math.floor,_Object=Object,keys=_Object.keys,evalWithContextData=exports.evalWithContextData=function evalWithContextData(key,object){return eval(key)},fieldArrayCalcLength=exports.fieldArrayCalcLength=function(e){var t=void 0;return t=e.fixedLength?e.length:Math.floor(Math.random()*e.length+1)},stringToFn=exports.stringToFn=function stringToFn(moduleName,string,db,object){var re=/(^[a-zA-Z.]*)/,matches=re.exec(string),strFn=void 0;return matches&&2===matches.length&&(strFn=moduleName+"."+string),re=/\((.*?)\)/,matches=re.exec(string),matches||(strFn=moduleName+"."+string+"()"),eval(strFn)},iamLastParent=exports.iamLastParent=function(e){var t=!1;if(this.isObject(e))for(var r=Object.keys(e),n=0;n1?!0:!1},repeatFN=exports.repeatFN=function(e,t,r){var n=0,a=function i(){t(function(){n+=1,n>=e?r():n%2e3==0?setTimeout(i,0):i()})};a()},eachSeries=exports.eachSeries=function(e,t,r){if(r=r||function(){},!e.length)return r();var n=0,a=function i(){t(e[n],function(t){t?(r(t),r=function(){}):(n+=1,n>=e.length?r():i())})};a()},isArray=exports.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)?!0:!1},isObject=exports.isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)?!0:!1},getKeys=exports.getKeys=function(e){var t=keys(e);return this.isArray(e)||(t=this.isArrayLike(e)?t.filter(function(e){return floor(Number(e))==e}):t.sort()),t},isArrayLike=exports.isArrayLike=function(e){if(!this.isObject(e))return!1;if(this.isGlobalObject(e))return!1;if(!("length"in e))return!1;var t=e.length;return 0===t?!0:t-1 in e},GLOBAL_OBJECT=new Function("return this")(),isGlobalObject=exports.isGlobalObject=function(e){return e===GLOBAL_OBJECT}},function(e,t){e.exports=require("faker")},function(e,t){e.exports=require("chance")},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e){var t={"(quiz)$":"$1zes","^(ox)$":"$1en","([m|l])ouse$":"$1ice","(matr|vert|ind)ix|ex$":"$1ices","(x|ch|ss|sh)$":"$1es","([^aeiouy]|qu)y$":"$1ies","(hive)$":"$1s","(?:([^f])fe|([lr])f)$":"$1$2ves","(shea|lea|loa|thie)f$":"$1ves",sis$:"ses","([ti])um$":"$1a","(tomat|potat|ech|her|vet)o$":"$1oes","(bu)s$":"$1ses","(alias)$":"$1es","(octop)us$":"$1i","(ax|test)is$":"$1es","(us)$":"$1es",s$:"s"},r={move:"moves",foot:"feet",goose:"geese",sex:"sexes",child:"children",man:"men",tooth:"teeth",person:"people"},n=["sheep","fish","deer","series","species","money","rice","information","equipment"];if(n.indexOf(e.toLowerCase())>=0)return e;for(var a in r){var i=new RegExp(a+"$","i"),o=r[a];if(i.test(e))return e.replace(i,o)}var s=t;for(var u in s){var i=new RegExp(u,"i");if(i.test(e))return e.replace(i,s[u])}return e+"s"}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e,t,r){return new Promise(function(n){var a=regeneratorRuntime.mark(function v(e,t,r){var n,a,i,o;return regeneratorRuntime.wrap(function(s){for(;;)switch(s.prev=s.next){case 0:if(n=e.split(r.symbol),a=t,n&&0!==n.length){s.next=4;break}return s.abrupt("return");case 4:if(t){s.next=6;break}return s.abrupt("return");case 6:i=0;case 7:if(!(i0)){s.next=24;break}return s.delegateYield(v(n.join(r.symbol),t,r),"t0",22);case 22:s.next=25;break;case 24:return s.abrupt("return");case 25:case"end":return s.stop()}},v,this)}),i=regeneratorRuntime.mark(function d(e,t,r){var n,i,o,s;return regeneratorRuntime.wrap(function(u){for(;;)switch(u.prev=u.next){case 0:n=e.split(r.symbol),i=t,o=0;case 3:if(!(o { if (path){ if ( utils.isArray(value) ){ - if (value[1].virtual){ + if (value[0].virtual){ this.virtualPaths.push(path.toString()) } } else { @@ -167,9 +167,8 @@ export default class Mocker { generator(field, cb) { if ( utils.isArray(field) ){ let fieldConfig = field[0] - let arrayConfig = field[1] let array = [] - let length = utils.fieldArrayCalcLength(arrayConfig) + let length = utils.fieldArrayCalcLength(fieldConfig) for (let i = 0; i < length; i++) { array.push(this.generateNormalField(fieldConfig)) } diff --git a/test/mocker.test.js b/test/mocker.test.js index 7f06603..3bc7cd6 100644 --- a/test/mocker.test.js +++ b/test/mocker.test.js @@ -359,6 +359,131 @@ describe('Mocker: Generators (Fields)', function() { }) }) + describe('Options: Array', function() { + it('It should recognise static field', function(done) { + var limit = 10 + var model = 'hello' + var arr = [] + for (var i = 0; i < limit; i++) { + arr.push(model) + } + + var situation = { + sites: [{ + static: model, + length: 10, + fixedLength: true + }] + } + var result = { + sites: arr + } + + var m = mocker({ + situation: situation + }) + m.generateEntity(situation, function(data) { + expect(data) + .to.deep.equal(result) + .to.not.be.undefined + .to.not.be.null + done() + }) + }) + + it('It should recognise functions field', function(done) { + var limit = 10 + var model = 'hello' + var arr = [] + for (var i = 0; i < limit; i++) { + arr.push(model) + } + + var situation = { + sites: [{ + function() { + return 'hello' + }, + + length: 10, + fixedLength: true + }] + } + var result = { + sites: arr + } + + var m = mocker({ + situation: situation + }) + m.generateEntity(situation, function(data) { + expect(data) + .to.deep.equal(result) + .to.not.be.undefined + .to.not.be.null + done() + }) + }) + + it('It should recognise fakerJs field', function(done) { + var situation = { + sites: [{ + faker: 'lorem.words()[0]', + length: 10, + fixedLength: false + }] + } + + var m = mocker({ + situation: situation + }) + m.generateEntity(situation, function(data) { + expect(data.sites) + .to.be.an('array') + .to.have.length.below(11) + .to.not.be.undefined + .to.not.be.null + for (var i = 0; i < data.sites.length; i++) { + expect(data.sites[i]) + .to.be.a('string') + .to.not.be.undefined + .to.not.be.null + } + + done() + }) + }) + + it('It should recognise chanceJs field', function(done) { + var situation = { + sites: [{ + chance: 'integer', + length: 10, + fixedLength: false + }] + } + + var m = mocker({ + situation: situation + }) + m.generateEntity(situation, function(data) { + expect(data.sites) + .to.be.an('array') + .to.have.length.below(11) + .to.not.be.undefined + .to.not.be.null + for (var i = 0; i < data.sites.length; i++) { + expect(data.sites[i]) + .to.be.a('number') + .to.not.be.undefined + .to.not.be.null + } + + done() + }) + }) + }) + describe('Options: Virtual Fields', function() { var situation = { exampleVirtual: {