Merge branch 'upload'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parents 2bbe573, eb94667 · Browse files · 5 files·+100 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+100 added−23 removed
- tests:+66 added−0 removed
- other:+34 added−23 removed
Provenance
- Commit
5495fbc- Tree
caf76d5- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent 1
2bbe573- Parent 2
eb94667
5 files changed+100 added−23 removed
modified:examples/upload/views/style.sass.css+3 added-10 removed
10 unmodified lines1112131415161718192034 unmodified lines55565758575859606162636465665910 unmodified lines :color #343434h1 :text-shadow 1px 2px 2px #ddd :font-size 60pxh2 :margin-top 15px #wrapper :position relative :margin 100px auto34 unmodified linesform .panel :float left :width 100% :clear both a :color #1ABFF1 :-webkit-transition-property padding :-webkit-transition-duration 0.15sa:hover :padding 0 5pxa:hover:before :content 'visit: 'No newline at end of file :margin-bottom 15pxNo newline at end of filemodified:examples/upload/views/upload.haml.html+8 added
12345678910111213141516171819%h1 Upload:if typeof images !== 'undefined' .images :each img in images %img{ src: img }
%h2 Singles%form{ method: 'post', enctype: 'multipart/form-data' } %input{ type: 'file', name: 'images[0]' } %input{ type: 'file', name: 'images[1]' } %input{ type: 'file', name: 'images[2]' } .panel %input{ type: 'submit', value: 'Upload' } %h2 Multiple%form{ method: 'post', enctype: 'multipart/form-data' } %input{ type: 'file', name: 'images[]', multiple: 'multiple' } .panel %input{ type: 'submit', value: 'Upload' }No newline at end of fileNo newline at end of filemodified:examples/upload/app.js-1 removed
6 unmodified lines78910111112131422 unmodified lines6 unmodified lines use(MethodOverride) use(ContentLength) use(CommonLogger) set('root', __dirname) enable('cache view contents')})
get('/', function(){ this.redirect('/upload')22 unmodified linesmodified:lib/express/utils.js+23 added-12 removed
1 2 // Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4 /**5+ * Module dependencies.6+ */7+ 8+var queryString = require('querystring')9+10+/**11 * JSON aliases.12 */13 14 JSON.encode = JSON.stringify89 unmodified lines104 105 /**106 * Merge param _key_ and _val_ into _params_. Key107 * should be a query string key such as 'user[name]',102- * and _val_ is it's associated value. The root _params_108+ * and _val_ is it's associated object. The root _params_109 * object is returned.110 *105- * This is primarily used by Express for merging multi-part106- * body values together.107- *111 * @param {string} key109- * @param {string} val112+ * @param {mixed} val113 * @return {hash}114 * @api public115 */116 117 exports.mergeParam = function(key, val, params) {115- var keys = key.match(/(\w+)/g),116- orig = params117- for (var i = 0, len = keys.length; i < len; ++i)118- if (i == len - 1)119- params[keys[i]] = val120- else121- params = (params[keys[i]] = params[keys[i]] || {})118+ var orig = params,119+ keys = key.trim().match(/\w+/g),120+ array = /\[\]$/.test(key)121+ $(keys).reduce(queryString.parseQuery(key), function(parts, key, i){122+ if (i === keys.length - 1)123+ if (key in params)124+ params[key] instanceof Array ?125+ params[key].push(val) :126+ params[key] = [params[key], val]127+ else128+ params[key] = array ? [val] : val129+ if (!(key in params)) params[key] = {}130+ params = params[key]131+ return parts[key]132+ })133 return orig134 }modified:spec/spec.utils.js+66 added
66 unmodified lines67 params.user.name.should.eql 'tj'68 params.user.email.should.eql 'tj@vision-media.ca'69 end70 end71+ 72+ describe 'with an object as value'73+ it 'should preserve it'74+ params = {}75+ utils.mergeParam('images[]', { name: 1 }, params)76+ utils.mergeParam('images[]', { name: 2 }, params)77+ params.images.should.eql [{ name: 1}, { name: 2 }]78+ end79+ end80+ 81+ describe 'key[number]'82+ it 'should merge correctly'83+ params = { images: { one: 'foo.png' }}84+ utils.mergeParam('images[0]', 'bar.png', params)85+ params.images.one.should.eql 'foo.png'86+ params.images[0].should.eql 'bar.png'87+ end88+ end89+ 90+ describe 'key[]'91+ describe 'with a single value'92+ it 'should still be an array'93+ params = {}94+ utils.mergeParam('images[]', '1', params)95+ params.images.should.eql ['1']96+ end97+ end98+ 99+ describe 'with empty params'100+ it 'should merge correctly'101+ params = {}102+ utils.mergeParam('images[]', '1', params)103+ utils.mergeParam('images[]', '2', params)104+ params.images.should.eql ['1', '2']105+ end106+ end107+ 108+ describe 'with populated params'109+ it 'should convert to an array'110+ params = { images: 'foo.png'}111+ utils.mergeParam('images[]', '1', params)112+ params.images.should.eql ['foo.png', '1']113+ end114+ end115+ 116+ describe 'with several merges'117+ it 'should push values'118+ params = {}119+ utils.mergeParam('images[]', '1', params)120+ utils.mergeParam('images[]', '2', params)121+ utils.mergeParam('images[]', '3', params)122+ params.images.should.eql ['1', '2', '3']123+ end124+ end125+ 126+ describe 'when nested'127+ it 'should marge correctly'128+ params = {}129+ utils.mergeParam('user[tj][images][]', '1', params)130+ utils.mergeParam('user[tj][images][]', '2', params)131+ utils.mergeParam('user[tj][images][]', '3', params)132+ params.user.should.eql { tj: { images: ['1', '2', '3'] }}133+ end134+ end135+ end136+ 137 end138 end