Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hook-use-url

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hook-use-url - npm Package Compare versions

Comparing version 2.0.11 to 2.0.12

117

index.js

@@ -11,4 +11,2 @@ import { useSearchParams } from "react-router-dom";

class Url {
location = null;
navigate = null;
searchParams = null;

@@ -23,15 +21,41 @@

constructor(location, navigate, searchParams) {
this.location = location;
this.navigate = navigate;
constructor(
searchParams,
protocol,
domain,
domainWithPort,
port,
path,
fragment
) {
this.searchParams = searchParams;
this.protocol = window?.location?.protocol;
this.domain = window?.location?.hostname;
this.domainWithPort = window?.location?.host;
this.port = window?.location?.port;
this.path = this?.location?.pathname || "/";
this.fragment = window?.location?.hash;
this.protocol = protocol || window?.location?.protocol;
this.domain = domain || window?.location?.hostname;
this.domainWithPort = domainWithPort || window?.location?.host;
this.port = port || window?.location?.port;
this.path = path || window?.location?.pathname || "/";
this.fragment = fragment || window?.location?.hash;
}
__clone(
searchParams,
protocol,
domain,
domainWithPort,
port,
path,
fragment
) {
return new Url(
searchParams || this.searchParams,
protocol || this.protocol,
domain || this.domain,
domainWithPort || this.domainWithPort,
port || this.port,
path || this.path,
fragment || this.fragment
);
}
getProtocol() {

@@ -114,2 +138,3 @@ return this.protocol;

removeQuery(variablesToKeep) {
let _new = this.__clone();
variablesToKeep = variablesToKeep || [];

@@ -125,11 +150,12 @@ if (variablesToKeep) {

variablesToKeep.forEach((_variableToKeep) => {
valuesMap[_variableToKeep] = this.getAll(_variableToKeep);
valuesMap[_variableToKeep] = _new.getAll(_variableToKeep);
});
}
this.searchParams = new URLSearchParams({});
_new.searchParams = new URLSearchParams({});
variablesToKeep.forEach((_variableToKeep) => {
if (valuesMap[_variableToKeep]) {
valuesMap[_variableToKeep].forEach((_value) => {
this.add(_variableToKeep, _value);
_new = _new.add(_variableToKeep, _value);
});

@@ -139,25 +165,29 @@ }

return this;
return _new;
}
removeFragment() {
this.fragment = "";
return this;
let _new = this.__clone();
_new.fragment = "";
return _new;
}
removePath() {
this.path = "/";
return this;
let _new = this.__clone();
_new.path = "";
return _new;
}
setPath(value, doKeepQuery) {
let _new = this.__clone();
if (doKeepQuery !== true) {
this.removeQuery();
this.removeFragment();
_new = _new.removeQuery();
_new = _new.removeFragment();
}
this.path = value || "/";
return this;
_new.path = value || "/";
return _new;
}
setFragment(value) {
let _new = this.__clone();
value = value || "";

@@ -167,7 +197,9 @@ if (value.slice(0, 1) !== "#") {

}
this.fragment = value || "";
return this;
_new.fragment = value;
return _new;
}
set(variable, value) {
let _new = this.__clone();
if (variable) {

@@ -179,46 +211,49 @@ if (value === null) {

if (typeof value === "undefined") {
this.searchParams.delete(variable);
_new.searchParams.delete(variable);
} else if (typeof value === "string") {
this.searchParams.set(variable, value);
_new.searchParams.set(variable, value);
} else if (typeof value === "object" && Array.isArray(value)) {
this.searchParams.delete(variable);
_new.searchParams.delete(variable);
if (value.length > 0) {
value.forEach((_value) => {
this.searchParams.append(variable, _value);
_new.searchParams.append(variable, _value);
});
}
} else {
this.searchParams.set(variable, JSON.stringify(value));
_new.searchParams.set(variable, JSON.stringify(value));
}
}
return this;
return _new;
}
add(variable, value) {
let _new = this.__clone();
if (variable && typeof variable === "string") {
let _current = this.searchParams.getAll(variable);
let _current = _new.searchParams.getAll(variable);
if (_current.includes(value) === false) {
this.searchParams.append(variable, value);
_new.searchParams.append(variable, value);
}
}
return this;
return _new;
}
subtract(variable, value) {
let _new = this.__clone();
if (variable) {
let _current = this.searchParams.getAll(variable);
this.searchParams.delete(variable);
let _current = _new.searchParams.getAll(variable);
_new.searchParams.delete(variable);
_current = _current.filter((x) => x !== value);
if (_current.length > 0) {
_current.forEach((x) => this.searchParams.append(variable, x));
_current.forEach((x) => _new.searchParams.append(variable, x));
}
}
return this;
return _new;
}
delete(variable) {
let _new = this.__clone();
if (variable) {
this.searchParams.delete(variable);
_new.searchParams.delete(variable);
}
return this;
return _new;
}

@@ -237,3 +272,3 @@

return new Url(location, navigate, searchParams);
return new Url(searchParams);
}
{
"name": "hook-use-url",
"version": "2.0.11",
"version": "2.0.12",
"keywords": [

@@ -5,0 +5,0 @@ "react-router",

@@ -50,13 +50,13 @@ `npm i hook-use-url`

| `url.getUri()` | Get full URI without protocol, domain, or port | `/some-path?x1=b#foo` |
| `url.get(variable)` | Get value of `x1` from query `url.get("x1")` | `some-string-value` |
| `url.getAll(variable)` | Get values of `x1` from query. Always returns an array. `url.getAll("x1")` | `[]`, `["value"]`, `["a", "b"]` |
| `url.get(variable)` | Get value of `x1` from query `url.get("x1")` | `some-string-value` |
| `url.getAll(variable)` | Get values of `x1` from query. Always returns an array. `url.getAll("x1")` | `[]`, `["value"]`, `["a", "b"]` |
| `url.removeQuery(variablesToKeep)` | Remove query from URI. May pass in _optional_ array of variables to keep: `url.removeQuery(["x1"])` | `url` object to chain more methods. |
| `url.removeFragment()` | Remove fragment from URI | `url` object to chain more methods. |
| `url.removePath()` | Remove path from URI | `url` object to chain more methods. |
| `url.setPath(value, doKeepQuery)` | Set path & either keep rest of query/fragment or not. Default is not. `url.setPath("/about", true)` | `url` object to chain more methods. |
| `url.setFragment(value)` | Set new value for fragment. May be any falsy value or string. `url.setFragment('title')` | `url` object to chain more methods. |
| `url.set(variable, value)` | Set value of variable in url `url.set("x1", "y")` | `url` object to chain more methods. |
| `url.add(variable, value)` | Add one more value or set 1st value for the variable in url. `url.add("tag", "foo")` | `url` object to chain more methods. |
| `url.subtract(variable, value)` | Subtract one value from the variable in url. `url.subtract("tag", "foo")` | `url` object to chain more methods. |
| `url.delete(variable)` | Delete single or multiple values of the variable. `url.delete("tag", "foo")` | `url` object to chain more methods. |
| `url.setPath(value, doKeepQuery)` | Set path & either keep rest of query/fragment or not. Default is not. `url.setPath("/about", true)` | `url` object to chain more methods. |
| `url.setFragment(value)` | Set new value for fragment. May be any falsy value or string. `url.setFragment('title')` | `url` object to chain more methods. |
| `url.set(variable, value)` | Set value of variable in url `url.set("x1", "y")` | `url` object to chain more methods. |
| `url.add(variable, value)` | Add one more value or set 1st value for the variable in url. `url.add("tag", "foo")` | `url` object to chain more methods. |
| `url.subtract(variable, value)` | Subtract one value from the variable in url. `url.subtract("tag", "foo")` | `url` object to chain more methods. |
| `url.delete(variable)` | Delete single or multiple values of the variable. `url.delete("tag", "foo")` | `url` object to chain more methods. |
| `url.push()` | Set URI at browser's address input and push history event to browser's history | `url` object to chain more methods. |

@@ -63,0 +63,0 @@ | `url.replace()` | Set URI at browser's address input and replace current history event with new at browser's history | `url` object to chain more methods. |

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc