Skip to content

Convert form parameters to an object using the same logic as Rack

License

Notifications You must be signed in to change notification settings

citizensadvice/form-to-rack-params

Repository files navigation

formToRackParams(form: HTMLFormElement | FormData): Object

npm version

Converts a <Form>'s parameters to an object using the same logic as Rack.

This is mainly intended for testing JavaScript components that generate HTML forms to be submitted to Rails.

<form>
  <input
    type="hidden"
    name="foo[bar][][id]"
    value="1"
  />
  <input
    type="hidden"
    name="foo[bar][][id]"
    value="2"
  />
</form>
import formToRackParams from '@citizensadvice/form-to-rack-params';

const form = document.querySelector('form');
formToRakeParams(form);

/* =>
  foo: {
    bar: [
      { id: '1' },
      { id: '2' },
    ],
  }
*/

About

Convert form parameters to an object using the same logic as Rack

Resources

License

Stars

Watchers

Forks

Packages

No packages published