Skip to content

Updates #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
npm-debug.log
node_modules/
dist
.idea/
*.dist*
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "6"
- "7"
31 changes: 31 additions & 0 deletions formobj.js
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 };
48 changes: 48 additions & 0 deletions lib/deserialize.js
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;
}
});
}
103 changes: 0 additions & 103 deletions lib/formobj.js

This file was deleted.

12 changes: 12 additions & 0 deletions lib/getInputs.js
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));
}
41 changes: 41 additions & 0 deletions lib/getJSON.js
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;
}
28 changes: 28 additions & 0 deletions lib/getQueryString.js
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);
}
5 changes: 5 additions & 0 deletions lib/isForm.js
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');
}
}
59 changes: 28 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@
"json"
],
"scripts": {
"build": "browserify lib/formobj.js -t [ babelify --presets [ es2015 ] ] -o dist/formobj.js -s SerializeForm",
"build:test": "browserify test/formobj.test.js -t [ babelify --presets [ es2015 ] ] -o test/dist/test.js",
"dev": "concurrently -r 'nswatch' 'static-server -p 8000'",
"lint": "eslint . --ignore-pattern dist",
"prepublish": "npm run build"
},
"watch": {
"lib/*.js": [
"lint",
"build",
"build:test"
],
"test/*.js": [
"lint",
"build:test"
]
"build": "scriptkit build",
"dev": "scriptkit dev",
"test": "cat test/formobj.test.dist.js | tape-run --browser phantomjs --render tap-spec",
"prepublish": "scriptkit"
},
"repository": {
"type": "git",
Expand All @@ -36,28 +24,37 @@
"url": "https://github.com/firstandthird/formobj/issues"
},
"homepage": "https://github.com/firstandthird/formobj#readme",
"dependencies": {},
"dependencies": {
"domassist": "^1.4.1"
},
"eslintConfig": {
"env": {
"browser": true
},
"extends": "firstandthird"
},
"module": "formobj.js",
"scriptkit": {
"scripts": {
"babel": {
"exclude": [
"node_modules/tape-rollup/*"
]
},
"files": {
"dist/formobj.js": "formobj.js",
"test/formobj.test.dist.js": "test/formobj.test.js"
}
}
},
"devDependencies": {
"babel-preset-es2015": "^6.6.0",
"babelify": "^7.3.0",
"browserify": "^13.1.1",
"concurrently": "^3.1.0",
"eslint": "^3.4.0",
"eslint-config-firstandthird": "^3.0.2",
"eslint-config-firstandthird": "^3.2.0",
"eslint-plugin-import": "^2.2.0",
"nswatch": "^0.2.0",
"static-server": "^2.0.3",
"tape": "^4.6.2"
},
"babel": {
"presets": [
"es2015"
]
"phantomjs": "^2.1.7",
"scriptkit": "0.0.22",
"static-server": "^2.0.4",
"tap-spec": "^4.1.1",
"tape-rollup": "4.6.4",
"tape-run": "^2.1.5"
}
}
Loading