From 5605cc7ae7292832da75c27a6d676c07f6a5b7e5 Mon Sep 17 00:00:00 2001 From: Raiyan Kamal Date: Wed, 3 Feb 2016 20:40:08 -0500 Subject: [PATCH 1/6] Corrected typo in list of color names --- fake.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fake.js b/fake.js index 477fef6..f3c953b 100644 --- a/fake.js +++ b/fake.js @@ -56,7 +56,7 @@ var colors = [ 'gray', 'indianred', 'khaki', 'lightcoral', 'lightsalmon', 'lightyellow', 'maroon', 'mistyrose', 'navajowhite', 'mocassin', 'orange', 'orangered', 'peru', 'red', 'rosybrown', 'saddlebrown', 'sandybrown', 'sienna', - 'silver', 'slategray', 'tan', 'tomato', 'yeal', 'navy', 'black', + 'silver', 'slategray', 'tan', 'tomato', 'teal', 'navy', 'black', ]; From 8a4771587338147537a4bdc59fbb1f19239f67ee Mon Sep 17 00:00:00 2001 From: Raiyan Kamal Date: Mon, 22 Feb 2016 22:12:01 -0500 Subject: [PATCH 2/6] Package added as dependency: lodash --- package.js | 1 + 1 file changed, 1 insertion(+) diff --git a/package.js b/package.js index 4fdaf88..ab07381 100644 --- a/package.js +++ b/package.js @@ -7,6 +7,7 @@ Package.describe({ Package.on_use(function (api, where) { + api.use(["erasaur:meteor-lodash@4.0.0"]); api.export('Fake', ['client', 'server']); api.add_files('fake.js', ['client', 'server']); }); From 5b891bedf7d63e5c902c8f1d20ba806b7e66d264 Mon Sep 17 00:00:00 2001 From: Raiyan Kamal Date: Mon, 22 Feb 2016 22:14:50 -0500 Subject: [PATCH 3/6] >Generation of fake objects from simple schema definition. Number, String and Boolean fields are supported. --- fake.js | 35 +++++++++++++++++++++++++++++++++-- package.js | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/fake.js b/fake.js index f3c953b..4c2817b 100644 --- a/fake.js +++ b/fake.js @@ -254,6 +254,37 @@ Fake.color = function() { +var _getRandomNumber = function (min, max, isInteger) { + var r = Math.random() * (max - min); + if (isInteger) + r = Math.round(r); + return r; +}; - - +Fake.simpleSchemaDoc = function(schema) { + var fakeObj = {}; + _.each(schema._schemaKeys, function (key) { + var schemaKey = schema._schema[key], + type = schema._schema[key].type.name, + value = null; + switch(type) { + case 'String': + value = Fake.word(); + break; + case 'Number': + var max = _.get(schemaKey, 'max', Number.MAX_SAFE_INTEGER), + min = _.get(schemaKey, 'min', Number.MIN_SAFE_INTEGER); + decimal = _.get(schemaKey, 'decimal', false) + value = _getRandomNumber(min, max, !decimal); + break; + case 'Boolean': + value = _getRandomNumber(0, 1) > 0.5; + break; + } + if(_.isString(value) || _.isNumber(value) || _.isBoolean(value)) { + lodash.set(fakeObj, key, value); + } + }); + + return fakeObj; +}; diff --git a/package.js b/package.js index ab07381..1d352a5 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: "anti:fake", - version: "0.4.1", + version: "0.4.2.1", summary: "Random text and data generator", git: "https://github.com/anticoders/meteor-fake.git", }); From faa41d3a84d8826271d2a92eb52104ec8eb9c671 Mon Sep 17 00:00:00 2001 From: Raiyan Kamal Date: Mon, 22 Feb 2016 22:16:12 -0500 Subject: [PATCH 4/6] Version number corrected --- package.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.js b/package.js index 1d352a5..924bd1f 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: "anti:fake", - version: "0.4.2.1", + version: "0.4.1_1", summary: "Random text and data generator", git: "https://github.com/anticoders/meteor-fake.git", }); From dc38af54682cf9e304bc22921617c6e665084307 Mon Sep 17 00:00:00 2001 From: Raiyan Date: Mon, 22 Feb 2016 22:53:10 -0500 Subject: [PATCH 5/6] Updated docs to include usage of `Fake.simpleSchemaDoc` --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index b23396f..f96e04e 100644 --- a/README.md +++ b/README.md @@ -132,5 +132,28 @@ Selects a random element from provided array. // 'pear' +  + +  +### `Fake.simpleSchemaDoc(schema)` + +Returns a random object created from [SimpleSchema](http://github.com/aldeed/meteor-simple-schema) definition. + +*Example:* + BookSchema = new SimpleSchema({ + title: { + type: String + }, + pages: { + type: Number + }, + available: { + type: Boolean + } + }); + var fakeDoc = Fake.simpleSchemaDoc(BookSchema); + // { "title": "Tendy Orbiter", "pages": 112, "available": true } + +Only `Number`, `String` and `Boolean` type fields are supported at the moment. From 999c1a7d67fb876b4f2c06ccfc4f1c0eaf993ddc Mon Sep 17 00:00:00 2001 From: Raiyan Kamal Date: Tue, 23 Feb 2016 22:03:29 -0500 Subject: [PATCH 6/6] - Support added for generating strings within min, max limit - Fixed bug in generating numbers with limit --- fake.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/fake.js b/fake.js index 4c2817b..bb6c399 100644 --- a/fake.js +++ b/fake.js @@ -254,8 +254,18 @@ Fake.color = function() { +var _getRandomString = function (min, max) { + var length = Math.round(Math.random() * (max - min)) + min; + var s = '', ds = _.truncate(Fake.sentence(), {length: length, omission: '.'}); + while ((s.length + ds.length) <= length) { + s += ds; + ds = Fake.sentence(); + } + return s; +}; + var _getRandomNumber = function (min, max, isInteger) { - var r = Math.random() * (max - min); + var r = Math.random() * (max - min) + min; if (isInteger) r = Math.round(r); return r; @@ -266,15 +276,18 @@ Fake.simpleSchemaDoc = function(schema) { _.each(schema._schemaKeys, function (key) { var schemaKey = schema._schema[key], type = schema._schema[key].type.name, + max = _.get(schemaKey, 'max', Number.MAX_SAFE_INTEGER), + min = _.get(schemaKey, 'min', Number.MIN_SAFE_INTEGER), value = null; + min = _.clamp(min, Number.MIN_SAFE_INTEGER, max); switch(type) { case 'String': - value = Fake.word(); + max = _.clamp(max, 0, 100); + min = _.clamp(min, 0, max); + value = _getRandomString(min, max); break; case 'Number': - var max = _.get(schemaKey, 'max', Number.MAX_SAFE_INTEGER), - min = _.get(schemaKey, 'min', Number.MIN_SAFE_INTEGER); - decimal = _.get(schemaKey, 'decimal', false) + var decimal = _.get(schemaKey, 'decimal', false) value = _getRandomNumber(min, max, !decimal); break; case 'Boolean':