Created
November 1, 2016 20:02
-
-
Save deoxxa/ae0f495fdb8f6f6eeac564e000ca0446 to your computer and use it in GitHub Desktop.
This file contains 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
// @flow | |
type Element<ET> = ET & { | |
uniqueId: string, | |
groupId?: ?string, | |
}; | |
type Group<GT> = GT & { | |
id: string, | |
elements: Array<string>, | |
}; | |
type Page<PT, ET, GT> = PT & { | |
elements: Array<Element<ET>>, | |
groups?: Array<Group<GT>>, | |
}; | |
type Document<PT, ET, GT> = { | |
pages: Array<Page<PT, ET, GT>>, | |
}; | |
export function getElementById(document: Document<*, *, *>, uniqueId: string) { | |
const { pages } = document; | |
for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) { | |
const { elements } = pages[pageIndex]; | |
for (let elementIndex = 0; elementIndex < elements.length; elementIndex++) { | |
const element = elements[elementIndex]; | |
if (element.uniqueId === uniqueId) { | |
return { pageIndex, elementIndex, element }; | |
} | |
} | |
} | |
return { pageIndex: -1, elementIndex: -1, element: null }; | |
} | |
export function getGroupById(document: Document<*, *, *>, groupId: string) { | |
const { pages } = document; | |
for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) { | |
const { groups = [] } = pages[pageIndex]; | |
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) { | |
const group = groups[groupIndex]; | |
if (group.id === groupId) { | |
return { pageIndex, groupIndex, group }; | |
} | |
} | |
} | |
return { pageIndex: -1, groupIndex: -1, group: null }; | |
} | |
export function getGroupByIdFromPage(page: Page<*, *, *>, groupId: string) { | |
const { groups = [] } = page; | |
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) { | |
const group = groups[groupIndex]; | |
if (group.id === groupId) { | |
return { groupIndex, group }; | |
} | |
} | |
return { groupIndex: -1, group: null }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment