diff --git a/README.md b/README.md index e22f147..f97236d 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/src/lib/Schema.ts b/src/lib/Schema.ts index 71558ea..adea72d 100644 --- a/src/lib/Schema.ts +++ b/src/lib/Schema.ts @@ -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 { diff --git a/src/lib/tests/Schema.Array.spec.ts b/src/lib/tests/Schema.Array.spec.ts index 4b6a905..2ebb9f1 100644 --- a/src/lib/tests/Schema.Array.spec.ts +++ b/src/lib/tests/Schema.Array.spec.ts @@ -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: {