hash.js

A simple and flexible JavaScript library that manages "window.location.hash". With the ability to support queries. (#value?query)

This project is maintained by irmmr

The query string components

These components are for managing queries as strings and do not process queries at all.

// Hash is => #Hello?repeat=10

// Returns => Object { repeat: 10 }
Hash.q.get();

// Returns => "repeat=10"
Hash.q.str.get();

.set()

Set query string.

.set(string)

string (string) Query value

returns HashCpQueryStr

// Hash is => #hey?name=jj
// After => #hey?name=f&age=90
Hash.q.str.set("name=f&age=90");

.get()

Get query as string.

.get()

returns string

// Hash is => #hey?name=jj&age=90
// Returns => "name=jj&age=90"
Hash.q.str.get();

.add()

Add string to query.

.add(value, {options})

value (string) Amount to be added.

position (string) Define hole position.

returns HashCpQueryStr

.add(value, position)

value (string) Amount to be added.

options (object) Options to define position, index.

returns HashCpQueryStr

Position entry

// Hash is => #hey?wea=cloud
// After => #hey?simplewea=cloud
Hash.q.str.add("simple", "before");

// Hash is => #hey?wea=cloud
// After => #hey?wea=cloudsimple
Hash.q.str.add("simple", "after");

// Hash is => #hey?wea=cloud
// After => #hey?weasimple=cloud
Hash.q.str.add("simple", "after:wea");

// Hash is => #hey?wea=cloud
// After => #hey?wea=simplecloud
Hash.q.str.add("simple", "after:=");

// Hash is => #hey?wea=cloud
// After => #hey?wBigea=cloud
Hash.q.str.add("Big", "index:1");

// Hash is => #hey?wea=cloud&m=14
// After => #hey?wea=>cloud&m=>14
Hash.q.str.add(">", {
  position: "after:=",
  multiple: true,
});

.have()

Check hash value exists.

.have()

Looking for any query as string.

.have(data)

Check if hash query str contains anything.

data (string) Any word or string to check

.have(data)

Check if any hash exists (multiple).

data (array)<string> Words to check

returns boolean

// Hash is => #Hello?name=lola
// Returns => true
Hash.q.str.have();

// Hash is => #Hello
// Returns => false
Hash.q.str.have();

// Hash is => #Hello?name=lola
// Returns => true
Hash.q.str.have("me");

// Hash is => #Hello?name=lola
// Returns => false
Hash.q.str.have("s");

// Hash is => #Hello?name=lola
// Returns => true
Hash.q.str.have(["name", "lola", "a"]);

.is()

Check equality.

.is(data)

data (string) Anything to check

returns boolean

// Hash is => #pa?p=11&flame=783
// Returns => true
Hash.q.str.is("p=11&flame=783");

// Hash is => #pa?p=11&flame=783
// Returns => false
Hash.q.str.is("p=11&fl");

.remove()

Remove parts of hash query as string.

.remove(value)

values (string RegExp) Word/RegExp to remove

.remove(values)

values (array)<string RegExp> Words to remove

returns HashCpQueryStr

// Hash is => #msg?name=hoe&date=802-5225
// After => #msg?name=hoe&5225
Hash.q.str.remove("date=802-");

// Hash is => #msg?name=hoe&date=802-5225
// After => #msg?na=hoe&date=-
Hash.q.str.remove(["me", /[\d+]/g]);

.replace()

Replace hash query as string.

.replace(from, to)

from (string RegExp) Find anything

to (string) Replace to anything

returns HashCpQueryStr

// Hash is => #msg?name=hoe&age=10
// After => #msg?name=jay&age=10
Hash.q.str.replace("hoe", "jay");

// Hash is => #msg?name=hoe&age=10
// After => #msg?name=hoe+age=10
Hash.q.str.replace(/\&/g, "+");