Skip to content

Commit

Permalink
chore: optimize getControl
Browse files Browse the repository at this point in the history
  • Loading branch information
deleonio committed Oct 16, 2021
1 parent d808b47 commit fcd80bd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
8 changes: 4 additions & 4 deletions packages/form/nyc.config.js
Expand Up @@ -16,8 +16,8 @@ module.exports = {
},
sourceMap: false,
instrument: true,
statements: 55,
branches: 40,
functions: 53,
lines: 57,
statements: 58,
branches: 43,
functions: 55,
lines: 60,
};
37 changes: 37 additions & 0 deletions packages/form/src/controls/controls.test.ts
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { SinonSpy, spy } from 'sinon';
import { FormFactory } from '.';

import { FormatHandler } from '../handlers/format.handler';
import { DEFAULT_IBAN_FORMATTER } from '../handlers/formatters/iban.formatter';
Expand Down Expand Up @@ -376,4 +377,40 @@ describe(`Test: Controls`, () => {
expect(input.changeListeners.size).equal(0);
});
});

describe(`Test getForm and getInput`, () => {
const form = FormFactory.createForm('test', {
ansprache: {
anrede: 'Herr',
vorname: 'Elke',
nachname: 'Mustermann',
},
anschrift: {},
alter: 0,
});

describe(`Test getForm`, () => {
it('getForm valid', () => {
expect(form.getForm('ansprache') instanceof FormControl).be.true;
});

it('getForm empty', () => {
expect(form.getInput('alter') instanceof InputControl).be.true;
expect(form.getForm('alter') instanceof FormControl).be.false;
expect(form.getForm('alter') === undefined).be.true;
});
});

describe(`Test getInput`, () => {
it('getInput valid', () => {
expect(form.getInput('alter') instanceof InputControl).be.true;
});

it('getInput empty', () => {
expect(form.getForm('ansprache') instanceof FormControl).be.true;
expect(form.getInput('ansprache') instanceof InputControl).be.false;
expect(form.getInput('ansprache') === undefined).be.true;
});
});
});
});
30 changes: 27 additions & 3 deletions packages/form/src/controls/controls.ts
@@ -1,4 +1,4 @@
import { SetOf } from '@leanup/lib';
import { Log, SetOf } from '@leanup/lib';

import { FormatHandler } from '../handlers/format.handler';
import { ValidationHandler } from '../handlers/validation.handler';
Expand Down Expand Up @@ -373,8 +373,32 @@ export class FormControl extends AbstractControl {
return Array.from(this.controls);
}

public getControl(name: string): FormControl | InputControl | undefined {
return Array.from(this.controls).find((control: FormControl | InputControl) => {
public getControl<T extends AbstractControl>(name: string): T | undefined {
return Array.from(this.controls).find((control) => {
return control.name === name;
}) as T | undefined;
}

private getForms(): FormControl[] {
return Array.from(this.controls).filter((control: FormControl | InputControl) => {
return control instanceof FormControl;
}) as FormControl[];
}

public getForm(name: string): FormControl | undefined {
return Array.from(this.getForms()).find((control: FormControl) => {
return control.name === name;
});
}

private getInputs(): InputControl[] {
return Array.from(this.controls).filter((control: FormControl | InputControl) => {
return control instanceof InputControl;
}) as InputControl[];
}

public getInput(name: string): InputControl | undefined {
return Array.from(this.getInputs()).find((control: InputControl) => {
return control.name === name;
});
}
Expand Down

0 comments on commit fcd80bd

Please sign in to comment.