🎨 Refactor into pure.js
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import resources from '../resources'
|
||||
import { prop, compose, flatten, map, filter, isEmpty, not, any, includes, curry } from 'ramda'
|
||||
import { prop, compose, filter } from 'ramda'
|
||||
import { includesElOf, getAllResources, tagsNotEmpty } from '../utils/pure'
|
||||
|
||||
// Polyfill for flat
|
||||
if (!Array.prototype.flat) {
|
||||
@@ -22,14 +23,6 @@ if (!Array.prototype.flat) {
|
||||
})
|
||||
}
|
||||
|
||||
// True if list2 has element that appears in list1
|
||||
// includesElOf([1, 2], [3]) -> false
|
||||
// includesElOf([1, 2])([3]) -> false
|
||||
// includesElOf([1, 2], [2]) -> true
|
||||
// includesElOf([1, 2])([2]) -> true
|
||||
// includesElOf :: [a] -> [a] -> Bool
|
||||
// const includesElOf = curry((list1, list2) => any(flip(includes)(list2), list1))
|
||||
|
||||
export const state = () => ({
|
||||
resources: resources.map(category => ({
|
||||
...category,
|
||||
@@ -57,17 +50,12 @@ export const getters = {
|
||||
return Object.assign(state.resources.find(category => category.title.toLowerCase() === categoryTitle.toLowerCase()))
|
||||
},
|
||||
findByTags: state => tags => {
|
||||
// true if list2 has element that appears in list1 else false
|
||||
// includesElOf [a] -> [a] -> Bool
|
||||
const includesElOf = curry((list1, list2) => any(el => includes(el, list2), list1))
|
||||
// getAllResources :: [Category] -> [Resource]
|
||||
const getAllResources = compose(flatten, map(prop('resources')))
|
||||
// tagsNotEmpty :: [Resource] -> Bool
|
||||
const tagsNotEmpty = compose(not, isEmpty, prop('tags'))
|
||||
// containsTags :: [Resource] -> [Resource]
|
||||
const containsTags = filter(tagsNotEmpty)
|
||||
// includesDesiredTags :: Resource -> Bool
|
||||
const includesDesiredTags = compose(includesElOf(tags), prop('tags'))
|
||||
// findResourcesByTag :: [Resource] -> [Resource]
|
||||
const findResourcesByTag = filter(compose(includesElOf(tags), prop('tags')))
|
||||
const findResourcesByTag = filter(includesDesiredTags)
|
||||
// getDesiredResources :: [Category] -> [Resource]
|
||||
const getDesiredResources = compose(findResourcesByTag, containsTags, getAllResources)
|
||||
|
||||
|
||||
33
utils/pure.js
Normal file
33
utils/pure.js
Normal file
@@ -0,0 +1,33 @@
|
||||
/*eslint-disable */
|
||||
import * as R from 'ramda'
|
||||
|
||||
/// Types
|
||||
const Resource = {
|
||||
title: String,
|
||||
cleanTitle: String,
|
||||
desc: String,
|
||||
path: String,
|
||||
url: String,
|
||||
tags: [String],
|
||||
}
|
||||
|
||||
const Category = {
|
||||
title: String,
|
||||
slug: String,
|
||||
resources: [Resource],
|
||||
}
|
||||
|
||||
/// Functions
|
||||
// getAllResources :: [Category] -> [Resource]
|
||||
const getAllResources = R.compose(R.flatten, R.map(R.prop('resources')))
|
||||
|
||||
// tagsNotEmpty :: Resource -> Bool
|
||||
const tagsNotEmpty = R.compose(R.not, R.isEmpty, R.prop('tags'))
|
||||
|
||||
// true if list2 has element that appears in list1 else false
|
||||
// includesElOf([1, 2])([2]) -> true
|
||||
// includesElOf([1, 2], [3]) -> false
|
||||
// includesElOf :: [a] -> [a] -> Bool
|
||||
const includesElOf = R.curry((list1, list2) => R.any(el => R.includes(el, list2), list1))
|
||||
|
||||
export { getAllResources, tagsNotEmpty, includesElOf }
|
||||
Reference in New Issue
Block a user