1.12.0 • Published 6 years ago

urli v1.12.0

Weekly downloads
2
License
ISC
Repository
-
Last release
6 years ago

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
ArgumentType
SchemaUrlObject|String|null
LocationUrlObject|String|null
UrlObject
PropertyTypeOptional
originStringtrue
hrefStringtrue
pathnameStringtrue
hashStringtrue
searchStringtrue

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";
1.12.0

6 years ago

1.11.2

6 years ago

1.11.1

6 years ago

1.11.0

6 years ago

1.10.0

6 years ago

1.9.0

6 years ago

1.8.7

6 years ago

1.8.6

6 years ago

1.8.5

6 years ago

1.8.4

6 years ago

1.8.3

6 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.4

6 years ago

1.6.3

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago