* 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]
24 lines
657 B
JavaScript
24 lines
657 B
JavaScript
"use strict"
|
|
|
|
var parseQueryString = require("../querystring/parse")
|
|
|
|
// Returns `{path, params}` from `url`
|
|
module.exports = function(url) {
|
|
var queryIndex = url.indexOf("?")
|
|
var hashIndex = url.indexOf("#")
|
|
var queryEnd = hashIndex < 0 ? url.length : hashIndex
|
|
var pathEnd = queryIndex < 0 ? queryEnd : queryIndex
|
|
var path = url.slice(0, pathEnd).replace(/\/{2,}/g, "/")
|
|
|
|
if (!path) path = "/"
|
|
else {
|
|
if (path[0] !== "/") path = "/" + path
|
|
if (path.length > 1 && path[path.length - 1] === "/") path = path.slice(0, -1)
|
|
}
|
|
return {
|
|
path: path,
|
|
params: queryIndex < 0
|
|
? {}
|
|
: parseQueryString(url.slice(queryIndex + 1, queryEnd)),
|
|
}
|
|
}
|