From f4e4d7ec3564bdcb077a39df392f4dd5fb252e49 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 20 Jan 2015 10:53:01 +0100 Subject: [PATCH] v1.0.0 --- .gitignore | 3 ++- README.md | 49 +++++++++++++++++++++++++++++++++++++++ gulpfile.js | 28 ++++++++++++++++++++++ index.coffee | 1 + index.js | 1 + lib/form-fix-array.coffee | 17 ++++++++++++++ lib/form-fix-array.js | 27 +++++++++++++++++++++ package.json | 32 +++++++++++++++++++++++++ 8 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 gulpfile.js create mode 100644 index.coffee create mode 100644 index.js create mode 100644 lib/form-fix-array.coffee create mode 100644 lib/form-fix-array.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore index f86fa8e..23981d5 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file +# Example .gitignore files: https://github.com/github/gitignore +/node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..49b7e17 --- /dev/null +++ b/README.md @@ -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"] +} +*/ +``` diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..bb7f05f --- /dev/null +++ b/gulpfile.js @@ -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']); \ No newline at end of file diff --git a/index.coffee b/index.coffee new file mode 100644 index 0000000..ea84b55 --- /dev/null +++ b/index.coffee @@ -0,0 +1 @@ +module.exports = require "./lib/form-fix-array" diff --git a/index.js b/index.js new file mode 100644 index 0000000..37d165e --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require("./lib/form-fix-array"); diff --git a/lib/form-fix-array.coffee b/lib/form-fix-array.coffee new file mode 100644 index 0000000..b5291a5 --- /dev/null +++ b/lib/form-fix-array.coffee @@ -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 diff --git a/lib/form-fix-array.js b/lib/form-fix-array.js new file mode 100644 index 0000000..c9798e1 --- /dev/null +++ b/lib/form-fix-array.js @@ -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; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..21d7638 --- /dev/null +++ b/package.json @@ -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" + } +}