Skip to content

Instantly share code, notes, and snippets.

@nickovchinnikov
Last active June 18, 2018 17:44
Show Gist options
  • Save nickovchinnikov/b13819a8bda89da5d1b0d44e02e3276d to your computer and use it in GitHub Desktop.
Save nickovchinnikov/b13819a8bda89da5d1b0d44e02e3276d to your computer and use it in GitHub Desktop.
const itemsToTable = items => {
return items.reduce((prevItem, currentItem) => {
const cursor = prevItem.length > 0 ? prevItem.length - 1 : 0
if (!Array.isArray(prevItem[cursor])) {
prevItem[cursor] = []
}
const currentColumn = cursor >= 0 ? prevItem[cursor] : []
const isEmptyColumn = currentColumn.length === 0
const isWinningHand = currentColumn[0].WinningHand === currentItem.WinningHand
if (isEmptyColumn || isWinningHand) {
prevItem[cursor].push(currentItem)
}
if (!isEmptyColumn && !isWinningHand) {
prevItem[cursor + 1] = [currentItem]
}
return prevItem
}, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment