Skip to content

Commit 8f885fd

Browse files
committed
refactor($state): move glob builder to common
1 parent 020b8a8 commit 8f885fd

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

src/common.js

+46
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,52 @@ function flattenPrototypeChain(obj) {
219219
});
220220
return result;
221221
}
222+
223+
var GlobBuilder = (function() {
224+
225+
function Glob(text) {
226+
227+
var glob = text.split('.');
228+
229+
// Returns true if glob matches current $state name.
230+
this.matches = function(name) {
231+
var segments = name.split('.');
232+
233+
// match greedy starts
234+
if (glob[0] === '**') {
235+
segments = segments.slice(segments.indexOf(glob[1]));
236+
segments.unshift('**');
237+
}
238+
// match greedy ends
239+
if (glob[glob.length - 1] === '**') {
240+
segments.splice(segments.indexOf(glob[glob.length - 2]) + 1, Number.MAX_VALUE);
241+
segments.push('**');
242+
}
243+
if (glob.length != segments.length) return false;
244+
245+
// match single stars
246+
for (var i = 0, l = glob.length; i < l; i++) {
247+
if (glob[i] === '*') segments[i] = '*';
248+
}
249+
250+
return segments.join('') === glob.join('');
251+
};
252+
}
253+
254+
return {
255+
// Checks text to see if it looks like a glob.
256+
is: function(text) {
257+
return text.indexOf('*') > -1;
258+
},
259+
260+
// Factories a glob matcher from a string
261+
fromString: function(text) {
262+
if (!this.is(text)) return null;
263+
return new Glob(text);
264+
}
265+
};
266+
})();
267+
222268
/**
223269
* @ngdoc overview
224270
* @name ui.router.util

src/state.js

+1-47
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,3 @@
1-
var GlobBuilder = (function() {
2-
3-
function Glob(text) {
4-
5-
var glob = text.split('.');
6-
7-
// Returns true if glob matches current $state name.
8-
this.matches = function(state) {
9-
var segments = state.name.split('.');
10-
11-
// match greedy starts
12-
if (glob[0] === '**') {
13-
segments = segments.slice(segments.indexOf(glob[1]));
14-
segments.unshift('**');
15-
}
16-
// match greedy ends
17-
if (glob[glob.length - 1] === '**') {
18-
segments.splice(segments.indexOf(glob[glob.length - 2]) + 1, Number.MAX_VALUE);
19-
segments.push('**');
20-
}
21-
if (glob.length != segments.length) return false;
22-
23-
// match single stars
24-
for (var i = 0, l = glob.length; i < l; i++) {
25-
if (glob[i] === '*') segments[i] = '*';
26-
}
27-
28-
return segments.join('') === glob.join('');
29-
};
30-
}
31-
32-
return {
33-
// Checks text to see if it looks like a glob.
34-
is: function(text) {
35-
return text.indexOf('*') > -1;
36-
},
37-
38-
// Factories a glob matcher from a string
39-
fromString: function(text) {
40-
if (!this.is(text)) return null;
41-
return new Glob(text);
42-
}
43-
};
44-
})();
45-
46-
471
function StateQueueManager(states, builder, $urlRouterProvider, $state) {
482
var queue = [], abstractKey = 'abstract';
493

@@ -1024,7 +978,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
1024978
var glob = isString(stateOrName) && GlobBuilder.fromString(stateOrName);
1025979

1026980
if (glob) {
1027-
if (!glob.matches($state.$current)) return false;
981+
if (!glob.matches($state.$current.name)) return false;
1028982
stateOrName = $state.$current.name;
1029983
}
1030984
var state = matcher.find(stateOrName), include = $state.$current.includes;

test/commonSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
describe('common', function() {
2+
describe('GlobBuilder', function() {
3+
it('should match glob strings', function() {
4+
expect(GlobBuilder.is('*')).toBe(true);
5+
expect(GlobBuilder.is('**')).toBe(true);
6+
expect(GlobBuilder.is('*.*')).toBe(true);
7+
8+
expect(GlobBuilder.is('')).toBe(false);
9+
expect(GlobBuilder.is('.')).toBe(false);
10+
});
11+
12+
it('should construct glob matchers', function() {
13+
expect(GlobBuilder.fromString('')).toBeNull();
14+
15+
var state = { name: 'about.person.item' };
16+
17+
expect(GlobBuilder.fromString('*.person.*').matches(state)).toBe(true);
18+
expect(GlobBuilder.fromString('*.person.**').matches(state)).toBe(true);
19+
20+
expect(GlobBuilder.fromString('**.item.*').matches(state)).toBe(false);
21+
expect(GlobBuilder.fromString('**.item').matches(state)).toBe(true);
22+
expect(GlobBuilder.fromString('**.stuff.*').matches(state)).toBe(false);
23+
expect(GlobBuilder.fromString('*.*.*').matches(state)).toBe(true);
24+
25+
expect(GlobBuilder.fromString('about.*.*').matches(state)).toBe(true);
26+
expect(GlobBuilder.fromString('about.**').matches(state)).toBe(true);
27+
expect(GlobBuilder.fromString('*.about.*').matches(state)).toBe(false);
28+
expect(GlobBuilder.fromString('about.*.*').matches(state)).toBe(true);
29+
});
30+
});
31+
});

test/stateSpec.js

-31
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,6 @@ describe('state helpers', function() {
2626
};
2727
});
2828

29-
30-
describe('GlobBuilder', function() {
31-
it('should match glob strings', function() {
32-
expect(GlobBuilder.is('*')).toBe(true);
33-
expect(GlobBuilder.is('**')).toBe(true);
34-
expect(GlobBuilder.is('*.*')).toBe(true);
35-
36-
expect(GlobBuilder.is('')).toBe(false);
37-
expect(GlobBuilder.is('.')).toBe(false);
38-
});
39-
40-
it('should construct glob matchers', function() {
41-
expect(GlobBuilder.fromString('')).toBeNull();
42-
43-
var state = { name: 'about.person.item' };
44-
45-
expect(GlobBuilder.fromString('*.person.*').matches(state)).toBe(true);
46-
expect(GlobBuilder.fromString('*.person.**').matches(state)).toBe(true);
47-
48-
expect(GlobBuilder.fromString('**.item.*').matches(state)).toBe(false);
49-
expect(GlobBuilder.fromString('**.item').matches(state)).toBe(true);
50-
expect(GlobBuilder.fromString('**.stuff.*').matches(state)).toBe(false);
51-
expect(GlobBuilder.fromString('*.*.*').matches(state)).toBe(true);
52-
53-
expect(GlobBuilder.fromString('about.*.*').matches(state)).toBe(true);
54-
expect(GlobBuilder.fromString('about.**').matches(state)).toBe(true);
55-
expect(GlobBuilder.fromString('*.about.*').matches(state)).toBe(false);
56-
expect(GlobBuilder.fromString('about.*.*').matches(state)).toBe(true);
57-
});
58-
});
59-
6029
describe('StateMatcher', function() {
6130
it('should find states by name', function() {
6231
var states = {}, matcher = new StateMatcher(states), home = { name: 'home' };

0 commit comments

Comments
 (0)