Skip to content

Commit

Permalink
feat: Added length on the array function generator, thanks @zamnuts
Browse files Browse the repository at this point in the history
  • Loading branch information
danibram committed May 27, 2017
1 parent 5c91e5f commit 1f17519
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ Data generation goes with model based composed by generators, the generators can
faker: 'random.arrayElement(db.users).userId'
//Chance
chance: 'integer'
//Function
function: function (){ return /**/ }
//Function that has included index and length, as params
function: function (index, length){ return /**/ }

//Array config
length: 10,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Schema extends Generator {

let length = fieldArrayCalcLength(fieldConfig, na.length, this)

let array = Array.from(new Array(length)).map((el, index) => this.generateField(fieldConfig, index))
let array = Array.from(new Array(length)).map((el, index) => this.generateField(fieldConfig, index, length))

return array.concat(na)
} else {
Expand Down
25 changes: 25 additions & 0 deletions src/lib/tests/Schema.Array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,31 @@ test('Array: It should recognise context in function field', async t => {
})
})

test('Array: Function generator should include index and length', async t => {

let expectedResult = {
test: Array.from(new Array(10)).map(( _, index) => ({ index, length: 10 }) )
}

let model = {
test: [{
function: function(index, length) { // index is provided
return {
index,
length
}
},
fixedLength: true,
length: 10
}]
}

let schema = new Schema('web', model, 1)
let data = schema.build()

t.deepEqual(data[0], expectedResult)
})

test('Array: It should concat elements', async t => {
let model = {
name: {
Expand Down

0 comments on commit 1f17519

Please sign in to comment.