From b4621f3c90c18ce6ae230d0a931e6bb0a2c09ced Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Sat, 17 Sep 2016 01:16:20 -0500 Subject: [PATCH] feat(StateBuilder): Calculate parent state name when ends in two wildcards `**` feat(StateBuilder): Validate states with `parent:` do not have dots in their name --- src/state/stateBuilder.ts | 12 +++++++++++- src/state/stateMatcher.ts | 4 +--- test/core/stateHelperSpec.ts | 6 +++++- test/testUtils.ts | 3 ++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/state/stateBuilder.ts b/src/state/stateBuilder.ts index 0c440226b..a19b6f5cc 100644 --- a/src/state/stateBuilder.ts +++ b/src/state/stateBuilder.ts @@ -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; } diff --git a/src/state/stateMatcher.ts b/src/state/stateMatcher.ts index 0baa1532c..aa779851f 100644 --- a/src/state/stateMatcher.ts +++ b/src/state/stateMatcher.ts @@ -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)); diff --git a/test/core/stateHelperSpec.ts b/test/core/stateHelperSpec.ts index da5f6119a..ba5ae06e3 100644 --- a/test/core/stateHelperSpec.ts +++ b/test/core/stateHelperSpec.ts @@ -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', @@ -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(); + }); }); }); diff --git a/test/testUtils.ts b/test/testUtils.ts index 9e5e2d5e8..baca66ded 100644 --- a/test/testUtils.ts +++ b/test/testUtils.ts @@ -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)); }