URL named params parser
If you work in web development you have probably found yourself in a situation where you need to extract named params from a URL.
Url-params-parse will give you several useful methods that will help you manage named and query params.
import { UrlParser } from 'url-params-parser'
// UrlParser(url, placeholder)
const urlParser = UrlParser(
"https://address.com/employees/show/1234/developer/reports/asc",
"/employees/show/:id/:title/reports/:order"
)
urlParser.namedParams
// returns { id: "1234", title: "developer", order: "asc" }
)
These are some other useful methods:
namedParams
Returns an object with all the named params and their values
urlParser.namedParams
// returns { id: "1234", title: "developer", order: "asc" }
namedParamsKeys
Returns an array with all the named param keys
urlParser.namedParamsKeys
// returns ["id", "title", "order"]
namedParamsValues
Returns an array with all the named param values
urlParser.namedParamsValues
// returns ["1234", "developer", "asc"]
queryParams
Returns an object with all query params and their values
urlParser.queryParams
// returns { climate: "change", "sea-level": "rising" }
queryParamsKeys
Returns an array with all the query param values
urlParser.queryParamsKeys
// returns [ climate, "sea-level" ]
queryParamsValues
Returns an array with all the query param values
urlParser.queryParamsValues
// returns [ "change", "rising" ]
pathNames
Returns an array with all the elements of a pathname
urlParser.pathNames
// returns [ "employees", "show", "1234", "developer", "reports", "asc" ]
url-params-parser is available under the MIT license.