Skip to content
Myr

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

5 files+100 added−23 removed

modified:examples/upload/views/style.sass.css+3 added-10 removed
modified:examples/upload/views/upload.haml.html+8 added
modified:examples/upload/app.js-1 removed
modified:lib/express/utils.js+23 added-12 removed
12// Express - Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */7 8var queryString = require('querystring')910/**11 * JSON aliases.12 */1314JSON.encode = JSON.stringify
89 unmodified lines
104105/**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 */116117exports.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 lines
67 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 end138end