-
Notifications
You must be signed in to change notification settings - Fork 65
Handle partially parsed objects (w/ querystrings as keys) #27
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,15 +35,7 @@ function parse(parts, parent, key, val) { | |
var part = parts.shift(); | ||
// end | ||
if (!part) { | ||
if (Array.isArray(parent[key])) { | ||
parent[key].push(val); | ||
} else if ('object' == typeof parent[key]) { | ||
parent[key] = val; | ||
} else if ('undefined' == typeof parent[key]) { | ||
parent[key] = val; | ||
} else { | ||
parent[key] = [parent[key], val]; | ||
} | ||
set(parent, key, val); | ||
// array | ||
} else { | ||
var obj = parent[key] = parent[key] || []; | ||
|
@@ -95,8 +87,8 @@ function merge(parent, key, val){ | |
* Parse the given obj. | ||
*/ | ||
|
||
function parseObject(obj){ | ||
var ret = { base: {} }; | ||
function parseObject(obj, base){ | ||
var ret = { base: base || {} }; | ||
Object.keys(obj).forEach(function(name){ | ||
merge(ret, name, obj[name]); | ||
}); | ||
|
@@ -232,10 +224,13 @@ function stringifyObject(obj, prefix) { | |
|
||
function set(obj, key, val) { | ||
var v = obj[key]; | ||
if (undefined === v) { | ||
if ('undefined' == typeof v) { | ||
obj[key] = val; | ||
} else if (Array.isArray(v)) { | ||
v.push(val); | ||
obj[key] = v.concat(val); | ||
} else if (('object' == typeof v) && ('[object Object]' == toString.call(val))) { | ||
// partially parsed object | ||
parseObject(val, v); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, putting this recursive call inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. meh yeah it's, the whole lib is pretty obscure, it's a strange thing to parse, as long as we have strong test cases we should be fine |
||
} else { | ||
obj[key] = [v, val]; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need this? where's v coming from that we need typeof
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh it's right above haha, we dont need typeof then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just consistency really, and because
undefined
can be redefined in Javascript, meaning the safest way to check forundefined
is withtypeof
. It's a habit of mine...EDIT: I knew this, but just tried it... blows my mind:
EDIT 2: Apparently this behavior was eliminated in newer versions of V8? The above was in Node v0.4.8, v0.6.2 doesn't allow it... thank goodness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah gotcha, personally I avoid defensive programming whenever possible to delegate the issue. plus there are a ton of retarded things you can do in js, people just shouldn't do them haha