Skip to content

Commit adb1fc8

Browse files
refactor(UrlRule): Change match and handler params from (path, search, query) to (urlService)
1 parent 2092e96 commit adb1fc8

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"test": "karma start",
1010
"watch": "run-p watch:*",
1111
"watch:buildjs": "tsc -w",
12+
"watch:buildesm": "tsc -w -m es6 --outDir lib-esm",
1213
"watch:test": "karma start --singleRun=false --autoWatch=true --autoWatchInterval=1",
1314
"debug": "karma start --singleRun=false --autoWatch=true --autoWatchInterval=1 --browsers=Chrome"
1415
},

src/url/interface.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { LocationConfig } from "../common/coreservices";
22
import { Obj } from "../common/common";
33
import { ParamType } from "../params/type";
44
import { Param } from "../params/param";
5+
import { UrlService } from "./urlService";
6+
import { UIRouter } from "../router";
57

68
export interface ParamFactory {
79
/** Creates a new [[Param]] from a CONFIG block */
@@ -21,14 +23,26 @@ export interface UrlMatcherConfig {
2123
paramType(name, type?)
2224
}
2325

24-
/** @return truthy or falsey */
26+
/**
27+
* A function that matches the URL for a [[UrlRule]]
28+
*
29+
* Implementations should match against the current
30+
* [[UrlService.path]], [[UrlService.search]], and [[UrlService.hash]]
31+
*
32+
* @return truthy or falsey
33+
*/
2534
export interface UrlRuleMatchFn {
26-
(path: string, search: Obj, hash: string): any;
35+
(urlService?: UrlService, router?: UIRouter): any;
2736
}
2837

29-
/** Handler invoked when a rule is matched */
38+
/**
39+
* Handler invoked when a rule is matched
40+
*
41+
* The matched value from the rule's [[UrlRuleMatchFn]] is passed as the first argument
42+
* The handler should return a string (to redirect), or void
43+
*/
3044
export interface UrlRuleHandlerFn {
31-
(matchObject: any, path?: string, search?: Obj, hash?: string): (string|boolean|void);
45+
(matchValue?: any, urlService?: UrlService, router?: UIRouter): (string|void);
3246
}
3347

3448
export type UrlRuleType = "STATE" | "URLMATCHER" | "STRING" | "REGEXP" | "RAW" | "OTHER";

src/url/urlRouter.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,14 @@ export class UrlRouter implements Disposable {
7272
sync(evt?) {
7373
if (evt && evt.defaultPrevented) return;
7474

75-
let $url = this._router.urlService;
76-
let path = $url.path(),
77-
search = $url.search(),
78-
hash = $url.hash();
75+
let router = this._router;
76+
let $url = router.urlService;
7977

8078
function check(rule: UrlRule) {
81-
let match = rule && rule.match(path, search, hash);
79+
let match = rule && rule.match($url, router);
8280
if (!match) return false;
8381

84-
let result = rule.handler(match, path, search, hash);
82+
let result = rule.handler(match, $url, router);
8583
if (isString(result)) $url.url(result, true);
8684
return true;
8785
}

src/url/urlRule.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RawParams } from "../params/interface";
88
import { UrlRule, UrlRuleMatchFn, UrlRuleHandlerFn, UrlRuleType } from "./interface";
99
import { StateService } from "../state/stateService";
1010
import { UIRouterGlobals } from "../globals";
11+
import { UrlService } from "./urlService";
1112

1213
/**
1314
* Creates a [[UrlRule]]
@@ -113,8 +114,8 @@ export class RegExpRule implements UrlRule {
113114
newurl.replace(/\$(\$|\d{1,2})/, (m, what) =>
114115
match[what === '$' ? 0 : Number(what)]);
115116

116-
match(path: string): RegExpExecArray {
117-
return this.regexp.exec(path);
117+
match($url: UrlService): RegExpExecArray {
118+
return this.regexp.exec($url.path());
118119
}
119120
}
120121

@@ -168,8 +169,8 @@ export class UrlMatcherRule implements UrlRule {
168169
(match: RawParams) =>
169170
newurl.format(match);
170171

171-
match = (path: string, search: any, hash: string) =>
172-
this.urlMatcher.exec(path, search, hash);
172+
match = ($url: UrlService) =>
173+
this.urlMatcher.exec($url.path(), $url.search(), $url.hash());
173174
}
174175

175176
/**
@@ -193,8 +194,8 @@ export class StateUrlRule implements UrlRule {
193194
this.$state = router.stateService;
194195
}
195196

196-
match = (path: string, search: any, hash: string) =>
197-
this.state.url.exec(path, search, hash);
197+
match = ($url: UrlService) =>
198+
this.state.url.exec($url.path(), $url.search(), $url.hash());
198199

199200
/**
200201
* Checks if the router should start a new transition.
@@ -210,7 +211,6 @@ export class StateUrlRule implements UrlRule {
210211
if (this.shouldTransition(match)) {
211212
this.$state.transitionTo(this.state, match, { inherit: true, source: "url" });
212213
}
213-
return true;
214214
};
215215
}
216216

test/urlRouterSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("UrlRouter", function () {
2626

2727
beforeEach(function () {
2828
let rule1 = $ur.urlRuleFactory.fromRegExp(/\/baz/, "/b4z");
29-
let rule1b = $ur.urlRuleFactory.fromMatchFn(path => /baz/.test(path) && path.replace('baz', 'b4z'));
29+
// let rule1 = $ur.urlRuleFactory.fromMatchFn($url => /baz/.test($url.path()) && $url.path().replace('baz', 'b4z'));
3030
$ur.addRule(rule1);
3131

3232
$ur.when('/foo/:param', function ($match) {

0 commit comments

Comments
 (0)