Created
November 20, 2016 04:31
-
-
Save samcorcos/124d6d005c65ffbb36354054a823a8bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatData = data => { | |
// We're sorting by alphabetically so we need the alphabet | |
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); | |
// Need somewhere to store our data | |
const dataBlob = {}; | |
const sectionIds = []; | |
const rowIds = []; | |
// Each section is going to represent a letter in the alphabet so we loop over the alphabet | |
for (let sectionId = 0; sectionId < alphabet.length; sectionId++) { | |
// Get the character we're currently looking for | |
const currentChar = alphabet[sectionId]; | |
// Get users whose first name starts with the current letter | |
const users = data.filter((user) => user.name.toUpperCase().indexOf(currentChar) === 0); | |
// If there are any users who have a first name starting with the current letter then we'll | |
// add a new section otherwise we just skip over it | |
if (users.length > 0) { | |
// Add a section id to our array so the listview knows that we've got a new section | |
sectionIds.push(sectionId); | |
// Store any data we would want to display in the section header. In our case we want to show | |
// the current character | |
dataBlob[sectionId] = { character: currentChar }; | |
// Setup a new array that we can store the row ids for this section | |
rowIds.push([]); | |
// Loop over the valid users for this section | |
for (let i = 0; i < users.length; i++) { | |
// Create a unique row id for the data blob that the listview can use for reference | |
const rowId = `${sectionId}:${i}`; | |
// Push the row id to the row ids array. This is what listview will reference to pull | |
// data from our data blob | |
rowIds[rowIds.length - 1].push(rowId); | |
// Store the data we care about for this row | |
dataBlob[rowId] = users[i]; | |
} | |
} | |
} | |
return { dataBlob, sectionIds, rowIds }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment