m4nuC / async-busboy

Promise based multipart form parser for KoaJS
MIT License
167 stars 58 forks source link

Adds support for empty key named inputs. #22

Closed jakeandreoli closed 7 years ago

jakeandreoli commented 7 years ago

This fixes the issue I reported, you can view it here: #21

Changes made:

I followed the same coding standards enforced by the file and any functions that were no longer needed I commented the same way other deprecated functions were.

How to test:

Nameless indices

<input type="text" name="todo[]" value="Clean the bathroom"> <!-- Will be treated as todo[0] -->
<input type="text" name="todo[]" value="Pick up milk"> <!-- Will be treated as todo[1] -->

Result:

[
    "Clean the bathroom", // 0
    "Pick up milk" // 1
]

Named indices

<input type="text" name="todo[Bathroom]" value="Clean me!"> <!-- Will be treated as todo[Bathroom] -->
<input type="text" name="todo[Milk]" value="Buy me!"> <!-- Will be treated as todo[Milk] -->

Result:

{
    Bathroom: "Clean me!",
    Milk: "Buy me!"
}

Mixing nameless and named indices.

Mixing nameless and named indices for the same field causes issues and is discouraged; however it is possible. There may be a workaround in the future, but for now this is the best way for implementation.

Nameless first.

<input type="text" name="todo[]" value="Clean me!"> <!-- Will be treated as todo[0] -->
<input type="text" name="todo[Milk]" value="Buy me!"> <!-- Will be treated as todo[1] -->

Result:

[
    "Clean me!", // 0
    "Buy me!" // 1
]

Named first.

<input type="text" name="todo[Bathroom]" value="Clean me!"> <!-- Will be treated as todo[Bathroom] -->
<input type="text" name="todo[]" value="Buy me!"> <!-- Will be treated as todo[""] -->

Result:

{
    Bathroom: "Clean me!",
    "": "Buy me!"
}
m4nuC commented 7 years ago

As much as I appreciate the work that you have done here (beautifully formated PR 👍 !). This exact issue has aleady been solve by #6 that was prior to yours. Thanks a lot for contributing.