<commit_before>import { FormArray } from '@angular/forms';
import { FormlyFieldConfig } from '../components/formly.field.config';
import { FieldType } from './field.type';
import { clone } from '../utils';
import { FormlyFormBuilder } from '../services/formly.form.builder';

export abstract class FieldArrayType extends FieldType {
  formControl: FormArray;

  static createControl(model: any, field: FormlyFieldConfig): FormArray {
    const form = new FormArray(
      [],
      field.validators ? field.validators.validation : undefined,
      field.asyncValidators ? field.asyncValidators.validation : undefined,
    );

    field.fieldGroup = [];
    (model || []).forEach((m: any, i: number) => field.fieldGroup.push(
      { ...clone(field.fieldArray), key: `${i}` },
    ));

    return form;
  }

 constructor(private builder: FormlyFormBuilder) {
    super();
  }

  add() {
    const i = this.field.fieldGroup.length;
    this.model.push({});
    this.field.fieldGroup.push(
      { ...clone(this.field.fieldArray), key: `${this.field.fieldGroup.length}` },
    );

    this.builder.buildForm(this.formControl, [this.field.fieldGroup[i]], this.model, this.options);
    (<any> this.options).resetTrackModelChanges();
  }

  remove(i) {
    this.formControl.removeAt(i);
    this.field.fieldGroup.splice(i, 1);
    this.field.fieldGroup.forEach((f, index) => f.key = `${index}`);
    this.model.splice(i, 1);
    (<any> this.options).resetTrackModelChanges();
  }
}<commit_msg>feat(core): allow passing the index and the initial model for FieldArrayType::add method. (#869)<commit_after>import { FormArray, FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '../components/formly.field.config';
import { FieldType } from './field.type';
import { clone } from '../utils';
import { FormlyFormBuilder } from '../services/formly.form.builder';
import { isNullOrUndefined } from 'util';

export abstract class FieldArrayType extends FieldType {
  formControl: FormArray;

  static createControl(model: any, field: FormlyFieldConfig): FormArray {
    const form = new FormArray(
      [],
      field.validators ? field.validators.validation : undefined,
      field.asyncValidators ? field.asyncValidators.validation : undefined,
    );

    field.fieldGroup = [];
    (model || []).forEach((m: any, i: number) => field.fieldGroup.push(
      { ...clone(field.fieldArray), key: `${i}` },
    ));

    return form;
  }

 constructor(private builder: FormlyFormBuilder) {
    super();
  }

  add(i?: number, initialModel?: any) {
    i = isNullOrUndefined(i) ? this.field.fieldGroup.length : i;

    this.model.splice(i, 0, initialModel ? clone(initialModel) : {});
    this.formControl.insert(i, new FormGroup({}));
    this.field.fieldGroup.splice(i, 0, { ...clone(this.field.fieldArray) });

    this.field.fieldGroup.forEach((field, index) => {
      field.key = `${index}`;
    });

    this.builder.buildForm(this.formControl, [this.field.fieldGroup[i]], this.model, this.options);
    (<any> this.options).resetTrackModelChanges();
  }

  remove(i: number) {
    this.formControl.removeAt(i);
    this.field.fieldGroup.splice(i, 1);
    this.field.fieldGroup.forEach((f, index) => f.key = `${index}`);
    this.model.splice(i, 1);
    (<any> this.options).resetTrackModelChanges();
  }
}
{'fraction_non_alphanumeric': 0.1649577804583836, 'fraction_numerical': 0.0027141133896260556, 'mean_word_length': 3.711647727272727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '813094', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1032, 'n_words': 205}