@nano-router/history
Advanced tools
Comparing version 1.5.0 to 2.0.0-alpha.6
{ | ||
"name": "@nano-router/history", | ||
"version": "1.5.0", | ||
"description": "Fork of the history package to make it tree shakeable", | ||
"version": "2.0.0-alpha.6+1dfcaec", | ||
"description": "Manage session history with JavaScript", | ||
"main": "src/index.js", | ||
@@ -19,3 +19,5 @@ "module": "src/index.js", | ||
"mocha": "^8.1.1", | ||
"unexpected": "^11.14.0" | ||
"sinon": "^9.0.3", | ||
"unexpected": "^11.14.0", | ||
"unexpected-sinon": "^10.11.2" | ||
}, | ||
@@ -25,3 +27,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "4d70e2134fadc6b391e7e2df04e793ddc56f60ae" | ||
"gitHead": "1dfcaec6c88254b5c5fb183cfab6bfd9b8e1a905" | ||
} |
import { | ||
allowTx, | ||
createEvents, | ||
createHref, | ||
getNextLocation, | ||
@@ -14,2 +13,10 @@ ensureRootRelative, | ||
function createPath({ pathname = "/", search = "", hash = "" }) { | ||
return pathname + search + hash; | ||
} | ||
function createHref(to) { | ||
return typeof to === "string" ? to : createPath(to); | ||
} | ||
export function createBrowserHistory(options = {}) { | ||
@@ -152,2 +159,32 @@ const { window = document.defaultView } = options; | ||
function pushLocation(url, target) { | ||
function retry() { | ||
pushLocation(url); | ||
} | ||
if (target && target !== "_self") { | ||
window.open(url, target, "noopener"); | ||
} else { | ||
const nextAction = "PUSH"; | ||
const nextLocation = getNextLocation(location, url); | ||
if (allowTx(blockers, nextAction, nextLocation, retry)) { | ||
window.location.assign(url); | ||
} | ||
} | ||
} | ||
function replaceLocation(url) { | ||
const nextAction = "REPLACE"; | ||
const nextLocation = getNextLocation(location, url); | ||
function retry() { | ||
replaceLocation(url); | ||
} | ||
if (allowTx(blockers, nextAction, nextLocation, retry)) { | ||
window.location.replace(url); | ||
} | ||
} | ||
function go(delta) { | ||
@@ -164,5 +201,6 @@ globalHistory.go(delta); | ||
}, | ||
createHref, | ||
push, | ||
replace, | ||
pushLocation, | ||
replaceLocation, | ||
go, | ||
@@ -169,0 +207,0 @@ back() { |
@@ -5,4 +5,3 @@ import { | ||
createKey, | ||
createHref, | ||
parsePath, | ||
parseUrl, | ||
getNextLocation, | ||
@@ -25,3 +24,3 @@ ensureRootRelative, | ||
key: createKey(), | ||
...(typeof entry === "string" ? parsePath(entry) : entry), | ||
...(typeof entry === "string" ? parseUrl(entry) : entry), | ||
}; | ||
@@ -41,2 +40,3 @@ | ||
let location = entries[index]; | ||
let openedWindow = null; | ||
const listeners = createEvents(); | ||
@@ -82,2 +82,36 @@ const blockers = createEvents(); | ||
function pushLocation(url, target) { | ||
function retry() { | ||
pushLocation(url); | ||
} | ||
if (target && target !== "_self") { | ||
openedWindow = { url, target }; | ||
} else { | ||
const nextAction = "PUSH"; | ||
const nextLocation = getNextLocation(location, url); | ||
if (allowTx(blockers, nextAction, nextLocation, retry)) { | ||
entries[index] = nextLocation; | ||
action = nextAction; | ||
location = nextLocation; | ||
} | ||
} | ||
} | ||
function replaceLocation(url) { | ||
const nextAction = "REPLACE"; | ||
const nextLocation = getNextLocation(location, url); | ||
function retry() { | ||
replaceLocation(url); | ||
} | ||
if (allowTx(blockers, nextAction, nextLocation, retry)) { | ||
entries[index] = nextLocation; | ||
action = nextAction; | ||
location = nextLocation; | ||
} | ||
} | ||
function go(delta) { | ||
@@ -107,5 +141,9 @@ const nextIndex = clamp(index + delta, 0, entries.length - 1); | ||
}, | ||
createHref, | ||
get openedWindow() { | ||
return openedWindow; | ||
}, | ||
push, | ||
replace, | ||
pushLocation, | ||
replaceLocation, | ||
go, | ||
@@ -112,0 +150,0 @@ back() { |
@@ -39,10 +39,13 @@ export function createKey() { | ||
function createPath({ pathname = "/", search = "", hash = "" }) { | ||
return pathname + search + hash; | ||
} | ||
export function parseUrl(url) { | ||
const partialPath = { href: url, search: "", hash: "" }; | ||
export function parsePath(path) { | ||
const partialPath = {}; | ||
if (url) { | ||
let path = url; | ||
const protocolIndex = url.indexOf("//"); | ||
if (protocolIndex !== -1) { | ||
const pathIndex = url.indexOf("/", protocolIndex + 2); | ||
path = url.slice(pathIndex); | ||
} | ||
if (path) { | ||
const hashIndex = path.indexOf("#"); | ||
@@ -68,10 +71,5 @@ if (hashIndex >= 0) { | ||
export function createHref(to) { | ||
return typeof to === "string" ? to : createPath(to); | ||
} | ||
export function getNextLocation(currentLocation, to, state = null) { | ||
return { | ||
...currentLocation, | ||
...(typeof to === "string" ? parsePath(to) : to), | ||
...(typeof to === "string" ? parseUrl(to) : to), | ||
state, | ||
@@ -78,0 +76,0 @@ key: createKey(), |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
23357
9
723
0
1
32
4
2
1