Socket
Socket
Sign inDemoInstall

core-router

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.2 to 1.0.0

dist/lib/types.js

74

dist/core-router.es5.js

@@ -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"

}
}
}
# 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

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc