Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First batch * Updating formObj * Removing .DS_Store
- Loading branch information
1 parent
ef4c929
commit 2d9c965
Showing
12 changed files
with
281 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
npm-debug.log | ||
node_modules/ | ||
dist | ||
.idea/ | ||
*.dist* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- "6" | ||
- "7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import deserialize from './lib/deserialize'; | ||
import getInputs from './lib/getInputs'; | ||
import getJSON from './lib/getJSON'; | ||
import getQueryString from './lib/getQueryString'; | ||
import isForm from './lib/isForm'; | ||
|
||
function formobj(form, selector) { | ||
isForm(form); | ||
|
||
const api = { | ||
getInputs() { | ||
return getInputs(form, selector); | ||
}, | ||
|
||
getJSON() { | ||
return getJSON(form, selector); | ||
}, | ||
|
||
deserialize(data) { | ||
return deserialize(data, getInputs(form, selector)); | ||
}, | ||
|
||
getQueryString() { | ||
return getQueryString(api.getJSON(form)); | ||
} | ||
}; | ||
|
||
return api; | ||
} | ||
|
||
export { formobj as default, deserialize, getInputs, getJSON, isForm, getQueryString }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { toArray } from 'domassist'; | ||
|
||
export default function deserialize(data, inputs) { | ||
const index = {}; | ||
|
||
if (!Array.isArray(inputs)) { | ||
inputs = toArray(inputs); | ||
} | ||
|
||
inputs.forEach(input => { | ||
const name = input.getAttribute('name'); | ||
let val = data[name]; | ||
|
||
if (typeof index[name] === 'undefined') { | ||
index[name] = 0; | ||
} else { | ||
index[name] = index[name] + 1; | ||
} | ||
|
||
if (Array.isArray(val) && input.tagName !== 'SELECT' && !input.multiple) { | ||
val = val[index[name]]; | ||
} | ||
|
||
if (typeof val === 'undefined') { | ||
return; | ||
} | ||
|
||
if (input.type === 'checkbox' && val === true) { | ||
input.checked = true; | ||
} else if (input.type === 'radio' && input.value === val) { | ||
input.checked = true; | ||
} else if (input.tagName === 'SELECT') { | ||
let v = val; | ||
|
||
if (!Array.isArray(val)) { | ||
v = [val]; | ||
} | ||
|
||
toArray(input.options) | ||
.filter(option => v.indexOf(option.value) > -1) | ||
.forEach(option => { | ||
option.selected = true; | ||
}); | ||
} else { | ||
input.value = val; | ||
} | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { find, toArray } from 'domassist'; | ||
import isForm from './isForm'; | ||
|
||
export default function getInputs(form, selector = '[name]') { | ||
isForm(form); | ||
|
||
if (typeof selector !== 'string') { | ||
throw new Error('Invalid selector'); | ||
} | ||
|
||
return toArray(find(selector, form)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import getInputs from './getInputs'; | ||
import { toArray } from 'domassist'; | ||
|
||
export default function getJSON(form, selector) { | ||
const inputs = getInputs(form, selector); | ||
const output = {}; | ||
|
||
inputs.forEach(input => { | ||
const name = input.getAttribute('name'); | ||
let value; | ||
|
||
if (input.type === 'checkbox') { | ||
value = input.checked; | ||
} else if (input.type === 'radio') { | ||
if (input.checked) { | ||
value = input.value; | ||
} else { | ||
return; | ||
} | ||
} else if (input.tagName === 'SELECT' && input.multiple) { | ||
value = toArray(input.options) | ||
.filter(option => option.selected) | ||
.map(option => option.value); | ||
} else { | ||
value = input.value; | ||
} | ||
|
||
// Radio will have multiple matching `name` attributes and we don't want them all. | ||
if (typeof output[name] !== 'undefined' && input.type !== 'radio') { | ||
if (Array.isArray(output[name])) { | ||
output[name].push(value); | ||
} else { | ||
output[name] = [output[name], value]; | ||
} | ||
} else { | ||
output[name] = value; | ||
} | ||
}); | ||
|
||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import getJSON from './getJSON'; | ||
|
||
export default function getQueryString(form) { | ||
let data; | ||
let queryString = ''; | ||
|
||
if (form instanceof HTMLElement) { | ||
data = getJSON(form); | ||
} else { | ||
data = form; | ||
} | ||
|
||
const formatValue = (k, v) => `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`; | ||
|
||
Object.keys(data).forEach(key => { | ||
const value = data[key]; | ||
|
||
if (Array.isArray(value)) { | ||
value.forEach(k => { | ||
queryString += formatValue(key, k); | ||
}); | ||
} else { | ||
queryString += formatValue(key, value); | ||
} | ||
}); | ||
|
||
return queryString.substring(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default function isForm(el) { | ||
if (!el || !el.tagName || el.tagName !== 'FORM') { | ||
throw new Error('Must pass in a form element'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.