A constructor for manipulating and querying URLs
8.73 kB
import URL from "urli";
const location = new URL(Schema, LocationObject|String);
location.setSchema(String)
-> URL
url.set({
pathname? : String,
href? : String,
hash? : String,
origin? : String,
search? : String
});
let url = new URL({ href: "http://localhost:3000/?string" });
url.set({
pathname : "/path/name"
});
url.toString();
-> "http://localhost:3000/path/name?string=1"
Any path which is prefixed with :
will be converted to a key value pair which is accessible at location.params
const location = new URL(
"/user/:userID",
"/user/SeanJM"
);
location.params.userID -> "SeanJM"
const location = new URL("/user/:userID", {
pathname: "/user/SeanJM",
search : ""
});
location.params.userID -> "SeanJM"
Arrays
const location = new URL("/user/:userID?depth[]=:number+:id", {
pathname: "/user/SeanJM",
search : "?depth[]=0+o8jk&depth[]=1+99qE&depth[]=2+eBPs"
});
location.search.depth -> [{
number: 0,
id : "o8jk"
}, {
number: 1,
id : "99qE"
}, {
number: 2,
id : "eBPs"
}]
Objects & Constants
const location = new URL("/post/:postID?origin=board+:category+:page", {
pathname: "/post/ezAYhlkuGEz",
search : "?origin=board+food+1"
});
location.search.origin -> {
category: "food",
page: 1
}
location.search.origin.category = "fitness";
location.toString() -> "/post/ezAYhlkuGEz?origin=board+fitness+1"
location.params.userID = "HungryHippo";
location.toString();
-> "/user/HungryHippo"
Using set
method
location.params
.set({ userID: "HungryHippo" });
.toString();
-> "/user/HungryHippo"
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
return url.params.startsWith("starts/with");
-> true
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
return url.params.is("starts/with/this");
-> true
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
url.params.push("login");
return url.toString();
-> "http://localhost:3000/starts/with/this/login"
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
url.params.push({ name: "login" });
return url.params.name;
-> "login"
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
url.params.unshift("login");
return url.toString();
-> "http://localhost:3000/login/starts/with/this"
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
url.params.unshift({ name: "login" });
return url.params.name;
-> "login"
location.search.comments = "100";
location.toString();
-> "/user/HungryHippo?comments=100"
const loc = new URL({ href: "http://localhost:3000/login?reset" });
loc.search.reset -> 1
loc.toString() -> "http://localhost:3000/login?reset=1"
set(props: Object)
get(key)
-> value
get(Array[key])
-> { key: value }
copy()
params.toString()
search.toString()
search.fromString(String)
The copy
method will create a whole new object instance
location.copy();
const url = new Url({ href: "http://www.location.com/#hash" });
url.hash -> Hash { value : "#hash" }
url.location.hash -> "#hash"
// You can omit the "#" when setting the value
url.hash.value = "my-hash";
url.hash.toString() -> "#my-hash";