Skip to content

Commit 0cef86e

Browse files
feat(Path): allow state or name as param to nodeForState
1 parent 7baa79e commit 0cef86e

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/path/path.ts

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
/// <reference path='../../typings/angularjs/angular.d.ts' />
22

3-
import {extend, isArray, isString, identity, noop, Predicate,
4-
defaults, map, omit, pluck, find, pipe, prop, eq} from "../common/common";
5-
import trace from "../common/trace";
6-
import {runtime} from "../common/angular1"
7-
import {IPromise} from "angular";
8-
9-
import {INode, IParamsNode, IParamsPath} from "./interface";
3+
import {extend, isString, find, pipe, parse, prop, eq} from "../common/common";
4+
import {INode} from "./interface";
105

116
import {IState, IStateDeclaration, IStateOrName} from "../state/interface";
12-
import TargetState from "../state/targetState"
13-
14-
import {IResolvables} from "../resolve/interface";
15-
import Resolvable from "../resolve/resolvable";
167

178
const stateMatches = (state: IState|IStateDeclaration) => (node) => node.state === state || node.state.self === state;
189
const stateNameMatches = (stateName: string) => (node) => node.state.name === stateName;
@@ -39,9 +30,9 @@ export default class Path<NODE extends INode> {
3930
*/
4031
pathFromRootTo(toState: IStateOrName): Path<NODE> {
4132
let predicate = isString(toState) ? stateNameMatches(<string> toState) : stateMatches(<IState> toState);
42-
var node = find(this._nodes, predicate);
43-
var elementIdx = this._nodes.indexOf(node);
44-
if (elementIdx == -1) throw new Error("This Path does not contain the toPathElement");
33+
let node = find(this._nodes, predicate);
34+
let elementIdx = this._nodes.indexOf(node);
35+
if (elementIdx === -1) throw new Error("This Path does not contain the toPathElement");
4536
return this.slice(0, elementIdx + 1);
4637
}
4738

@@ -77,8 +68,9 @@ export default class Path<NODE extends INode> {
7768
}
7869

7970
/** Gets the first node that exactly matches the given state */
80-
nodeForState(state: IState): NODE {
81-
return find(this._nodes, pipe(prop('state'), eq(state)));
71+
nodeForState(state: IStateOrName): NODE {
72+
let propName = (isString(state) ? "state.name" : "state");
73+
return find(this._nodes, pipe(parse(propName), eq(state)));
8274
}
8375

8476
/** Returns the Path's nodes wrapped in a new array */

0 commit comments

Comments
 (0)