| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { sortTypes } from '../../utils/helpers'
-
- const _insertInBetweens = bySort => {
- const postListWithInBetweens = []
- Object.keys(bySort).forEach(term => {
- if(term) {
- postListWithInBetweens.push({
- slug: term,
- title: term,
- inbetween: true
- })
- }
- bySort[term].forEach(post => postListWithInBetweens.push(post))
- })
- return postListWithInBetweens
- }
-
- const _arrangeByMaterial = postsList => {
- const byMaterial = {
- clay: []
- }
- postsList.forEach(post => {
- post.materials.forEach(mat => {
- if(!byMaterial[mat]) byMaterial[mat] = []
- byMaterial[mat].push(post)
- })
- })
- const flatPacked = _insertInBetweens(byMaterial)
- return flatPacked
- }
-
- const _arrangeByType = postsList => {
- const byType = {}
- postsList.forEach(post => {
- const subtypes = post.subtypes
- subtypes.forEach(type => {
- if(!byType[type]) byType[type] = []
- byType[type].push(post)
- })
- })
- const flatPacked = _insertInBetweens(byType)
- return flatPacked
- }
-
- const _arrangeByAlpha = (postsList, seenTitles) => {
- const alphabet = [...'9abcdefghijklmnopqrstuvwxyz']
- const flatPacked = []
-
- const storeTitle = letter => {
- flatPacked.push({ slug: letter, title: letter, inbetween: true })
- seenTitles.push(letter)
- }
-
- postsList.forEach(post => {
- const lastWord = post.slug.split('-').filter(c => c).pop()
- const firstCharaOflastWord = lastWord[0]
- const firstCharaOfSortWord = post.sortname ? post.sortname[0] : 'z'
- let charaIndex = alphabet.indexOf(firstCharaOflastWord) < alphabet.indexOf(firstCharaOfSortWord) ? alphabet.indexOf(firstCharaOflastWord) : alphabet.indexOf(firstCharaOfSortWord)
-
- // Kamaka problem
- if(seenTitles.includes(letter)) {
- charaIndex = alphabet.indexOf(firstCharaOflastWord) > alphabet.indexOf(firstCharaOfSortWord) ? alphabet.indexOf(firstCharaOflastWord) : alphabet.indexOf(firstCharaOfSortWord)
- }
-
- storeTitle(alphabet[charaIndex])
- flatPacked.push(post)
- })
- return flatPacked
- }
-
- const _arrangeByEpisode = postsList => {
- const byEpisode = {}
- postsList.forEach(post => {
- const relatedEpisode = post.related_episode
- if(!byEpisode[relatedEpisode]) byEpisode[relatedEpisode] = []
- byEpisode[relatedEpisode].push(post)
- })
- const flatPacked = _insertInBetweens(byEpisode)
- return flatPacked
- }
-
- const repackBySort = (postsList, sortedBy, seenTitles) => {
- let repacked = postsList
- if(sortedBy == sortTypes.material) {
- repacked = _arrangeByMaterial(postsList)
- } else if(sortedBy == sortTypes.episode) {
- repacked = _arrangeByEpisode(postsList)
- } else if(sortedBy == sortTypes.subtype) {
- repacked = _arrangeByType(postsList)
- } else if(sortedBy == sortTypes.alpha) {
- repacked = _arrangeByAlpha(postsList, seenTitles)
- }
- return repacked
- }
-
- const allBySlug = state => {
- return Object.values(state.all).reduce((bySlug, post) => {
- bySlug[post.slug] = post
- return bySlug
- }, {})
- }
-
- export {
- allBySlug,
- repackBySort,
- _arrangeByMaterial,
- _arrangeByType,
- _arrangeByAlpha,
- _arrangeByEpisode
- }
|