Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 1045967

Browse files
committed
Merge pull request #39 from jprider63/master
Functionality to create FormData objects.
2 parents 06aea09 + b4fb532 commit 1045967

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/DOM/XHR/FormData.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
"use strict";
3+
4+
// module DOM.XHR.FormData
5+
6+
exports.newFormData = function() {
7+
return new FormData();
8+
};
9+
10+
exports.appendString = function(form) {
11+
return function( key) {
12+
return function( val) {
13+
form.append( key, val);
14+
return {};
15+
};
16+
};
17+
};
18+
19+
exports.appendWithName = function(form) {
20+
return function( key) {
21+
return function( val) {
22+
return function( name) {
23+
form.append( key, val, name);
24+
return {};
25+
};
26+
};
27+
};
28+
};
29+

src/DOM/XHR/FormData.purs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module DOM.XHR.FormData (FormDataValue(..), toFormData) where
2+
3+
import Data.Foldable
4+
import Data.Tuple
5+
import DOM.File.Types
6+
import DOM.XHR.Types
7+
import Prelude
8+
9+
-- | Possible values of a `FormData`.
10+
data FormDataValue =
11+
FormDataString String
12+
| FormDataFile String File -- ^ File name and `File` object.
13+
| FormDataBlob String Blob -- ^ Blob name and `Blob` object.
14+
15+
-- | Convert an associated array of keys and values to a `FormData`.
16+
toFormData :: forall f . (Foldable f) => f (Tuple String FormDataValue) -> FormData
17+
toFormData dat =
18+
let form = newFormData unit in
19+
let _unit = foldMap (appendData form) dat in
20+
form
21+
22+
where
23+
appendData form (Tuple key (FormDataString val)) = appendString form key val
24+
appendData form (Tuple key (FormDataFile name val)) = appendWithName form key val name
25+
appendData form (Tuple key (FormDataBlob name val)) = appendWithName form key val name
26+
27+
foreign import newFormData :: Unit -> FormData
28+
29+
foreign import appendString :: FormData -> String -> String -> Unit
30+
foreign import appendWithName :: forall a . FormData -> String -> a -> String -> Unit

0 commit comments

Comments
 (0)