Skip to content

Commit

Permalink
feat(StateBuilder): Calculate parent state name when ends in two wild…
Browse files Browse the repository at this point in the history
…cards `**`

feat(StateBuilder): Validate states with `parent:` do not have dots in their name
  • Loading branch information
christopherthielen committed Sep 17, 2016
1 parent 4ce27c9 commit b4621f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/state/stateBuilder.ts
Expand Up @@ -288,7 +288,17 @@ export class StateBuilder {

parentName(state: State) {
let name = state.name || "";
if (name.indexOf('.') !== -1) return name.substring(0, name.lastIndexOf('.'));

let segments = name.split('.');
if (segments.length > 1) {
if (state.parent) {
throw new Error(`States that specify the 'parent:' property should not have a '.' in their name (${name})`);
}
var lastSegment = segments.pop();
if (lastSegment === '**') segments.pop();
return segments.join(".");
}

if (!state.parent) return "";
return isString(state.parent) ? state.parent : state.parent.name;
}
Expand Down
4 changes: 1 addition & 3 deletions src/state/stateMatcher.ts
Expand Up @@ -26,9 +26,7 @@ export class StateMatcher {
return state;
} else if (isStr) {
let matches = values(this._states)
.map(state => ({ state, glob: new Glob(state.name)}))
.filter(({state, glob}) => glob.matches(name))
.map(({state, glob}) => state);
.filter(state => new Glob(state.name).matches(name));

if (matches.length > 1) {
console.log(`stateMatcher.find: Found multiple matches for ${name} using glob: `, matches.map(match => match.name));
Expand Down
6 changes: 5 additions & 1 deletion test/core/stateHelperSpec.ts
Expand Up @@ -13,8 +13,9 @@ describe('state helpers', function() {
states['home.about.people.person'] = { name: 'home.about.people.person', parent: states['home.about.people'] };
states['home.about.company'] = { name: 'home.about.company', parent: states['home.about'] };
states['other'] = { name: 'other', parent: states[''] };
states['other.foo'] = { name: 'other.foo', parent: states['other'] };
states['other.foo'] = { name: 'other.foo' };
states['other.foo.bar'] = { name: 'other.foo.bar' };
states['home.error'] = { name: 'home.error', parent: states['home'] };

states['home.withData'] = {
name: 'home.withData',
Expand Down Expand Up @@ -106,6 +107,9 @@ describe('state helpers', function() {
it('should return empty string if state has no parent', function() {
expect(builder.parentName(states[''])).toBe("");
});
it('should error if parent: is specified *AND* the state name has a dot (.) in it', function() {
expect(() => builder.parentName(states['home.error'])).toThrowError();
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion test/testUtils.ts
Expand Up @@ -8,7 +8,8 @@ export function tree2Array(tree, inheritName) {
function processState(parent, state, name) {
var substates = omit.apply(null, [state].concat(stateProps));
var thisState = pick.apply(null, [state].concat(stateProps));
thisState = extend(thisState, {name: name, parent: parent});
thisState.name = name;
if (!inheritName) thisState.parent = parent;

return [thisState].concat(processChildren(thisState, substates));
}
Expand Down

0 comments on commit b4621f3

Please sign in to comment.