* Clarify pathname docs, follow spec with fragments - Valid URLs must not contain a `#` within its fragment. https://github.com/MithrilJS/mithril.js/issues/2445 - Our docs were a little confusing and misleading - `m.pathname` isn't aware of URLs, just path names. - Removed the relevant extension to `m.parseQueryString` required to support the hash parsing extension. Now we just shave it off and ignore it. - Fix support for arbitrary prefixes, so prefixes like `?#` are handled correctly. - Add a bunch of tests to cover various areas of confusion and unusual edge cases. * Update with PR [skip ci]
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
"use strict"
|
|
|
|
// The extra `data` parameter is for if you want to append to an existing
|
|
// parameters object.
|
|
module.exports = function(string) {
|
|
if (string === "" || string == null) return {}
|
|
if (string.charAt(0) === "?") string = string.slice(1)
|
|
|
|
var entries = string.split("&"), counters = {}, data = {}
|
|
for (var i = 0; i < entries.length; i++) {
|
|
var entry = entries[i].split("=")
|
|
var key = decodeURIComponent(entry[0])
|
|
var value = entry.length === 2 ? decodeURIComponent(entry[1]) : ""
|
|
|
|
if (value === "true") value = true
|
|
else if (value === "false") value = false
|
|
|
|
var levels = key.split(/\]\[?|\[/)
|
|
var cursor = data
|
|
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 isValue = j === levels.length - 1
|
|
if (level === "") {
|
|
var key = levels.slice(0, j).join()
|
|
if (counters[key] == null) {
|
|
counters[key] = Array.isArray(cursor) ? cursor.length : 0
|
|
}
|
|
level = counters[key]++
|
|
}
|
|
if (isValue) cursor[level] = value
|
|
else if (cursor[level] == null) cursor[level] = isNumber ? [] : {}
|
|
cursor = cursor[level]
|
|
}
|
|
}
|
|
return data
|
|
}
|