import { ValidationSchemas } from '~/domain'; import { ManyTestsOptions, TestFnModel } from './types'; export class ManyTestsHelper { constructor(private readonly options: ManyTestsOptions) {} public valid = (valid: boolean): string => { return valid ? 'valid' : 'invalid'; }; public getTestFnModel = (schema: ValidationSchemas, valid: boolean, value: unknown, index: number): TestFnModel => { const testKey = `[ ${this.getTestId(value, index)} ]`; const input = value; return { testKey, input, schema, valid, }; }; public getTestId = (value: any, index: number): string | number => { if (value === '') { return "''"; } if (value === undefined) { return 'undefined'; } if (value === null) { return 'null'; } if (this.isPrimitiveValue(value)) { return value; } if (typeof value === 'object') { const keys = Object.keys(value); if (keys.length === 0) { return '{}'; } if (keys.length === 1 && keys.includes('value')) { return `{ value: ${value.value} }`; } } return `index = ${index}`; }; public isPrimitiveValue = (value: unknown): value is number | string => { return value !== undefined && typeof value !== 'object'; }; }