Skip to content

Commit

Permalink
add randexp support, improve test system, version up
Browse files Browse the repository at this point in the history
  • Loading branch information
danibram committed Feb 15, 2016
1 parent 52426bf commit a84609b
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/Schema.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Schema.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/utils.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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"
}
}
4 changes: 3 additions & 1 deletion src/Schema.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/mocker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
24 changes: 24 additions & 0 deletions test/providers/randexp.test.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
})

0 comments on commit a84609b

Please sign in to comment.