Skip to content
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

#2025 array of querystring without number #2053

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/buildQueryString.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ Deep data structures are serialized in a way that is understood by popular web a
```javascript
var querystring = m.buildQueryString({a: ["hello", "world"]})

// querystring is "a[0]=hello&a[1]=world"
// querystring is "a[]=hello&a[]=world"
```

1 change: 1 addition & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- API: `m.mount()` will only render its own root when called, it will not trigger a `redraw()` ([#1592](https://github.com/MithrilJS/mithril.js/pull/1592))
- API: Assigning to `vnode.state` (as in `vnode.state = ...`) is no longer supported. Instead, an error is thrown if `vnode.state` changes upon the invocation of a lifecycle hook.
- API: `m.request` will no longer reject the Promise on server errors (eg. status >= 400) if the caller supplies an `extract` callback. This gives applications more control over handling server responses.
- API: `m.buildQueryString` and `m.parseQueryString` are changed to remove index of array ([#2025](https://github.com/MithrilJS/mithril.js/pull/2025))

#### News

Expand Down
4 changes: 2 additions & 2 deletions docs/parseQueryString.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var data = m.parseQueryString("?a=hello&b=world")
Querystrings that contain bracket notation are correctly parsed into deep data structures

```javascript
m.parseQueryString("a[0]=hello&a[1]=world")
m.parseQueryString("a[]=hello&a[]=world")

// data is {a: ["hello", "world"]}
```
```
2 changes: 1 addition & 1 deletion querystring/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function(object) {
function destructure(key, value) {
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
destructure(key + "[" + i + "]", value[i])
destructure(key + "[]", value[i])
}
}
else if (Object.prototype.toString.call(value) === "[object Object]") {
Expand Down
23 changes: 13 additions & 10 deletions querystring/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function(string) {
if (string === "" || string == null) return {}
if (string.charAt(0) === "?") string = string.slice(1)

var entries = string.split("&"), data = {}, counters = {}
var entries = string.split("&"), data = {}
for (var i = 0; i < entries.length; i++) {
var entry = entries[i].split("=")
var key = decodeURIComponent(entry[0])
Expand All @@ -18,17 +18,20 @@ module.exports = function(string) {
if (key.indexOf("[") > -1) levels.pop()
for (var j = 0; j < levels.length; j++) {
var level = levels[j], nextLevel = levels[j + 1]
var isNumber = nextLevel == "" || !isNaN(parseInt(nextLevel, 10))
var isArray = nextLevel == ""
var isValue = j === levels.length - 1
if (level === "") {
var key = levels.slice(0, j).join()
if (counters[key] == null) counters[key] = 0
level = counters[key]++
}
if (cursor[level] == null) {
cursor[level] = isValue ? value : isNumber ? [] : {}
var levelValue = isValue ? value : isArray ? [] : {}

if (cursor instanceof Array) {
cursor.push(levelValue)
} else {
if (cursor[level] == null) {
cursor[level] = levelValue
} else {
levelValue = cursor[level]
}
}
cursor = cursor[level]
cursor = levelValue
}
}
return data
Expand Down
13 changes: 4 additions & 9 deletions querystring/tests/test-buildQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,22 @@ o.spec("buildQueryString", function() {
o("handles nested array", function() {
var string = buildQueryString({a: ["x", "y"]})

o(string).equals("a%5B0%5D=x&a%5B1%5D=y")
o(string).equals("a%5B%5D=x&a%5B%5D=y")
})
o("handles array w/ dupe values", function() {
var string = buildQueryString({a: ["x", "x"]})

o(string).equals("a%5B0%5D=x&a%5B1%5D=x")
})
o("handles deep nested array", function() {
var string = buildQueryString({a: [["x", "y"]]})

o(string).equals("a%5B0%5D%5B0%5D=x&a%5B0%5D%5B1%5D=y")
o(string).equals("a%5B%5D=x&a%5B%5D=x")
})
o("handles deep nested array in object", function() {
var string = buildQueryString({a: {b: ["x", "y"]}})

o(string).equals("a%5Bb%5D%5B0%5D=x&a%5Bb%5D%5B1%5D=y")
o(string).equals("a%5Bb%5D%5B%5D=x&a%5Bb%5D%5B%5D=y")
})
o("handles deep nested object in array", function() {
var string = buildQueryString({a: [{b: 1, c: 2}]})

o(string).equals("a%5B0%5D%5Bb%5D=1&a%5B0%5D%5Bc%5D=2")
o(string).equals("a%5B%5D%5Bb%5D=1&a%5B%5D%5Bc%5D=2")
})
o("handles date", function() {
var string = buildQueryString({a: new Date(0)})
Expand Down
18 changes: 7 additions & 11 deletions querystring/tests/test-parseQueryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,19 @@ o.spec("parseQueryString", function() {
var data = parseQueryString("a[b][c]=x&a[b][d]=y")
o(data).deepEquals({a: {b: {c: "x", d: "y"}}})
})
o("parses nested array", function() {
o("parses object with number key", function() {
var data = parseQueryString("a[0]=x&a[1]=y")
o(data).deepEquals({a: ["x", "y"]})
o(data).deepEquals({a: {"0": "x", "1": "y"}})
})
o("parses deep nested array", function() {
o("parses deep nested object with number key", function() {
var data = parseQueryString("a[0][0]=x&a[0][1]=y")
o(data).deepEquals({a: [["x", "y"]]})
})
o("parses deep nested object in array", function() {
var data = parseQueryString("a[0][c]=x&a[0][d]=y")
o(data).deepEquals({a: [{c: "x", d: "y"}]})
o(data).deepEquals({a: {"0": {"0": "x", "1": "y"}}})
})
o("parses deep nested array in object", function() {
var data = parseQueryString("a[b][0]=x&a[b][1]=y")
o(data).deepEquals({a: {b: ["x", "y"]}})
var data = parseQueryString("a[b][]=x&a[b][]=y&a[c][]=z")
o(data).deepEquals({a: {b: ["x", "y"], c: ["z"]}})
})
o("parses array without index", function() {
o("parses array", function() {
var data = parseQueryString("a[]=x&a[]=y&b[]=w&b[]=z")
o(data).deepEquals({a: ["x", "y"], b: ["w", "z"]})
})
Expand Down