-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathutils.js
More file actions
98 lines (81 loc) · 2.57 KB
/
utils.js
File metadata and controls
98 lines (81 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Immutable from 'immutable';
const isImmutableIterable = Immutable.Iterable.isIterable;
const utils = {
/** Contains exactly one item. */
UNITARY_LIST: Immutable.List(['empty_list']),
isImmutableIterable,
/**
* Return the keys from a set of data.
*
* @example
* - getKeys({ foo: 'bar', baz: 'qux' }) will return [foo, baz].
* - getKeys([2, 3, 5]) will return [0, 1, 2].
*
* @param {Immutable.Iterable} immutableData
* @returns {Array} An array of keys for the data.
*/
getKeys(immutableData) {
if (__DEV__ && !isImmutableIterable(immutableData)) {
console.warn(`Can't get keys: Data is not Immutable: ${JSON.stringify(immutableData)}`);
}
return immutableData.keySeq().toArray();
},
/**
* Return a 2D array of row keys.
*
* @example
* - getRowIdentities({ section1: ['row1', 'row2'], section2: ['row1'] })
* will return [[0, 1], [0]].
*
* @param {Immutable.Iterable} immutableSectionData
* @returns {Array}
*/
getRowIdentities(immutableSectionData) {
if (__DEV__ && !isImmutableIterable(immutableSectionData)) {
console.warn(`Can't get row identities: Data is not Immutable: ${JSON.stringify(immutableSectionData)}`);
}
const sectionRowKeys = immutableSectionData.map(this.getKeys);
return sectionRowKeys.valueSeq().toArray();
},
/**
* @param {String|Number} key
* @param {Immutable.Iterable|Object|Array} data
* @returns {*} The value at the given key, whether the data is Immutable or not.
*/
getValueFromKey(key, data) {
return data.get ? data.get(key) : data[key];
},
/**
* Returns true if the data would render as empty in a ListView: that is,
* if it either has no items, or only section headers with no section data.
*/
isEmptyListView(immutableData, enableEmptySections) {
if (!immutableData || immutableData.isEmpty()) {
return true;
}
if (!Immutable.Map.isMap(immutableData) || enableEmptySections) {
return false;
}
return immutableData.every((item) => !item || item.isEmpty());
},
flattenMap(data) {
return data.reduce(
(flattened, section, key) => flattened.set(key, section).merge(section),
Immutable.OrderedMap().asMutable(),
).asImmutable();
},
isSectionHeader(item) {
return Immutable.Map.isMap(item) || Immutable.List.isList(item)
},
getStickyHeaderIndices(items) {
return items
.valueSeq()
.reduce((arr, item, i) => {
if (utils.isSectionHeader(item)) {
arr.push(i);
}
return arr;
}, []);
},
};
export default utils;