Skip to content

Commit

Permalink
Add jsform (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: flywind <[email protected]>
  • Loading branch information
juancarlospaco and ringabout authored Jan 4, 2021
1 parent 31e69e8 commit 7b900a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

- Added `jsxmlserializer` module for [`XMLSerializer`](https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer) for the JavaScript target.



- Added `jsformdata` module for [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) for the JavaScript target.
- Added `jssets` module, Set for the JavaScript target
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
- Added `jsheaders` module for [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) for the JavaScript target.
58 changes: 58 additions & 0 deletions src/fusion/js/jsformdata.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## - `FormData` for the JavaScript target: https://developer.mozilla.org/en-US/docs/Web/API/FormData
when not defined(js):
{.fatal: "Module jsformdata is designed to be used with the JavaScript backend.".}

type FormData* = ref object of JsRoot ## FormData API.

func newFormData*(): FormData {.importjs: "new FormData()".}

func add*(this: FormData; name: cstring; value: SomeNumber | bool | cstring) {.importjs: "#.append(#, #)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
## Duplicate keys are allowed and order is preserved.

func add*(this: FormData; name: cstring; value: SomeNumber | bool | cstring, filename: cstring) {.importjs: "#.append(#, #, #)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
## Duplicate keys are allowed and order is preserved.

func delete*(this: FormData; name: cstring) {.importjs: "#.$1(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/delete
## Deletes *all items* with the same key name.

func getAll*(this: FormData; name: cstring): seq[cstring] {.importjs: "#.$1(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/getAll

func hasKey*(this: FormData; name: cstring): bool {.importjs: "#.has(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/has

func keys*(this: FormData): seq[cstring] {.importjs: "Array.from(#.$1())".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/keys

func values*(this: FormData): seq[cstring] {.importjs: "Array.from(#.$1())".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/values

func pairs*(this: FormData): seq[tuple[key, val: cstring]] {.importjs: "Array.from(#.entries())".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries

func put*(this: FormData; name, value, filename: cstring) {.importjs: "#.set(#, #, #)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/set

func `[]=`*(this: FormData; name, value: cstring) {.importjs: "#.set(#, #)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/set

func `[]`*(this: FormData; name: cstring): cstring {.importjs: "#.get(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/FormData/get

func clear*(this: FormData) {.importjs:
"(() => { const frmdt = #; Array.from(frmdt.keys()).forEach((key) => frmdt.delete(key)) })()".}
## Convenience func to delete all items from `FormData`.


runnableExamples:
if defined(fusionJsFormdataTests):
let data: FormData = newFormData()
data["key0"] = "value0".cstring
data.add("key1", "value1")
data.delete("key1")
doAssert data.hasKey("key0")
doAssert data["key0"] == "value0".cstring
data.clear()

0 comments on commit 7b900a3

Please sign in to comment.