Skip to content

Commit b4621f3

Browse files
feat(StateBuilder): Calculate parent state name when ends in two wildcards **
feat(StateBuilder): Validate states with `parent:` do not have dots in their name
1 parent 4ce27c9 commit b4621f3

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/state/stateBuilder.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,17 @@ export class StateBuilder {
288288

289289
parentName(state: State) {
290290
let name = state.name || "";
291-
if (name.indexOf('.') !== -1) return name.substring(0, name.lastIndexOf('.'));
291+
292+
let segments = name.split('.');
293+
if (segments.length > 1) {
294+
if (state.parent) {
295+
throw new Error(`States that specify the 'parent:' property should not have a '.' in their name (${name})`);
296+
}
297+
var lastSegment = segments.pop();
298+
if (lastSegment === '**') segments.pop();
299+
return segments.join(".");
300+
}
301+
292302
if (!state.parent) return "";
293303
return isString(state.parent) ? state.parent : state.parent.name;
294304
}

src/state/stateMatcher.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export class StateMatcher {
2626
return state;
2727
} else if (isStr) {
2828
let matches = values(this._states)
29-
.map(state => ({ state, glob: new Glob(state.name)}))
30-
.filter(({state, glob}) => glob.matches(name))
31-
.map(({state, glob}) => state);
29+
.filter(state => new Glob(state.name).matches(name));
3230

3331
if (matches.length > 1) {
3432
console.log(`stateMatcher.find: Found multiple matches for ${name} using glob: `, matches.map(match => match.name));

test/core/stateHelperSpec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ describe('state helpers', function() {
1313
states['home.about.people.person'] = { name: 'home.about.people.person', parent: states['home.about.people'] };
1414
states['home.about.company'] = { name: 'home.about.company', parent: states['home.about'] };
1515
states['other'] = { name: 'other', parent: states[''] };
16-
states['other.foo'] = { name: 'other.foo', parent: states['other'] };
16+
states['other.foo'] = { name: 'other.foo' };
1717
states['other.foo.bar'] = { name: 'other.foo.bar' };
18+
states['home.error'] = { name: 'home.error', parent: states['home'] };
1819

1920
states['home.withData'] = {
2021
name: 'home.withData',
@@ -106,6 +107,9 @@ describe('state helpers', function() {
106107
it('should return empty string if state has no parent', function() {
107108
expect(builder.parentName(states[''])).toBe("");
108109
});
110+
it('should error if parent: is specified *AND* the state name has a dot (.) in it', function() {
111+
expect(() => builder.parentName(states['home.error'])).toThrowError();
112+
});
109113
});
110114
});
111115

test/testUtils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export function tree2Array(tree, inheritName) {
88
function processState(parent, state, name) {
99
var substates = omit.apply(null, [state].concat(stateProps));
1010
var thisState = pick.apply(null, [state].concat(stateProps));
11-
thisState = extend(thisState, {name: name, parent: parent});
11+
thisState.name = name;
12+
if (!inheritName) thisState.parent = parent;
1213

1314
return [thisState].concat(processChildren(thisState, substates));
1415
}

0 commit comments

Comments
 (0)