rx/presenters

View on GitHub
views/mdc/assets/js/utils/form-data.js

Summary

Maintainability
A
0 mins
Test Coverage
// Turns an Object into FormData. Note that a nested array will come out as a hash with numeric keys
// and will need to be handled accordingly server-side.
export function buildFormData(formData, data, parentKey) {
  if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File) && !(data instanceof Blob)) {
    Object.keys(data).forEach(key => {
      buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key);
    });
  } else {
    const value = data == null ? '' : data;
    formData.append(parentKey, value);
  }
}