Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Jul 28, 2024
1 parent 22fa5b8 commit 029ca7d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ import util from '@substrate-system/util'
import { attributesToString } from '@substrate-system/util'
```

### `parseForm`

Serialize a form and return a plain object. If a form control with the same name appears more than once, the property will be converted to an array.

```ts
function parseForm (form:HTMLFormElement):Record<string, unknown>
```

### `attributesToString`

Take an array of attributes, and transform them into a string format. This can be useful for creating [web components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components).
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function attributesToString (attrs:Attr[]):string {
* If a form control with the same name appears more than once, the
* property will be converted to an array.
*/
export function formToObject (form:HTMLFormElement):Record<string, unknown> {
export function parseForm (form:HTMLFormElement):Record<string, unknown> {
const formData = new FormData(form)
const object:Record<string, unknown> = {}

Expand Down
4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
attributesAsObject,
objectToString,
setAttributes,
formToObject
parseForm
} from '../src/index.js'

test('formToObject', t => {
Expand All @@ -14,7 +14,7 @@ test('formToObject', t => {
</form>`

const form = document.querySelector('form')!
const obj = formToObject(form)
const obj = parseForm(form)
t.deepEqual(obj, {
hello: 'world',
testnull: ''
Expand Down

0 comments on commit 029ca7d

Please sign in to comment.