import { RegexValidator } from '../util/regex-validator' ; const OBJECT = "object" ; const BOOLEAN = "boolean" ; export class FormDataProvider { convertToFormData ( jObject ) { return this . convertFormData ( jObject ) ; } convertFormData ( jObject , currentFormData ? , parentKey ? ) { let formData = currentFormData || new FormData ( ) ; let propName = '' ; for ( var columnName in jObject ) { propName = ! parentKey ? columnName : `${ parentKey }[${ columnName }]` ; if ( Array . isArray ( jObject [ columnName ] ) ) { jObject [ columnName ] . forEach ( ( row , index ) => { propName = `${ columnName }[${ index }]` ; if ( typeof row === OBJECT ) this . convertFormData ( row , formData , propName ) ; else this . nonObjectValueBind ( row , formData , propName ) ; } ) ; } else if ( jObject [ columnName ] !== null && typeof jObject [ columnName ] === OBJECT && ! ( jObject [ columnName ] instanceof File || jObject [ columnName ] instanceof FileList ) ) { this . convertFormData ( jObject [ columnName ] , formData , propName ) ; } else { this . nonObjectValueBind ( jObject [ columnName ] , formData , propName ) ; } } return formData ; } nonObjectValueBind ( value , formData , propName ) { if ( typeof value === BOOLEAN ) { formData . append ( propName , + value ? '1' : '0' ) ; } else if ( value instanceof FileList ) { for ( var i = 0 ; i < value . length ; i ++ ) { let itemValue = value . item ( i ) ; formData . append ( `${ propName }[${ i }]` , itemValue ) ; } } else { if ( RegexValidator . isNotBlank ( value ) ) formData . append ( propName , value ) ; } } }