[Navigation]: Add a method to map the stack routes to an array.

Summary:
Add a method to map the stack routes to an array.
This commit is contained in:
Hedger Wang 2015-08-06 01:51:37 -07:00
parent a6ff11f1d2
commit 8463625bd9
2 changed files with 46 additions and 11 deletions

View File

@ -202,11 +202,19 @@ class RouteStack {
var nodes = this._routeNodes;
while (ii < nodes.size) {
var node = nodes.get(ii);
callback(node.value, ii, node.key);
callback.call(context, node.value, ii, node.key);
ii++;
}
}
mapToArray(callback: IterationCallback, context: ?Object): Array<any> {
var result = [];
this.forEach((route, index, key) => {
result.push(callback.call(context, route, index, key));
});
return result;
}
/**
* Returns a Set excluding any routes contained within the stack given.
*/

View File

@ -330,19 +330,46 @@ describe('NavigationRouteStack:', () => {
var stack = new NavigationRouteStack(0, ['a', 'b']);
var logs = [];
var keys = {};
var context = {name: 'yo'};
stack.forEach((route, index, key) => {
logs.push([
route,
index,
(typeof key === 'string' && key.length > 0 && !(key in keys)),
]);
keys[key] = true;
});
stack.forEach(function (route, index, key) {
assetStringNotEmpty(key);
if (!keys.hasOwnProperty(key)) {
keys[key] = true;
logs.push([
route,
index,
this.name,
]);
}
}, context);
expect(logs).toEqual([
['a', 0, true],
['b', 1, true],
['a', 0, 'yo'],
['b', 1, 'yo'],
]);
});
it('Maps to an array', () => {
var stack = new NavigationRouteStack(0, ['a', 'b']);
var keys = {};
var context = {name: 'yo'};
var logs = stack.mapToArray(function(route, index, key) {
assetStringNotEmpty(key);
if (!keys.hasOwnProperty(key)) {
keys[key] = true;
return [
route,
index,
this.name,
];
}
}, context);
expect(logs).toEqual([
['a', 0, 'yo'],
['b', 1, 'yo'],
]);
});