router-hook
Advanced tools
Comparing version 5.0.1 to 5.0.2
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
@@ -13,8 +25,9 @@ }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.A = exports.useHistory = exports.useRouter = exports.route = exports.useBlockRoute = exports.useSetRoute = exports.useLocation = exports.RouterProvider = void 0; | ||
const Provider_1 = __importStar(require("./Provider")); | ||
exports.RouterProvider = Provider_1.default; | ||
exports.useLocation = Provider_1.useLocation; | ||
exports.useSetRoute = Provider_1.useSetRoute; | ||
exports.useBlockRoute = Provider_1.useBlockRoute; | ||
exports.useHistory = Provider_1.useHistory; | ||
Object.defineProperty(exports, "useLocation", { enumerable: true, get: function () { return Provider_1.useLocation; } }); | ||
Object.defineProperty(exports, "useSetRoute", { enumerable: true, get: function () { return Provider_1.useSetRoute; } }); | ||
Object.defineProperty(exports, "useBlockRoute", { enumerable: true, get: function () { return Provider_1.useBlockRoute; } }); | ||
Object.defineProperty(exports, "useHistory", { enumerable: true, get: function () { return Provider_1.useHistory; } }); | ||
const route_1 = __importDefault(require("./route")); | ||
@@ -21,0 +34,0 @@ exports.route = route_1.default; |
import { ReactElement } from "react"; | ||
export declare function useSetRoute(): any; | ||
export declare function useLocation(): any; | ||
export declare function useBlockRoute(): any; | ||
export declare function useHistory(): any; | ||
export declare function useSetRoute(): (path?: string) => boolean; | ||
export declare function useLocation(): URL; | ||
export declare function useBlockRoute(): [boolean, (block: boolean) => void]; | ||
export declare function useHistory(): import("history").BrowserHistory<object>; | ||
export default function Provider({ children }: { | ||
children: ReactElement; | ||
}): JSX.Element; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
@@ -13,2 +25,3 @@ }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useHistory = exports.useBlockRoute = exports.useLocation = exports.useSetRoute = void 0; | ||
const react_1 = __importStar(require("react")); | ||
@@ -15,0 +28,0 @@ const useRouter_1 = __importDefault(require("./useRouter")); |
@@ -1,5 +0,8 @@ | ||
/// <reference types="react" /> | ||
export default function route(path: string): { | ||
match: (location: string, render?: import("react").FunctionComponent<{}>) => {}; | ||
toUrl: (params?: object) => string; | ||
}; | ||
import { ReactNode } from "react"; | ||
interface Route<P = Record<string, string>> { | ||
path: string; | ||
match(location: string, render?: (p: P) => ReactNode): ReactNode | P | undefined; | ||
toUrl(params?: Record<string, unknown>): string; | ||
} | ||
export default function route<P = Record<string, string>>(path: string): Route<P>; | ||
export {}; |
@@ -11,11 +11,16 @@ "use strict"; | ||
const toPath = path_to_regexp_1.compile(path); | ||
const matchLocation = (location) => { | ||
const m = regex.exec(location); | ||
return m ? getParams(keys, m) : undefined; | ||
}; | ||
return { | ||
match: (location, render) => { | ||
const match = regex.exec(location); | ||
const params = match ? getParams(keys, match) : undefined; | ||
if (params) { | ||
return render ? render(params) : params; | ||
path, | ||
match(location, render) { | ||
const matched = matchLocation(location); | ||
if (matched) { | ||
return render ? render(matched) : matched; | ||
} | ||
return undefined; | ||
}, | ||
toUrl: (params) => { | ||
toUrl(params) { | ||
const defined = params | ||
@@ -22,0 +27,0 @@ ? Object.entries(params).reduce((acc, [k, v]) => { |
@@ -1,2 +0,3 @@ | ||
declare function useRouter(): (boolean | URL | import("history").History<{}> | ((path?: string) => boolean) | ((value: any) => void))[]; | ||
import { createBrowserHistory } from "history"; | ||
declare function useRouter(): [URL, (path?: string) => boolean, boolean, (value: boolean) => void, ReturnType<typeof createBrowserHistory>]; | ||
export default useRouter; |
@@ -24,3 +24,3 @@ "use strict"; | ||
}, [blocking]); | ||
const setIsBlocking = react_1.useCallback(value => { | ||
const setIsBlocking = react_1.useCallback((value) => { | ||
setBlocking(value); | ||
@@ -27,0 +27,0 @@ if (value === false) { |
{ | ||
"name": "router-hook", | ||
"author": "Tim Field <tim@mohiohio.com>", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"main": "dist/index.js", | ||
@@ -13,19 +13,21 @@ "homepage": "https://github.com/tim-field/router-hook/#readme", | ||
"dependencies": { | ||
"history": "^4.9.0", | ||
"path-to-regexp": "^6.0.0" | ||
"history": "^5.0.0", | ||
"path-to-regexp": "^6.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/history": "4.7.5", | ||
"@types/react": "16.9.34", | ||
"@types/react-dom": "16.9.6", | ||
"@typescript-eslint/eslint-plugin": "2.29.0", | ||
"@typescript-eslint/parser": "2.29.0", | ||
"eslint": "6.8.0", | ||
"@types/history": "4.7.6", | ||
"@types/react": "16.9.38", | ||
"@types/react-dom": "16.9.8", | ||
"@typescript-eslint/eslint-plugin": "3.3.0", | ||
"@typescript-eslint/parser": "3.3.0", | ||
"eslint": "7.3.0", | ||
"eslint-config-prettier": "6.11.0", | ||
"eslint-plugin-prettier": "3.1.3", | ||
"eslint-plugin-react": "7.19.0", | ||
"eslint-plugin-react-hooks": "1.7.0", | ||
"eslint-plugin-prettier": "3.1.4", | ||
"eslint-plugin-react": "7.20.0", | ||
"eslint-plugin-react-hooks": "4.0.4", | ||
"prettier": "1.19.1", | ||
"tslib": "1.11.1", | ||
"typescript": "3.8.3", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"tslib": "2.0.0", | ||
"typescript": "3.9.5", | ||
"yalc": "1.0.0-pre.35" | ||
@@ -32,0 +34,0 @@ }, |
import { pathToRegexp, Key, compile } from "path-to-regexp" | ||
import { ReactNode } from "react" | ||
function getParams(keys: Key[], match: RegExpExecArray) { | ||
function getParams<P = Record<string, string>>( | ||
keys: Key[], | ||
match: RegExpExecArray | ||
): P { | ||
return keys.reduce( | ||
(acc, key, i) => ({ [key.name]: match[i + 1], ...acc }), | ||
{} | ||
{} as P | ||
) | ||
} | ||
export default function route(path: string) { | ||
interface Route<P = Record<string, string>> { | ||
path: string | ||
match( | ||
location: string, | ||
render?: (p: P) => ReactNode | ||
): ReactNode | P | undefined | ||
toUrl(params?: Record<string, unknown>): string | ||
} | ||
export default function route<P = Record<string, string>>( | ||
path: string | ||
): Route<P> { | ||
const keys: Key[] = [] | ||
@@ -15,11 +30,17 @@ const regex = pathToRegexp(path, keys) | ||
const matchLocation = (location: string): P | undefined => { | ||
const m = regex.exec(location) | ||
return m ? getParams<P>(keys, m) : undefined | ||
} | ||
return { | ||
match: (location: string, render?: React.FunctionComponent) => { | ||
const match = regex.exec(location) | ||
const params = match ? getParams(keys, match) : undefined | ||
if (params) { | ||
return render ? render(params) : params | ||
path, | ||
match(location: string, render?: (p: P) => ReactNode) { | ||
const matched = matchLocation(location) | ||
if (matched) { | ||
return render ? render(matched) : matched | ||
} | ||
return undefined | ||
}, | ||
toUrl: (params?: object) => { | ||
toUrl(params?: Record<string, unknown>) { | ||
const defined = params | ||
@@ -26,0 +47,0 @@ ? Object.entries(params).reduce((acc, [k, v]) => { |
@@ -9,3 +9,9 @@ import { useEffect, useState, useCallback } from "react" | ||
function useRouter() { | ||
function useRouter(): [ | ||
URL, | ||
(path?: string) => boolean, | ||
boolean, | ||
(value: boolean) => void, | ||
ReturnType<typeof createBrowserHistory> | ||
] { | ||
const [location, setLocation] = useState(() => cloneLocation()) | ||
@@ -31,3 +37,3 @@ const [blocking, setBlocking] = useState(false) | ||
const setIsBlocking = useCallback(value => { | ||
const setIsBlocking = useCallback((value: boolean) => { | ||
setBlocking(value) | ||
@@ -34,0 +40,0 @@ if (value === false) { |
Sorry, the diff of this file is not supported yet
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
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
18223
433
16
+ Addedhistory@5.3.0(transitive)
- Removedhistory@4.10.1(transitive)
- Removedresolve-pathname@3.0.0(transitive)
- Removedtiny-invariant@1.3.3(transitive)
- Removedtiny-warning@1.0.3(transitive)
- Removedvalue-equal@1.0.1(transitive)
Updatedhistory@^5.0.0
Updatedpath-to-regexp@^6.1.0