Skip to content

Commit 2092e96

Browse files
refactor(UrlRouter): Change UrlRuleType from enum to string
refactor(UrlRouter): Move interfaces around
1 parent 4c39dcb commit 2092e96

File tree

5 files changed

+94
-53
lines changed

5 files changed

+94
-53
lines changed

src/url/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @coreapi
33
* @module url
44
*/ /** for typedoc */
5+
export * from "./interface";
56
export * from "./urlMatcher";
67
export * from "./urlMatcherFactory";
78
export * from "./urlRouter";

src/url/interface.ts

+72-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { LocationServices, $InjectorLike, LocationConfig } from "../common/coreservices";
2-
import { UrlRule } from "./urlRule";
3-
import { UrlMatcher } from "./urlMatcher";
4-
import { IInjectable } from "../common/common";
1+
import { LocationConfig } from "../common/coreservices";
2+
import { Obj } from "../common/common";
53
import { ParamType } from "../params/type";
64
import { Param } from "../params/param";
75

@@ -23,3 +21,73 @@ export interface UrlMatcherConfig {
2321
paramType(name, type?)
2422
}
2523

24+
/** @return truthy or falsey */
25+
export interface UrlRuleMatchFn {
26+
(path: string, search: Obj, hash: string): any;
27+
}
28+
29+
/** Handler invoked when a rule is matched */
30+
export interface UrlRuleHandlerFn {
31+
(matchObject: any, path?: string, search?: Obj, hash?: string): (string|boolean|void);
32+
}
33+
34+
export type UrlRuleType = "STATE" | "URLMATCHER" | "STRING" | "REGEXP" | "RAW" | "OTHER";
35+
export interface UrlRule {
36+
/** The type of the rule */
37+
type: UrlRuleType;
38+
39+
/**
40+
* This function should match the url and return match details
41+
*/
42+
match: UrlRuleMatchFn;
43+
44+
/**
45+
* This function is called after the rule matched the url.
46+
* This function handles the rule match event.
47+
*/
48+
handler: UrlRuleHandlerFn;
49+
50+
priority: number;
51+
}
52+
53+
54+
// export interface UrlService {
55+
// // LocationServices
56+
// // todo: switch back to url()
57+
// setUrl(newurl: string, replace?: boolean): void;
58+
// path(): string;
59+
// search(): { [key: string]: any };
60+
// hash(): string;
61+
// onChange(callback: Function): Function;
62+
//
63+
// config: {
64+
// // LocationConfig
65+
// port(): number;
66+
// protocol(): string;
67+
// host(): string;
68+
// baseHref(): string;
69+
// html5Mode(): boolean;
70+
// hashPrefix(): string;
71+
//
72+
// hashPrefix(newprefix: string): string;
73+
//
74+
// // MatcherConfig
75+
// caseInsensitive(value?: boolean): boolean;
76+
// strictMode(value?: boolean): boolean;
77+
// defaultSquashPolicy(value?: (boolean|string)): (boolean|string);
78+
// paramType(name, type?)
79+
// }
80+
//
81+
// rules: {
82+
// // UrlRouterProvider
83+
// addRule(rule: UrlRule): UrlRouterProvider;
84+
// otherwise(rule: string | (($injector: $InjectorLike, $location: LocationServices) => string)): UrlRouterProvider ;
85+
// when(what: (RegExp|UrlMatcher|string), handler: string|IInjectable, ruleCallback?) ;
86+
// }
87+
//
88+
// deferIntercept(defer?: boolean);
89+
//
90+
// // UrlRouter
91+
// sync(evt?);
92+
// listen(): Function;
93+
// }

src/url/urlRouter.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* @module url
44
*/ /** for typedoc */
55
import { removeFrom, createProxyFunctions, find } from "../common/common";
6-
import { isFunction, isString, isArray } from "../common/predicates";
6+
import { isFunction, isString } from "../common/predicates";
77
import { UrlMatcher } from "./urlMatcher";
88
import { RawParams } from "../params/interface";
99
import { Disposable } from "../interface";
1010
import { UIRouter } from "../router";
1111
import { val, is, pattern } from "../common/hof";
12-
import { UrlRuleFactory, UrlRule, UrlRuleMatchFn, isUrlRule, UrlRuleHandlerFn } from "./urlRule";
12+
import { UrlRuleFactory } from "./urlRule";
1313
import { TargetState } from "../state/targetState";
14+
import { UrlRule, UrlRuleMatchFn, UrlRuleHandlerFn } from "./interface";
1415

1516
/** @hidden */
1617
function appendBasePath(url: string, isHtml5: boolean, absolute: boolean, baseHref: string): string {
@@ -179,7 +180,7 @@ export class UrlRouter implements Disposable {
179180
}
180181

181182
addRule(rule: UrlRule) {
182-
if (!isUrlRule(rule)) throw new Error("invalid rule");
183+
if (!UrlRuleFactory.isUrlRule(rule)) throw new Error("invalid rule");
183184
this.rules.push(rule);
184185
return () => this.removeRule(rule);
185186
}
@@ -228,12 +229,12 @@ export class UrlRouter implements Disposable {
228229
[isString, (_redirectTo: string) => rf.fromMatchFn(val(_redirectTo))],
229230
[isFunction, (_redirectTo: UrlRuleMatchFn) => rf.fromMatchFn(_redirectTo)],
230231
[is(TargetState), (target: TargetState) => ($state.go(target.name(), target.params(), target.options()), true)],
231-
[val(true), invalidArgument]
232+
[val(true), error]
232233
]);
233234

234235
this.otherwiseFn = makeRule(redirectTo);
235236

236-
function invalidArgument() {
237+
function error() {
237238
throw new Error("'redirectTo' must be a string, function, TargetState, or have a state: 'target' property");
238239
}
239240
};

src/url/urlRule.ts

+13-41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { UrlMatcher } from "./urlMatcher";
22
import { isString, isDefined } from "../common/predicates";
33
import { UIRouter } from "../router";
4-
import { Obj, extend, identity } from "../common/common";
5-
import { RawParams } from "../params/interface";
4+
import { extend, identity } from "../common/common";
65
import { is, pattern } from "../common/hof";
76
import { State } from "../state/stateObject";
8-
import { UIRouterGlobals } from "../globals";
7+
import { RawParams } from "../params/interface";
8+
import { UrlRule, UrlRuleMatchFn, UrlRuleHandlerFn, UrlRuleType } from "./interface";
99
import { StateService } from "../state/stateService";
10+
import { UIRouterGlobals } from "../globals";
1011

1112
/**
1213
* Creates a [[UrlRule]]
@@ -25,6 +26,9 @@ export class UrlRuleFactory {
2526
return this.router.urlMatcherFactory.compile(pattern);
2627
}
2728

29+
static isUrlRule = obj =>
30+
obj && ['type', 'match', 'handler', 'priority'].every(key => isDefined(obj[key]));
31+
2832
create(what: string|State|UrlMatcher|RegExp, handler?): UrlRule {
2933
const makeRule = pattern([
3034
[isString, () => this.fromString(what as string, handler)],
@@ -38,7 +42,7 @@ export class UrlRuleFactory {
3842
}
3943

4044
fromString = (pattern: string, handler: string|UrlMatcher|UrlRuleHandlerFn) =>
41-
extend(this.fromMatcher(this.compile(pattern), handler), { type: UrlRuleType.STRING });
45+
extend(this.fromMatcher(this.compile(pattern), handler), { type: "STRING" });
4246

4347
fromMatcher = (urlMatcher: UrlMatcher, handler: string|UrlMatcher|UrlRuleHandlerFn) =>
4448
new UrlMatcherRule(urlMatcher, (isString(handler) ? this.compile(handler) : handler));
@@ -53,38 +57,6 @@ export class UrlRuleFactory {
5357
new RawUrlRule(match);
5458
}
5559

56-
/** @return truthy or falsey */
57-
export interface UrlRuleMatchFn {
58-
(path: string, search: Obj, hash: string): any;
59-
}
60-
61-
/** Handler invoked when a rule is matched */
62-
export interface UrlRuleHandlerFn {
63-
(matchObject: any, path?: string, search?: Obj, hash?: string): (string|boolean|void);
64-
}
65-
66-
export enum UrlRuleType { STATE, URLMATCHER, STRING, REGEXP, RAW, OTHER }
67-
export interface UrlRule {
68-
/** The type of the rule */
69-
type: UrlRuleType;
70-
71-
/**
72-
* This function should match the url and return match details
73-
*/
74-
match: UrlRuleMatchFn;
75-
76-
/**
77-
* This function is called after the rule matched the url.
78-
* This function handles the rule match event.
79-
*/
80-
handler: UrlRuleHandlerFn;
81-
82-
priority: number;
83-
}
84-
85-
export const isUrlRule = obj =>
86-
obj && ['type', 'match', 'handler', 'priority'].every(key => isDefined(obj[key]));
87-
8860
/**
8961
* A UrlRule which matches based on a regular expression
9062
*
@@ -119,7 +91,7 @@ export const isUrlRule = obj =>
11991
* ```
12092
*/
12193
export class RegExpRule implements UrlRule {
122-
type = UrlRuleType.REGEXP;
94+
type: UrlRuleType = "REGEXP";
12395
handler: UrlRuleHandlerFn;
12496
priority = 0;
12597

@@ -184,7 +156,7 @@ export class RegExpRule implements UrlRule {
184156
* ```
185157
*/
186158
export class UrlMatcherRule implements UrlRule {
187-
type = UrlRuleType.URLMATCHER;
159+
type: UrlRuleType = "URLMATCHER";
188160
handler: UrlRuleHandlerFn;
189161
priority = 0;
190162

@@ -211,7 +183,7 @@ export class UrlMatcherRule implements UrlRule {
211183
* ```
212184
*/
213185
export class StateUrlRule implements UrlRule {
214-
type = UrlRuleType.STATE;
186+
type: UrlRuleType = "STATE";
215187
priority = 0;
216188
$state: StateService;
217189
globals: UIRouterGlobals;
@@ -248,10 +220,10 @@ export class StateUrlRule implements UrlRule {
248220
* The value from the `match` function is passed through as the `handler` result.
249221
*/
250222
export class RawUrlRule implements UrlRule {
251-
type = UrlRuleType.RAW;
223+
type: UrlRuleType = "RAW";
252224
priority = 0;
253225

254226
constructor(public match: UrlRuleMatchFn) { }
255227

256228
handler = identity
257-
}
229+
}

test/urlRouterSpec.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { UrlMatcher, UrlMatcherFactory, UrlRouter, StateService, UIRouter } from
22
import { TestingPlugin } from "./_testingPlugin";
33
import { LocationServices } from "../src/common/coreservices";
44
import { UrlService } from "../src/url/urlService";
5-
import { UrlRuleType } from "../src/url/urlRule";
65

76
declare var inject;
87

@@ -72,7 +71,7 @@ describe("UrlRouter", function () {
7271
match: () => count++,
7372
handler: match => match,
7473
priority: 0,
75-
type: UrlRuleType.OTHER,
74+
type: "OTHER",
7675
};
7776

7877
let dereg = $ur.addRule(rule as any);
@@ -92,7 +91,7 @@ describe("UrlRouter", function () {
9291
match: () => count++,
9392
handler: match => match,
9493
priority: 0,
95-
type: UrlRuleType.OTHER,
94+
type: "OTHER",
9695
};
9796
$ur.addRule(rule as any);
9897

0 commit comments

Comments
 (0)