Skip to content

Instantly share code, notes, and snippets.

@alicewriteswrongs
Last active July 6, 2018 13:57
Show Gist options
  • Save alicewriteswrongs/66b2d699a12da91a4a2cf5490d592ce0 to your computer and use it in GitHub Desktop.
Save alicewriteswrongs/66b2d699a12da91a4a2cf5490d592ce0 to your computer and use it in GitHub Desktop.
const getAndSortLessons = () => {
const currentActive = $.jStorage.get("l/activeQueue")
const activeCount = currentActive.length
const currentLessons = $.jStorage.get("l/lessonQueue")
const allLessons = [...currentActive, ...currentLessons]
const [radicals, kanji, vocabulary] = allLessons.reduce(
([radicals, kanji, vocabulary], item) => {
if (item.rad) {
return [[...radicals, item], kanji, vocabulary]
}
if (item.kan) {
return [radicals, [...kanji, item], vocabulary]
}
return [radicals, kanji, [...vocabulary, item]]
},
[[], [], []]
)
return {
radicals,
kanji,
vocabulary,
activeCount
}
}
const setUpdatedLessons = (reorderedLessons, activeCount) => {
const newActiveList = reorderedLessons.slice(0, activeCount)
const lessonsList = reorderedLessons.slice(activeCount)
$.jStorage.set("l/lessonQueue", reorderedLessons)
$.jStorage.set("l/activeQueue", newActiveList)
}
const reorderRadicalsKanjiFirst = () => {
const {
radicals,
kanji,
vocabulary,
activeCount
} = getAndSortLessons()
const reorderedLessons = [...radicals, ...kanji, ...vocabulary]
setUpdatedLessons(reorderedLessons, activeCount)
}
const reorderVocabFirst = () => {
const {
radicals,
kanji,
vocabulary,
activeCount
} = getAndSortLessons()
const reorderedLessons = [...vocabulary, ...radicals, ...kanji]
setUpdatedLessons(reorderedLessons, activeCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment