|
1 | | -export function assert(expr, failDescription) { |
2 | | - if (!expr) { |
3 | | - throw new Error(`[react-native-router-flux] ${failDescription}`); |
4 | | - } |
5 | | -} |
| 1 | +// searches for the deepest explicitly set value for a key |
| 2 | +// in a navigationState tree. |
| 3 | +export function deepestExplicitValueForKey(navigationState, key) { |
| 4 | + let current; |
| 5 | + let selected = navigationState; |
6 | 6 |
|
7 | | -export default { |
8 | | - |
9 | | - // searches for the deepest explicitly set value for a key |
10 | | - // in a navigationState tree. |
11 | | - deepestExplicitValueForKey(navigationState, key) { |
12 | | - let current; |
13 | | - let selected = navigationState; |
14 | | - |
15 | | - while (selected.hasOwnProperty('children')) { |
16 | | - if (!selected.tabs) { |
17 | | - // for pushed children, iterate through each, recording key value, |
18 | | - // until reaching the selected child |
19 | | - for (let i = 0; i < selected.index; i++) { |
20 | | - if (typeof(selected.children[i][key]) !== 'undefined') { |
21 | | - current = selected.children[i][key]; |
22 | | - } |
| 7 | + while (selected.hasOwnProperty('children')) { |
| 8 | + if (!selected.tabs) { |
| 9 | + // for pushed children, iterate through each, recording key value, |
| 10 | + // until reaching the selected child |
| 11 | + for (let i = 0; i < selected.index; i++) { |
| 12 | + if (typeof(selected.children[i][key]) !== 'undefined') { |
| 13 | + current = selected.children[i][key]; |
23 | 14 | } |
24 | 15 | } |
25 | | - // set the new selected child and check for a key value |
26 | | - selected = selected.children[selected.index]; |
27 | | - if (typeof(selected[key]) !== 'undefined') { |
28 | | - current = selected[key]; |
29 | | - } |
30 | 16 | } |
31 | | - |
32 | | - // fallback to the root key value |
33 | | - if (typeof(current) === 'undefined') { |
34 | | - current = navigationState[key]; |
| 17 | + // set the new selected child and check for a key value |
| 18 | + selected = selected.children[selected.index]; |
| 19 | + if (typeof(selected[key]) !== 'undefined') { |
| 20 | + current = selected[key]; |
35 | 21 | } |
| 22 | + } |
36 | 23 |
|
37 | | - return current; |
38 | | - }, |
39 | | -}; |
| 24 | + // fallback to the root key value |
| 25 | + if (typeof(current) === 'undefined') { |
| 26 | + current = navigationState[key]; |
| 27 | + } |
| 28 | + |
| 29 | + return current; |
| 30 | +} |
40 | 31 |
|
41 | 32 | export function assert(expr, failDescription) { |
42 | 33 | if (!expr) { |
43 | 34 | throw new Error(`[react-native-router-flux] ${failDescription}`); |
44 | 35 | } |
45 | 36 | } |
| 37 | + |
| 38 | +export default { |
| 39 | + deepestExplicitValueForKey, |
| 40 | + assert, |
| 41 | +}; |
0 commit comments