Skip to content

Commit 7a4a116

Browse files
chore(Params): fixing tests: remove private state._params and add public params: { [key: string]: Param }
- fetch Params[] dynamic using `values(this.params)` - fix node.parameter() - fix StateBuilder.url (do not expect state.params to be set, b/c 'params' is built after)
1 parent cc76df2 commit 7a4a116

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/common/common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ export function map(collection: any, callback: any): any {
287287
return result;
288288
}
289289

290+
/** Given an object, return its enumerable property values */
291+
export const values: (<T> (obj: TypedMap<T>) => T[]) = (obj) => Object.keys(obj).map(key => obj[key]);
292+
293+
/** Reduce function that returns true if all of the values are truthy. */
294+
export const allTrueR = (memo: boolean, elem) => memo && elem;
295+
/** Reduce function that returns true if any of the values are truthy. */
296+
export const anyTrueR = (memo: boolean, elem) => memo || elem;
297+
290298
/** Push an object to an array, return the array */
291299
export const push = (arr: any[], obj) => { arr.push(obj); return arr; };
292300
/** Reduce function which un-nests a single level of arrays */

src/path/node.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference path='../../typings/angularjs/angular.d.ts' />
2-
import {extend, pick, prop, propEq, pairs, map, find} from "../common/common";
2+
import {extend, pick, prop, propEq, pairs, map, find, allTrueR} from "../common/common";
33
import {State} from "../state/state";
44
import Param from "../params/param";
55
import Type from "../params/type";
@@ -41,13 +41,12 @@ export default class Node {
4141
}
4242

4343
parameter(name: string): Param {
44-
return find(this.schema, prop('id'));
44+
return find(this.schema, propEq("id", name));
4545
}
4646

47-
equals(node: Node, keys?: string[]): boolean {
48-
return this.state === node.state && (keys || Object.keys(this.values)).map(key => {
49-
return this.parameter(key).type.equals(this.values[key], node.values[key]);
50-
}).reduce((previous, current) => previous && current, true);
47+
equals(node: Node, keys = this.schema.map(prop('id'))): boolean {
48+
const paramValsEq = key => this.parameter(key).type.equals(this.values[key], node.values[key]);
49+
return this.state === node.state && keys.map(paramValsEq).reduce(allTrueR, true);
5150
}
5251

5352
static clone(node: Node, update: any = {}) {

src/state/state.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
extend, defaults, copy, equalForKeys, forEach, find, prop,
3-
propEq, ancestors, noop, isDefined, isObject, isString
3+
propEq, ancestors, noop, isDefined, isObject, isString, values
44
} from "../common/common";
55
import Queue from "../common/queue";
66
import {IServiceProviderFactory, IPromise} from "angular";
@@ -53,15 +53,14 @@ export class State {
5353
public resolve: IResolveDeclarations;
5454
public resolvePolicy: any;
5555
public url: UrlMatcher;
56+
public params: { [key: string]: Param };
5657
public views: IViewDeclarations;
5758
public self: IStateDeclaration;
5859
public navigable: State;
5960
public path: State[];
6061
public data: any;
6162
public includes: (name: string) => boolean;
6263

63-
private _params: Param[] = [];
64-
6564
constructor(config?: IStateDeclaration) {
6665
extend(this, config);
6766
// Object.freeze(this);
@@ -120,13 +119,13 @@ export class State {
120119

121120
return (this.parent && opts.inherit && this.parent.parameters() || [])
122121
.concat(this.url && this.url.parameters({ inherit: false }) || [])
123-
.concat(this._params);
122+
.concat(values(this.params));
124123
}
125124

126125
parameter(id: string, opts: any = {}): Param {
127126
return (
128127
this.url && this.url.parameter(id, opts) ||
129-
find(this._params, propEq('id', id)) ||
128+
find(values(this.params), propEq('id', id)) ||
130129
opts.inherit && this.parent && this.parent.parameter(id)
131130
);
132131
}

src/state/stateBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default function StateBuilder(root, matcher, $urlMatcherFactoryProvider)
2828
url: function(state) {
2929
const parsed = parseUrl(state.url), parent = state.parent;
3030
const url = !parsed ? state.url : $urlMatcherFactoryProvider.compile(parsed.val, {
31-
params: state.params,
31+
params: state.params || {},
3232
paramMap: function(paramConfig, isSearch) {
3333
if (state.reloadOnSearch === false && isSearch) paramConfig = extend(paramConfig || {}, { dynamic: true });
3434
return paramConfig;

0 commit comments

Comments
 (0)