core-router
Advanced tools
Comparing version 0.1.2 to 1.0.0
@@ -404,43 +404,40 @@ /** | ||
var match$1 = function (rule) { | ||
var _a; | ||
var match$1 = function (rule, urlLike) { | ||
var _a, _b; | ||
var path = rule.path ? rule.path.replace(/\/$/, "") : undefined; | ||
var search = (_a = rule.search, (_a !== null && _a !== void 0 ? _a : {})); | ||
return function (urlLike) { | ||
var _a; | ||
var url = asUrl(urlLike); | ||
if (url.origin !== location.origin) | ||
var url = asUrl(urlLike); | ||
if (url.origin !== location.origin) | ||
return null; | ||
var params = {}; | ||
// Match path | ||
if (path !== undefined) { | ||
var matcher = match(path, { | ||
end: true, | ||
decode: decodeURIComponent | ||
}); | ||
var pathResult = matcher(url.pathname); | ||
if (!pathResult) | ||
return null; | ||
var params = {}; | ||
// Match path | ||
if (path !== undefined) { | ||
var matcher = match(path, { | ||
end: true, | ||
decode: decodeURIComponent | ||
}); | ||
var pathResult = matcher(url.pathname); | ||
if (!pathResult) | ||
return null; | ||
Object.assign(params, pathResult.params); | ||
} | ||
// Match search | ||
for (var _i = 0, _b = Object.entries(search); _i < _b.length; _i++) { | ||
var _c = _b[_i], name_1 = _c[0], pattern = _c[1]; | ||
var matcher = match(pattern, { | ||
end: true, | ||
decode: decodeURIComponent, | ||
delimiter: "&" | ||
}); | ||
var queryResult = matcher(encodeURIComponent((_a = url.searchParams.get(name_1), (_a !== null && _a !== void 0 ? _a : "")))); | ||
if (!queryResult) { | ||
return null; | ||
} | ||
Object.assign(params, queryResult.params); | ||
} | ||
var match$$1 = { params: params, url: url }; | ||
if (rule.condition && !rule.condition(match$$1)) { | ||
Object.assign(params, pathResult.params); | ||
} | ||
// Match search | ||
for (var _i = 0, _c = Object.entries(search); _i < _c.length; _i++) { | ||
var _d = _c[_i], name_1 = _d[0], pattern = _d[1]; | ||
var matcher = match(pattern, { | ||
end: true, | ||
decode: decodeURIComponent, | ||
delimiter: "&" | ||
}); | ||
var queryResult = matcher(encodeURIComponent((_b = url.searchParams.get(name_1), (_b !== null && _b !== void 0 ? _b : "")))); | ||
if (!queryResult) { | ||
return null; | ||
} | ||
return match$$1; | ||
}; | ||
Object.assign(params, queryResult.params); | ||
} | ||
var match$$1 = { params: params, url: url }; | ||
if (rule.condition && !rule.condition(match$$1)) { | ||
return null; | ||
} | ||
return match$$1; | ||
}; | ||
@@ -456,3 +453,4 @@ | ||
}; | ||
var toHref = function (rule, params) { | ||
function toHref(rule, params) { | ||
if (params === void 0) { params = {}; } | ||
var _a; | ||
@@ -486,3 +484,3 @@ var pathname = rule.path | ||
return href; | ||
}; | ||
} | ||
@@ -489,0 +487,0 @@ /* istanbul ignore file */ |
@@ -410,43 +410,40 @@ (function (global, factory) { | ||
var match$1 = function (rule) { | ||
var _a; | ||
var match$1 = function (rule, urlLike) { | ||
var _a, _b; | ||
var path = rule.path ? rule.path.replace(/\/$/, "") : undefined; | ||
var search = (_a = rule.search, (_a !== null && _a !== void 0 ? _a : {})); | ||
return function (urlLike) { | ||
var _a; | ||
var url = asUrl(urlLike); | ||
if (url.origin !== location.origin) | ||
var url = asUrl(urlLike); | ||
if (url.origin !== location.origin) | ||
return null; | ||
var params = {}; | ||
// Match path | ||
if (path !== undefined) { | ||
var matcher = match(path, { | ||
end: true, | ||
decode: decodeURIComponent | ||
}); | ||
var pathResult = matcher(url.pathname); | ||
if (!pathResult) | ||
return null; | ||
var params = {}; | ||
// Match path | ||
if (path !== undefined) { | ||
var matcher = match(path, { | ||
end: true, | ||
decode: decodeURIComponent | ||
}); | ||
var pathResult = matcher(url.pathname); | ||
if (!pathResult) | ||
return null; | ||
Object.assign(params, pathResult.params); | ||
} | ||
// Match search | ||
for (var _i = 0, _b = Object.entries(search); _i < _b.length; _i++) { | ||
var _c = _b[_i], name_1 = _c[0], pattern = _c[1]; | ||
var matcher = match(pattern, { | ||
end: true, | ||
decode: decodeURIComponent, | ||
delimiter: "&" | ||
}); | ||
var queryResult = matcher(encodeURIComponent((_a = url.searchParams.get(name_1), (_a !== null && _a !== void 0 ? _a : "")))); | ||
if (!queryResult) { | ||
return null; | ||
} | ||
Object.assign(params, queryResult.params); | ||
} | ||
var match$$1 = { params: params, url: url }; | ||
if (rule.condition && !rule.condition(match$$1)) { | ||
Object.assign(params, pathResult.params); | ||
} | ||
// Match search | ||
for (var _i = 0, _c = Object.entries(search); _i < _c.length; _i++) { | ||
var _d = _c[_i], name_1 = _d[0], pattern = _d[1]; | ||
var matcher = match(pattern, { | ||
end: true, | ||
decode: decodeURIComponent, | ||
delimiter: "&" | ||
}); | ||
var queryResult = matcher(encodeURIComponent((_b = url.searchParams.get(name_1), (_b !== null && _b !== void 0 ? _b : "")))); | ||
if (!queryResult) { | ||
return null; | ||
} | ||
return match$$1; | ||
}; | ||
Object.assign(params, queryResult.params); | ||
} | ||
var match$$1 = { params: params, url: url }; | ||
if (rule.condition && !rule.condition(match$$1)) { | ||
return null; | ||
} | ||
return match$$1; | ||
}; | ||
@@ -462,3 +459,4 @@ | ||
}; | ||
var toHref = function (rule, params) { | ||
function toHref(rule, params) { | ||
if (params === void 0) { params = {}; } | ||
var _a; | ||
@@ -492,3 +490,3 @@ var pathname = rule.path | ||
return href; | ||
}; | ||
} | ||
@@ -495,0 +493,0 @@ /* istanbul ignore file */ |
@@ -5,44 +5,41 @@ "use strict"; | ||
var as_url_1 = require("./as-url"); | ||
exports.match = function (rule) { | ||
var _a; | ||
exports.match = function (rule, urlLike) { | ||
var _a, _b; | ||
var path = rule.path ? rule.path.replace(/\/$/, "") : undefined; | ||
var search = (_a = rule.search, (_a !== null && _a !== void 0 ? _a : {})); | ||
return function (urlLike) { | ||
var _a; | ||
var url = as_url_1.asUrl(urlLike); | ||
if (url.origin !== location.origin) | ||
var url = as_url_1.asUrl(urlLike); | ||
if (url.origin !== location.origin) | ||
return null; | ||
var params = {}; | ||
// Match path | ||
if (path !== undefined) { | ||
var matcher = path_to_regexp_1.match(path, { | ||
end: true, | ||
decode: decodeURIComponent | ||
}); | ||
var pathResult = matcher(url.pathname); | ||
if (!pathResult) | ||
return null; | ||
var params = {}; | ||
// Match path | ||
if (path !== undefined) { | ||
var matcher = path_to_regexp_1.match(path, { | ||
end: true, | ||
decode: decodeURIComponent | ||
}); | ||
var pathResult = matcher(url.pathname); | ||
if (!pathResult) | ||
return null; | ||
Object.assign(params, pathResult.params); | ||
} | ||
// Match search | ||
for (var _i = 0, _b = Object.entries(search); _i < _b.length; _i++) { | ||
var _c = _b[_i], name_1 = _c[0], pattern = _c[1]; | ||
var matcher = path_to_regexp_1.match(pattern, { | ||
end: true, | ||
decode: decodeURIComponent, | ||
delimiter: "&" | ||
}); | ||
var queryResult = matcher(encodeURIComponent((_a = url.searchParams.get(name_1), (_a !== null && _a !== void 0 ? _a : "")))); | ||
if (!queryResult) { | ||
return null; | ||
} | ||
Object.assign(params, queryResult.params); | ||
} | ||
var match = { params: params, url: url }; | ||
if (rule.condition && !rule.condition(match)) { | ||
Object.assign(params, pathResult.params); | ||
} | ||
// Match search | ||
for (var _i = 0, _c = Object.entries(search); _i < _c.length; _i++) { | ||
var _d = _c[_i], name_1 = _d[0], pattern = _d[1]; | ||
var matcher = path_to_regexp_1.match(pattern, { | ||
end: true, | ||
decode: decodeURIComponent, | ||
delimiter: "&" | ||
}); | ||
var queryResult = matcher(encodeURIComponent((_b = url.searchParams.get(name_1), (_b !== null && _b !== void 0 ? _b : "")))); | ||
if (!queryResult) { | ||
return null; | ||
} | ||
return match; | ||
}; | ||
Object.assign(params, queryResult.params); | ||
} | ||
var match = { params: params, url: url }; | ||
if (rule.condition && !rule.condition(match)) { | ||
return null; | ||
} | ||
return match; | ||
}; | ||
//# sourceMappingURL=match.js.map |
@@ -13,3 +13,4 @@ "use strict"; | ||
}; | ||
exports.toHref = function (rule, params) { | ||
function toHref(rule, params) { | ||
if (params === void 0) { params = {}; } | ||
var _a; | ||
@@ -43,3 +44,4 @@ var pathname = rule.path | ||
return href; | ||
}; | ||
} | ||
exports.toHref = toHref; | ||
//# sourceMappingURL=to-href.js.map |
@@ -1,4 +0,4 @@ | ||
import { GenericParams, Match, Rule } from "./rule"; | ||
import { GenericParams, Match, Rule } from "./types"; | ||
export { GenericParams, Match, Rule }; | ||
export { match } from "./match"; | ||
export { toHref } from "./to-href"; |
@@ -1,2 +0,2 @@ | ||
import { Match, Rule } from "./rule"; | ||
export declare const match: <T extends {}>(rule: Rule<T>) => (urlLike: string | URL) => Match<T> | null; | ||
import { Match, Rule } from "./types"; | ||
export declare const match: <T extends {}>(rule: Rule<T>, urlLike: string | URL) => Match<T> | null; |
@@ -1,2 +0,3 @@ | ||
import { Rule } from "./rule"; | ||
export declare const toHref: <T extends {}>(rule: Rule<T>, params: T) => string; | ||
import { Rule } from "./types"; | ||
export declare function toHref(rule: Rule<{}>): string; | ||
export declare function toHref<T extends {}>(rule: Rule<T>, params: T): string; |
{ | ||
"name": "core-router", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Primitives for building your own router", | ||
"keywords": [ | ||
"react", | ||
"react-router", | ||
"routing", | ||
@@ -97,2 +98,2 @@ "url" | ||
} | ||
} | ||
} |
120
README.md
# core-router | ||
Primitives for building your own router. | ||
## Matching URLs with `match()` | ||
`match()` provides flexible rule-based URL matching. | ||
You can: | ||
1. Use a path-only rule: | ||
```js | ||
match({ path: "/account/:accountId" }, "/account/123"); | ||
result = { | ||
params: { | ||
accountId: "123"; | ||
} | ||
} | ||
``` | ||
2. Combine path, search params, and a condition: | ||
```js | ||
match( | ||
{ | ||
path: "/account/:accountId", | ||
search: { | ||
from: ":fromDate?", | ||
to: ":toDate?" | ||
}, | ||
condition: ({ params }) => { | ||
return isDate(params.fromDate) && isDate(params.toDate); | ||
} | ||
}, | ||
"/account/123?from=2020-01-01&to=2020-01-31" | ||
); | ||
result = { | ||
params: { | ||
accountId: "123", | ||
fromDate: "2020-01-01", | ||
toDate: "2020-01-31" | ||
} | ||
}; | ||
``` | ||
3. Or fallback to completely custom logic: | ||
```js | ||
match( | ||
{ | ||
condition: ({ url }) => { | ||
return url.hash === "#secret"; | ||
} | ||
}, | ||
"/you/must/have/the#secret" | ||
); | ||
result = { | ||
params: {} | ||
}; | ||
``` | ||
Absolute URLs will only match if they belong to the same domain: | ||
```js | ||
// On https://foo.com: | ||
match({ path: "/hello" }, "https://bar.com/hello"); | ||
result = null; | ||
``` | ||
Params will be decoded for you: | ||
```js | ||
match({ search: { q: ":searchTerms" } }, "/?q=hello%20world"); | ||
result = { | ||
params: { | ||
searchTerms: "hello world" | ||
} | ||
}; | ||
``` | ||
## Generating URLs with `toHref()` | ||
Any rule that can be used for `match()` can also be used to generate a relative URL. | ||
```js | ||
toHref({ path: "/account/:accountId" }, { accountId: "123" }); | ||
result = "/account/123"; | ||
``` | ||
If required params are not supplied, an error will be thrown: | ||
```js | ||
toHref({ path: "/account/:accountId" }, {}); | ||
// Error! | ||
``` | ||
Similarly, if the `condition` is not met, an error will alos be thrown: | ||
```js | ||
toHref( | ||
{ | ||
condition: ({ url }) => url.hash === "#secret" | ||
}, | ||
"/#incorrect" | ||
); | ||
// Error! | ||
``` | ||
This ensures that for any rule, a URL generated by `toHref()` will also `match()`: | ||
```js | ||
const href = toHref(myRule, myParams); | ||
const matchResult = match(myRule, href); | ||
expect(matchResult).not.toBe(null); | ||
expect(matchResult.params).toEqual(myParams); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
0
124
0
119722
22
1099