master
Sven Slootweg 9 years ago
parent 687d85598d
commit f4e4d7ec35

3
.gitignore vendored

@ -1,3 +1,4 @@
# https://git-scm.com/docs/gitignore
# https://help.github.com/articles/ignoring-files
# Example .gitignore files: https://github.com/github/gitignore
# Example .gitignore files: https://github.com/github/gitignore
/node_modules/

@ -0,0 +1,49 @@
# form-fix-array
This makes sure that form data is handled correctly when sent to a server.
While arrays are a natively supported feature of HTTP form data (whether URL-encoded or multipart/form-data), implementation differences exist. Especially PHP is notorious for refusing to recognize a field as an array unless it specifically has array brackets in the field name. This means that `field=val1&field=val2` will simply be interpreted as `val2`, whereas `field[]=val1&field[]=val2` will be interpreted as an array containing `val1` and `val2`.
This module ensures that all fields containing an array of values have a `[]` suffix, adding it where necessary (and doing nothing where it is already there). That way, every receiving server should be able to handle the request.
It does not currently support objects ('associative arrays'), only plain arrays.
## License
[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer. A donation and/or attribution are appreciated, but not required.
## Donate
My income consists entirely of donations for my projects. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!
You can donate using Bitcoin, PayPal, Gratipay, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else.
## Contributing
Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the `.coffee` files, not the `.js` files.
Build tool of choice is `gulp`; simply run `gulp` while developing, and it will watch for changes.
Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.
## Usage
```javascript
var formFixArray = require("form-fix-array");
var sampleFormData = {
"fieldOne": "value 1",
"fieldTwo": ["value 2a", "value 2b"],
"fieldThree[]": ["value 3a", "value 3b"]
}
var fixedFormData = formFixArray(sampleFormData);
/* Result:
{
"fieldOne": "value 1",
"fieldTwo[]": ["value 2a", "value 2b"],
"fieldThree[]": ["value 3a", "value 3b"]
}
*/
```

@ -0,0 +1,28 @@
var gulp = require('gulp');
/* CoffeeScript compile deps */
var path = require('path');
var gutil = require('gulp-util');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var coffee = require('gulp-coffee');
var cache = require('gulp-cached');
var remember = require('gulp-remember');
var plumber = require('gulp-plumber');
var source = ["lib/**/*.coffee", "index.coffee"]
gulp.task('coffee', function() {
return gulp.src(source, {base: "."})
.pipe(plumber())
.pipe(cache("coffee"))
.pipe(coffee({bare: true}).on('error', gutil.log)).on('data', gutil.log)
.pipe(remember("coffee"))
.pipe(gulp.dest("."));
});
gulp.task('watch', function () {
gulp.watch(source, ['coffee']);
});
gulp.task('default', ['coffee', 'watch']);

@ -0,0 +1 @@
module.exports = require "./lib/form-fix-array"

@ -0,0 +1 @@
module.exports = require("./lib/form-fix-array");

@ -0,0 +1,17 @@
endsWith = (str, suffix) -> str.indexOf(suffix, str.length - suffix.length) != -1
module.exports = (formFields) ->
correctedFormFields = {}
for fieldKey, fieldValue of formFields
newKey = switch Array.isArray fieldValue
when false then fieldKey
when true
if endsWith(fieldKey, "[]")
fieldKey
else
fieldKey + "[]"
correctedFormFields[newKey] = fieldValue
return correctedFormFields

@ -0,0 +1,27 @@
var endsWith;
endsWith = function(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
module.exports = function(formFields) {
var correctedFormFields, fieldKey, fieldValue, newKey;
correctedFormFields = {};
for (fieldKey in formFields) {
fieldValue = formFields[fieldKey];
newKey = (function() {
switch (Array.isArray(fieldValue)) {
case false:
return fieldKey;
case true:
if (endsWith(fieldKey, "[]")) {
return fieldKey;
} else {
return fieldKey + "[]";
}
}
})();
correctedFormFields[newKey] = fieldValue;
}
return correctedFormFields;
};

@ -0,0 +1,32 @@
{
"name": "form-fix-array",
"version": "1.0.0",
"description": "Fixes key names for HTTP form data, to ensure that arrays are handled correctly.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/joepie91/node-form-fix-array"
},
"keywords": [
"form-data",
"http",
"array"
],
"author": "Sven Slootweg",
"license": "WTFPL",
"devDependencies": {
"gulp": "~3.8.0",
"gulp-cached": "~0.0.3",
"gulp-coffee": "~2.0.1",
"gulp-concat": "~2.2.0",
"gulp-livereload": "~2.1.0",
"gulp-nodemon": "~1.0.4",
"gulp-plumber": "~0.6.3",
"gulp-remember": "~0.2.0",
"gulp-rename": "~1.2.0",
"gulp-util": "~2.2.17"
}
}
Loading…
Cancel
Save