urli
v1.12.0
Published
An interface for manipulating and querying a URL
Downloads
21
Readme
URLi (URL Interface)
A constructor for manipulating and querying URLs
File size
8.73 kB
Usage
import URL from "urli";
const location = new URL(Schema, Location);
Types
Arguments
Argument|Type -|- Schema|UrlObject|String|null Location|UrlObject|String|null
UrlObject
Property|Type|Optional
-|-
origin
|String
|true
href
|String
|true
pathname
|String
|true
hash
|String
|true
search
|String
|true
Example Usage
location.setSchema(String)
-> URL
Methods
URL.prototype.set
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"
URL.prototype.test
const url = new URL("/language/:lang");
url.test("/lang/english") -> false
url.test("/language/english") -> true
let url = new URL({ href: "http://localhost:3000/?string" });
url.set({
pathname : "/path/name"
});
url.toString();
-> "http://localhost:3000/path/name?string=1"
Params
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"
Changing values of params
location.params.userID = "HungryHippo";
location.toString();
-> "/user/HungryHippo"
Using set
method
location.params
.set({ userID: "HungryHippo" });
.toString();
-> "/user/HungryHippo"
Methods
startsWith
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
return url.params.startsWith("starts/with");
-> true
is
let url = new URL({
href: "http://localhost:3000/starts/with/this"
});
return url.params.is("starts/with/this");
-> true
push
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"
unshift
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"
Changing values of search
location.search.comments = "100";
location.toString();
-> "/user/HungryHippo?comments=100"
Search queries default values
const loc = new URL({ href: "http://localhost:3000/login?reset" });
loc.search.reset -> 1
loc.toString() -> "http://localhost:3000/login?reset=1"
Methods
set(props: Object)
get(key)
-> value
get(Array[key])
-> { key: value }
copy()
params.toString()
search.toString()
search.fromString(String)
copy()
The copy
method will create a whole new object instance
location.copy();
Hash
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";