mithril-vndb/docs/parsePathname.md
Isiah Meadows 85bfd0f77d
Clarify pathname docs, follow spec with fragments (#2448)
* 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]
2019-07-03 06:22:25 -04:00

1.5 KiB

parsePathname(string)


Description

Turns a string of the form /path/user?a=1&b=2 to an object

var object = m.parsePathname("/path/user?a=1&b=2")
// {path: "/path/user", params: {a: "1", b: "2"}}

Signature

object = m.parsePathname(string)

Argument Type Required Description
string String Yes A URL
returns Object A {path, params} pair where path is the normalized path and params is the parsed parameters.

How to read signatures


How it works

The m.parsePathname method creates an object from a path with a possible query string. It is useful for parsing a local path name into its parts, and it's what m.route uses internally to normalize paths to later match them. It uses m.parseQueryString to parse the query parameters into an object.

var data = m.parsePathname("/path/user?a=hello&b=world")

// data.path is "/path/user"
// data.params is {a: "hello", b: "world"}

General-purpose URL parsing

The method is called parsePathname because it applies to pathnames. If you want a general-purpose URL parser, you should use the global URL class instead.