diff --git a/CHANGELOG.md b/CHANGELOG.md index b3a4ace..a100e1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History +## (1.1.1) +- Added RandExpJs generator +- Improve test system (I know im improving it! =P) + ## (1.1.0) - Added casualJs - Added self option diff --git a/README.md b/README.md index b07ef48..898ddb2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ [![GitHub license](https://img.shields.io/github/license/danibram/mocker-data-generator.svg?style=flat-square)][npm-home-module][![GitHub license](https://img.shields.io/npm/dt/mocker-data-generator.svg?style=flat-square)][npm-home-module][![GitHub license](https://img.shields.io/badge/awesome-true-orange.svg?style=flat-square)][npm-home-module] -A simplified way to generate masive mock data based on a schema, and you can use super cool libraries like fakerJs and chanceJs to generate fake data. +A simplified way to generate masive 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. ## Getting started @@ -173,6 +173,20 @@ Data generation goes with model based composed by generators, the generators can { chance: 'street_suffixes()[0]["name"]' } //Run chance.street_suffixes() takes first result and the name inside ``` +- ***casual***: you can use directly use casualJs functions, you can do: (note that, db (actual entities generated), object (actual entity generated) are injected), ***you must pass an exactly JSON syntax***: + +```javascript + { casual: 'country' } + { chance: 'array_of_digits()' } + { casual: 'array_of_digits(3)[0]' } +``` + +- ***randexp***: pass a regexp string to use randexp generator. + + ```javascript + { randexp: /hello+ (world|to you)/ } + ``` + - ***[Array]***: you can pass an array that indicates an array of data you can create, passing in the first field the generator (function, faker, or array(not Tested)), and in the second field pass a config object (length, fixedLentgh) - ***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. False by default. diff --git a/lib/Schema.js b/lib/Schema.js index d345a05..c37f89c 100644 --- a/lib/Schema.js +++ b/lib/Schema.js @@ -128,6 +128,8 @@ var Schema = function () { return (0, _utils.stringToFn)('chance', config.chance, object, db); } else if (config.casual) { return (0, _utils.stringToFn)('casual', config.casual, object, db); + } else if (config.randexp) { + return (0, _utils.randexpWrapper)(config.randexp); } else if (config.self) { return (0, _utils.stringToFn)('object', config.self, object, db); } else if (config.db) { diff --git a/lib/Schema.js.map b/lib/Schema.js.map index a7c90e7..5f25e0d 100644 --- a/lib/Schema.js.map +++ b/lib/Schema.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/Schema.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,IAAM,SAAS,sBAAT;;AAEN,IAAI,UAAU,SAAV,OAAU,CAAU,GAAV,EAAe,GAAf,EAAoB,WAApB,EAAiC;;;AAC3C,QAAI,CAAC,WAAD,EAAc;AAAE,sBAAc,EAAd,CAAF;KAAlB;AACA,WAAO,IAAP,CAAY,GAAZ,EACK,GADL,CACS,UAAC,CAAD,EAAO;AACR,YAAI,QAAQ,IAAI,CAAJ,CAAR,CADI;;AAGR,YAAI,OAAO,YAAY,KAAZ,CAAkB,CAAlB,CAAP,CAHI;AAIR,aAAK,IAAL,CAAU,CAAV,EAJQ;;AAOR,YAAI,0BAAc,KAAd,CAAJ,EAA0B;;AAEtB,gBAAI,IAAJ,EAAS;AACL,oBAAK,oBAAQ,KAAR,CAAL,EAAqB;AACjB,wBAAI,MAAM,CAAN,KAAY,MAAM,CAAN,EAAS,OAAT,EAAiB;AAC7B,8BAAK,YAAL,CAAkB,IAAlB,CAAuB,KAAK,QAAL,EAAvB,EAD6B;qBAAjC;iBADJ,MAIO;AACH,wBAAI,MAAM,OAAN,EAAc;AACd,8BAAK,YAAL,CAAkB,IAAlB,CAAuB,KAAK,QAAL,EAAvB,EADc;qBAAlB;iBALJ;aADJ;;AAYA,gBAAI,kBAAkB,MAAK,YAAL,CAAkB,KAAlB,CAAlB,CAdkB;;AAgBtB,gBAAI,CAAC,0BAAc,CAAd,CAAD,EAAkB;AAClB,oBAAI,CAAJ,IAAS,eAAT,CADkB;aAAtB,MAEO;AACH,oBAAI,MAAM,EAAE,KAAF,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,eAAe,CAAf,GAAmB,mBAAnB,GAAyC,OAAK,IAAL,GAAY,mBAArD,CAAd,CAD2B;AAE3B;mCAAO,OAAK,EAAL,CAAQ,OAAK,IAAL;6BAAf,CAF2B;yBAA/B;;AAKA,uCAAe,GAAf,CAAmB,UAAC,KAAD,EAAW;;AAE1B,gCAAI,MAAM,GAAN,EAAW;AACX,uCADW;6BAAf;;AAIA,yCAAa,CAAb,IAAkB,EAAC,QAAQ,KAAR,EAAnB,CAN0B;;AAQ1B,mCAAK,WAAL,CAAiB,YAAjB,EAR0B;AAS1B,mCAAK,EAAL,CAAQ,OAAK,IAAL,CAAR,CAAmB,IAAnB,CAAwB,OAAK,MAAL,CAAxB,CAT0B;AAU1B,mCAAK,MAAL,GAAc,EAAd,CAV0B;yBAAX,CAAnB;wBAzB8B;;;iBAA3B,MAqCA;AACH,4BAAQ,KAAR,CAAc,eAAe,KAAK,OAAL,GAAe,qCAA9B,CAAd,CADG;iBArCA;AAwCP,mBAAO,KAAK,EAAL,CAAQ,KAAK,IAAL,CAAf,CAxEM;;;;WA9FO","file":"Schema.js","sourcesContent":["import {isObject, isArray, iamLastParent, iamLastChild, fieldArrayCalcLength, stringToFn, evalWithContextData, isConditional, fnCallWithContext} from './utils'\nimport faker from 'faker'\nimport casual from 'casual'\nimport Chance from 'chance'\nconst chance = new Chance()\n\nlet iterate = function (obj, res, currentPath) {\n if (!currentPath) { currentPath = [] }\n Object.keys(obj)\n .map((k) => {\n let value = obj[k]\n\n let path = currentPath.slice(0)\n path.push(k)\n\n\n if (iamLastParent(value)) {\n\n if (path){\n if ( isArray(value) ){\n if (value[0] && value[0].virtual){\n this.virtualPaths.push(path.toString())\n }\n } else {\n if (value.virtual){\n this.virtualPaths.push(path.toString())\n }\n }\n }\n\n let fieldCalculated = this.proccessLeaf(value)\n\n if (!isConditional(k)){\n res[k] = fieldCalculated\n } else {\n let key = k.split(',')\n if (evalWithContextData(key[0], this.result)){\n res[key[1]] = fieldCalculated\n }\n }\n } else {\n res[k] = {}\n iterate.call(this, value, res[k], path)\n }\n })\n}\n\nexport default class Schema {\n constructor(name, cfg, options){\n this.schema = cfg\n this.name = name\n this.options = options\n\n // Temp fields\n this.db = {}\n this.result = {}\n this.virtualPaths = []\n }\n\n proccessLeaf (field) {\n\n if ( isArray(field) ){\n let fieldConfig = field[0]\n let array = []\n let na = []\n\n if (fieldConfig.concat){\n na = evalWithContextData(fieldConfig.concat, this.result, this.db)\n //Strict Mode\n na = (fieldConfig.concatStrict) ? [...new Set(na)] : na\n }\n\n let length = fieldArrayCalcLength(fieldConfig, na.length)\n\n for (let i = 0; i < length; i++) {\n array.push(this.generateField(fieldConfig))\n }\n\n return array.concat(na)\n } else if (field.related){\n return this.generateField(field)\n } else {\n return this.generateField(field)\n }\n }\n\n generateField(config) {\n let object = this.result\n let db = this.db\n\n if (config.faker){\n return stringToFn('faker', config.faker, object, db)\n } else if (config.chance) {\n return stringToFn('chance', config.chance, object, db)\n } else if (config.casual) {\n return stringToFn('casual', config.casual, object, db)\n } else if (config.self) {\n return stringToFn('object', config.self, object, db)\n } else if (config.db) {\n return stringToFn('db', config.db, object, db)\n } else if (config.related) {\n let entities = this.db[config.related]\n let i = Math.floor(entities.length * Math.random());\n\n if (!config.get){\n return entities[i]\n } else {\n let entity = entities[i]\n return stringToFn('object', config.get, entity, db)\n }\n\n } else if (config.values) {\n let i = Math.floor(config.values.length * Math.random());\n return config.values[i]\n } else if (config.function) {\n return fnCallWithContext(config.function, object, db)\n } else if (config.static) {\n return config.static\n } else if (config.hasOwnProperty('incrementalId')) {\n let n = 0\n\n if (db[this.name] && db[this.name].length){\n n = db[this.name].length\n }\n if (config.incrementalId === true){\n config.incrementalId = 0\n }\n return (n + parseInt(config.incrementalId))\n } else {\n return null\n }\n }\n\n buildSingle (schema) {\n if (iamLastParent(schema)) {\n this.result = this.proccessLeaf(schema)\n } else {\n iterate.call(this, schema, this.result)\n }\n }\n\n build (db){\n this.result = {}\n this.db = db ? db : {}\n this.db[this.name] = []\n if (Number.isInteger(this.options)){\n\n /*Array.from(new Array(this.options)).map(() => {\n this.buildSingle(this.schema)\n this.db[this.name].push(this.result)\n this.result = {}\n })*/\n\n /*for (var i = 0; i < this.options; i++) {\n this.buildSingle(this.schema)\n this.db[this.name].push(this.result)\n this.result = {}\n }*/\n\n for (var i=0, il=this.options; i {\n\n if (f === '.') {\n return\n }\n\n entityConfig[f] = {static: value}\n\n this.buildSingle(entityConfig)\n this.db[this.name].push(this.result)\n this.result = {}\n })\n } else {\n console.error('An string ' + this.options + ', is not recognized as a parameter.')\n }\n return this.db[this.name]\n }\n\n\n}\n"]} \ No newline at end of file +{"version":3,"sources":["../src/Schema.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,IAAM,SAAS,sBAAT;;AAEN,IAAI,UAAU,SAAV,OAAU,CAAU,GAAV,EAAe,GAAf,EAAoB,WAApB,EAAiC;;;AAC3C,QAAI,CAAC,WAAD,EAAc;AAAE,sBAAc,EAAd,CAAF;KAAlB;AACA,WAAO,IAAP,CAAY,GAAZ,EACK,GADL,CACS,UAAC,CAAD,EAAO;AACR,YAAI,QAAQ,IAAI,CAAJ,CAAR,CADI;;AAGR,YAAI,OAAO,YAAY,KAAZ,CAAkB,CAAlB,CAAP,CAHI;AAIR,aAAK,IAAL,CAAU,CAAV,EAJQ;;AAOR,YAAI,0BAAc,KAAd,CAAJ,EAA0B;;AAEtB,gBAAI,IAAJ,EAAS;AACL,oBAAK,oBAAQ,KAAR,CAAL,EAAqB;AACjB,wBAAI,MAAM,CAAN,KAAY,MAAM,CAAN,EAAS,OAAT,EAAiB;AAC7B,8BAAK,YAAL,CAAkB,IAAlB,CAAuB,KAAK,QAAL,EAAvB,EAD6B;qBAAjC;iBADJ,MAIO;AACH,wBAAI,MAAM,OAAN,EAAc;AACd,8BAAK,YAAL,CAAkB,IAAlB,CAAuB,KAAK,QAAL,EAAvB,EADc;qBAAlB;iBALJ;aADJ;;AAYA,gBA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ADG;iCAFP;6BADJ,MAMO;AACH,wCAAQ,KAAR,CAAc,eAAe,CAAf,GAAmB,mBAAnB,GAAyC,OAAK,IAAL,GAAY,cAArD,CAAd,CADG;AAEH;uCAAO,OAAK,EAAL,CAAQ,OAAK,IAAL;iCAAf,CAFG;6BANP;yBAHJ;;AAgBA,4BAAK,CAAC,oBAAQ,cAAR,CAAD,EAA0B;AAC3B,oCAAQ,KAAR,CAAc,eAAe,CAAf,GAAmB,mBAAnB,GAAyC,OAAK,IAAL,GAAY,mBAArD,CAAd,CAD2B;AAE3B;mCAAO,OAAK,EAAL,CAAQ,OAAK,IAAL;6BAAf,CAF2B;yBAA/B;;AAKA,uCAAe,GAAf,CAAmB,UAAC,KAAD,EAAW;;AAE1B,gCAAI,MAAM,GAAN,EAAW;AACX,uCADW;6BAAf;;AAIA,yCAAa,CAAb,IAAkB,EAAC,QAAQ,KAAR,EAAnB,CAN0B;;AAQ1B,mCAAK,WAAL,CAAiB,YAAjB,EAR0B;AAS1B,mCAAK,EAAL,CAAQ,OAAK,IAAL,CAAR,CAAmB,IAAnB,CAAwB,OAAK,MAAL,CAAxB,CAT0B;AAU1B,mCAAK,MAAL,GAAc,EAAd,CAV0B;yBAAX,CAAnB;wBAzB8B;;;iBAA3B,MAqCA;AACH,4BAAQ,KAAR,CAAc,eAAe,KAAK,OAAL,GAAe,qCAA9B,CAAd,CADG;iBArCA;AAwCP,mBAAO,KAAK,EAAL,CAAQ,KAAK,IAAL,CAAf,CAxEM;;;;WAhGO","file":"Schema.js","sourcesContent":["import {isObject, isArray, iamLastParent, iamLastChild, fieldArrayCalcLength, stringToFn, evalWithContextData, isConditional, fnCallWithContext, randexpWrapper} from './utils'\nimport faker from 'faker'\nimport casual from 'casual'\nimport Chance from 'chance'\nconst chance = new Chance()\n\nlet iterate = function (obj, res, currentPath) {\n if (!currentPath) { currentPath = [] }\n Object.keys(obj)\n .map((k) => {\n let value = obj[k]\n\n let path = currentPath.slice(0)\n path.push(k)\n\n\n if (iamLastParent(value)) {\n\n if (path){\n if ( isArray(value) ){\n if (value[0] && value[0].virtual){\n this.virtualPaths.push(path.toString())\n }\n } else {\n if (value.virtual){\n this.virtualPaths.push(path.toString())\n }\n }\n }\n\n let fieldCalculated = this.proccessLeaf(value)\n\n if (!isConditional(k)){\n res[k] = fieldCalculated\n } else {\n let key = k.split(',')\n if (evalWithContextData(key[0], this.result)){\n res[key[1]] = fieldCalculated\n }\n }\n } else {\n res[k] = {}\n iterate.call(this, value, res[k], path)\n }\n })\n}\n\nexport default class Schema {\n constructor(name, cfg, options){\n this.schema = cfg\n this.name = name\n this.options = options\n\n // Temp fields\n this.db = {}\n this.result = {}\n this.virtualPaths = []\n }\n\n proccessLeaf (field) {\n\n if ( isArray(field) ){\n let fieldConfig = field[0]\n let array = []\n let na = []\n\n if (fieldConfig.concat){\n na = evalWithContextData(fieldConfig.concat, this.result, this.db)\n //Strict Mode\n na = (fieldConfig.concatStrict) ? [...new Set(na)] : na\n }\n\n let length = fieldArrayCalcLength(fieldConfig, na.length)\n\n for (let i = 0; i < length; i++) {\n array.push(this.generateField(fieldConfig))\n }\n\n return array.concat(na)\n } else if (field.related){\n return this.generateField(field)\n } else {\n return this.generateField(field)\n }\n }\n\n generateField(config) {\n let object = this.result\n let db = this.db\n\n if (config.faker){\n return stringToFn('faker', config.faker, object, db)\n } else if (config.chance) {\n return stringToFn('chance', config.chance, object, db)\n } else if (config.casual) {\n return stringToFn('casual', config.casual, object, db)\n } else if (config.randexp) {\n return randexpWrapper(config.randexp)\n } else if (config.self) {\n return stringToFn('object', config.self, object, db)\n } else if (config.db) {\n return stringToFn('db', config.db, object, db)\n } else if (config.related) {\n let entities = this.db[config.related]\n let i = Math.floor(entities.length * Math.random());\n\n if (!config.get){\n return entities[i]\n } else {\n let entity = entities[i]\n return stringToFn('object', config.get, entity, db)\n }\n\n } else if (config.values) {\n let i = Math.floor(config.values.length * Math.random());\n return config.values[i]\n } else if (config.function) {\n return fnCallWithContext(config.function, object, db)\n } else if (config.static) {\n return config.static\n } else if (config.hasOwnProperty('incrementalId')) {\n let n = 0\n\n if (db[this.name] && db[this.name].length){\n n = db[this.name].length\n }\n if (config.incrementalId === true){\n config.incrementalId = 0\n }\n return (n + parseInt(config.incrementalId))\n } else {\n return null\n }\n }\n\n buildSingle (schema) {\n if (iamLastParent(schema)) {\n this.result = this.proccessLeaf(schema)\n } else {\n iterate.call(this, schema, this.result)\n }\n }\n\n build (db){\n this.result = {}\n this.db = db ? db : {}\n this.db[this.name] = []\n if (Number.isInteger(this.options)){\n\n /*Array.from(new Array(this.options)).map(() => {\n this.buildSingle(this.schema)\n this.db[this.name].push(this.result)\n this.result = {}\n })*/\n\n /*for (var i = 0; i < this.options; i++) {\n this.buildSingle(this.schema)\n this.db[this.name].push(this.result)\n this.result = {}\n }*/\n\n for (var i=0, il=this.options; i {\n\n if (f === '.') {\n return\n }\n\n entityConfig[f] = {static: value}\n\n this.buildSingle(entityConfig)\n this.db[this.name].push(this.result)\n this.result = {}\n })\n } else {\n console.error('An string ' + this.options + ', is not recognized as a parameter.')\n }\n return this.db[this.name]\n }\n\n\n}\n"]} \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js index bc3102b..04f31dd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.conditionalField = exports.calculateKey = exports.cleanVirtuals = exports.isConditional = exports.iamLastChild = exports.iamLastParent = exports.stringToFn = exports.fieldArrayCalcLength = exports.fnCallWithContext = exports.evalWithContextData = exports.isObject = exports.isArray = undefined; +exports.conditionalField = exports.calculateKey = exports.cleanVirtuals = exports.isConditional = exports.iamLastChild = exports.iamLastParent = exports.stringToFn = exports.randexpWrapper = exports.fieldArrayCalcLength = exports.fnCallWithContext = exports.evalWithContextData = exports.isObject = exports.isArray = undefined; var _faker = require('faker'); @@ -17,6 +17,10 @@ var _casual = require('casual'); var _casual2 = _interopRequireDefault(_casual); +var _randexp = require('randexp'); + +var _randexp2 = _interopRequireDefault(_randexp); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var casual = _casual2.default; @@ -50,6 +54,10 @@ var fieldArrayCalcLength = exports.fieldArrayCalcLength = function fieldArrayCal return length; }; +var randexpWrapper = exports.randexpWrapper = function randexpWrapper(randexpString) { + return new _randexp2.default(randexpString).gen(); +}; + var stringToFn = exports.stringToFn = function stringToFn(moduleName, string, object, db) { var re = /(^[a-zA-Z.]*)/; //aZ.aZ diff --git a/lib/utils.js.map b/lib/utils.js.map index a4559e9..fd0bf5e 100644 --- a/lib/utils.js.map +++ b/lib/utils.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/utils.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAIA,IAAM,yBAAN;AACA,IAAM,uBAAN;AACA,IAAM,SAAS,sBAAT;;AAEC,IAAM,4BAAU,SAAV,OAAU,CAAS,GAAT,EAAc;AACjC,WAAO,OAAO,SAAP,CAAiB,QAAjB,CAA0B,IAA1B,CAA+B,GAA/B,MAAwC,gBAAxC,CAD0B;CAAd;;AAIhB,IAAM,8BAAW,SAAX,QAAW,CAAU,GAAV,EAAe;AACnC,WAAO,OAAO,SAAP,CAAiB,QAAjB,CAA0B,IAA1B,CAA+B,GAA/B,MAAwC,iBAAxC,CAD4B;CAAf;;AAIjB,IAAM,oDAAuB,SAAvB,mBAAuB,CAAU,GAAV,EAAe,MAAf,EAAuB,EAAvB,EAA2B;;AAE3D,WAAO,KAAK,GAAL,CAAP,CAF2D;CAA3B;;AAK7B,IAAM,gDAAoB,SAApB,iBAAoB,CAAU,EAAV,EAAc,MAAd,EAAsB,EAAtB,EAA0B;AACvD,WAAO,GAAG,IAAH,CAAQ,EAAC,cAAD,EAAS,MAAT,EAAa,YAAb,EAAoB,cAApB,EAA4B,cAA5B,EAAR,CAAP,CADuD;CAA1B;;AAI1B,IAAM,sDAAuB,SAAvB,oBAAuB,CAAU,MAAV,EAAkB,gBAAlB,EA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tDuD;;;;;;AAuD3D,6BAAkB,4BAAlB,oGAAsB;gBAAX,kBAAW;SAAtB;;;;;;;;;;;;;;KAvD2D;;AAyD3D,WAAO,MAAP,CAzD2D;CAAlC;;AA4DtB,IAAM,sCAAe,SAAf,YAAe,CAAS,CAAT,EAAW;AACnC,QAAI,CAAC,cAAc,CAAd,CAAD,EAAkB;AAClB,eAAO,CAAP,CADkB;KAAtB,MAEO;AACH,YAAI,MAAM,EAAE,KAAF,CAAQ,GAAR,CAAN,CADD;AAEH,eAAO,IAAI,CAAJ,CAAP,CAFG;KAFP;CADwB;;AASrB,IAAM,8CAAmB,SAAnB,gBAAmB,CAAU,GAAV,EAAe,CAAf,EAAkB,MAAlB,EAA0B,MAA1B,EAAiC;AAC7D,QAAI,CAAC,cAAc,CAAd,CAAD,EAAkB;AAClB,YAAI,CAAJ,IAAS,MAAT,CADkB;KAAtB,MAEO;AACH,YAAI,MAAM,EAAE,KAAF,CAAQ,GAAR,CAAN,CADD;AAEH,YAAI,oBAAoB,IAAI,CAAJ,CAApB,EAA4B,MAA5B,CAAJ,EAAwC;AACpC,gBAAI,IAAI,CAAJ,CAAJ,IAAc,MAAd,CADoC;SAAxC;KAJJ;AAQA,WAT6D;CAAjC","file":"utils.js","sourcesContent":["import Faker from 'faker'\nimport Chance from 'chance'\nimport Casual from 'casual'\n\nconst casual = Casual\nconst faker = Faker\nconst chance = new Chance()\n\nexport const isArray = function(arg) {\n return Object.prototype.toString.call(arg) === '[object Array]';\n}\n\nexport const isObject = function (arg) {\n return Object.prototype.toString.call(arg) === '[object Object]';\n}\n\nexport const evalWithContextData = function (key, object, db) {\n // In this (way, we can pass object and use inside the eval string\n return eval(key)\n}\n\nexport const fnCallWithContext = function (fn, object, db) {\n return fn.call({object, db, faker, chance, casual})\n}\n\nexport const fieldArrayCalcLength = function (config, fixedArrayLength) {\n let length\n if (config.fixedLength) {\n length = config.length - fixedArrayLength\n } else {\n length = Math.floor((Math.random() * config.length) + 1)\n }\n return length\n}\n\nexport const stringToFn = function (moduleName, string, object, db) {\n\n let re = /(^[a-zA-Z.]*)/ //aZ.aZ\n let matches = re.exec(string)\n let strFn\n if (matches && matches.length === 2){\n strFn = moduleName + '.' + string\n }\n\n re = /\\((.*?)\\)/ //Match ()\n matches = re.exec(string)\n if (!matches && ['casual', 'db', 'object'].indexOf(moduleName) < 0){\n strFn = moduleName + '.' + string + '()'\n }\n\n return eval(strFn)\n}\n\nexport const iamLastParent = function(obj) {\n let last = false\n if (isObject(obj)) {\n let ks = Object.keys(obj)\n\n for (let i = 0; i < ks.length; i++) {\n let k = ks[i]\n last = iamLastChild(obj, k)\n if (!last){\n break\n }\n }\n } else {\n last = true\n }\n return last\n}\n\nexport const iamLastChild = function (parent, k) {\n if (isArray(parent[k])) {\n let last = false\n\n if (parent[k].length === 0){\n return true\n }\n\n for (let i = 0; i < parent[k].length; i++) {\n let el = parent[k][i]\n last = !isObject(el)\n if (last){\n break\n }\n }\n return last\n } else {\n return !isObject(parent[k])\n }\n}\n\nexport const isConditional = function (str) {\n let arr = str.split(',')\n return arr.length > 1\n}\n\nexport const cleanVirtuals = function (paths, object, options) {\n\n //clean specific paths\n let objectCleaner = function *(path, obj, options) {\n let lvls = path.split(options.symbol)\n let dest = obj\n\n if (!lvls || lvls.length === 0) { return }\n if (!obj) { return }\n\n for (var i = 0; i< lvls.length; i++) {\n let field = lvls[i]\n if (i === lvls.length - 1 && dest[field]) {\n if (Object.getOwnPropertyNames(dest[field]).length < 1) {\n delete dest[field]\n break\n }\n } else {\n dest = dest[field]\n }\n }\n lvls.pop()\n\n if (lvls.length > 0){\n yield *objectCleaner(lvls.join(options.symbol), obj, options)\n } else {\n return\n }\n }\n\n let forEachPath = function *(path, object, options) {\n let lvls = path.split(options.symbol)\n let dest = object\n\n for (var i = 0; i < lvls.length; i++) {\n let field = lvls[i]\n if (i === lvls.length - 1) {\n // delete specific path\n delete dest[field]\n //clean specific path\n yield *objectCleaner(path, object, options)\n } else {\n dest = dest[field]\n }\n }\n }\n\n let forPaths = function *(paths, object, options) {\n for (var i = 0; i < paths.length; i++) {\n let path = paths[i]\n yield *forEachPath(path, object, options)\n }\n }\n\n let it = forPaths(paths, object, options);\n for (const res of it) { }\n\n return object\n}\n\nexport const calculateKey = function(k){\n if (!isConditional(k)){\n return k\n } else {\n let key = k.split(',')\n return key[0]\n }\n}\n\nexport const conditionalField = function (acc, k, result, object){\n if (!isConditional(k)){\n acc[k] = result\n } else {\n let key = k.split(',')\n if (evalWithContextData(key[0], object)){\n acc[key[1]] = result\n }\n }\n return\n}\n"]} \ No newline at end of file +{"version":3,"sources":["../src/utils.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAKA,IAAM,yBAAN;AACA,IAAM,uBAAN;AACA,IAAM,SAAS,sBAAT;;AAEC,IAAM,4BAAU,SAAV,OAAU,CAAS,GAAT,EAAc;AACjC,WAAO,OAAO,SAAP,CAAiB,QAAjB,CAA0B,IAA1B,CAA+B,GAA/B,MAAwC,gBAAxC,CAD0B;CAAd;;AAIhB,IAAM,8BAAW,SAAX,QAAW,CAAU,GAAV,EAAe;AACnC,WAAO,OAAO,SAAP,CAAiB,QAAjB,CAA0B,IAA1B,CAA+B,GAA/B,MAAwC,iBAAxC,CAD4B;CAAf;;AAIjB,IAAM,oDAAuB,SAAvB,mBAAuB,CAAU,GAAV,EAAe,MAAf,EAAuB,EAAvB,EAA2B;;AAE3D,WAAO,KAAK,GAAL,CAAP,CAF2D;CAA3B;;AAK7B,IAAM,gDAAoB,SAApB,iBAAoB,CAAU,EAAV,EAAc,MAAd,EAAsB,EAAtB,EAA0B;AACvD,WAAO,GAAG,IAAH,CAAQ,EAAC,cAAD,EAAS,MAAT,EAAa,YAAb,EAAoB,cAApB,EAA4B,cAA5B,EAAR,CAAP,CADuD;CAA1B;;AAI1B,IAAM,sDAAuB,SAAvB,oBAAuB,CAAU,MAAV,EAAkB,gBAAlB,EAAoC;AACpE,QAAI,kBAAJ,CADoE;AAEpE,Q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/CuD;;AAsD3D,QAAI,KAAK,SAAS,KAAT,EAAgB,MAAhB,EAAwB,OAAxB,CAAL,CAtDuD;;;;;;AAuD3D,6BAAkB,4BAAlB,oGAAsB;gBAAX,kBAAW;SAAtB;;;;;;;;;;;;;;KAvD2D;;AAyD3D,WAAO,MAAP,CAzD2D;CAAlC;;AA4DtB,IAAM,sCAAe,SAAf,YAAe,CAAS,CAAT,EAAW;AACnC,QAAI,CAAC,cAAc,CAAd,CAAD,EAAkB;AAClB,eAAO,CAAP,CADkB;KAAtB,MAEO;AACH,YAAI,MAAM,EAAE,KAAF,CAAQ,GAAR,CAAN,CADD;AAEH,eAAO,IAAI,CAAJ,CAAP,CAFG;KAFP;CADwB;;AASrB,IAAM,8CAAmB,SAAnB,gBAAmB,CAAU,GAAV,EAAe,CAAf,EAAkB,MAAlB,EAA0B,MAA1B,EAAiC;AAC7D,QAAI,CAAC,cAAc,CAAd,CAAD,EAAkB;AAClB,YAAI,CAAJ,IAAS,MAAT,CADkB;KAAtB,MAEO;AACH,YAAI,MAAM,EAAE,KAAF,CAAQ,GAAR,CAAN,CADD;AAEH,YAAI,oBAAoB,IAAI,CAAJ,CAApB,EAA4B,MAA5B,CAAJ,EAAwC;AACpC,gBAAI,IAAI,CAAJ,CAAJ,IAAc,MAAd,CADoC;SAAxC;KAJJ;AAQA,WAT6D;CAAjC","file":"utils.js","sourcesContent":["import Faker from 'faker'\nimport Chance from 'chance'\nimport Casual from 'casual'\nimport RandExp from 'randexp'\n\nconst casual = Casual\nconst faker = Faker\nconst chance = new Chance()\n\nexport const isArray = function(arg) {\n return Object.prototype.toString.call(arg) === '[object Array]';\n}\n\nexport const isObject = function (arg) {\n return Object.prototype.toString.call(arg) === '[object Object]';\n}\n\nexport const evalWithContextData = function (key, object, db) {\n // In this (way, we can pass object and use inside the eval string\n return eval(key)\n}\n\nexport const fnCallWithContext = function (fn, object, db) {\n return fn.call({object, db, faker, chance, casual})\n}\n\nexport const fieldArrayCalcLength = function (config, fixedArrayLength) {\n let length\n if (config.fixedLength) {\n length = config.length - fixedArrayLength\n } else {\n length = Math.floor((Math.random() * config.length) + 1)\n }\n return length\n}\n\nexport const randexpWrapper = function (randexpString){\n return new RandExp(randexpString).gen()\n}\n\nexport const stringToFn = function (moduleName, string, object, db) {\n\n let re = /(^[a-zA-Z.]*)/ //aZ.aZ\n let matches = re.exec(string)\n let strFn\n if (matches && matches.length === 2){\n strFn = moduleName + '.' + string\n }\n\n re = /\\((.*?)\\)/ //Match ()\n matches = re.exec(string)\n if (!matches && ['casual', 'db', 'object'].indexOf(moduleName) < 0){\n strFn = moduleName + '.' + string + '()'\n }\n\n return eval(strFn)\n}\n\nexport const iamLastParent = function(obj) {\n let last = false\n if (isObject(obj)) {\n let ks = Object.keys(obj)\n\n for (let i = 0; i < ks.length; i++) {\n let k = ks[i]\n last = iamLastChild(obj, k)\n if (!last){\n break\n }\n }\n } else {\n last = true\n }\n return last\n}\n\nexport const iamLastChild = function (parent, k) {\n if (isArray(parent[k])) {\n let last = false\n\n if (parent[k].length === 0){\n return true\n }\n\n for (let i = 0; i < parent[k].length; i++) {\n let el = parent[k][i]\n last = !isObject(el)\n if (last){\n break\n }\n }\n return last\n } else {\n return !isObject(parent[k])\n }\n}\n\nexport const isConditional = function (str) {\n let arr = str.split(',')\n return arr.length > 1\n}\n\nexport const cleanVirtuals = function (paths, object, options) {\n\n //clean specific paths\n let objectCleaner = function *(path, obj, options) {\n let lvls = path.split(options.symbol)\n let dest = obj\n\n if (!lvls || lvls.length === 0) { return }\n if (!obj) { return }\n\n for (var i = 0; i< lvls.length; i++) {\n let field = lvls[i]\n if (i === lvls.length - 1 && dest[field]) {\n if (Object.getOwnPropertyNames(dest[field]).length < 1) {\n delete dest[field]\n break\n }\n } else {\n dest = dest[field]\n }\n }\n lvls.pop()\n\n if (lvls.length > 0){\n yield *objectCleaner(lvls.join(options.symbol), obj, options)\n } else {\n return\n }\n }\n\n let forEachPath = function *(path, object, options) {\n let lvls = path.split(options.symbol)\n let dest = object\n\n for (var i = 0; i < lvls.length; i++) {\n let field = lvls[i]\n if (i === lvls.length - 1) {\n // delete specific path\n delete dest[field]\n //clean specific path\n yield *objectCleaner(path, object, options)\n } else {\n dest = dest[field]\n }\n }\n }\n\n let forPaths = function *(paths, object, options) {\n for (var i = 0; i < paths.length; i++) {\n let path = paths[i]\n yield *forEachPath(path, object, options)\n }\n }\n\n let it = forPaths(paths, object, options);\n for (const res of it) { }\n\n return object\n}\n\nexport const calculateKey = function(k){\n if (!isConditional(k)){\n return k\n } else {\n let key = k.split(',')\n return key[0]\n }\n}\n\nexport const conditionalField = function (acc, k, result, object){\n if (!isConditional(k)){\n acc[k] = result\n } else {\n let key = k.split(',')\n if (evalWithContextData(key[0], object)){\n acc[key[1]] = result\n }\n }\n return\n}\n"]} \ No newline at end of file diff --git a/package.json b/package.json index 2347180..b542810 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mocker-data-generator", - "version": "1.1.0", + "version": "1.1.1", "description": "A simplified way to generate mock data, builds using a simple schema and with the FakerJs", "main": "./lib", "repository": { @@ -45,6 +45,7 @@ "babel-polyfill": "^6.5.0", "casual": "^1.5.1", "chance": "^1.0.0", - "faker": "^3.0.1" + "faker": "^3.0.1", + "randexp": "^0.4.2" } } diff --git a/src/Schema.js b/src/Schema.js index 6b98fd7..28d1e1e 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -1,4 +1,4 @@ -import {isObject, isArray, iamLastParent, iamLastChild, fieldArrayCalcLength, stringToFn, evalWithContextData, isConditional, fnCallWithContext} from './utils' +import {isObject, isArray, iamLastParent, iamLastChild, fieldArrayCalcLength, stringToFn, evalWithContextData, isConditional, fnCallWithContext, randexpWrapper} from './utils' import faker from 'faker' import casual from 'casual' import Chance from 'chance' @@ -94,6 +94,8 @@ export default class Schema { return stringToFn('chance', config.chance, object, db) } else if (config.casual) { return stringToFn('casual', config.casual, object, db) + } else if (config.randexp) { + return randexpWrapper(config.randexp) } else if (config.self) { return stringToFn('object', config.self, object, db) } else if (config.db) { diff --git a/src/utils.js b/src/utils.js index 322f1dc..f164c1d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,7 @@ import Faker from 'faker' import Chance from 'chance' import Casual from 'casual' +import RandExp from 'randexp' const casual = Casual const faker = Faker @@ -33,6 +34,10 @@ export const fieldArrayCalcLength = function (config, fixedArrayLength) { return length } +export const randexpWrapper = function (randexpString){ + return new RandExp(randexpString).gen() +} + export const stringToFn = function (moduleName, string, object, db) { let re = /(^[a-zA-Z.]*)/ //aZ.aZ diff --git a/test/mocker.test.js b/test/mocker.test.js index d3a9e7f..d48c67a 100644 --- a/test/mocker.test.js +++ b/test/mocker.test.js @@ -27,6 +27,7 @@ describe('Mocker: Generators (Fields)', function() { require('./providers/faker.test') require('./providers/chance.test') require('./providers/casual.test') + require('./providers/randexp.test') require('./options/self.test') require('./options/db.test') require('./options/related.test') diff --git a/test/providers/randexp.test.js b/test/providers/randexp.test.js new file mode 100644 index 0000000..48c0fdd --- /dev/null +++ b/test/providers/randexp.test.js @@ -0,0 +1,24 @@ +var mocker = require('../../lib') +var expect = require('chai').expect +var assert = require('chai').assert +var faker = require('faker') +var util = require('util') + +var m = mocker() + +describe('Options: RandexpJs', function() { + it('Should be "/hello+ (world|to you)/"', function(done) { + try { + var res = m.proccessLeaf({ + randexp: /hello+ (world|to you)/ + }) + expect(res) + .to.be.a('string') + .to.not.be.null + .to.not.be.undefined + done() + } catch (x) { + done(x) + } + }) +})